[508106] Adds JUnit test

Bug: 508106
Cherry-picked-from: 508138
Change-Id: Ie2344cc3868d389a6e15c1c399cd58a2f896310d
Signed-off-by: Laurent Redor <laurent.redor@obeo.fr>
diff --git a/plugins/org.eclipse.sirius.tests.junit/data/unit/tools/undoEdgeCreationToolDangling/dEdgeUndoDangling.aird b/plugins/org.eclipse.sirius.tests.junit/data/unit/tools/undoEdgeCreationToolDangling/dEdgeUndoDangling.aird
new file mode 100644
index 0000000..84b609c
--- /dev/null
+++ b/plugins/org.eclipse.sirius.tests.junit/data/unit/tools/undoEdgeCreationToolDangling/dEdgeUndoDangling.aird
@@ -0,0 +1,62 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<viewpoint:DAnalysis xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:description="http://www.eclipse.org/sirius/description/1.1.0" xmlns:description_1="http://www.eclipse.org/sirius/diagram/description/1.1.0" xmlns:diagram="http://www.eclipse.org/sirius/diagram/1.1.0" xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore" xmlns:notation="http://www.eclipse.org/gmf/runtime/1.0.2/notation" xmlns:style="http://www.eclipse.org/sirius/diagram/description/style/1.1.0" xmlns:viewpoint="http://www.eclipse.org/sirius/1.1.0" xsi:schemaLocation="http://www.eclipse.org/sirius/description/1.1.0 http://www.eclipse.org/sirius/1.1.0#//description http://www.eclipse.org/sirius/diagram/description/1.1.0 http://www.eclipse.org/sirius/diagram/1.1.0#//description http://www.eclipse.org/sirius/diagram/description/style/1.1.0 http://www.eclipse.org/sirius/diagram/1.1.0#//description/style" xmi:id="_JwAXgLMoEeaRrZVBLKADjA" selectedViews="_KCWEsLMoEeaRrZVBLKADjA" version="8.1.1">
+  <models xmi:type="ecore:EPackage" href="dEdgeUndoDangling.ecore#/"/>
+  <ownedViews xmi:type="viewpoint:DRepresentationContainer" xmi:id="_KCWEsLMoEeaRrZVBLKADjA" initialized="true">
+    <ownedRepresentations xmi:type="diagram:DSemanticDiagram" xmi:id="_KzSZcLMoEeaRrZVBLKADjA" name="diagram">
+      <ownedAnnotationEntries xmi:type="description:AnnotationEntry" xmi:id="_KzTAgLMoEeaRrZVBLKADjA" source="GMF_DIAGRAMS">
+        <data xmi:type="notation:Diagram" xmi:id="_KzTAgbMoEeaRrZVBLKADjA" type="Sirius" element="_KzSZcLMoEeaRrZVBLKADjA" measurementUnit="Pixel">
+          <children xmi:type="notation:Node" xmi:id="_KzTAg7MoEeaRrZVBLKADjA" type="2001" element="_KzSZcbMoEeaRrZVBLKADjA">
+            <children xmi:type="notation:Node" xmi:id="_KzTnkLMoEeaRrZVBLKADjA" type="5002">
+              <layoutConstraint xmi:type="notation:Location" xmi:id="_KzTnkbMoEeaRrZVBLKADjA" y="5"/>
+            </children>
+            <children xmi:type="notation:Node" xmi:id="_KzU1srMoEeaRrZVBLKADjA" type="3003" element="_KzSZcrMoEeaRrZVBLKADjA">
+              <styles xmi:type="notation:ShapeStyle" xmi:id="_KzU1s7MoEeaRrZVBLKADjA" fontName="Cantarell"/>
+              <layoutConstraint xmi:type="notation:Bounds" xmi:id="_KzU1tLMoEeaRrZVBLKADjA"/>
+            </children>
+            <styles xmi:type="notation:ShapeStyle" xmi:id="_KzTAhLMoEeaRrZVBLKADjA" fontName="Cantarell" fontHeight="8"/>
+            <layoutConstraint xmi:type="notation:Bounds" xmi:id="_KzTAhbMoEeaRrZVBLKADjA" width="30" height="30"/>
+          </children>
+          <children xmi:type="notation:Node" xmi:id="_KzUOoLMoEeaRrZVBLKADjA" type="2001" element="_KzSZdrMoEeaRrZVBLKADjA">
+            <children xmi:type="notation:Node" xmi:id="_KzU1sLMoEeaRrZVBLKADjA" type="5002">
+              <layoutConstraint xmi:type="notation:Location" xmi:id="_KzU1sbMoEeaRrZVBLKADjA" y="5"/>
+            </children>
+            <children xmi:type="notation:Node" xmi:id="_KzVcwLMoEeaRrZVBLKADjA" type="3003" element="_KzSZd7MoEeaRrZVBLKADjA">
+              <styles xmi:type="notation:ShapeStyle" xmi:id="_KzVcwbMoEeaRrZVBLKADjA" fontName="Cantarell"/>
+              <layoutConstraint xmi:type="notation:Bounds" xmi:id="_KzVcwrMoEeaRrZVBLKADjA"/>
+            </children>
+            <styles xmi:type="notation:ShapeStyle" xmi:id="_KzUOobMoEeaRrZVBLKADjA" fontName="Cantarell" fontHeight="8"/>
+            <layoutConstraint xmi:type="notation:Bounds" xmi:id="_KzUOorMoEeaRrZVBLKADjA" x="96" width="30" height="30"/>
+          </children>
+          <styles xmi:type="notation:DiagramStyle" xmi:id="_KzTAgrMoEeaRrZVBLKADjA"/>
+        </data>
+      </ownedAnnotationEntries>
+      <ownedDiagramElements xmi:type="diagram:DNode" xmi:id="_KzSZcbMoEeaRrZVBLKADjA" name="ClassA" width="3" height="3" resizeKind="NSEW">
+        <target xmi:type="ecore:EClass" href="dEdgeUndoDangling.ecore#//ClassA"/>
+        <semanticElements xmi:type="ecore:EClass" href="dEdgeUndoDangling.ecore#//ClassA"/>
+        <ownedStyle xmi:type="diagram:Square" xmi:id="_KzSZcrMoEeaRrZVBLKADjA" labelPosition="node">
+          <labelColor xmi:type="viewpoint:RGBValues" xmi:id="_KzSZc7MoEeaRrZVBLKADjA"/>
+          <description xmi:type="style:SquareDescription" href="dEdgeUndoDangling.odesign#//@ownedViewpoints[name='dEdgeUndoDangling']/@ownedRepresentations[name='Package']/@defaultLayer/@nodeMappings[name='EClazz']/@style"/>
+          <borderColor xmi:type="viewpoint:RGBValues" xmi:id="_KzSZdLMoEeaRrZVBLKADjA"/>
+          <color xmi:type="viewpoint:RGBValues" xmi:id="_KzSZdbMoEeaRrZVBLKADjA" red="136" green="136" blue="136"/>
+        </ownedStyle>
+        <actualMapping xmi:type="description_1:NodeMapping" href="dEdgeUndoDangling.odesign#//@ownedViewpoints[name='dEdgeUndoDangling']/@ownedRepresentations[name='Package']/@defaultLayer/@nodeMappings[name='EClazz']"/>
+      </ownedDiagramElements>
+      <ownedDiagramElements xmi:type="diagram:DNode" xmi:id="_KzSZdrMoEeaRrZVBLKADjA" name="ClassB" width="3" height="3" resizeKind="NSEW">
+        <target xmi:type="ecore:EClass" href="dEdgeUndoDangling.ecore#//ClassB"/>
+        <semanticElements xmi:type="ecore:EClass" href="dEdgeUndoDangling.ecore#//ClassB"/>
+        <ownedStyle xmi:type="diagram:Square" xmi:id="_KzSZd7MoEeaRrZVBLKADjA" labelPosition="node">
+          <labelColor xmi:type="viewpoint:RGBValues" xmi:id="_KzSZeLMoEeaRrZVBLKADjA"/>
+          <description xmi:type="style:SquareDescription" href="dEdgeUndoDangling.odesign#//@ownedViewpoints[name='dEdgeUndoDangling']/@ownedRepresentations[name='Package']/@defaultLayer/@nodeMappings[name='EClazz']/@style"/>
+          <borderColor xmi:type="viewpoint:RGBValues" xmi:id="_KzSZebMoEeaRrZVBLKADjA"/>
+          <color xmi:type="viewpoint:RGBValues" xmi:id="_KzSZerMoEeaRrZVBLKADjA" red="136" green="136" blue="136"/>
+        </ownedStyle>
+        <actualMapping xmi:type="description_1:NodeMapping" href="dEdgeUndoDangling.odesign#//@ownedViewpoints[name='dEdgeUndoDangling']/@ownedRepresentations[name='Package']/@defaultLayer/@nodeMappings[name='EClazz']"/>
+      </ownedDiagramElements>
+      <description xmi:type="description_1:DiagramDescription" href="dEdgeUndoDangling.odesign#//@ownedViewpoints[name='dEdgeUndoDangling']/@ownedRepresentations[name='Package']"/>
+      <filterVariableHistory xmi:type="diagram:FilterVariableHistory" xmi:id="_KzSZe7MoEeaRrZVBLKADjA"/>
+      <activatedLayers xmi:type="description_1:Layer" href="dEdgeUndoDangling.odesign#//@ownedViewpoints[name='dEdgeUndoDangling']/@ownedRepresentations[name='Package']/@defaultLayer"/>
+      <target xmi:type="ecore:EPackage" href="dEdgeUndoDangling.ecore#/"/>
+    </ownedRepresentations>
+    <viewpoint xmi:type="description:Viewpoint" href="dEdgeUndoDangling.odesign#//@ownedViewpoints[name='dEdgeUndoDangling']"/>
+  </ownedViews>
+</viewpoint:DAnalysis>
diff --git a/plugins/org.eclipse.sirius.tests.junit/data/unit/tools/undoEdgeCreationToolDangling/dEdgeUndoDangling.ecore b/plugins/org.eclipse.sirius.tests.junit/data/unit/tools/undoEdgeCreationToolDangling/dEdgeUndoDangling.ecore
new file mode 100644
index 0000000..3009f28
--- /dev/null
+++ b/plugins/org.eclipse.sirius.tests.junit/data/unit/tools/undoEdgeCreationToolDangling/dEdgeUndoDangling.ecore
@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<ecore:EPackage xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+    xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore" name="package" nsURI="http://package" nsPrefix="package">
+  <eClassifiers xsi:type="ecore:EClass" name="ClassA"/>
+  <eClassifiers xsi:type="ecore:EClass" name="ClassB"/>
+</ecore:EPackage>
diff --git a/plugins/org.eclipse.sirius.tests.junit/data/unit/tools/undoEdgeCreationToolDangling/dEdgeUndoDangling.odesign b/plugins/org.eclipse.sirius.tests.junit/data/unit/tools/undoEdgeCreationToolDangling/dEdgeUndoDangling.odesign
new file mode 100644
index 0000000..331ffd4
--- /dev/null
+++ b/plugins/org.eclipse.sirius.tests.junit/data/unit/tools/undoEdgeCreationToolDangling/dEdgeUndoDangling.odesign
@@ -0,0 +1,44 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<description:Group xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:description="http://www.eclipse.org/sirius/description/1.1.0" xmlns:description_1="http://www.eclipse.org/sirius/diagram/description/1.1.0" xmlns:style="http://www.eclipse.org/sirius/diagram/description/style/1.1.0" xmlns:tool="http://www.eclipse.org/sirius/diagram/description/tool/1.1.0" xmlns:tool_1="http://www.eclipse.org/sirius/description/tool/1.1.0" name="My" version="8.1.0">
+  <ownedViewpoints name="dEdgeUndoDangling" modelFileExtension="*.ecore">
+    <ownedRepresentations xsi:type="description_1:DiagramDescription" name="Package" domainClass="ecore.EPackage" enablePopupBars="true">
+      <metamodel href="http://www.eclipse.org/emf/2002/Ecore#/"/>
+      <defaultLayer name="Default">
+        <nodeMappings name="EClazz" semanticCandidatesExpression="feature:eContents" domainClass="ecore.EClass">
+          <style xsi:type="style:SquareDescription" labelPosition="node" resizeKind="NSEW">
+            <borderColor xsi:type="description:SystemColor" href="environment:/viewpoint#//@systemColors/@entries[name='black']"/>
+            <labelColor xsi:type="description:SystemColor" href="environment:/viewpoint#//@systemColors/@entries[name='black']"/>
+            <color xsi:type="description:SystemColor" href="environment:/viewpoint#//@systemColors/@entries[name='gray']"/>
+          </style>
+        </nodeMappings>
+        <edgeMappings name="ReferenceInvalid" preconditionExpression="false" sourceMapping="//@ownedViewpoints[name='dEdgeUndoDangling']/@ownedRepresentations[name='Package']/@defaultLayer/@nodeMappings[name='EClazz']" targetMapping="//@ownedViewpoints[name='dEdgeUndoDangling']/@ownedRepresentations[name='Package']/@defaultLayer/@nodeMappings[name='EClazz']" targetFinderExpression="feature:eType" sourceFinderExpression="feature:eContainer" domainClass="EReference" useDomainElement="true">
+          <style sizeComputationExpression="2">
+            <strokeColor xsi:type="description:SystemColor" href="environment:/viewpoint#//@systemColors/@entries[name='gray']"/>
+            <centerLabelStyleDescription>
+              <labelColor xsi:type="description:SystemColor" href="environment:/viewpoint#//@systemColors/@entries[name='black']"/>
+            </centerLabelStyleDescription>
+          </style>
+        </edgeMappings>
+        <toolSections>
+          <ownedTools xsi:type="tool:EdgeCreationDescription" name="Create Reference" edgeMappings="//@ownedViewpoints[name='dEdgeUndoDangling']/@ownedRepresentations[name='Package']/@defaultLayer/@edgeMappings[name='ReferenceInvalid']">
+            <sourceVariable name="source"/>
+            <targetVariable name="target"/>
+            <sourceViewVariable name="sourceView"/>
+            <targetViewVariable name="targetView"/>
+            <initialOperation>
+              <firstModelOperations xsi:type="tool_1:ChangeContext" browseExpression="var:source">
+                <subModelOperations xsi:type="tool_1:CreateInstance" typeName="EReference" referenceName="eStructuralFeatures">
+                  <subModelOperations xsi:type="tool_1:ChangeContext" browseExpression="var:instance">
+                    <subModelOperations xsi:type="tool_1:SetValue" featureName="name" valueExpression="toto"/>
+                    <subModelOperations xsi:type="tool_1:SetValue" featureName="eType" valueExpression="var:target"/>
+                    <subModelOperations xsi:type="tool:CreateEdgeView" mapping="//@ownedViewpoints[name='dEdgeUndoDangling']/@ownedRepresentations[name='Package']/@defaultLayer/@edgeMappings[name='ReferenceInvalid']" containerViewExpression="[sourceView.eContainer()/]" sourceExpression="var:source" targetExpression="var:target"/>
+                  </subModelOperations>
+                </subModelOperations>
+              </firstModelOperations>
+            </initialOperation>
+          </ownedTools>
+        </toolSections>
+      </defaultLayer>
+    </ownedRepresentations>
+  </ownedViewpoints>
+</description:Group>
diff --git a/plugins/org.eclipse.sirius.tests.junit/src/org/eclipse/sirius/tests/suite/diagram/AllDiagramPluginsTests.java b/plugins/org.eclipse.sirius.tests.junit/src/org/eclipse/sirius/tests/suite/diagram/AllDiagramPluginsTests.java
index d4636fa..b6c4d2b 100644
--- a/plugins/org.eclipse.sirius.tests.junit/src/org/eclipse/sirius/tests/suite/diagram/AllDiagramPluginsTests.java
+++ b/plugins/org.eclipse.sirius.tests.junit/src/org/eclipse/sirius/tests/suite/diagram/AllDiagramPluginsTests.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2010, 2015 THALES GLOBAL SERVICES.
+ * Copyright (c) 2010, 2016 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
@@ -215,6 +215,7 @@
 import org.eclipse.sirius.tests.unit.diagram.tools.NoteBehaviorOnHideRevealDeleteManyElementsTest;
 import org.eclipse.sirius.tests.unit.diagram.tools.PartAndLabelPartSelectionDeletionTest;
 import org.eclipse.sirius.tests.unit.diagram.tools.ToolsApplicabilityTest;
