Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChristian W. Damus2014-04-28 15:56:48 +0000
committerChristian W. Damus2014-04-28 15:57:05 +0000
commit6eee799142cc91efd4c13bd4293b2f3a768af316 (patch)
tree6029bbe75fd0e9c67d1884e1a810b5c430a4441b /tests/junit/plugins/core/org.eclipse.papyrus.infra.core.tests/test
parentadee8e415f23e8a259a8fbad37a2cf08159c579d (diff)
downloadorg.eclipse.papyrus-6eee799142cc91efd4c13bd4293b2f3a768af316.tar.gz
org.eclipse.papyrus-6eee799142cc91efd4c13bd4293b2f3a768af316.tar.xz
org.eclipse.papyrus-6eee799142cc91efd4c13bd4293b2f3a768af316.zip
422257: [Performances] Memory leaks
https://bugs.eclipse.org/bugs/show_bug.cgi?id=422257 Ensure that ResourceUndoContexts are flushed when the corresponding resources are unloaded, to let operations be removed from the history as soon as possible (promotes garbage collection).
Diffstat (limited to 'tests/junit/plugins/core/org.eclipse.papyrus.infra.core.tests/test')
-rw-r--r--tests/junit/plugins/core/org.eclipse.papyrus.infra.core.tests/test/org/eclipse/papyrus/infra/core/resource/ResourceAdapterTest.java266
-rw-r--r--tests/junit/plugins/core/org.eclipse.papyrus.infra.core.tests/test/org/eclipse/papyrus/infra/core/tests/AllTests.java4
2 files changed, 269 insertions, 1 deletions
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
new file mode 100644
index 00000000000..5e35d740871
--- /dev/null
+++ b/tests/junit/plugins/core/org.eclipse.papyrus.infra.core.tests/test/org/eclipse/papyrus/infra/core/resource/ResourceAdapterTest.java
@@ -0,0 +1,266 @@
+/*
+ * Copyright (c) 2014 CEA 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
+ *
+ */
+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.emf.ecore.resource.impl.ResourceSetImpl;
+import org.eclipse.papyrus.junit.utils.tests.AbstractPapyrusTest;
+import org.junit.After;
+import org.junit.Before;
+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";
+
+ private ResourceSet rset;
+
+ private Fixture fixture;
+
+ 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 framework
+ //
+
+ @Before
+ public void createFixture() {
+ rset = new ResourceSetImpl();
+ fixture = new Fixture();
+
+ rset.eAdapters().add(fixture);
+ }
+
+ @After
+ public void disposeFixture() {
+ fixture = null;
+
+ for(Resource next : rset.getResources()) {
+ next.unload();
+ next.eAdapters().clear();
+ }
+
+ rset.getResources().clear();
+ rset.eAdapters().clear();
+ rset = null;
+ }
+
+ URI getTestResourceURI() {
+ // Doesn't matter the resource; this one's conveniently available
+ return URI.createURI(getClass().getResource("Bug402525.ecore").toExternalForm(), true);
+ }
+
+ 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 handleResourceRemoved(Resource resource) {
+ called(resource, RESOURCE_REMOVED);
+ }
+
+ @Override
+ protected void handleResourceLoaded(Resource resource) {
+ called(resource, RESOURCE_LOADED);
+ }
+
+ @Override
+ protected void handleResourceUnloaded(Resource resource) {
+ called(resource, RESOURCE_UNLOADED);
+ }
+
+ @Override
+ protected void handleResourceURI(Resource resource, URI oldURI, URI newURI) {
+ called(resource, RESOURCE_URI);
+ uri(oldURI, newURI);
+ }
+
+ @Override
+ protected void handleRootAdded(Resource resource, EObject root) {
+ called(resource, RESOURCE_ROOT_ADDED);
+ root(root);
+ }
+
+ @Override
+ protected void handleRootRemoved(Resource resource, EObject root) {
+ called(resource, RESOURCE_ROOT_REMOVED);
+ root(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 1d389ba9ad7..f19ba7c4588 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
@@ -10,6 +10,7 @@
* Remi Schnekenburger (CEA LIST) remi.schnekenburger@cea.fr - Initial API and implementation
* Christian W. Damus (CEA LIST) - add test for AdapterUtils
* Christian W. Damus (CEA) - bug 402525
+ * Christian W. Damus (CEA) - bug 422257
*****************************************************************************/
package org.eclipse.papyrus.infra.core.tests;
@@ -18,6 +19,7 @@ import org.eclipse.papyrus.infra.core.lifecycleevents.LifeCycleEventsProviderTes
import org.eclipse.papyrus.infra.core.resource.AbstractModelWithSharedResourceTest;
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.services.ComposedServiceTest;
import org.eclipse.papyrus.infra.core.services.ServicesRegistryTest;
import org.eclipse.papyrus.infra.core.utils.AdapterUtilsTest;
@@ -30,7 +32,7 @@ import org.junit.runners.Suite.SuiteClasses;
@RunWith(Suite.class)
@SuiteClasses({
// {oep.resource}
-ModelSetTest.class, AbstractModelWithSharedResourceTest.class, NestingTransactionalCommandStackTest.class,
+ModelSetTest.class, AbstractModelWithSharedResourceTest.class, NestingTransactionalCommandStackTest.class, ResourceAdapterTest.class,
// {oep}.core.services
ComposedServiceTest.class, ServicesRegistryTest.class,
// {oep}.core.lifecycleevents

Back to the top