Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMaxime Porhel2018-09-28 11:57:38 +0000
committerMaxime Porhel2018-09-28 13:37:27 +0000
commit6e23ad466b959068d9444664984d1ca59245fcb8 (patch)
treebfb1afe4cc2f228325f13beeb8aa8803a41a1c8d
parent828c9cb50d2669c928f20b941507698dde9bc0b6 (diff)
downloadorg.eclipse.sirius-6e23ad466b959068d9444664984d1ca59245fcb8.tar.gz
org.eclipse.sirius-6e23ad466b959068d9444664984d1ca59245fcb8.tar.xz
org.eclipse.sirius-6e23ad466b959068d9444664984d1ca59245fcb8.zip
[525261] Do not duplicate uids while copying the representations
Bug: 525261 Change-Id: I623b3e99c231a2ae1f1be61f6416c477d8996a0c Signed-off-by: Maxime Porhel <maxime.porhel@obeo.fr>
-rw-r--r--plugins/org.eclipse.sirius.tests.junit/src/org/eclipse/sirius/tests/unit/diagram/copier/RepresentationCopierTest.java65
-rw-r--r--plugins/org.eclipse.sirius/src/org/eclipse/sirius/business/api/dialect/AbstractRepresentationDialectServices.java12
2 files changed, 69 insertions, 8 deletions
diff --git a/plugins/org.eclipse.sirius.tests.junit/src/org/eclipse/sirius/tests/unit/diagram/copier/RepresentationCopierTest.java b/plugins/org.eclipse.sirius.tests.junit/src/org/eclipse/sirius/tests/unit/diagram/copier/RepresentationCopierTest.java
index 5117522bad..3db1da2932 100644
--- a/plugins/org.eclipse.sirius.tests.junit/src/org/eclipse/sirius/tests/unit/diagram/copier/RepresentationCopierTest.java
+++ b/plugins/org.eclipse.sirius.tests.junit/src/org/eclipse/sirius/tests/unit/diagram/copier/RepresentationCopierTest.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2010, 2014 THALES GLOBAL SERVICES.
+ * Copyright (c) 2010, 2018 THALES GLOBAL SERVICES.
* 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
@@ -14,16 +14,21 @@ import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
+import java.util.stream.Collectors;
+import java.util.stream.StreamSupport;
import org.eclipse.core.runtime.NullProgressMonitor;
+import org.eclipse.emf.ecore.EAttribute;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.util.EcoreUtil;
+import org.eclipse.emf.ecore.util.EcoreUtil.EqualityHelper;
import org.eclipse.emf.transaction.RecordingCommand;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.emf.transaction.util.TransactionUtil;
import org.eclipse.gmf.runtime.notation.Diagram;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.sirius.business.api.dialect.DialectManager;
+import org.eclipse.sirius.common.tools.api.util.StringUtil;
import org.eclipse.sirius.diagram.DDiagram;
import org.eclipse.sirius.diagram.DDiagramElement;
import org.eclipse.sirius.diagram.DSemanticDiagram;
@@ -34,6 +39,8 @@ import org.eclipse.sirius.tests.unit.diagram.GenericTestCase;
import org.eclipse.sirius.tests.unit.diagram.modelers.uml.UML2ModelerConstants;
import org.eclipse.sirius.ui.business.api.dialect.DialectUIManager;
import org.eclipse.sirius.viewpoint.DRepresentation;
+import org.eclipse.sirius.viewpoint.IdentifiedElement;
+import org.eclipse.sirius.viewpoint.ViewpointPackage;
import org.eclipse.sirius.viewpoint.description.Viewpoint;
import org.eclipse.ui.IEditorPart;
@@ -149,16 +156,72 @@ public class RepresentationCopierTest extends GenericTestCase implements UML2Mod
assertEquals(numberOfSemanticElementsBeforeCopy, numberOfSemanticElementsAfterCopy);
}
+ public void testNoDuplicatedUids() throws Exception {
+ final String newRepresentationName = "New Representation Space Name ";
+ DDiagram copy = copyDiagram(newRepresentationName);
+ /* check that copy has the name asked */
+ assertTrue(originalDiagram != copy);
+ assertFalse(originalDiagram.getUid().equals(copy.getUid()));
+
+ checkContentUids(originalDiagram, copy);
+
+ copy = copyUseCaseDiagram(newRepresentationName);
+ /* check that copy has the name asked */
+ assertTrue(originalUseCaseDiagram != copy);
+ assertFalse(originalUseCaseDiagram.getUid().equals(copy.getUid()));
+
+ }
+
+ private void checkContentUids(DDiagram origin, DDiagram copy) {
+ List<IdentifiedElement> copied = collectAllIdentifiedElements(originalDiagram);
+ List<IdentifiedElement> copies = collectAllIdentifiedElements(copy);
+
+ assertEquals("Copied and copies lists must have the same size.", copied.size(), copies.size());
+
+ // Specific equality helper which does not check id attributes nor containment lists.
+ // The containments list size/contents validity is ensured by check on the order of the copied/copies lists
+ // and their computation from the eAllContents methods.
+ EqualityHelper helper = new EqualityHelper() {
+
+ @Override
+ protected boolean haveEqualAttribute(EObject eObject1, EObject eObject2, EAttribute attribute) {
+ boolean ignored = attribute.isID() || ViewpointPackage.Literals.DREPRESENTATION__NAME.equals(attribute);
+ return ignored || super.haveEqualAttribute(eObject1, eObject2, attribute);
+ }
+ };
+ assertTrue(helper.equals(originalDiagram, copy));
+
+ for (int i = 0; i < copied.size(); i++) {
+ IdentifiedElement copiedElt = copied.get(i);
+ IdentifiedElement copyElt = copies.get(i);
+
+ boolean equality = helper.equals(copiedElt, copyElt);
+ boolean nullUIds = StringUtil.isEmpty(copiedElt.getUid()) || StringUtil.isEmpty(copyElt.getUid());
+ boolean duplicatedUids = copiedElt.getUid().equals(copyElt.getUid());
+
+ assertTrue("The only difference found between those two elements must be their uid. See " + copiedElt + " and its copy " + copyElt + ".", equality && !nullUIds && !duplicatedUids);
+ }
+ }
+
+ private List<IdentifiedElement> collectAllIdentifiedElements(DDiagram diagram) {
+ Iterable<EObject> it = () -> diagram.eAllContents();
+ List<IdentifiedElement> identifiedElements = StreamSupport.stream(it.spliterator(), false).filter(IdentifiedElement.class::isInstance).map(IdentifiedElement.class::cast)
+ .collect(Collectors.toList());
+ return identifiedElements;
+ }
+
public void testCopiedRepresentationName() throws Exception {
final String newRepresentationName = "New Representation Space Name ";
DDiagram copy = copyDiagram(newRepresentationName);
/* check that copy has the name asked */
assertTrue(originalDiagram != copy);
+ assertFalse(originalDiagram.getUid().equals(copy.getUid()));
assertEquals(newRepresentationName, copy.getName());
copy = copyUseCaseDiagram(newRepresentationName);
/* check that copy has the name asked */
assertTrue(originalUseCaseDiagram != copy);
+ assertFalse(originalUseCaseDiagram.getUid().equals(copy.getUid()));
assertEquals(newRepresentationName, copy.getName());
}
diff --git a/plugins/org.eclipse.sirius/src/org/eclipse/sirius/business/api/dialect/AbstractRepresentationDialectServices.java b/plugins/org.eclipse.sirius/src/org/eclipse/sirius/business/api/dialect/AbstractRepresentationDialectServices.java
index c8180040dc..36c2cea181 100644
--- a/plugins/org.eclipse.sirius/src/org/eclipse/sirius/business/api/dialect/AbstractRepresentationDialectServices.java
+++ b/plugins/org.eclipse.sirius/src/org/eclipse/sirius/business/api/dialect/AbstractRepresentationDialectServices.java
@@ -28,7 +28,6 @@ import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.resource.Resource;
-import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.sirius.business.api.helper.SiriusUtil;
import org.eclipse.sirius.business.api.helper.task.AbstractCommandTask;
import org.eclipse.sirius.business.api.query.EObjectQuery;
@@ -38,6 +37,7 @@ import org.eclipse.sirius.business.api.session.CustomDataConstants;
import org.eclipse.sirius.business.api.session.Session;
import org.eclipse.sirius.common.tools.api.interpreter.EvaluationException;
import org.eclipse.sirius.common.tools.api.interpreter.IInterpreter;
+import org.eclipse.sirius.common.tools.api.util.SiriusCopier;
import org.eclipse.sirius.common.tools.api.util.StringUtil;
import org.eclipse.sirius.ecore.extender.business.api.accessor.ModelAccessor;
import org.eclipse.sirius.ext.base.Option;
@@ -111,12 +111,12 @@ public abstract class AbstractRepresentationDialectServices implements DialectSe
public void notify(RepresentationNotification notification) {
// Empty default implementation.
}
-
+
@Override
public void updateRepresentationsExtendedBy(Session session, Viewpoint viewpoint, boolean activated) {
// No support for representation extension by default.
}
-
+
@Override
public void refresh(DRepresentation representation, IProgressMonitor monitor) {
refresh(representation, false, monitor);
@@ -186,10 +186,8 @@ public abstract class AbstractRepresentationDialectServices implements DialectSe
@Override
public DRepresentation copyRepresentation(final DRepresentation representation, final String name, final Session session, final IProgressMonitor monitor) {
- EcoreUtil.Copier copier = new EcoreUtil.Copier();
- DRepresentation newRepresentation = (DRepresentation) copier.copy(representation);
- copier.copyReferences();
- newRepresentation.setUid(EcoreUtil.generateUUID());
+ // Copy the representation and get new uid values for copies.
+ DRepresentation newRepresentation = SiriusCopier.Helper.copy(representation);
/* Set the correct name */
newRepresentation.setName(name);

Back to the top