From 7ed54ff0acd56f9620d733c76a36cd309b8b4731 Mon Sep 17 00:00:00 2001 From: Johannes Faltermeier Date: Tue, 27 Sep 2016 11:33:56 +0200 Subject: [PATCH] Bug 501971 - Cut Element Handling Change-Id: Ie67f9526d1ca389094e688415471019cceb56377 Signed-off-by: Johannes Faltermeier --- .../model/util/EObjectChangeNotifier.java | 27 +++- .../AllAPITests.launch | 9 +- .../AllRecordingTests.launch | 7 +- .../test/CreateDeleteOperationTest.java | 125 ++++++++++++------ 4 files changed, 115 insertions(+), 53 deletions(-) diff --git a/bundles/org.eclipse.emf.emfstore.common.model/src/org/eclipse/emf/emfstore/internal/common/model/util/EObjectChangeNotifier.java b/bundles/org.eclipse.emf.emfstore.common.model/src/org/eclipse/emf/emfstore/internal/common/model/util/EObjectChangeNotifier.java index 507806516..8c5e44f3c 100644 --- a/bundles/org.eclipse.emf.emfstore.common.model/src/org/eclipse/emf/emfstore/internal/common/model/util/EObjectChangeNotifier.java +++ b/bundles/org.eclipse.emf.emfstore.common.model/src/org/eclipse/emf/emfstore/internal/common/model/util/EObjectChangeNotifier.java @@ -22,6 +22,7 @@ import org.eclipse.emf.ecore.EObject; import org.eclipse.emf.ecore.EReference; import org.eclipse.emf.ecore.util.EContentAdapter; +import org.eclipse.emf.emfstore.internal.common.model.IdEObjectCollection; import org.eclipse.emf.emfstore.internal.common.model.impl.NotifiableIdEObjectCollectionImpl; /** @@ -247,7 +248,7 @@ private void handleMultiReference(List list) { final EObject eObject = (EObject) obj; if (!collection.contains(eObject)) { - collection.addCutElement(eObject); + collection.addCutElement(getCutElement(eObject)); } } } @@ -257,10 +258,32 @@ private void handleSingleReference(EObject newEObject) { if (ModelUtil.isSingleton(newEObject)) { return; } - collection.addCutElement(newEObject); + collection.addCutElement(getCutElement(newEObject)); } } + private EObject getCutElement(EObject eObject) { + final EObject referenceElement = eObject; + /* find topmost parent */ + while (eObject.eContainer() != null) { + /* + * Bug 501971 + * if we are contained in a different IdEObjectCollection (e.g. a second EMFStore project) simply add the + * used element from the reference as a cut element (default before 501971) + */ + if (IdEObjectCollection.class.isInstance(eObject)) { + return referenceElement; + } + eObject = eObject.eContainer(); + } + + /* + * Bug 501971 + * otherwise move the full containment tree + */ + return eObject; + } + /** * @param notification */ diff --git a/tests/org.eclipse.emf.emfstore.client.api.test/AllAPITests.launch b/tests/org.eclipse.emf.emfstore.client.api.test/AllAPITests.launch index 615de5b6c..cb91b42b2 100644 --- a/tests/org.eclipse.emf.emfstore.client.api.test/AllAPITests.launch +++ b/tests/org.eclipse.emf.emfstore.client.api.test/AllAPITests.launch @@ -3,7 +3,7 @@ - + @@ -12,8 +12,7 @@ - - + @@ -33,8 +32,8 @@ - - + + diff --git a/tests/org.eclipse.emf.emfstore.client.recording.test/AllRecordingTests.launch b/tests/org.eclipse.emf.emfstore.client.recording.test/AllRecordingTests.launch index bb9e68702..3a0a7e220 100644 --- a/tests/org.eclipse.emf.emfstore.client.recording.test/AllRecordingTests.launch +++ b/tests/org.eclipse.emf.emfstore.client.recording.test/AllRecordingTests.launch @@ -3,7 +3,7 @@ - + @@ -12,8 +12,7 @@ - - + @@ -33,7 +32,7 @@ - + diff --git a/tests/org.eclipse.emf.emfstore.client.recording.test/src/org/eclipse/emf/emfstore/client/recording/test/CreateDeleteOperationTest.java b/tests/org.eclipse.emf.emfstore.client.recording.test/src/org/eclipse/emf/emfstore/client/recording/test/CreateDeleteOperationTest.java index 0a55c9daf..efc34edd9 100644 --- a/tests/org.eclipse.emf.emfstore.client.recording.test/src/org/eclipse/emf/emfstore/client/recording/test/CreateDeleteOperationTest.java +++ b/tests/org.eclipse.emf.emfstore.client.recording.test/src/org/eclipse/emf/emfstore/client/recording/test/CreateDeleteOperationTest.java @@ -15,6 +15,7 @@ import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertSame; import static org.junit.Assert.assertTrue; import java.io.IOException; @@ -50,6 +51,7 @@ import org.eclipse.emf.emfstore.internal.common.model.impl.ProjectImpl; import org.eclipse.emf.emfstore.internal.common.model.util.ModelUtil; import org.eclipse.emf.emfstore.internal.server.model.versioning.operations.AbstractOperation; +import org.eclipse.emf.emfstore.internal.server.model.versioning.operations.CompositeOperation; import org.eclipse.emf.emfstore.internal.server.model.versioning.operations.CreateDeleteOperation; import org.eclipse.emf.emfstore.internal.server.model.versioning.operations.MultiReferenceOperation; import org.eclipse.emf.emfstore.internal.server.model.versioning.operations.ReferenceOperation; @@ -947,14 +949,15 @@ protected void doRun() { assertEquals(getProject(), ModelUtil.getProject(parentTestElement)); assertTrue(getProject().contains(testElement)); assertEquals(getProject(), ModelUtil.getProject(testElement)); - assertFalse(getProject().contains(newTestElement)); - assertFalse(getProject().contains(newChildElement1)); - assertFalse(getProject().contains(newChildElement2)); + assertTrue(getProject().contains(newTestElement)); + assertTrue(getProject().contains(newChildElement1)); + assertTrue(getProject().contains(newChildElement2)); assertTrue(getProject().contains(newChildElement3)); - assertEquals(2, newTestElement.getContainedElements().size()); + assertEquals(3, newTestElement.getContainedElements().size()); assertEquals(newChildElement1, newTestElement.getContainedElements().get(0)); assertEquals(newChildElement2, newTestElement.getContainedElements().get(1)); + assertEquals(newChildElement3, newTestElement.getContainedElements().get(2)); assertEquals(newTestElement, newChildElement1.getReferences().get(0)); assertEquals(newChildElement1, newChildElement2.getReferences().get(0)); @@ -984,9 +987,10 @@ protected void doRun() { assertTrue(getProject().contains(newChildElement2)); assertTrue(getProject().contains(newChildElement3)); - assertEquals(2, newTestElement.getContainedElements().size()); + assertEquals(3, newTestElement.getContainedElements().size()); assertEquals(newChildElement1, newTestElement.getContainedElements().get(0)); assertEquals(newChildElement2, newTestElement.getContainedElements().get(1)); + assertEquals(newChildElement3, newTestElement.getContainedElements().get(2)); assertEquals(newTestElement, newChildElement1.getReferences().get(0)); assertEquals(newChildElement1, newChildElement2.getReferences().get(0)); @@ -994,49 +998,20 @@ protected void doRun() { assertEquals(newChildElement3, testElement.getReferences().get(0)); final List operations = forceGetOperations(); - assertEquals(2, operations.size()); - final AbstractOperation operation = operations.get(0); - assertTrue(operation instanceof CreateDeleteOperation); - final CreateDeleteOperation createDeleteOperation = (CreateDeleteOperation) operation; - - final ModelElementId newTestElementId = ModelUtil.getProject(newTestElement).getModelElementId(newTestElement); - final TestElement copiedNewTestElement = (TestElement) createDeleteOperation.getModelElement(); - final TestElement copiedNewChildElement1 = copiedNewTestElement.getContainedElements().get(0); - final TestElement copiedNewChildElement2 = copiedNewTestElement.getContainedElements().get(1); - - assertEquals(2, copiedNewTestElement.getContainedElements().size()); - assertEquals(copiedNewTestElement, copiedNewChildElement1.getReferences().get(0)); - assertEquals(copiedNewChildElement1, copiedNewChildElement2.getReferences().get(0)); - assertEquals(1, copiedNewChildElement2.getReferences().size()); - - assertEquals(newTestElementId, createDeleteOperation.getModelElementId()); - assertEquals(1, createDeleteOperation.getSubOperations().size()); - assertFalse(createDeleteOperation.isDelete()); - assertTrue(CommonUtil.isSelfContained(createDeleteOperation, true)); - - // check sub-operations of 1st operation - final MultiReferenceOperation subOperation1 = (MultiReferenceOperation) createDeleteOperation - .getSubOperations().get( - 0); - - // sub-operation 1 - assertEquals(newChildElement2, getProject().getModelElement(subOperation1.getModelElementId())); - assertEquals(REFERENCES, subOperation1.getFeatureName()); - assertEquals(testElement, getProject().getModelElement(subOperation1.getReferencedModelElements().get(0))); + assertEquals(1, operations.size()); // check 2nd operation - final MultiReferenceOperation operation2 = (MultiReferenceOperation) operations.get(1); + final CompositeOperation operation1 = (CompositeOperation) operations.get(0); + + final MultiReferenceOperation operation2 = (MultiReferenceOperation) operation1.getMainOperation();// operations.get(0); assertEquals(parentTestElement, getProject().getModelElement(operation2.getModelElementId())); assertEquals(newTestElement, getProject().getModelElement(operation2.getReferencedModelElements().get(0))); - assertTrue(operations.get(1) instanceof MultiReferenceOperation); - final MultiReferenceOperation multiRefOp = (MultiReferenceOperation) operations.get(1); - - assertEquals(parentTestElement, getProject().getModelElement(multiRefOp.getModelElementId())); - assertEquals(TestElementFeatures.containedElements().getName(), multiRefOp.getFeatureName()); - assertEquals(newTestElement, getProject().getModelElement(multiRefOp.getReferencedModelElements().get(0))); - assertTrue(multiRefOp.isAdd()); + assertEquals(parentTestElement, getProject().getModelElement(operation2.getModelElementId())); + assertEquals(TestElementFeatures.containedElements().getName(), operation2.getFeatureName()); + assertEquals(newTestElement, getProject().getModelElement(operation2.getReferencedModelElements().get(0))); + assertTrue(operation2.isAdd()); } // END COMPLEX CODE @@ -1663,4 +1638,70 @@ public Void call() throws Exception { * child.getContainingWorkpackage()); assertSame(child, existing.getContainingWorkpackage()); * assertEquals(getProject().getAllModelElements().size(), 3); } */ + + @Test + public void testCreateCutElementsFromOtherProject() { + /* Setup */ + final ProjectSpace projectSpaceOriginal = getProjectSpace(); + final ProjectSpace projectSpaceCopy = cloneProjectSpace(projectSpaceOriginal); + + final TestElement testElementA = Create.testElement(); + final TestElement testElementB = Create.testElement(); + final TestElement testElementC = Create.testElement(); + testElementB.setContainedElement(testElementC); + + RunESCommand.run(new Callable() { + public Void call() throws Exception { + projectSpaceOriginal.getProject().getModelElements().add(testElementA); + projectSpaceCopy.getProject().getModelElements().add(testElementB); + return null; + } + }); + + /* Act */ + RunESCommand.run(new Callable() { + public Void call() throws Exception { + testElementA.setReference(testElementC); + return null; + } + }); + + /* Assert */ + // A+C + assertSame(testElementA, projectSpaceOriginal.getProject().getModelElements().get(0)); + assertSame(testElementC, projectSpaceOriginal.getProject().getModelElements().get(1)); + // B + assertSame(testElementB, projectSpaceCopy.getProject().getModelElements().get(0)); + } + + @Test + public void testCreateCutElementsFromNonProject() { + /* Setup */ + final ProjectSpace projectSpaceOriginal = getProjectSpace(); + + final TestElement testElementA = Create.testElement(); + final TestElement testElementB = Create.testElement(); + final TestElement testElementC = Create.testElement(); + testElementB.setContainedElement(testElementC); + + RunESCommand.run(new Callable() { + public Void call() throws Exception { + projectSpaceOriginal.getProject().getModelElements().add(testElementA); + return null; + } + }); + + /* Act */ + RunESCommand.run(new Callable() { + public Void call() throws Exception { + testElementA.setReference(testElementC); + return null; + } + }); + + /* Assert */ + // A+B(C) + assertSame(testElementA, projectSpaceOriginal.getProject().getModelElements().get(0)); + assertSame(testElementB, projectSpaceOriginal.getProject().getModelElements().get(1)); + } }