+import org.eclipse.sirius.tests.unit.diagram.tools.UndoAfterInconsistentEdgeCreationViewTest;
 import org.eclipse.sirius.tests.unit.diagram.tools.palette.ModifySessionOutsideEclipseTest;
 import org.eclipse.sirius.tests.unit.diagram.tools.palette.PaletteManagerWithFiltersTest;
 import org.eclipse.sirius.tests.unit.diagram.tools.palette.PaletteManagerWithLayersTest;
@@ -378,6 +379,7 @@
         // tools
         suite.addTestSuite(ToolsApplicabilityTest.class);
         suite.addTestSuite(EdgeCreationToolWithExtraMappingTest.class);
+        suite.addTestSuite(UndoAfterInconsistentEdgeCreationViewTest.class);
         suite.addTestSuite(CreationTest.class);
         suite.addTestSuite(CreateViewWithMappingImportTests.class);
         suite.addTestSuite(NoteBehaviorOnHideRevealDeleteElementTest.class);
diff --git a/plugins/org.eclipse.sirius.tests.junit/src/org/eclipse/sirius/tests/unit/diagram/tools/UndoAfterInconsistentEdgeCreationViewTest.java b/plugins/org.eclipse.sirius.tests.junit/src/org/eclipse/sirius/tests/unit/diagram/tools/UndoAfterInconsistentEdgeCreationViewTest.java
new file mode 100644
index 0000000..242a1f3
--- /dev/null
+++ b/plugins/org.eclipse.sirius.tests.junit/src/org/eclipse/sirius/tests/unit/diagram/tools/UndoAfterInconsistentEdgeCreationViewTest.java
@@ -0,0 +1,112 @@
+/*******************************************************************************
+ * Copyright (c) 2016 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
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *    Obeo - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.sirius.tests.unit.diagram.tools;
+
+import java.util.List;
+
+import org.eclipse.core.runtime.NullProgressMonitor;
+import org.eclipse.gmf.runtime.diagram.ui.parts.DiagramEditor;
+import org.eclipse.sirius.business.api.preferences.SiriusPreferencesKeys;
+import org.eclipse.sirius.diagram.DDiagram;
+import org.eclipse.sirius.diagram.DNode;
+import org.eclipse.sirius.tests.SiriusTestsPlugin;
+import org.eclipse.sirius.tests.support.api.SiriusDiagramTestCase;
+import org.eclipse.sirius.tests.support.api.TestsUtil;
+import org.eclipse.sirius.ui.business.api.dialect.DialectUIManager;
+import org.eclipse.sirius.ui.business.api.preferences.SiriusUIPreferencesKeys;
+
+import com.google.common.collect.Iterables;
+import com.google.common.collect.Lists;
+
+/**
+ * Test that performing an undo after an edge creation tool doesn't lead to
+ * dangling references on {@link DNode#getOutgoingEdges()} and
+ * {@link DNode#getIncomingEdges()}. See #508138 more details.
+ * 
+ * @author Florian Barbin
+ *
+ */
+public class UndoAfterInconsistentEdgeCreationViewTest extends SiriusDiagramTestCase {
+
+    private static final String REFERENCE_CREATE_TOOL_NAME = "Create Reference";
+
+    private static final String EDGE_PB_REPRESENTATION_DESC_NAME = "Package";
+
+    private static final String SEMANTIC_MODEL_PATH = "/" + SiriusTestsPlugin.PLUGIN_ID + "/data/unit/tools/undoEdgeCreationToolDangling/dEdgeUndoDangling.ecore";
+
+    private static final String SESSION_MODEL_PATH = "/" + SiriusTestsPlugin.PLUGIN_ID + "/data/unit/tools/undoEdgeCreationToolDangling/dEdgeUndoDangling.aird";
+
+    private static final String MODELER_PATH = "/" + SiriusTestsPlugin.PLUGIN_ID + "/data/unit/tools/undoEdgeCreationToolDangling/dEdgeUndoDangling.odesign";
+
+    private DDiagram diagram;
+
+    private DiagramEditor editor;
+
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();
+
+        TestsUtil.emptyEventsFromUIThread();
+
+        genericSetUp(SEMANTIC_MODEL_PATH, MODELER_PATH, SESSION_MODEL_PATH);
+
+        changeSiriusPreference(SiriusPreferencesKeys.PREF_AUTO_REFRESH.name(), true);
+        changeSiriusPreference(SiriusUIPreferencesKeys.PREF_REFRESH_ON_REPRESENTATION_OPENING.name(), true);
+
+        DialectUIManager.INSTANCE.closeEditor(editor, false);
+        TestsUtil.synchronizationWithUIThread();
+        diagram = (DDiagram) getRepresentations(EDGE_PB_REPRESENTATION_DESC_NAME).toArray()[0];
+        assertNotNull(diagram);
+        editor = (DiagramEditor) DialectUIManager.INSTANCE.openEditor(session, diagram, new NullProgressMonitor());
+        TestsUtil.synchronizationWithUIThread();
+        assertNotNull(editor);
+    }
+
+    /**
+     * This test performs the following actions:
+     * <ul>
+     * <li>Create an edge using the edge creation tool.</li>
+     * <li>Check that this edge has been removed by the refresh. Indeed, this
+     * edge creation tool creates a new DEdge inconsistent with the edge mapping
+     * precondition (always false)</li>
+     * <li>Perform an undo</li>
+     * <li>Make sure there is still no DEdge in the diagram and the DNodes do
+     * not have references toward a detached DEdge.</li>
+     * </ul>
+     * 
+     * @throws Exception
+     *             if an error occurs during the undo.
+     */
+    public void testEdgeEditPartCreationAfterFilterDeactivation() throws Exception {
+        List<DNode> nodeClasses = Lists.newArrayList(Iterables.filter(diagram.getDiagramElements(), DNode.class));
+        assertEquals("Bad input data : wrong class number", 2, nodeClasses.size());
+        DNode class1 = nodeClasses.get(0);
+        DNode class2 = nodeClasses.get(1);
+        applyEdgeCreationTool(REFERENCE_CREATE_TOOL_NAME, diagram, class1, class2);
+        assertEquals("The refresh should have removed the created DEdge", 0, diagram.getEdges().size());
+        assertEquals("The refresh should have removed the created DEdge", 0, class1.getOutgoingEdges().size());
+        assertEquals("The refresh should have removed the created DEdge", 0, class2.getIncomingEdges().size());
+        undo();
+        assertEquals("After undo, there should be no DEdge in the diagram", 0, diagram.getEdges().size());
+        assertEquals("After undo, the DNode should not reference any edges", 0, class1.getOutgoingEdges().size());
+        assertEquals("After undo, the DNode should not reference any edges", 0, class2.getIncomingEdges().size());
+    }
+
+    @Override
+    protected void tearDown() throws Exception {
+        DialectUIManager.INSTANCE.closeEditor(editor, false);
+        TestsUtil.synchronizationWithUIThread();
+        diagram = null;
+        editor = null;
+        super.tearDown();
+    }
+
+}