diff options
author | Christian W. Damus | 2016-11-29 17:33:45 +0000 |
---|---|---|
committer | Christian W. Damus | 2016-11-29 17:50:58 +0000 |
commit | 6e9931ef995ebed98ea22fd825f4ac5db7cccf16 (patch) | |
tree | ea3c5b925b8b41ed019b92dbc021063718b206b5 | |
parent | c2eba787d7ef716c1317e652fa6c6edbc87ada33 (diff) | |
download | org.eclipse.papyrus-6e9931ef995ebed98ea22fd825f4ac5db7cccf16.tar.gz org.eclipse.papyrus-6e9931ef995ebed98ea22fd825f4ac5db7cccf16.tar.xz org.eclipse.papyrus-6e9931ef995ebed98ea22fd825f4ac5db7cccf16.zip |
Bug 508404: [Copy/Paste] Copy and paste does not use registered EFactory
Update the CopierFactory internal API to use require a resource set
context and get the registered factory from it when available.
https://bugs.eclipse.org/bugs/show_bug.cgi?id=508404
Change-Id: Iccca171716df658f310b2a8c7b7a319f991b2002
13 files changed, 572 insertions, 96 deletions
diff --git a/plugins/infra/core/org.eclipse.papyrus.infra.core/src/org/eclipse/papyrus/infra/core/internal/clipboard/CopierFactory.java b/plugins/infra/core/org.eclipse.papyrus.infra.core/src/org/eclipse/papyrus/infra/core/internal/clipboard/CopierFactory.java index 0bbf74a1c73..9318b8fba7d 100644 --- a/plugins/infra/core/org.eclipse.papyrus.infra.core/src/org/eclipse/papyrus/infra/core/internal/clipboard/CopierFactory.java +++ b/plugins/infra/core/org.eclipse.papyrus.infra.core/src/org/eclipse/papyrus/infra/core/internal/clipboard/CopierFactory.java @@ -19,8 +19,12 @@ import java.util.concurrent.CopyOnWriteArrayList; import java.util.function.BiPredicate; import java.util.function.Supplier; +import org.eclipse.emf.ecore.EClass; +import org.eclipse.emf.ecore.EFactory; import org.eclipse.emf.ecore.EObject; +import org.eclipse.emf.ecore.EPackage; import org.eclipse.emf.ecore.EReference; +import org.eclipse.emf.ecore.resource.ResourceSet; import org.eclipse.emf.ecore.util.EcoreUtil.Copier; /** @@ -30,24 +34,61 @@ public class CopierFactory implements Supplier<Copier> { private static List<BiPredicate<? super EReference, ? super EObject>> referenceFilters = new CopyOnWriteArrayList<>(); + private final ResourceSet resourceSet; private final boolean useOriginalReferences; /** * The default copier factory that provides the usual EMF * copying semantics, except with filtering of references * as directed by {@linkplain #registerReferenceFilter(BiPredicate) registered filters}. + * + * @deprecated This does not provide copiers that account + * for {@link EFactory} instances registered in + * the context of the resource set + * @see <a href="http://eclip.se/508404">bug 508404</a> */ - public static CopierFactory DEFAULT = new CopierFactory(true); + @Deprecated + public static final CopierFactory DEFAULT = new CopierFactory(true); /** * Initializes me. * * @param useOriginalReferences * whether non-copied references should be used while copying + * + * @deprecated This does not provide copiers that account + * for {@link EFactory} instances registered in + * the context of the resource set + * @see <a href="http://eclip.se/508404">bug 508404</a> */ + @Deprecated public CopierFactory(boolean useOriginalReferences) { + this(null, useOriginalReferences); + } + + /** + * Initializes me with the resource set in which context I copy model elements. + * Copiers that I create will use original references. + * + * @param resourceSet + * my resource set context + */ + public CopierFactory(ResourceSet resourceSet) { + this(resourceSet, true); + } + + /** + * Initializes me with the resource set in which context I copy model elements. + * + * @param resourceSet + * my resource set context + * @param useOriginalReferences + * whether non-copied references should be used while copying + */ + public CopierFactory(ResourceSet resourceSet, boolean useOriginalReferences) { super(); + this.resourceSet = resourceSet; this.useOriginalReferences = useOriginalReferences; } @@ -85,11 +126,16 @@ public class CopierFactory implements Supplier<Copier> { } private Copier createReferenceFilteringCopier(BiPredicate<? super EReference, ? super EObject> referencePredicate) { - return new ReferenceFilteringCopier(true, isUseOriginalReferences(), referencePredicate); + return new ReferenceFilteringCopier(true, isUseOriginalReferences(), getPackageRegistry(), + referencePredicate); } private Copier createBasicCopier() { - return new Copier(true, isUseOriginalReferences()); + return new BasicCopier(true, isUseOriginalReferences(), getPackageRegistry()); + } + + private EPackage.Registry getPackageRegistry() { + return (resourceSet == null) ? EPackage.Registry.INSTANCE : resourceSet.getPackageRegistry(); } /** @@ -106,14 +152,43 @@ public class CopierFactory implements Supplier<Copier> { // Nested types // - private static class ReferenceFilteringCopier extends Copier { + private static class BasicCopier extends Copier { private static final long serialVersionUID = 1L; - private final BiPredicate<? super EReference, ? super EObject> referencePredicate; + private final EPackage.Registry packageRegistry; - ReferenceFilteringCopier(boolean resolveReferences, boolean useOriginalReferences, BiPredicate<? super EReference, ? super EObject> referencePredicate) { + BasicCopier(boolean resolveReferences, boolean useOriginalReferences, EPackage.Registry registry) { super(resolveReferences, useOriginalReferences); + this.packageRegistry = registry; + } + + @Override + protected EObject createCopy(EObject eObject) { + EClass eClass = getTarget(eObject); + EFactory eFactory = getEFactory(eClass); + return eFactory.create(eClass); + } + + EFactory getEFactory(EClass eClass) { + EFactory result = packageRegistry.getEFactory(eClass.getEPackage().getNsURI()); + + if (result == null) { + result = eClass.getEPackage().getEFactoryInstance(); + } + + return result; + } + } + + private static class ReferenceFilteringCopier extends BasicCopier { + private static final long serialVersionUID = 1L; + + private final BiPredicate<? super EReference, ? super EObject> referencePredicate; + + ReferenceFilteringCopier(boolean resolveReferences, boolean useOriginalReferences, EPackage.Registry registry, BiPredicate<? super EReference, ? super EObject> referencePredicate) { + super(resolveReferences, useOriginalReferences, registry); + this.referencePredicate = referencePredicate; } diff --git a/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/commands/DefaultCopyCommand.java b/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/commands/DefaultCopyCommand.java index 7c7abf28f38..adf7fd47aaa 100644 --- a/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/commands/DefaultCopyCommand.java +++ b/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/commands/DefaultCopyCommand.java @@ -8,7 +8,7 @@ * * Contributors: * Benoit Maggi (CEA LIST) benoit.maggi@cea.fr - Initial API and implementation - * Christian W. Damus - bug 502461 + * Christian W. Damus - bugs 502461, 508404 *****************************************************************************/ package org.eclipse.papyrus.infra.gmfdiag.common.commands; @@ -50,7 +50,7 @@ public class DefaultCopyCommand extends AbstractOverrideableCommand implements N super(domain); objectsToPutInClipboard = new ArrayList<Object>(); boolean keepReferences = Activator.getInstance().getPreferenceStore().getBoolean(PastePreferencesPage.KEEP_EXTERNAL_REFERENCES); - EcoreUtil.Copier copier = new CopierFactory(keepReferences).get(); + EcoreUtil.Copier copier = new CopierFactory(domain.getResourceSet(), keepReferences).get(); copier.copyAll(pObjectsToPutInClipboard); copier.copyReferences(); papyrusClipboard.addAllInternalCopyInClipboard(copier); diff --git a/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/commands/DefaultDiagramCopyCommand.java b/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/commands/DefaultDiagramCopyCommand.java index 9b174215559..f3e2d3f8cf5 100644 --- a/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/commands/DefaultDiagramCopyCommand.java +++ b/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/commands/DefaultDiagramCopyCommand.java @@ -1,5 +1,5 @@ /*****************************************************************************
- * Copyright (c) 2014 CEA LIST.
+ * Copyright (c) 2014, 2016 CEA LIST, 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,6 +8,7 @@ *
* Contributors:
* Benoit Maggi (CEA LIST) benoit.maggi@cea.fr - Initial API and implementation
+ * Christian W. Damus - bug 508404
*****************************************************************************/
package org.eclipse.papyrus.infra.gmfdiag.common.commands;
@@ -26,6 +27,7 @@ import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart; import org.eclipse.gmf.runtime.notation.Diagram;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.papyrus.infra.core.clipboard.PapyrusClipboard;
+import org.eclipse.papyrus.infra.core.internal.clipboard.CopierFactory;
import org.eclipse.papyrus.infra.gmfdiag.common.Activator;
import org.eclipse.papyrus.infra.gmfdiag.common.preferences.PastePreferencesPage;
@@ -54,7 +56,7 @@ public class DefaultDiagramCopyCommand extends AbstractOverrideableCommand imple super(domain);
objectsToPutInClipboard = new ArrayList<Object>();
Boolean keepReferences = Activator.getInstance().getPreferenceStore().getBoolean(PastePreferencesPage.KEEP_EXTERNAL_REFERENCES);
- EcoreUtil.Copier copier = new EcoreUtil.Copier(Boolean.TRUE, keepReferences);
+ EcoreUtil.Copier copier = new CopierFactory(domain.getResourceSet(), keepReferences).get();
List<EObject> objectToCopy = new ArrayList<EObject>();
if (pObjectsToPutInClipboard != null) {
@@ -78,7 +80,7 @@ public class DefaultDiagramCopyCommand extends AbstractOverrideableCommand imple if (pObjectsToPutInClipboard != null && !pObjectsToPutInClipboard.isEmpty()) {
IGraphicalEditPart next = pObjectsToPutInClipboard.iterator().next();
Diagram diagram = next.getNotationView().getDiagram();
- if(diagram != null){
+ if (diagram != null) {
papyrusClipboard.setContainerType(diagram.getType());
}
}
diff --git a/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/commands/DefaultDiagramPasteCommand.java b/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/commands/DefaultDiagramPasteCommand.java index fc687254f35..1c89dd51099 100644 --- a/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/commands/DefaultDiagramPasteCommand.java +++ b/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/commands/DefaultDiagramPasteCommand.java @@ -1,5 +1,5 @@ /***************************************************************************** - * Copyright (c) 2014 CEA LIST. + * Copyright (c) 2014, 2016 CEA LIST, 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,6 +8,7 @@ * * Contributors: * Benoit Maggi (CEA LIST) benoit.maggi@cea.fr - Initial API and implementation + * Christian W. Damus - bug 508404 *****************************************************************************/ package org.eclipse.papyrus.infra.gmfdiag.common.commands; @@ -46,6 +47,7 @@ import org.eclipse.gmf.runtime.notation.LayoutConstraint; import org.eclipse.gmf.runtime.notation.Shape; import org.eclipse.gmf.runtime.notation.View; import org.eclipse.papyrus.infra.core.clipboard.PapyrusClipboard; +import org.eclipse.papyrus.infra.core.internal.clipboard.CopierFactory; import org.eclipse.papyrus.infra.services.edit.service.ElementEditServiceUtils; import org.eclipse.papyrus.infra.services.edit.service.IElementEditService; import org.eclipse.swt.widgets.Control; @@ -92,7 +94,7 @@ public class DefaultDiagramPasteCommand extends AbstractTransactionalCommand { this.container = (View) targetEditPart.getModel(); this.targetEditPart = targetEditPart; - EcoreUtil.Copier copier = new EcoreUtil.Copier(); + EcoreUtil.Copier copier = new CopierFactory(editingDomain.getResourceSet()).get(); List<EObject> rootElementInClipboard = EcoreUtil.filterDescendants(filterEObject(papyrusClipboard)); copier.copyAll(rootElementInClipboard); diff --git a/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/commands/DefaultPasteCommand.java b/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/commands/DefaultPasteCommand.java index 4fd0ba49aef..888be902165 100644 --- a/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/commands/DefaultPasteCommand.java +++ b/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/commands/DefaultPasteCommand.java @@ -8,7 +8,7 @@ * * Contributors: * Benoit Maggi (CEA LIST) benoit.maggi@cea.fr - Initial API and implementation - * Christian W. Damus - bug 502461 + * Christian W. Damus - bugs 502461, 508404 *****************************************************************************/ package org.eclipse.papyrus.infra.gmfdiag.common.commands; @@ -71,7 +71,7 @@ public class DefaultPasteCommand extends AbstractCommand { List<EObject> rootElementToPaste = EcoreUtil.filterDescendants(eobjectsTopaste); // Copy all eObjects (inspired from PasteFromClipboardCommand) - EcoreUtil.Copier copier = CopierFactory.DEFAULT.get(); + EcoreUtil.Copier copier = new CopierFactory(domain.getResourceSet()).get(); copier.copyAll(rootElementToPaste); copier.copyReferences(); Map<EObject, EObject> duplicatedObjects = new HashMap<EObject, EObject>(); diff --git a/tests/junit/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common.tests/src/org/eclipse/papyrus/infra/gmfdiag/common/commands/tests/CreateEditBasedElementCommandTest.java b/tests/junit/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common.tests/src/org/eclipse/papyrus/infra/gmfdiag/common/commands/tests/CreateEditBasedElementCommandTest.java index a4d2d200e2b..a11e1c67c51 100644 --- a/tests/junit/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common.tests/src/org/eclipse/papyrus/infra/gmfdiag/common/commands/tests/CreateEditBasedElementCommandTest.java +++ b/tests/junit/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common.tests/src/org/eclipse/papyrus/infra/gmfdiag/common/commands/tests/CreateEditBasedElementCommandTest.java @@ -13,29 +13,15 @@ package org.eclipse.papyrus.infra.gmfdiag.common.commands.tests; -import static org.hamcrest.CoreMatchers.hasItem; import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertThat; -import java.lang.reflect.Proxy; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; - -import org.eclipse.emf.ecore.EClass; -import org.eclipse.emf.ecore.EFactory; -import org.eclipse.emf.ecore.EObject; -import org.eclipse.emf.ecore.EPackage; -import org.eclipse.emf.ecore.InternalEObject; import org.eclipse.gmf.runtime.emf.type.core.requests.CreateElementRequest; import org.eclipse.papyrus.infra.gmfdiag.common.commands.CreateEditBasedElementCommand; import org.eclipse.papyrus.junit.utils.rules.ModelSetFixture; import org.eclipse.papyrus.junit.utils.rules.PluginResource; import org.eclipse.papyrus.junit.utils.rules.ServiceRegistryModelSetFixture; import org.eclipse.papyrus.uml.service.types.element.UMLElementTypes; -import org.eclipse.uml2.uml.UMLFactory; -import org.eclipse.uml2.uml.UMLPackage; -import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -45,81 +31,21 @@ import org.junit.Test; @PluginResource("models/ExpansionModelTest.di") public class CreateEditBasedElementCommandTest { - @Rule - public final ModelSetFixture fixture = new ServiceRegistryModelSetFixture(); + private final ModelSetFixture model = new ServiceRegistryModelSetFixture(); - private List<String> invocations = new ArrayList<>(); + @Rule + public final CustomUMLFactoryFixture fixture = new CustomUMLFactoryFixture(model); @Test public void createElement() { - CreateElementRequest request = new CreateElementRequest(fixture.getModel(), UMLElementTypes.CLASS); + CreateElementRequest request = new CreateElementRequest(model.getModel(), UMLElementTypes.CLASS); CreateEditBasedElementCommand command = new CreateEditBasedElementCommand(request); assertThat(command.canExecute(), is(true)); - fixture.execute(command); + model.execute(command); // A new class is always given a default name by edit advice - assertThat(invocations, hasItem("setName")); - } - - // - // Test framework - // - - @Before - public void installUMLFactory() { - // Install reflective proxies for the standard package and factory that - // create reflective proxies for model elements - ClassLoader cl = getClass().getClassLoader(); - - EFactory[] eFactory = { null }; - EPackage[] ePackage = { null }; - - ePackage[0] = (EPackage) Proxy.newProxyInstance(cl, new Class[] { UMLPackage.class }, - (__, method, args) -> { - switch (method.getName()) { - case "getEFactoryInstance": - case "getUMLFactory": - return eFactory[0]; - default: - return method.invoke(UMLPackage.eINSTANCE, args); - } - }); - eFactory[0] = (EFactory) Proxy.newProxyInstance(cl, new Class[] { UMLFactory.class }, - (__, method, args) -> { - switch (method.getName()) { - case "getEPackage": - case "getUMLPackage": - return ePackage[0]; - case "create": - EClass eClass = (EClass) args[0]; - EObject realObject = (EObject) method.invoke(UMLFactory.eINSTANCE, args); - return Proxy.newProxyInstance(cl, new Class[] { - eClass.getInstanceClass(), - InternalEObject.class, - }, (___, method1, args1) -> { - recordInvocation(method1.getName()); - return method1.invoke(realObject, args1); - }); - default: - return method.invoke(UMLFactory.eINSTANCE, args); - } - }); - - EPackage.Registry registry = fixture.getResourceSet().getPackageRegistry(); - - // Replace all registrations of the UML package - for (Map.Entry<String, Object> next : registry.entrySet()) { - if (next.getValue() instanceof UMLPackage) { - next.setValue(ePackage[0]); - } - } - - // Just to make sure - registry.put(ePackage[0].getNsURI(), ePackage[0]); + fixture.assertInvocation("setName"); } - void recordInvocation(String methodName) { - invocations.add(methodName); - } } diff --git a/tests/junit/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common.tests/src/org/eclipse/papyrus/infra/gmfdiag/common/commands/tests/CustomUMLFactoryFixture.java b/tests/junit/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common.tests/src/org/eclipse/papyrus/infra/gmfdiag/common/commands/tests/CustomUMLFactoryFixture.java new file mode 100644 index 00000000000..966b6ec48b4 --- /dev/null +++ b/tests/junit/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common.tests/src/org/eclipse/papyrus/infra/gmfdiag/common/commands/tests/CustomUMLFactoryFixture.java @@ -0,0 +1,149 @@ +/***************************************************************************** + * Copyright (c) 2016 CEA LIST 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: + * CEA LIST - Initial API and implementation + * + *****************************************************************************/ + +package org.eclipse.papyrus.infra.gmfdiag.common.commands.tests; + +import static org.hamcrest.CoreMatchers.hasItem; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; + +import java.lang.reflect.Proxy; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +import org.eclipse.core.commands.operations.IUndoableOperation; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.emf.common.command.Command; +import org.eclipse.emf.ecore.EClass; +import org.eclipse.emf.ecore.EFactory; +import org.eclipse.emf.ecore.EObject; +import org.eclipse.emf.ecore.EPackage; +import org.eclipse.emf.ecore.InternalEObject; +import org.eclipse.papyrus.junit.utils.rules.AbstractModelFixture; +import org.eclipse.uml2.uml.UMLFactory; +import org.eclipse.uml2.uml.UMLPackage; +import org.hamcrest.Matcher; +import org.junit.rules.TestRule; +import org.junit.runner.Description; +import org.junit.runners.model.Statement; + +/** + * A wrapper for a {@code ResourceSetFixture} that installs a custom UML Factory + * in its resource set and provides assertions that test exercise of that factory. + */ +public class CustomUMLFactoryFixture implements TestRule { + + private final AbstractModelFixture<?> resourceSet; + + private final List<String> invocations = new ArrayList<>(); + + public CustomUMLFactoryFixture(AbstractModelFixture<?> resourceSet) { + super(); + + this.resourceSet = resourceSet; + } + + @Override + public Statement apply(Statement base, Description description) { + Statement myStatement = new Statement() { + + @Override + public void evaluate() throws Throwable { + installUMLFactory(); + base.evaluate(); + } + }; + + return resourceSet.apply(myStatement, description); + } + + public void assertInvocation(Matcher<? super String> matcher) { + assertThat(invocations, hasItem(matcher)); + } + + public void assertInvocation(String methodName) { + assertInvocation(is(methodName)); + } + + public void reset() { + invocations.clear(); + } + + public IStatus execute(IUndoableOperation operation) { + reset(); + return resourceSet.execute(operation); + } + + public void execute(Command command) { + reset(); + resourceSet.execute(command); + } + + private void installUMLFactory() { + // Install reflective proxies for the standard package and factory that + // create reflective proxies for model elements + ClassLoader cl = getClass().getClassLoader(); + + EFactory[] eFactory = { null }; + EPackage[] ePackage = { null }; + + ePackage[0] = (EPackage) Proxy.newProxyInstance(cl, new Class[] { UMLPackage.class }, + (__, method, args) -> { + switch (method.getName()) { + case "getEFactoryInstance": + case "getUMLFactory": + return eFactory[0]; + default: + return method.invoke(UMLPackage.eINSTANCE, args); + } + }); + eFactory[0] = (EFactory) Proxy.newProxyInstance(cl, new Class[] { UMLFactory.class }, + (__, method, args) -> { + switch (method.getName()) { + case "getEPackage": + case "getUMLPackage": + return ePackage[0]; + case "create": + EClass eClass = (EClass) args[0]; + EObject realObject = (EObject) method.invoke(UMLFactory.eINSTANCE, args); + return Proxy.newProxyInstance(cl, new Class[] { + eClass.getInstanceClass(), + InternalEObject.class, + }, (___, method1, args1) -> { + recordInvocation(method1.getName()); + return method1.invoke(realObject, args1); + }); + default: + return method.invoke(UMLFactory.eINSTANCE, args); + } + }); + + EPackage.Registry registry = resourceSet.getResourceSet().getPackageRegistry(); + + // Replace all registrations of the UML package + for (Map.Entry<String, Object> next : registry.entrySet()) { + if (next.getValue() instanceof UMLPackage) { + next.setValue(ePackage[0]); + } + } + + // Just to make sure + registry.put(ePackage[0].getNsURI(), ePackage[0]); + } + + private void recordInvocation(String methodName) { + invocations.add(methodName); + } + +} diff --git a/tests/junit/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common.tests/src/org/eclipse/papyrus/infra/gmfdiag/common/commands/tests/DefaultCopyCommandTest.java b/tests/junit/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common.tests/src/org/eclipse/papyrus/infra/gmfdiag/common/commands/tests/DefaultCopyCommandTest.java new file mode 100644 index 00000000000..f6963d0e9a3 --- /dev/null +++ b/tests/junit/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common.tests/src/org/eclipse/papyrus/infra/gmfdiag/common/commands/tests/DefaultCopyCommandTest.java @@ -0,0 +1,58 @@ +/***************************************************************************** + * Copyright (c) 2016 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.gmfdiag.common.commands.tests; + +import static java.util.Collections.singletonList; +import static org.hamcrest.CoreMatchers.containsString; +import static org.hamcrest.CoreMatchers.instanceOf; +import static org.junit.Assert.assertThat; + +import org.eclipse.papyrus.infra.gmfdiag.common.commands.DefaultCopyCommand; +import org.eclipse.papyrus.junit.utils.rules.ModelSetFixture; +import org.eclipse.papyrus.junit.utils.rules.PluginResource; +import org.eclipse.papyrus.junit.utils.rules.ServiceRegistryModelSetFixture; +import org.junit.Rule; +import org.junit.Test; + +/** + * Tests for the {@link DefaultCopyCommand} class. + */ +@PluginResource("models/ExpansionModelTest.di") +public class DefaultCopyCommandTest { + + private final ModelSetFixture model = new ServiceRegistryModelSetFixture(); + + @Rule + public final CustomUMLFactoryFixture fixture = new CustomUMLFactoryFixture(model); + + @Rule + public final PapyrusClipboardFixture clipboard = new PapyrusClipboardFixture(); + + @Test + public void copyElement() { + org.eclipse.uml2.uml.Class myClass = (org.eclipse.uml2.uml.Class) model.getModel().getOwnedType("MyClass"); + + fixture.reset(); + + new DefaultCopyCommand(model.getEditingDomain(), clipboard, singletonList(myClass)).dispose(); + + // The copied class has its name set via a structural feature setting + fixture.assertInvocation("eSetting"); + + assertThat(clipboard.getCopyFromSource(myClass), instanceOf(org.eclipse.uml2.uml.Class.class)); + org.eclipse.uml2.uml.Class copy = (org.eclipse.uml2.uml.Class) clipboard.getCopyFromSource(myClass); + assertThat(copy.getName(), containsString(myClass.getName())); + } + +} diff --git a/tests/junit/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common.tests/src/org/eclipse/papyrus/infra/gmfdiag/common/commands/tests/DefaultDiagramCopyCommandTest.java b/tests/junit/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common.tests/src/org/eclipse/papyrus/infra/gmfdiag/common/commands/tests/DefaultDiagramCopyCommandTest.java new file mode 100644 index 00000000000..655618bfc20 --- /dev/null +++ b/tests/junit/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common.tests/src/org/eclipse/papyrus/infra/gmfdiag/common/commands/tests/DefaultDiagramCopyCommandTest.java @@ -0,0 +1,60 @@ +/***************************************************************************** + * Copyright (c) 2016 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.gmfdiag.common.commands.tests; + +import static java.util.Collections.singletonList; +import static org.hamcrest.CoreMatchers.containsString; +import static org.hamcrest.CoreMatchers.instanceOf; +import static org.junit.Assert.assertThat; + +import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart; +import org.eclipse.papyrus.infra.gmfdiag.common.commands.DefaultDiagramCopyCommand; +import org.eclipse.papyrus.junit.utils.rules.ActiveDiagram; +import org.eclipse.papyrus.junit.utils.rules.PapyrusEditorFixture; +import org.eclipse.papyrus.junit.utils.rules.PluginResource; +import org.junit.Rule; +import org.junit.Test; + +/** + * Tests for the {@link DefaultDiagramCopyCommand} class. + */ +@PluginResource("models/ExpansionModelTest.di") +@ActiveDiagram("NewDiagram") +public class DefaultDiagramCopyCommandTest { + + private final PapyrusEditorFixture editor = new PapyrusEditorFixture(); + + @Rule + public final CustomUMLFactoryFixture fixture = new CustomUMLFactoryFixture(editor); + + @Rule + public final PapyrusClipboardFixture clipboard = new PapyrusClipboardFixture(); + + @Test + public void copyElement() { + org.eclipse.uml2.uml.Class myClass = (org.eclipse.uml2.uml.Class) editor.getModel().getOwnedType("MyClass"); + IGraphicalEditPart editPart = (IGraphicalEditPart) editor.findEditPart(myClass); + fixture.reset(); + + new DefaultDiagramCopyCommand(editor.getEditingDomain(), clipboard, singletonList(editPart)).dispose(); + + // The copied class has its name set via a structural feature setting + fixture.assertInvocation("eSetting"); + + assertThat(clipboard.getCopyFromSource(myClass), instanceOf(org.eclipse.uml2.uml.Class.class)); + org.eclipse.uml2.uml.Class copy = (org.eclipse.uml2.uml.Class) clipboard.getCopyFromSource(myClass); + assertThat(copy.getName(), containsString(myClass.getName())); + } + +} diff --git a/tests/junit/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common.tests/src/org/eclipse/papyrus/infra/gmfdiag/common/commands/tests/DefaultDiagramPasteCommandTest.java b/tests/junit/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common.tests/src/org/eclipse/papyrus/infra/gmfdiag/common/commands/tests/DefaultDiagramPasteCommandTest.java new file mode 100644 index 00000000000..eff3c39d080 --- /dev/null +++ b/tests/junit/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common.tests/src/org/eclipse/papyrus/infra/gmfdiag/common/commands/tests/DefaultDiagramPasteCommandTest.java @@ -0,0 +1,75 @@ +/***************************************************************************** + * Copyright (c) 2016 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.gmfdiag.common.commands.tests; + +import static java.util.Collections.singletonList; +import static org.hamcrest.CoreMatchers.containsString; +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; + +import java.util.List; +import java.util.stream.Collectors; + +import org.eclipse.emf.common.command.Command; +import org.eclipse.gmf.runtime.common.core.command.ICommand; +import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart; +import org.eclipse.papyrus.infra.gmfdiag.common.commands.DefaultDiagramCopyCommand; +import org.eclipse.papyrus.infra.gmfdiag.common.commands.DefaultDiagramPasteCommand; +import org.eclipse.papyrus.junit.utils.rules.ActiveDiagram; +import org.eclipse.papyrus.junit.utils.rules.PapyrusEditorFixture; +import org.eclipse.papyrus.junit.utils.rules.PluginResource; +import org.eclipse.uml2.uml.NamedElement; +import org.junit.Rule; +import org.junit.Test; + +/** + * Tests for the {@link DefaultDiagramPasteCommand} class. + */ +@PluginResource("models/ExpansionModelTest.di") +@ActiveDiagram("NewDiagram") +public class DefaultDiagramPasteCommandTest { + + private final PapyrusEditorFixture editor = new PapyrusEditorFixture(); + + @Rule + public final CustomUMLFactoryFixture fixture = new CustomUMLFactoryFixture(editor); + + @Rule + public final PapyrusClipboardFixture clipboard = new PapyrusClipboardFixture(); + + @Test + public void pasteElement() { + org.eclipse.uml2.uml.Class myClass = (org.eclipse.uml2.uml.Class) editor.getModel().getOwnedType("MyClass"); + IGraphicalEditPart editPart = (IGraphicalEditPart) editor.findEditPart(myClass); + + Command copy = new DefaultDiagramCopyCommand(editor.getEditingDomain(), clipboard, singletonList(editPart)); + + fixture.execute(copy); + + ICommand paste = new DefaultDiagramPasteCommand(editor.getEditingDomain(), "Paste", clipboard, editor.getActiveDiagram()); + + // The copied class has its name set via a structural feature setting + fixture.assertInvocation("eSetting"); + + fixture.execute(paste); + + // Find the copy + List<String> typeNamesLikeMyClass = editor.getModel().getOwnedTypes().stream() + .map(NamedElement::getName) + .filter(containsString(myClass.getName())::matches) + .collect(Collectors.toList()); + assertThat(typeNamesLikeMyClass.size(), is(2)); + } + +} diff --git a/tests/junit/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common.tests/src/org/eclipse/papyrus/infra/gmfdiag/common/commands/tests/DefaultPasteCommandTest.java b/tests/junit/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common.tests/src/org/eclipse/papyrus/infra/gmfdiag/common/commands/tests/DefaultPasteCommandTest.java new file mode 100644 index 00000000000..8b30a5fc46c --- /dev/null +++ b/tests/junit/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common.tests/src/org/eclipse/papyrus/infra/gmfdiag/common/commands/tests/DefaultPasteCommandTest.java @@ -0,0 +1,71 @@ +/***************************************************************************** + * Copyright (c) 2016 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.gmfdiag.common.commands.tests; + +import static java.util.Collections.singletonList; +import static org.hamcrest.CoreMatchers.containsString; +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; + +import java.util.List; +import java.util.stream.Collectors; + +import org.eclipse.emf.common.command.Command; +import org.eclipse.papyrus.infra.gmfdiag.common.commands.DefaultCopyCommand; +import org.eclipse.papyrus.infra.gmfdiag.common.commands.DefaultPasteCommand; +import org.eclipse.papyrus.junit.utils.rules.ModelSetFixture; +import org.eclipse.papyrus.junit.utils.rules.PluginResource; +import org.eclipse.papyrus.junit.utils.rules.ServiceRegistryModelSetFixture; +import org.eclipse.uml2.uml.NamedElement; +import org.junit.Rule; +import org.junit.Test; + +/** + * Tests for the {@link DefaultPasteCommand} class. + */ +@PluginResource("models/ExpansionModelTest.di") +public class DefaultPasteCommandTest { + + private final ModelSetFixture model = new ServiceRegistryModelSetFixture(); + + @Rule + public final CustomUMLFactoryFixture fixture = new CustomUMLFactoryFixture(model); + + @Rule + public final PapyrusClipboardFixture clipboard = new PapyrusClipboardFixture(); + + @Test + public void pasteElement() { + org.eclipse.uml2.uml.Class myClass = (org.eclipse.uml2.uml.Class) model.getModel().getOwnedType("MyClass"); + + Command copy = new DefaultCopyCommand(model.getEditingDomain(), clipboard, singletonList(myClass)); + + fixture.execute(copy); + + Command paste = new DefaultPasteCommand(model.getEditingDomain(), model.getModel(), clipboard); + + // The copied class has its name set via a structural feature setting + fixture.assertInvocation("eSetting"); + + fixture.execute(paste); + + // Find the copy + List<String> typeNamesLikeMyClass = model.getModel().getOwnedTypes().stream() + .map(NamedElement::getName) + .filter(containsString(myClass.getName())::matches) + .collect(Collectors.toList()); + assertThat(typeNamesLikeMyClass.size(), is(2)); + } + +} diff --git a/tests/junit/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common.tests/src/org/eclipse/papyrus/infra/gmfdiag/common/commands/tests/PapyrusClipboardFixture.java b/tests/junit/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common.tests/src/org/eclipse/papyrus/infra/gmfdiag/common/commands/tests/PapyrusClipboardFixture.java new file mode 100644 index 00000000000..c4c1932e04c --- /dev/null +++ b/tests/junit/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common.tests/src/org/eclipse/papyrus/infra/gmfdiag/common/commands/tests/PapyrusClipboardFixture.java @@ -0,0 +1,50 @@ +/***************************************************************************** + * Copyright (c) 2016 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.gmfdiag.common.commands.tests; + +import org.eclipse.papyrus.infra.core.clipboard.PapyrusClipboard; +import org.junit.rules.TestRule; +import org.junit.runner.Description; +import org.junit.runners.model.Statement; + +/** + * A specialization of the {@link PapyrusClipboard} that installs itself as the + * shared clipboard for the duration of a test. + */ +public class PapyrusClipboardFixture extends PapyrusClipboard<Object> implements TestRule { + + private static final long serialVersionUID = 1L; + + public PapyrusClipboardFixture() { + super(); + } + + @Override + public Statement apply(Statement base, Description description) { + return new Statement() { + @Override + public void evaluate() throws Throwable { + PapyrusClipboard<Object> oldInstance = PapyrusClipboard.getInstance(); + PapyrusClipboard.setInstance(PapyrusClipboardFixture.this); + + try { + base.evaluate(); + } finally { + PapyrusClipboard.setInstance(oldInstance); + } + } + }; + } + +} diff --git a/tests/junit/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common.tests/src/org/eclipse/papyrus/infra/gmfdiag/common/tests/AllTests.java b/tests/junit/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common.tests/src/org/eclipse/papyrus/infra/gmfdiag/common/tests/AllTests.java index e5a16370b03..207337bcd5e 100644 --- a/tests/junit/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common.tests/src/org/eclipse/papyrus/infra/gmfdiag/common/tests/AllTests.java +++ b/tests/junit/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common.tests/src/org/eclipse/papyrus/infra/gmfdiag/common/tests/AllTests.java @@ -8,11 +8,15 @@ * * Contributors: * Christian W. Damus (CEA) - initial API and implementation - * Christian W. Damus - bugs 465416, 474467, 507618 + * Christian W. Damus - bugs 465416, 474467, 507618, 508404 */ package org.eclipse.papyrus.infra.gmfdiag.common.tests; import org.eclipse.papyrus.infra.gmfdiag.common.commands.tests.CreateEditBasedElementCommandTest; +import org.eclipse.papyrus.infra.gmfdiag.common.commands.tests.DefaultCopyCommandTest; +import org.eclipse.papyrus.infra.gmfdiag.common.commands.tests.DefaultDiagramCopyCommandTest; +import org.eclipse.papyrus.infra.gmfdiag.common.commands.tests.DefaultDiagramPasteCommandTest; +import org.eclipse.papyrus.infra.gmfdiag.common.commands.tests.DefaultPasteCommandTest; import org.eclipse.papyrus.infra.gmfdiag.common.providers.tests.NotationLabelProviderTest; import org.eclipse.papyrus.infra.gmfdiag.common.sync.tests.SyncTests; import org.eclipse.papyrus.infra.gmfdiag.common.utils.GMFUnsafeTest; @@ -39,6 +43,10 @@ import org.junit.runners.Suite.SuiteClasses; SyncTests.class, NotationLabelProviderTest.class, CreateEditBasedElementCommandTest.class, + DefaultCopyCommandTest.class, + DefaultPasteCommandTest.class, + DefaultDiagramCopyCommandTest.class, + DefaultDiagramPasteCommandTest.class, }) public class AllTests { |