Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChristian W. Damus2015-11-24 18:45:12 +0000
committerChristian W. Damus2015-11-24 20:35:38 +0000
commit6e99ae12c6453caeebf232da1c6a9f7dadab08e3 (patch)
treeada3c534c81141f466f95918f48ce41035cd139e
parent35196434a6c6f8a52e17a03b224d46004525e8a1 (diff)
downloadorg.eclipse.papyrus-6e99ae12c6453caeebf232da1c6a9f7dadab08e3.tar.gz
org.eclipse.papyrus-6e99ae12c6453caeebf232da1c6a9f7dadab08e3.tar.xz
org.eclipse.papyrus-6e99ae12c6453caeebf232da1c6a9f7dadab08e3.zip
Bug 482949: Problems with ResourceAdapter.Transactional
https://bugs.eclipse.org/bugs/show_bug.cgi?id=482949 Fix ResourceAdapter.Transactional adding itself to the editing domain instead of removing itself on uninstall. Ensure JUnit coverage on par with the non-transactional variant.
-rw-r--r--plugins/infra/core/org.eclipse.papyrus.infra.core/src/org/eclipse/papyrus/infra/core/resource/ResourceAdapter.java80
-rw-r--r--tests/junit/plugins/core/org.eclipse.papyrus.infra.core.tests/.classpath14
-rw-r--r--tests/junit/plugins/core/org.eclipse.papyrus.infra.core.tests/.settings/org.eclipse.jdt.core.prefs6
-rw-r--r--tests/junit/plugins/core/org.eclipse.papyrus.infra.core.tests/META-INF/MANIFEST.MF2
-rw-r--r--tests/junit/plugins/core/org.eclipse.papyrus.infra.core.tests/test/org/eclipse/papyrus/infra/core/resource/AbstractResourceAdapterTest.java351
-rw-r--r--tests/junit/plugins/core/org.eclipse.papyrus.infra.core.tests/test/org/eclipse/papyrus/infra/core/resource/ResourceAdapterTest.java298
-rw-r--r--tests/junit/plugins/core/org.eclipse.papyrus.infra.core.tests/test/org/eclipse/papyrus/infra/core/resource/ResourceAdapterTransactionalTest.java138
-rw-r--r--tests/junit/plugins/core/org.eclipse.papyrus.infra.core.tests/test/org/eclipse/papyrus/infra/core/tests/AllTests.java7
8 files changed, 649 insertions, 247 deletions
diff --git a/plugins/infra/core/org.eclipse.papyrus.infra.core/src/org/eclipse/papyrus/infra/core/resource/ResourceAdapter.java b/plugins/infra/core/org.eclipse.papyrus.infra.core/src/org/eclipse/papyrus/infra/core/resource/ResourceAdapter.java
index 454dc5ea26f..ea15d69ab94 100644
--- a/plugins/infra/core/org.eclipse.papyrus.infra.core/src/org/eclipse/papyrus/infra/core/resource/ResourceAdapter.java
+++ b/plugins/infra/core/org.eclipse.papyrus.infra.core/src/org/eclipse/papyrus/infra/core/resource/ResourceAdapter.java
@@ -11,10 +11,16 @@
* Christian W. Damus - bug 399859
* Christian W. Damus - bug 465416
* Eike Stepper (CEA) - bug 466520
+ * Christian W. Damus - bug 482949
*
*/
package org.eclipse.papyrus.infra.core.resource;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.function.Predicate;
+
import org.eclipse.emf.common.command.Command;
import org.eclipse.emf.common.notify.Adapter;
import org.eclipse.emf.common.notify.Notification;
@@ -82,9 +88,9 @@ public abstract class ResourceAdapter extends AdapterImpl {
case Notification.SET:
case Notification.UNSET:
if (msg.getNewBooleanValue()) {
- handleResourceLoaded((Resource) notifier);
+ handleResourceLoaded0((Resource) notifier);
} else {
- handleResourceUnloaded((Resource) notifier);
+ handleResourceUnloaded0((Resource) notifier);
}
break;
}
@@ -171,7 +177,7 @@ public abstract class ResourceAdapter extends AdapterImpl {
// Don't report addition of roots when loading nor removal of roots when unloading
// because loading and unloading are semantically more significant events
- if (resource.isLoaded() && !resource.isLoading()) {
+ if (resource.isLoaded() && !isLoading(resource)) {
switch (msg.getEventType()) {
case Notification.ADD: {
Object newValue = msg.getNewValue();
@@ -226,10 +232,22 @@ public abstract class ResourceAdapter extends AdapterImpl {
// Pass
}
+ void handleResourceLoaded0(Resource resource) {
+ handleResourceLoaded(resource);
+ }
+
+ boolean isLoading(Resource.Internal resource) {
+ return resource.isLoading();
+ }
+
protected void handleResourceLoaded(Resource resource) {
// Pass
}
+ void handleResourceUnloaded0(Resource resource) {
+ handleResourceUnloaded(resource);
+ }
+
protected void handleResourceUnloaded(Resource resource) {
// Pass
}
@@ -258,6 +276,11 @@ public abstract class ResourceAdapter extends AdapterImpl {
private final NotificationFilter filter = NotificationFilter.NOT_TOUCH.and(createFilter());
+ private List<Notification> notifications;
+
+ /** Because we are invoked after the fact, we need to emulate the is-loading state. */
+ private Map<Resource, Boolean> loadingState;
+
/**
* Initializes me as a post-commit resource notification handler.
*/
@@ -308,12 +331,12 @@ public abstract class ResourceAdapter extends AdapterImpl {
@Override
public void resourceSetChanged(ResourceSetChangeEvent event) {
- handleResourceSetChangeEvent(event);
+ handleResourceSetChangeEvent0(event);
}
@Override
public Command transactionAboutToCommit(ResourceSetChangeEvent event) throws RollbackException {
- handleResourceSetChangeEvent(event);
+ handleResourceSetChangeEvent0(event);
return null;
}
@@ -358,7 +381,17 @@ public abstract class ResourceAdapter extends AdapterImpl {
}
}
} finally {
- editingDomain.addResourceSetListener(this);
+ editingDomain.removeResourceSetListener(this);
+ }
+ }
+
+ protected void handleResourceSetChangeEvent0(ResourceSetChangeEvent event) {
+ try {
+ notifications = event.getNotifications();
+ handleResourceSetChangeEvent(event);
+ } finally {
+ notifications = null;
+ loadingState = null;
}
}
@@ -387,5 +420,40 @@ public abstract class ResourceAdapter extends AdapterImpl {
protected final void removeAdapter(Notifier notifier) {
// Pass
}
+
+ @Override
+ boolean isLoading(Resource.Internal resource) {
+ return getLoadingState().computeIfAbsent(resource,
+ r -> notifications.stream().anyMatch(isLoadOf(r)));
+ }
+
+ private Predicate<Notification> isLoadOf(Resource resource) {
+ return msg -> (msg.getNotifier() == resource)
+ && (msg.getFeatureID(Resource.class) == Resource.RESOURCE__IS_LOADED)
+ && msg.getNewBooleanValue();
+ }
+
+ Map<Resource, Boolean> getLoadingState() {
+ if (loadingState == null) {
+ loadingState = new HashMap<>();
+ }
+ return loadingState;
+ }
+
+ void setLoading(Resource resource, boolean loading) {
+ getLoadingState().put(resource, loading);
+ }
+
+ @Override
+ void handleResourceLoaded0(Resource resource) {
+ setLoading(resource, true);
+ super.handleResourceLoaded0(resource);
+ }
+
+ @Override
+ void handleResourceUnloaded0(Resource resource) {
+ setLoading(resource, false);
+ super.handleResourceUnloaded0(resource);
+ }
}
}
diff --git a/tests/junit/plugins/core/org.eclipse.papyrus.infra.core.tests/.classpath b/tests/junit/plugins/core/org.eclipse.papyrus.infra.core.tests/.classpath
index 046bda900d2..83be1d9f78c 100644
--- a/tests/junit/plugins/core/org.eclipse.papyrus.infra.core.tests/.classpath
+++ b/tests/junit/plugins/core/org.eclipse.papyrus.infra.core.tests/.classpath
@@ -1,7 +1,7 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<classpath>
- <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5"/>
- <classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
- <classpathentry kind="src" path="test"/>
- <classpathentry kind="output" path="bin"/>
-</classpath>
+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+ <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
+ <classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
+ <classpathentry kind="src" path="test"/>
+ <classpathentry kind="output" path="bin"/>
+</classpath>
diff --git a/tests/junit/plugins/core/org.eclipse.papyrus.infra.core.tests/.settings/org.eclipse.jdt.core.prefs b/tests/junit/plugins/core/org.eclipse.papyrus.infra.core.tests/.settings/org.eclipse.jdt.core.prefs
index 4759947300a..62a08f4494d 100644
--- a/tests/junit/plugins/core/org.eclipse.papyrus.infra.core.tests/.settings/org.eclipse.jdt.core.prefs
+++ b/tests/junit/plugins/core/org.eclipse.papyrus.infra.core.tests/.settings/org.eclipse.jdt.core.prefs
@@ -1,10 +1,10 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
-org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5
-org.eclipse.jdt.core.compiler.compliance=1.5
+org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
+org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
-org.eclipse.jdt.core.compiler.source=1.5
+org.eclipse.jdt.core.compiler.source=1.8
org.eclipse.jdt.core.formatter.align_type_members_on_columns=false
org.eclipse.jdt.core.formatter.alignment_for_arguments_in_allocation_expression=16
org.eclipse.jdt.core.formatter.alignment_for_arguments_in_annotation=0
diff --git a/tests/junit/plugins/core/org.eclipse.papyrus.infra.core.tests/META-INF/MANIFEST.MF b/tests/junit/plugins/core/org.eclipse.papyrus.infra.core.tests/META-INF/MANIFEST.MF
index e992d2a3992..b525a277913 100644
--- a/tests/junit/plugins/core/org.eclipse.papyrus.infra.core.tests/META-INF/MANIFEST.MF
+++ b/tests/junit/plugins/core/org.eclipse.papyrus.infra.core.tests/META-INF/MANIFEST.MF
@@ -13,4 +13,4 @@ Bundle-Name: %pluginName
Bundle-Localization: fragment
Bundle-ManifestVersion: 2
Bundle-SymbolicName: org.eclipse.papyrus.infra.core.tests;singleton:=true
-Bundle-RequiredExecutionEnvironment: J2SE-1.5
+Bundle-RequiredExecutionEnvironment: JavaSE-1.8
diff --git a/tests/junit/plugins/core/org.eclipse.papyrus.infra.core.tests/test/org/eclipse/papyrus/infra/core/resource/AbstractResourceAdapterTest.java b/tests/junit/plugins/core/org.eclipse.papyrus.infra.core.tests/test/org/eclipse/papyrus/infra/core/resource/AbstractResourceAdapterTest.java
new file mode 100644
index 00000000000..0803216621e
--- /dev/null
+++ b/tests/junit/plugins/core/org.eclipse.papyrus.infra.core.tests/test/org/eclipse/papyrus/infra/core/resource/AbstractResourceAdapterTest.java
@@ -0,0 +1,351 @@
+/*
+ * Copyright (c) 2014, 2015 CEA, Christian W. Damus, and others.
+ *
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Christian W. Damus (CEA) - Initial API and implementation
+ * Christian W. Damus - bug 482949
+ *
+ */
+package org.eclipse.papyrus.infra.core.resource;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+
+import java.util.Set;
+
+import org.eclipse.emf.common.util.URI;
+import org.eclipse.emf.ecore.EObject;
+import org.eclipse.emf.ecore.EcoreFactory;
+import org.eclipse.emf.ecore.resource.Resource;
+import org.eclipse.emf.ecore.resource.ResourceSet;
+import org.eclipse.emf.ecore.resource.impl.ResourceImpl;
+import org.eclipse.papyrus.junit.framework.classification.tests.AbstractPapyrusTest;
+import org.eclipse.papyrus.junit.utils.rules.AbstractHouseKeeperRule.CleanUp;
+import org.eclipse.papyrus.junit.utils.rules.HouseKeeper;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+
+import com.google.common.collect.ImmutableSet;
+import com.google.common.collect.Sets;
+
+
+/**
+ * Abstract test suite for the {@link ResourceAdapter} class, providing tests
+ * for both the base and transactional variants.
+ */
+public abstract class AbstractResourceAdapterTest<T, A extends ResourceAdapter> extends AbstractPapyrusTest {
+
+ private static final String RESOURCE_ADDED = "handleResourceAdded";
+
+ private static final String RESOURCE_REMOVED = "handleResourceRemoved";
+
+ private static final String RESOURCE_LOADED = "handleResourceLoaded";
+
+ private static final String RESOURCE_UNLOADED = "handleResourceUnloaded";
+
+ private static final String RESOURCE_URI = "handleResourceURI";
+
+ private static final String RESOURCE_ROOT_ADDED = "handleRootAdded";
+
+ private static final String RESOURCE_ROOT_REMOVED = "handleRootRemoved";
+
+ @Rule
+ public final HouseKeeper houseKeeper = new HouseKeeper();
+
+ private T target;
+
+ private ResourceSet rset;
+
+ @CleanUp
+ private Fixture fixture;
+
+ public AbstractResourceAdapterTest() {
+ super();
+ }
+
+ @Test
+ public void testResourceAdded() {
+ Resource res = new ResourceImpl();
+ execute(() -> rset.getResources().add(res));
+
+ fixture.assertHooks(RESOURCE_ADDED);
+ fixture.assertResources(res);
+ }
+
+ @Test
+ public void testResourceRemoved() {
+ Resource res = new ResourceImpl();
+ execute(() -> rset.getResources().add(res));
+
+ fixture.reset();
+ execute(() -> rset.getResources().remove(0));
+
+ fixture.assertHooks(RESOURCE_REMOVED);
+ fixture.assertResources(res);
+ }
+
+ @Test
+ public void testResourceLoaded() {
+ Resource res = rset.createResource(getTestResourceURI());
+
+ fixture.reset();
+ safeExecute(() -> res.load(null));
+
+ fixture.assertHooks(RESOURCE_LOADED);
+ fixture.assertResources(res);
+ }
+
+ @Test
+ public void testResourceUnloaded() {
+ Resource res = rset.getResource(getTestResourceURI(), true);
+
+ fixture.reset();
+ execute(() -> res.unload());
+
+ fixture.assertHooks(RESOURCE_UNLOADED);
+ fixture.assertResources(res);
+ }
+
+ @Test
+ public void testResourceURI() {
+ URI oldURI = getTestResourceURI();
+ Resource res = rset.getResource(oldURI, true);
+
+ fixture.reset();
+ URI newURI = URI.createURI("http:///bogus.ecore");
+ execute(() -> res.setURI(newURI));
+
+ fixture.assertHooks(RESOURCE_URI);
+ fixture.assertResources(res);
+ fixture.assertURIs(oldURI, newURI);
+ }
+
+ @Test
+ public void testRootAdded() {
+ Resource res = rset.getResource(getTestResourceURI(), true);
+
+ fixture.reset();
+ EObject root = EcoreFactory.eINSTANCE.createEObject();
+ execute(() -> res.getContents().add(root));
+
+ fixture.assertHooks(RESOURCE_ROOT_ADDED);
+ fixture.assertResources(res);
+ fixture.assertRoots(root);
+ }
+
+ @Test
+ public void testRootRemoved() {
+ Resource res = rset.getResource(getTestResourceURI(), true);
+
+ fixture.reset();
+ EObject root = res.getContents().get(0);
+ execute(() -> res.getContents().remove(root));
+
+ fixture.assertHooks(RESOURCE_ROOT_REMOVED);
+ fixture.assertResources(res);
+ fixture.assertRoots(root);
+ }
+
+ @Test
+ public void testChangesInPreexistingResources_bug481151() {
+ // Need a different one than the usual, which already has the fixture attached
+ T newTarget = createTarget(houseKeeper);
+ ResourceSet newRset = getResourceSet(newTarget);
+
+ // A pre-existing resource
+ Resource resource = new ResourceImpl(URI.createURI("bogus://test"));
+ execute(() -> newRset.getResources().add(resource));
+
+ fixture.install(newTarget);
+
+ // Consequence of initial discovery
+ fixture.assertHooks(RESOURCE_ADDED);
+ fixture.reset();
+
+ EObject newRoot = EcoreFactory.eINSTANCE.createEObject();
+ execute(() -> resource.getContents().add(newRoot));
+
+ // An unloaded resource becomes implicitly loaded when its contents are changed
+ if (isTransactional()) {
+ // In the transactional case, all observations are delayed until the end
+ // of the transaction, so if a resource was loaded during the transaction,
+ // it doesn't matter what else happened to its contents because we don't
+ // observe any of those contents until later. So, there is no distinction
+ // to be had in the additional root-add event, and it isn't observed
+ fixture.assertHooks(RESOURCE_LOADED);
+ } else {
+ // In the non-transactional case, the add happens after load has completed,
+ // so the resource is no longer loading at that point, and we observe the
+ // root being added
+ fixture.assertHooks(RESOURCE_LOADED, RESOURCE_ROOT_ADDED);
+ }
+ }
+
+ @Test
+ public void testUninstallResourceAdapter_bug482949() {
+ // uninstall now
+ fixture.uninstall(target);
+
+ // We should not observe any events
+ Resource res = new ResourceImpl();
+ execute(() -> rset.getResources().add(res));
+
+ fixture.assertHooks();
+ fixture.assertResources();
+ }
+
+ //
+ // Test framework
+ //
+
+ @Before
+ public void createFixture() {
+ target = createTarget(houseKeeper);
+ rset = getResourceSet(target);
+ fixture = doCreateFixture();
+
+ fixture.install(target);
+ houseKeeper.cleanUpLater(fixture, f -> f.uninstall(target));
+ }
+
+ protected boolean isTransactional() {
+ return false;
+ }
+
+ protected abstract T createTarget(HouseKeeper keeper);
+
+ protected abstract ResourceSet getResourceSet(T target);
+
+ protected abstract void execute(Runnable command);
+
+ protected abstract void safeExecute(RunnableWithException command);
+
+ protected abstract Fixture doCreateFixture();
+
+ protected final T getTarget() {
+ return target;
+ }
+
+ URI getTestResourceURI() {
+ // Doesn't matter the resource; this one's conveniently available
+ return URI.createURI(getClass().getResource("Bug402525.ecore").toExternalForm(), true);
+ }
+
+ //
+ // Nested types
+ //
+
+ protected abstract class Fixture {
+
+ private Set<Resource> resources = Sets.newHashSet();
+
+ private Set<String> hooksCalled = Sets.newHashSet();
+
+ private Set<EObject> roots = Sets.newHashSet();
+
+ private URI oldURI;
+
+ private URI newURI;
+
+ private A adapter;
+
+ void reset() {
+ resources.clear();
+ hooksCalled.clear();
+ roots.clear();
+ oldURI = null;
+ newURI = null;
+ }
+
+ void assertResources(Resource... resources) {
+ assertThat(this.resources, is((Set<Resource>) ImmutableSet.copyOf(resources)));
+ }
+
+ void assertHooks(String... hooks) {
+ assertThat(this.hooksCalled, is((Set<String>) ImmutableSet.copyOf(hooks)));
+ }
+
+ void assertRoots(EObject... objects) {
+ assertThat(this.roots, is((Set<EObject>) ImmutableSet.copyOf(objects)));
+ }
+
+ void assertURIs(URI oldURI, URI newURI) {
+ assertThat(this.oldURI, is(oldURI));
+ assertThat(this.newURI, is(newURI));
+ }
+
+ private void called(Resource resource, String hook) {
+ hooksCalled.add(hook);
+ resources.add(resource);
+ }
+
+ private void root(EObject root) {
+ roots.add(root);
+ }
+
+ private void uri(URI oldURI, URI newURI) {
+ this.oldURI = oldURI;
+ this.newURI = newURI;
+ }
+
+ protected void handleResourceAdded(Resource resource) {
+ called(resource, RESOURCE_ADDED);
+ }
+
+ protected void handleResourceRemoved(Resource resource) {
+ called(resource, RESOURCE_REMOVED);
+ }
+
+ protected void handleResourceLoaded(Resource resource) {
+ called(resource, RESOURCE_LOADED);
+ }
+
+ protected void handleResourceUnloaded(Resource resource) {
+ called(resource, RESOURCE_UNLOADED);
+ }
+
+ protected void handleResourceURI(Resource resource, URI oldURI, URI newURI) {
+ called(resource, RESOURCE_URI);
+ uri(oldURI, newURI);
+ }
+
+ protected void handleRootAdded(Resource resource, EObject root) {
+ called(resource, RESOURCE_ROOT_ADDED);
+ root(root);
+ }
+
+ protected void handleRootRemoved(Resource resource, EObject root) {
+ called(resource, RESOURCE_ROOT_REMOVED);
+ root(root);
+ }
+
+ void install(T target) {
+ adapter = createAdapter();
+ doInstall(adapter, target);
+ }
+
+ void uninstall(T target) {
+ if (adapter != null) {
+ doUninstall(adapter, target);
+ adapter = null;
+ }
+ }
+
+ protected abstract A createAdapter();
+
+ protected abstract void doInstall(A adapter, T target);
+
+ protected abstract void doUninstall(A adapter, T target);
+ }
+
+ @FunctionalInterface
+ protected interface RunnableWithException {
+ void run() throws Exception;
+ }
+}
diff --git a/tests/junit/plugins/core/org.eclipse.papyrus.infra.core.tests/test/org/eclipse/papyrus/infra/core/resource/ResourceAdapterTest.java b/tests/junit/plugins/core/org.eclipse.papyrus.infra.core.tests/test/org/eclipse/papyrus/infra/core/resource/ResourceAdapterTest.java
index 9a78e1a355c..72db79ae738 100644
--- a/tests/junit/plugins/core/org.eclipse.papyrus.infra.core.tests/test/org/eclipse/papyrus/infra/core/resource/ResourceAdapterTest.java
+++ b/tests/junit/plugins/core/org.eclipse.papyrus.infra.core.tests/test/org/eclipse/papyrus/infra/core/resource/ResourceAdapterTest.java
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2014 CEA and others.
+ * Copyright (c) 2014, 2015 CEA, Christian W. Damus, and others.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
@@ -8,273 +8,115 @@
*
* Contributors:
* Christian W. Damus (CEA) - Initial API and implementation
+ * Christian W. Damus - bug 482949
*
*/
package org.eclipse.papyrus.infra.core.resource;
-import static org.hamcrest.CoreMatchers.is;
-import static org.hamcrest.MatcherAssert.assertThat;
-
-import java.util.Set;
+import static org.junit.Assert.fail;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
-import org.eclipse.emf.ecore.EcoreFactory;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
-import org.eclipse.emf.ecore.resource.impl.ResourceImpl;
-import org.eclipse.papyrus.junit.framework.classification.tests.AbstractPapyrusTest;
-import org.eclipse.papyrus.junit.utils.rules.AbstractHouseKeeperRule.CleanUp;
import org.eclipse.papyrus.junit.utils.rules.HouseKeeper;
-import org.junit.Before;
-import org.junit.Rule;
-import org.junit.Test;
-
-import com.google.common.collect.ImmutableSet;
-import com.google.common.collect.Sets;
/**
* Test suite for the {@link ResourceAdapter} class.
*/
-public class ResourceAdapterTest extends AbstractPapyrusTest {
-
- private static final String RESOURCE_ADDED = "handleResourceAdded";
-
- private static final String RESOURCE_REMOVED = "handleResourceRemoved";
-
- private static final String RESOURCE_LOADED = "handleResourceLoaded";
-
- private static final String RESOURCE_UNLOADED = "handleResourceUnloaded";
-
- private static final String RESOURCE_URI = "handleResourceURI";
-
- private static final String RESOURCE_ROOT_ADDED = "handleRootAdded";
-
- private static final String RESOURCE_ROOT_REMOVED = "handleRootRemoved";
-
- @Rule
- public final HouseKeeper houseKeeper = new HouseKeeper();
-
- private ResourceSet rset;
-
- @CleanUp
- private Fixture fixture;
+public class ResourceAdapterTest extends AbstractResourceAdapterTest<ResourceSet, ResourceAdapter> {
public ResourceAdapterTest() {
super();
}
- @Test
- public void testResourceAdded() {
- Resource res = new ResourceImpl();
- rset.getResources().add(res);
-
- fixture.assertHooks(RESOURCE_ADDED);
- fixture.assertResources(res);
- }
-
- @Test
- public void testResourceRemoved() {
- Resource res = new ResourceImpl();
- rset.getResources().add(res);
-
- fixture.reset();
- rset.getResources().remove(0);
-
- fixture.assertHooks(RESOURCE_REMOVED);
- fixture.assertResources(res);
- }
-
- @Test
- public void testResourceLoaded() throws Exception {
- Resource res = rset.createResource(getTestResourceURI());
-
- fixture.reset();
- res.load(null);
-
- fixture.assertHooks(RESOURCE_LOADED);
- fixture.assertResources(res);
- }
-
- @Test
- public void testResourceUnloaded() {
- Resource res = rset.getResource(getTestResourceURI(), true);
-
- fixture.reset();
- res.unload();
-
- fixture.assertHooks(RESOURCE_UNLOADED);
- fixture.assertResources(res);
- }
-
- @Test
- public void testResourceURI() {
- URI oldURI = getTestResourceURI();
- Resource res = rset.getResource(oldURI, true);
-
- fixture.reset();
- URI newURI = URI.createURI("http:///bogus.ecore");
- res.setURI(newURI);
-
- fixture.assertHooks(RESOURCE_URI);
- fixture.assertResources(res);
- fixture.assertURIs(oldURI, newURI);
- }
-
- @Test
- public void testRootAdded() {
- Resource res = rset.getResource(getTestResourceURI(), true);
-
- fixture.reset();
- EObject root = EcoreFactory.eINSTANCE.createEObject();
- res.getContents().add(root);
-
- fixture.assertHooks(RESOURCE_ROOT_ADDED);
- fixture.assertResources(res);
- fixture.assertRoots(root);
- }
-
- @Test
- public void testRootRemoved() {
- Resource res = rset.getResource(getTestResourceURI(), true);
-
- fixture.reset();
- EObject root = res.getContents().get(0);
- res.getContents().remove(root);
-
- fixture.assertHooks(RESOURCE_ROOT_REMOVED);
- fixture.assertResources(res);
- fixture.assertRoots(root);
- }
-
-
- @Test
- public void testChangesInPreexistingResources_bug481151() {
- // Need a different one than the usual, which already has the fixture attached
- ResourceSet rset = houseKeeper.createResourceSet();
-
- // A pre-existing resource
- Resource resource = new ResourceImpl(URI.createURI("bogus://test"));
- rset.getResources().add(resource);
-
- rset.eAdapters().add(fixture);
-
- // Consequence of initial discovery
- fixture.assertHooks(RESOURCE_ADDED);
- fixture.reset();
-
- EObject newRoot = EcoreFactory.eINSTANCE.createEObject();
- resource.getContents().add(newRoot);
-
- // An unloaded resource becomes implicitly loaded when its contents are changed
- fixture.assertHooks(RESOURCE_LOADED, RESOURCE_ROOT_ADDED);
- }
-
//
// Test framework
//
- @Before
- public void createFixture() {
- rset = houseKeeper.createResourceSet();
- fixture = new Fixture();
-
- rset.eAdapters().add(fixture);
+ @Override
+ protected ResourceSet createTarget(HouseKeeper keeper) {
+ return keeper.createResourceSet();
}
- URI getTestResourceURI() {
- // Doesn't matter the resource; this one's conveniently available
- return URI.createURI(getClass().getResource("Bug402525.ecore").toExternalForm(), true);
+ @Override
+ protected ResourceSet getResourceSet(ResourceSet target) {
+ return target;
}
- static class Fixture extends ResourceAdapter {
-
- private Set<Resource> resources = Sets.newHashSet();
-
- private Set<String> hooksCalled = Sets.newHashSet();
-
- private Set<EObject> roots = Sets.newHashSet();
-
- private URI oldURI;
-
- private URI newURI;
-
- void reset() {
- resources.clear();
- hooksCalled.clear();
- roots.clear();
- oldURI = null;
- newURI = null;
- }
-
- void assertResources(Resource... resources) {
- assertThat(this.resources, is((Set<Resource>) ImmutableSet.copyOf(resources)));
- }
-
- void assertHooks(String... hooks) {
- assertThat(this.hooksCalled, is((Set<String>) ImmutableSet.copyOf(hooks)));
- }
-
- void assertRoots(EObject... objects) {
- assertThat(this.roots, is((Set<EObject>) ImmutableSet.copyOf(objects)));
- }
-
- void assertURIs(URI oldURI, URI newURI) {
- assertThat(this.oldURI, is(oldURI));
- assertThat(this.newURI, is(newURI));
- }
-
- private void called(Resource resource, String hook) {
- hooksCalled.add(hook);
- resources.add(resource);
- }
-
- private void root(EObject root) {
- roots.add(root);
- }
-
- private void uri(URI oldURI, URI newURI) {
- this.oldURI = oldURI;
- this.newURI = newURI;
- }
-
- @Override
- protected void handleResourceAdded(Resource resource) {
- called(resource, RESOURCE_ADDED);
- }
+ @Override
+ protected void execute(Runnable command) {
+ command.run();
+ }
- @Override
- protected void handleResourceRemoved(Resource resource) {
- called(resource, RESOURCE_REMOVED);
+ @Override
+ protected void safeExecute(RunnableWithException command) {
+ try {
+ command.run();
+ } catch (Exception e) {
+ e.printStackTrace();
+ fail("Command execution failed: " + e.getMessage());
}
+ }
- @Override
- protected void handleResourceLoaded(Resource resource) {
- called(resource, RESOURCE_LOADED);
- }
+ @Override
+ protected AbstractResourceAdapterTest<ResourceSet, ResourceAdapter>.Fixture doCreateFixture() {
+ return new MyFixture();
+ }
- @Override
- protected void handleResourceUnloaded(Resource resource) {
- called(resource, RESOURCE_UNLOADED);
- }
+ class MyFixture extends Fixture {
@Override
- protected void handleResourceURI(Resource resource, URI oldURI, URI newURI) {
- called(resource, RESOURCE_URI);
- uri(oldURI, newURI);
+ protected void doInstall(ResourceAdapter adapter, ResourceSet target) {
+ if (!target.eAdapters().contains(adapter)) {
+ target.eAdapters().add(adapter);
+ }
}
@Override
- protected void handleRootAdded(Resource resource, EObject root) {
- called(resource, RESOURCE_ROOT_ADDED);
- root(root);
+ protected void doUninstall(ResourceAdapter adapter, ResourceSet target) {
+ target.eAdapters().remove(adapter);
}
@Override
- protected void handleRootRemoved(Resource resource, EObject root) {
- called(resource, RESOURCE_ROOT_REMOVED);
- root(root);
+ protected ResourceAdapter createAdapter() {
+ return new ResourceAdapter() {
+ @Override
+ protected void handleResourceAdded(Resource resource) {
+ MyFixture.this.handleResourceAdded(resource);
+ }
+
+ @Override
+ protected void handleResourceRemoved(Resource resource) {
+ MyFixture.this.handleResourceRemoved(resource);
+ }
+
+ @Override
+ protected void handleResourceLoaded(Resource resource) {
+ MyFixture.this.handleResourceLoaded(resource);
+ }
+
+ @Override
+ protected void handleResourceUnloaded(Resource resource) {
+ MyFixture.this.handleResourceUnloaded(resource);
+ }
+
+ @Override
+ protected void handleResourceURI(Resource resource, URI oldURI, URI newURI) {
+ MyFixture.this.handleResourceURI(resource, oldURI, newURI);
+ }
+
+ @Override
+ protected void handleRootAdded(Resource resource, EObject root) {
+ MyFixture.this.handleRootAdded(resource, root);
+ }
+
+ @Override
+ protected void handleRootRemoved(Resource resource, EObject root) {
+ MyFixture.this.handleRootRemoved(resource, root);
+ }
+ };
}
}
}
diff --git a/tests/junit/plugins/core/org.eclipse.papyrus.infra.core.tests/test/org/eclipse/papyrus/infra/core/resource/ResourceAdapterTransactionalTest.java b/tests/junit/plugins/core/org.eclipse.papyrus.infra.core.tests/test/org/eclipse/papyrus/infra/core/resource/ResourceAdapterTransactionalTest.java
new file mode 100644
index 00000000000..97e6cd3a857
--- /dev/null
+++ b/tests/junit/plugins/core/org.eclipse.papyrus.infra.core.tests/test/org/eclipse/papyrus/infra/core/resource/ResourceAdapterTransactionalTest.java
@@ -0,0 +1,138 @@
+/*
+ * Copyright (c) 2015 Christian W. Damus and others.
+ *
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Christian W. Damus - Initial API and implementation
+ *
+ */
+package org.eclipse.papyrus.infra.core.resource;
+
+import org.eclipse.emf.common.util.URI;
+import org.eclipse.emf.common.util.WrappedException;
+import org.eclipse.emf.ecore.EObject;
+import org.eclipse.emf.ecore.resource.Resource;
+import org.eclipse.emf.ecore.resource.ResourceSet;
+import org.eclipse.emf.transaction.RecordingCommand;
+import org.eclipse.emf.transaction.TransactionalEditingDomain;
+import org.eclipse.papyrus.junit.utils.rules.HouseKeeper;
+
+
+/**
+ * Test suite for the {@link ResourceAdapter.Transactional} class.
+ */
+public class ResourceAdapterTransactionalTest extends AbstractResourceAdapterTest<TransactionalEditingDomain, ResourceAdapter.Transactional> {
+
+ public ResourceAdapterTransactionalTest() {
+ super();
+ }
+
+ //
+ // Test framework
+ //
+
+ @Override
+ protected boolean isTransactional() {
+ return true;
+ }
+
+ @Override
+ protected TransactionalEditingDomain createTarget(HouseKeeper keeper) {
+ return keeper.createSimpleEditingDomain();
+ }
+
+ @Override
+ protected ResourceSet getResourceSet(TransactionalEditingDomain target) {
+ return target.getResourceSet();
+ }
+
+ @Override
+ protected void execute(Runnable command) {
+ TransactionalEditingDomain domain = getTarget();
+ domain.getCommandStack().execute(new RecordingCommand(domain, "Test") {
+
+ @Override
+ protected void doExecute() {
+ command.run();
+ }
+ });
+ }
+
+ @Override
+ protected void safeExecute(RunnableWithException command) {
+ TransactionalEditingDomain domain = getTarget();
+ domain.getCommandStack().execute(new RecordingCommand(domain, "Test") {
+
+ @Override
+ protected void doExecute() {
+ try {
+ command.run();
+ } catch (Exception e) {
+ throw new WrappedException(e);
+ }
+ }
+ });
+ }
+
+ @Override
+ protected Fixture doCreateFixture() {
+ return new MyFixture();
+ }
+
+ class MyFixture extends Fixture {
+
+ @Override
+ protected void doInstall(ResourceAdapter.Transactional adapter, TransactionalEditingDomain target) {
+ adapter.install(target);
+ }
+
+ @Override
+ protected void doUninstall(ResourceAdapter.Transactional adapter, TransactionalEditingDomain target) {
+ adapter.uninstall(target);
+ }
+
+ @Override
+ protected ResourceAdapter.Transactional createAdapter() {
+ return new ResourceAdapter.Transactional() {
+ @Override
+ protected void handleResourceAdded(Resource resource) {
+ MyFixture.this.handleResourceAdded(resource);
+ }
+
+ @Override
+ protected void handleResourceRemoved(Resource resource) {
+ MyFixture.this.handleResourceRemoved(resource);
+ }
+
+ @Override
+ protected void handleResourceLoaded(Resource resource) {
+ MyFixture.this.handleResourceLoaded(resource);
+ }
+
+ @Override
+ protected void handleResourceUnloaded(Resource resource) {
+ MyFixture.this.handleResourceUnloaded(resource);
+ }
+
+ @Override
+ protected void handleResourceURI(Resource resource, URI oldURI, URI newURI) {
+ MyFixture.this.handleResourceURI(resource, oldURI, newURI);
+ }
+
+ @Override
+ protected void handleRootAdded(Resource resource, EObject root) {
+ MyFixture.this.handleRootAdded(resource, root);
+ }
+
+ @Override
+ protected void handleRootRemoved(Resource resource, EObject root) {
+ MyFixture.this.handleRootRemoved(resource, root);
+ }
+ };
+ }
+ }
+}
diff --git a/tests/junit/plugins/core/org.eclipse.papyrus.infra.core.tests/test/org/eclipse/papyrus/infra/core/tests/AllTests.java b/tests/junit/plugins/core/org.eclipse.papyrus.infra.core.tests/test/org/eclipse/papyrus/infra/core/tests/AllTests.java
index 9595ef227d1..13fe1249ad1 100644
--- a/tests/junit/plugins/core/org.eclipse.papyrus.infra.core.tests/test/org/eclipse/papyrus/infra/core/tests/AllTests.java
+++ b/tests/junit/plugins/core/org.eclipse.papyrus.infra.core.tests/test/org/eclipse/papyrus/infra/core/tests/AllTests.java
@@ -14,6 +14,7 @@
* Christian W. Damus (CEA) - bug 399859
* Christian W. Damus - bug 456934
* Christian W. Damus - bug 468030
+ * Christian W. Damus - bug 482949
*
*****************************************************************************/
package org.eclipse.papyrus.infra.core.tests;
@@ -25,13 +26,14 @@ import org.eclipse.papyrus.infra.core.resource.AbstractModelWithSharedResourceTe
import org.eclipse.papyrus.infra.core.resource.ModelSetTest;
import org.eclipse.papyrus.infra.core.resource.NestingTransactionalCommandStackTest;
import org.eclipse.papyrus.infra.core.resource.ResourceAdapterTest;
+import org.eclipse.papyrus.infra.core.resource.ResourceAdapterTransactionalTest;
import org.eclipse.papyrus.infra.core.services.ComposedServiceTest;
import org.eclipse.papyrus.infra.core.services.ServicesRegistryTest;
import org.eclipse.papyrus.infra.core.utils.AdapterUtilsTest;
import org.eclipse.papyrus.infra.core.utils.JobBasedFutureTest;
import org.eclipse.papyrus.infra.core.utils.JobExecutorServiceTest;
-import org.junit.runner.RunWith;
import org.eclipse.papyrus.junit.framework.classification.ClassificationSuite;
+import org.junit.runner.RunWith;
import org.junit.runners.Suite.SuiteClasses;
@@ -39,7 +41,8 @@ import org.junit.runners.Suite.SuiteClasses;
@RunWith(ClassificationSuite.class)
@SuiteClasses({
// {oep.resource}
- ModelSetTest.class, AbstractModelWithSharedResourceTest.class, NestingTransactionalCommandStackTest.class, ResourceAdapterTest.class,
+ ModelSetTest.class, AbstractModelWithSharedResourceTest.class, NestingTransactionalCommandStackTest.class,
+ ResourceAdapterTest.class, ResourceAdapterTransactionalTest.class,
// {oep}.core.language
LanguageServiceTest.class,
// {oep}.core.services

Back to the top