diff options
author | Christian W. Damus | 2013-09-16 19:41:46 +0000 |
---|---|---|
committer | Christian W. Damus | 2013-09-16 20:02:46 +0000 |
commit | 0777ed71475833b262df3d052832060620a3bdc3 (patch) | |
tree | 5347c220ee97a09f4b08985989f36473b6631da0 /tests/junit/extraplugins | |
parent | 9ef34fcdcdab280c603ac684baf494bb79839800 (diff) | |
download | org.eclipse.papyrus-0777ed71475833b262df3d052832060620a3bdc3.tar.gz org.eclipse.papyrus-0777ed71475833b262df3d052832060620a3bdc3.tar.xz org.eclipse.papyrus-0777ed71475833b262df3d052832060620a3bdc3.zip |
415369: [CDO] Support controlled resources and lazy loading
https://bugs.eclipse.org/bugs/show_bug.cgi?id=415369
Add automated tests for the most basic control and uncontrol use cases.
Diffstat (limited to 'tests/junit/extraplugins')
5 files changed, 415 insertions, 3 deletions
diff --git a/tests/junit/extraplugins/cdo/org.eclipse.papyrus.cdo.core.tests/META-INF/MANIFEST.MF b/tests/junit/extraplugins/cdo/org.eclipse.papyrus.cdo.core.tests/META-INF/MANIFEST.MF index d90b27ed92d..bd17d818e6d 100644 --- a/tests/junit/extraplugins/cdo/org.eclipse.papyrus.cdo.core.tests/META-INF/MANIFEST.MF +++ b/tests/junit/extraplugins/cdo/org.eclipse.papyrus.cdo.core.tests/META-INF/MANIFEST.MF @@ -7,10 +7,14 @@ Require-Bundle: org.eclipse.papyrus.cdo.core;bundle-version="0.10.1";visibility: org.eclipse.net4j.jvm;bundle-version="[4.1.0,5.0.0)",
org.eclipse.papyrus.uml.tools;bundle-version="0.10.1",
org.eclipse.papyrus.infra.gmfdiag.common;bundle-version="0.10.1",
- org.eclipse.core.expressions;bundle-version="3.4.0"
+ org.eclipse.core.expressions;bundle-version="3.4.0",
+ org.eclipse.papyrus.infra.services.controlmode;bundle-version="0.10.1",
+ org.eclipse.papyrus.infra.emf;bundle-version="0.10.1",
+ org.eclipse.papyrus.infra.services.resourceloading.preferences;bundle-version="0.10.1"
Export-Package: org.eclipse.papyrus.cdo.core.resource.tests,
org.eclipse.papyrus.cdo.core.tests,
org.eclipse.papyrus.cdo.core.util.tests,
+ org.eclipse.papyrus.cdo.internal.core.controlmode.tests;x-internal:=true,
org.eclipse.papyrus.cdo.internal.core.exporter.tests;x-internal:=true,
org.eclipse.papyrus.cdo.internal.core.expressions.tests;x-internal:=true,
org.eclipse.papyrus.cdo.internal.core.importer.tests;x-internal:=true,
diff --git a/tests/junit/extraplugins/cdo/org.eclipse.papyrus.cdo.core.tests/src/org/eclipse/papyrus/cdo/core/tests/AbstractPapyrusCDOTest.java b/tests/junit/extraplugins/cdo/org.eclipse.papyrus.cdo.core.tests/src/org/eclipse/papyrus/cdo/core/tests/AbstractPapyrusCDOTest.java index 63a158dcb4f..01fd56634d4 100644 --- a/tests/junit/extraplugins/cdo/org.eclipse.papyrus.cdo.core.tests/src/org/eclipse/papyrus/cdo/core/tests/AbstractPapyrusCDOTest.java +++ b/tests/junit/extraplugins/cdo/org.eclipse.papyrus.cdo.core.tests/src/org/eclipse/papyrus/cdo/core/tests/AbstractPapyrusCDOTest.java @@ -13,7 +13,11 @@ package org.eclipse.papyrus.cdo.core.tests; import static org.hamcrest.CoreMatchers.instanceOf; import static org.junit.Assert.assertThat; +import static org.junit.Assert.fail; +import java.lang.annotation.Annotation; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; import java.util.Map; import java.util.regex.Pattern; @@ -161,7 +165,38 @@ public abstract class AbstractPapyrusCDOTest { protected CDOTransaction createTransaction() { IInternalPapyrusRepository repo = getInternalPapyrusRepository(); - CDOTransaction result = getTransaction(repo.createTransaction(new ResourceSetImpl())); + CDOTransaction result = getTransaction(repo.createTransaction(createResourceSet())); + + return result; + } + + protected CDOView createView() { + IInternalPapyrusRepository repo = getInternalPapyrusRepository(); + + CDOView result = getInternalPapyrusRepository().getCDOView(repo.createReadOnlyView(createResourceSet())); + + return result; + } + + private ResourceSet createResourceSet() { + ResourceSet result = null; + + Method factory = getAnnotatedMethod(ResourceSetFactory.class); + if(factory != null) { + try { + result = (ResourceSet)factory.invoke(this); + } catch (InvocationTargetException e) { + e.getTargetException().printStackTrace(); + fail("Failed to create resource set for test case: " + e.getTargetException().getLocalizedMessage()); + } catch (IllegalAccessException e) { + fail("Resource-set factory method not accessible: " + factory.getName()); + } + } + + if(result == null) { + // default + result = new ResourceSetImpl(); + } return result; } @@ -170,6 +205,19 @@ public abstract class AbstractPapyrusCDOTest { return cast(getInternalPapyrusRepository().getCDOView(resourceSet), CDOTransaction.class); } + protected final Method getAnnotatedMethod(Class<? extends Annotation> annotationType) { + Method result = null; + + for(Method next : getClass().getMethods()) { + if(next.isAnnotationPresent(annotationType)) { + result = next; + break; + } + } + + return result; + } + protected String getRepositoryURL() { return repoURL; } @@ -247,10 +295,12 @@ public abstract class AbstractPapyrusCDOTest { public static <T extends Number & Comparable<T>> Matcher<T> lessThan(final T max) { return new BaseMatcher<T>() { + @Override public void describeTo(Description description) { description.appendText("< ").appendValue(max); } + @Override @SuppressWarnings("unchecked") public boolean matches(Object item) { return ((T)item).compareTo(max) < 0; @@ -261,10 +311,12 @@ public abstract class AbstractPapyrusCDOTest { public static <T extends Number & Comparable<T>> Matcher<T> lessThanOrEqualTo(final T max) { return new BaseMatcher<T>() { + @Override public void describeTo(Description description) { description.appendText("<= ").appendValue(max); } + @Override @SuppressWarnings("unchecked") public boolean matches(Object item) { return ((T)item).compareTo(max) <= 0; @@ -275,10 +327,12 @@ public abstract class AbstractPapyrusCDOTest { public static <T> Matcher<Iterable<T>> hasSize(final int size) { return new BaseMatcher<Iterable<T>>() { + @Override public void describeTo(Description description) { description.appendText("has size ").appendValue(size); } + @Override public boolean matches(Object item) { return Iterables.size((Iterable<?>)item) == size; } diff --git a/tests/junit/extraplugins/cdo/org.eclipse.papyrus.cdo.core.tests/src/org/eclipse/papyrus/cdo/core/tests/AllTests.java b/tests/junit/extraplugins/cdo/org.eclipse.papyrus.cdo.core.tests/src/org/eclipse/papyrus/cdo/core/tests/AllTests.java index 43633eba7dd..9b44a696882 100644 --- a/tests/junit/extraplugins/cdo/org.eclipse.papyrus.cdo.core.tests/src/org/eclipse/papyrus/cdo/core/tests/AllTests.java +++ b/tests/junit/extraplugins/cdo/org.eclipse.papyrus.cdo.core.tests/src/org/eclipse/papyrus/cdo/core/tests/AllTests.java @@ -13,6 +13,7 @@ package org.eclipse.papyrus.cdo.core.tests; import org.eclipse.papyrus.cdo.core.resource.tests.CDOAwareModelSetTest; import org.eclipse.papyrus.cdo.core.util.tests.AllUtilTests; +import org.eclipse.papyrus.cdo.internal.core.controlmode.tests.CDOControlModeTest; import org.eclipse.papyrus.cdo.internal.core.exporter.tests.AllModelExportTests; import org.eclipse.papyrus.cdo.internal.core.expressions.tests.AllExpressionsTests; import org.eclipse.papyrus.cdo.internal.core.importer.tests.AllModelImportTests; @@ -27,7 +28,8 @@ import org.junit.runners.Suite.SuiteClasses; @RunWith(Suite.class) @SuiteClasses({ AllUtilTests.class, AllExpressionsTests.class, // AllInternalCoreTests.class, CDOAwareModelSetTest.class, // -AllModelImportTests.class, AllModelExportTests.class }) +AllModelImportTests.class, AllModelExportTests.class, // +CDOControlModeTest.class }) public class AllTests { public AllTests() { diff --git a/tests/junit/extraplugins/cdo/org.eclipse.papyrus.cdo.core.tests/src/org/eclipse/papyrus/cdo/core/tests/ResourceSetFactory.java b/tests/junit/extraplugins/cdo/org.eclipse.papyrus.cdo.core.tests/src/org/eclipse/papyrus/cdo/core/tests/ResourceSetFactory.java new file mode 100644 index 00000000000..aeafa1c5559 --- /dev/null +++ b/tests/junit/extraplugins/cdo/org.eclipse.papyrus.cdo.core.tests/src/org/eclipse/papyrus/cdo/core/tests/ResourceSetFactory.java @@ -0,0 +1,27 @@ +/***************************************************************************** + * Copyright (c) 2013 CEA LIST. + * + * 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: + * CEA LIST - Initial API and implementation + *****************************************************************************/ +package org.eclipse.papyrus.cdo.core.tests; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + + +/** + * This is the ResourceSetFactory type. Enjoy. + */ +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.METHOD) +public @interface ResourceSetFactory { + // empty +} diff --git a/tests/junit/extraplugins/cdo/org.eclipse.papyrus.cdo.core.tests/src/org/eclipse/papyrus/cdo/internal/core/controlmode/tests/CDOControlModeTest.java b/tests/junit/extraplugins/cdo/org.eclipse.papyrus.cdo.core.tests/src/org/eclipse/papyrus/cdo/internal/core/controlmode/tests/CDOControlModeTest.java new file mode 100644 index 00000000000..9ccd4e55e04 --- /dev/null +++ b/tests/junit/extraplugins/cdo/org.eclipse.papyrus.cdo.core.tests/src/org/eclipse/papyrus/cdo/internal/core/controlmode/tests/CDOControlModeTest.java @@ -0,0 +1,325 @@ +/***************************************************************************** + * Copyright (c) 2013 CEA LIST. + * + * 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: + * CEA LIST - Initial API and implementation + *****************************************************************************/ +package org.eclipse.papyrus.cdo.internal.core.controlmode.tests; + +import static com.google.common.base.Predicates.instanceOf; +import static com.google.common.collect.Iterables.find; +import static org.hamcrest.CoreMatchers.containsString; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.fail; +import static org.junit.Assume.assumeThat; + +import java.util.List; + +import org.eclipse.core.commands.ExecutionException; +import org.eclipse.core.runtime.NullProgressMonitor; +import org.eclipse.emf.cdo.eresource.CDOResource; +import org.eclipse.emf.cdo.transaction.CDOTransaction; +import org.eclipse.emf.cdo.view.CDOView; +import org.eclipse.emf.common.util.URI; +import org.eclipse.emf.ecore.EObject; +import org.eclipse.emf.ecore.resource.Resource; +import org.eclipse.emf.ecore.util.EcoreUtil; +import org.eclipse.emf.ecore.util.InternalEList; +import org.eclipse.gmf.runtime.common.core.command.ICommand; +import org.eclipse.papyrus.cdo.core.resource.CDOAwareModelSet; +import org.eclipse.papyrus.cdo.core.tests.AbstractPapyrusCDOTest; +import org.eclipse.papyrus.cdo.core.tests.ResourceSetFactory; +import org.eclipse.papyrus.cdo.internal.core.PapyrusRepositoryManager; +import org.eclipse.papyrus.infra.core.resource.ModelSet; +import org.eclipse.papyrus.infra.core.services.ServiceException; +import org.eclipse.papyrus.infra.core.services.ServicesRegistry; +import org.eclipse.papyrus.infra.emf.utils.ServiceUtilsForEObject; +import org.eclipse.papyrus.infra.emf.utils.ServiceUtilsForResource; +import org.eclipse.papyrus.infra.emf.utils.ServiceUtilsForResourceInitializerService; +import org.eclipse.papyrus.infra.emf.utils.ServiceUtilsForResourceSet; +import org.eclipse.papyrus.infra.services.controlmode.ControlModeManager; +import org.eclipse.papyrus.infra.services.controlmode.ControlModeRequest; +import org.eclipse.papyrus.infra.services.resourceloading.preferences.StrategyChooser; +import org.eclipse.uml2.uml.Package; +import org.eclipse.uml2.uml.UMLFactory; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.google.common.collect.Lists; + +/** + * Basic tests for controlled-resource support in CDO model repositories. + */ +public class CDOControlModeTest extends AbstractPapyrusCDOTest { + + private final List<Runnable> cleanupActions = Lists.newArrayListWithExpectedSize(2); + + private int oldControlModeStrategy; + + public CDOControlModeTest() { + super(); + } + + @Test + public void testControlOneResource() { + CDOResource res = createUMLModel(); + Package root = getPackage(res); + + Package sub1 = root.getNestedPackages().get(0); + + save(res); // save now so that all model elements are already persistent + + control(sub1, "sub1.uml"); + + save(res); + + // create a new view in which to re-load the model + + CDOView view = createView(); + CDOResource res2 = view.getResource(res.getPath()); + Package root2 = getPackage(res2); + + InternalEList<Package> subs = (InternalEList<Package>)root2.getNestedPackages(); + assertProxy(subs.basicGet(0), "sub1.uml"); + } + + @Test + public void testControlOneResource_newlyAddedObjects() { + CDOResource res = createUMLModel(); + Package root = getPackage(res); + + Package sub1 = root.getNestedPackages().get(0); + + control(sub1, "sub1.uml"); + + save(res); + + // create a new view in which to re-load the model + + CDOView view = createView(); + CDOResource res2 = view.getResource(res.getPath()); + Package root2 = getPackage(res2); + + InternalEList<Package> subs = (InternalEList<Package>)root2.getNestedPackages(); + assertProxy(subs.basicGet(0), "sub1.uml"); + } + + @Test + public void testUncontrolOneResource() { + CDOResource res = createUMLModel(); + Package root = getPackage(res); + + Package sub1 = root.getNestedPackages().get(0); + + control(sub1, "sub1.uml"); + + save(res); + + // create a new view in which to re-load the model + + CDOView view = createView(); + CDOResource res2 = view.getResource(res.getPath()); + Package root2 = getPackage(res2); + + InternalEList<Package> subs = (InternalEList<Package>)root2.getNestedPackages(); + assumeProxy(subs.basicGet(0), "sub1.uml"); + + uncontrol(sub1); + + save(res); + + view = createView(); + res2 = view.getResource(res.getPath()); + root2 = getPackage(res2); + + subs = (InternalEList<Package>)root2.getNestedPackages(); + assertNotProxy(subs.basicGet(0)); + } + + // + // Test framework + // + + @Before + public void setControlModeStrategy() { + final int MAGIC_NUMBER_OF_DONT_LOAD_ANYTHING_STRATEGY = 2; + + oldControlModeStrategy = new StrategyChooser().getCurrentStrategy(); + StrategyChooser.setCurrentStrategy(MAGIC_NUMBER_OF_DONT_LOAD_ANYTHING_STRATEGY); + } + + @After + public void restoreControlModeStrategy() { + StrategyChooser.setCurrentStrategy(oldControlModeStrategy); + } + + @After + public void cleanup() { + for(Runnable next : cleanupActions) { + next.run(); + } + + cleanupActions.clear(); + } + + void cleanupLater(Runnable runnable) { + cleanupActions.add(runnable); + } + + @ResourceSetFactory + public ModelSet createModelSet() { + ModelSet result = null; + + try { + final ServicesRegistry services = new ServicesRegistry(); + + cleanupLater(new Runnable() { + + @Override + public void run() { + try { + services.disposeRegistry(); + } catch (ServiceException e) { + // ignore (best-effort clean-up) + } + } + }); + + services.startRegistry(); + services.add(ModelSet.class, 10, new CDOAwareModelSet(PapyrusRepositoryManager.INSTANCE)); + services.add(ServiceUtilsForResourceInitializerService.class, 10, new ServiceUtilsForResourceInitializerService()); + services.startServicesByClassKeys(ModelSet.class, ServiceUtilsForResourceInitializerService.class); + + result = services.getService(ModelSet.class); + } catch (ServiceException e) { + e.printStackTrace(); + fail("Failed to create ServiceRegistry for ModelSet: " + e.getLocalizedMessage()); + } + + return result; + } + + CDOResource createUMLModel() { + CDOTransaction transaction = createTransaction(); + CDOResource result = transaction.createResource(getResourcePath("test.uml")); + + try { + ModelSet model = ServiceUtilsForResourceSet.getInstance().getModelSet(transaction.getResourceSet()); + model.createModels(result.getURI()); + } catch (ServiceException e) { + e.printStackTrace(); + fail("Failed to initialize Papyrus model: " + e.getLocalizedMessage()); + } + + result.getContents().add(createTestPackageTree()); + return result; + } + + Package createTestPackageTree() { + Package result = createPackage("test"); + + Package sub1 = createPackage("sub1"); + result.getNestedPackages().add(sub1); + sub1.getNestedPackages().add(createPackage("sub1sub1")); + sub1.getNestedPackages().add(createPackage("sub1sub2")); + result.getNestedPackages().add(createPackage("sub2")); + + return result; + } + + Package createPackage(String name) { + Package result = UMLFactory.eINSTANCE.createPackage(); + result.setName(name); + result.setURI(String.format("http://www.eclipse.org/papyrus/test/%s/2013", name)); + return result; + } + + Package getPackage(Resource resource) { + return (Package)find(resource.getContents(), instanceOf(Package.class), null); + } + + void save(Resource context) { + try { + ServiceUtilsForResource.getInstance().getModelSet(context).save(new NullProgressMonitor()); + } catch (Exception e) { + e.printStackTrace(); + fail("Failed to save model: " + e.getLocalizedMessage()); + } + } + + private ControlModeRequest request(EObject objectToControl, String fragmentName) { + ControlModeRequest result = null; + + try { + ModelSet modelSet = ServiceUtilsForEObject.getInstance().getModelSet(objectToControl); + URI fragmentURI = objectToControl.eResource().getURI().trimSegments(1).appendSegment(fragmentName); + result = new ControlModeRequest(modelSet.getTransactionalEditingDomain(), objectToControl, fragmentURI); + } catch (Exception e) { + e.printStackTrace(); + fail("Failed to create ControlModeRequest: " + e.getLocalizedMessage()); + } + + return result; + } + + void control(EObject objectToControl, String fragmentName) { + ICommand control = ControlModeManager.getInstance().getControlCommand(request(objectToControl, fragmentName)); + + try { + control.execute(new NullProgressMonitor(), null); + } catch (ExecutionException e) { + e.printStackTrace(); + fail("Failed to execute control-mode command: " + e.getLocalizedMessage()); + } + } + + private ControlModeRequest request(EObject objectToControl) { + ControlModeRequest result = null; + + try { + ModelSet modelSet = ServiceUtilsForEObject.getInstance().getModelSet(objectToControl); + result = new ControlModeRequest(modelSet.getTransactionalEditingDomain(), objectToControl); + } catch (Exception e) { + e.printStackTrace(); + fail("Failed to create ControlModeRequest: " + e.getLocalizedMessage()); + } + + return result; + } + + void uncontrol(EObject objectToControl) { + ICommand uncontrol = ControlModeManager.getInstance().getUncontrolCommand(request(objectToControl)); + + try { + uncontrol.execute(new NullProgressMonitor(), null); + } catch (ExecutionException e) { + e.printStackTrace(); + fail("Failed to execute control-mode command: " + e.getLocalizedMessage()); + } + } + + void assertProxy(EObject object, String uriSubString) { + assertThat("Not a proxy", object.eIsProxy(), is(true)); + assertThat(EcoreUtil.getURI(object).toString(), containsString(uriSubString)); + } + + void assumeProxy(EObject object, String uriSubString) { + assumeThat("Not a proxy", object.eIsProxy(), is(true)); + assumeThat(EcoreUtil.getURI(object).toString(), containsString(uriSubString)); + } + + void assertNotProxy(EObject object) { + assertThat("Object is a proxy", object.eIsProxy(), is(false)); + } + + void waitForUpdate(CDOView view, long updateTime) { + assertThat("Timed out waiting for read-only view to update.", view.waitForUpdate(updateTime, 5000L), is(true)); + } +} |