[548631] Fix tests and bugs after name and description move

Bug: 548631
Change-Id: I3a9bd957062db26cd023099722fc75e49fae0e0a
Signed-off-by: Pierre Guilet <pierre.guilet@obeo.fr>
diff --git a/plugins/org.eclipse.sirius.tests.junit/src/org/eclipse/sirius/tests/unit/common/DRepresentationDescriptorChangeIdTests.java b/plugins/org.eclipse.sirius.tests.junit/src/org/eclipse/sirius/tests/unit/common/DRepresentationDescriptorChangeIdTests.java
index 110c031..c68c970 100644
--- a/plugins/org.eclipse.sirius.tests.junit/src/org/eclipse/sirius/tests/unit/common/DRepresentationDescriptorChangeIdTests.java
+++ b/plugins/org.eclipse.sirius.tests.junit/src/org/eclipse/sirius/tests/unit/common/DRepresentationDescriptorChangeIdTests.java
@@ -61,6 +61,8 @@
 
     private DDiagram diagram2;
 
+    private IEditorPart editor;
+
     @Override
     protected void setUp() throws Exception {
         super.setUp();
@@ -68,7 +70,8 @@
         initViewpoint(DESIGN_VIEWPOINT_NAME);
         diagram = (DDiagram) getRepresentations(ENTITIES_DESC_NAME).toArray()[0];
         diagram2 = (DDiagram) getRepresentations(ENTITIES_DESC_NAME).toArray()[1];
-
+        editor = DialectUIManager.INSTANCE.openEditor(session, diagram, new NullProgressMonitor());
+        TestsUtil.synchronizationWithUIThread();
     }
 
     /**
@@ -76,9 +79,6 @@
      * {@link DRepresentationDescriptor} is updated.
      */
     public void testDRepresentationElementUpdate() {
-        final IEditorPart editor = DialectUIManager.INSTANCE.openEditor(session, diagram, new NullProgressMonitor());
-        TestsUtil.synchronizationWithUIThread();
-
         final EPackage ePackage = (EPackage) semanticModel;
         final EClass eClass = createEClass(ePackage);
 
@@ -102,9 +102,6 @@
      * {@link DRepresentationDescriptor} is updated.
      */
     public void testGMFElementUpdate() {
-        final IEditorPart editor = DialectUIManager.INSTANCE.openEditor(session, diagram, new NullProgressMonitor());
-        TestsUtil.synchronizationWithUIThread();
-
         final EPackage ePackage = (EPackage) semanticModel;
         final EClass eClass = createEClass(ePackage);
 
@@ -162,9 +159,6 @@
      * {@link DRepresentationDescriptor} is updated.
      */
     public void testDRepresentationUpdate() {
-        final IEditorPart editor = DialectUIManager.INSTANCE.openEditor(session, diagram, new NullProgressMonitor());
-        TestsUtil.synchronizationWithUIThread();
-
         String changeIdBeforeModification = new DRepresentationQuery(diagram).getRepresentationDescriptor().getChangeId();
 
         session.getTransactionalEditingDomain().getCommandStack().execute(new RecordingCommand(session.getTransactionalEditingDomain()) {
@@ -179,8 +173,6 @@
 
         assertNotEquals("Change id has not been updated.", changeIdBeforeModification, changeIdAfterModification);
 
-        DialectUIManager.INSTANCE.closeEditor(editor, false);
-        TestsUtil.synchronizationWithUIThread();
     }
 
     /**
@@ -188,9 +180,6 @@
      * {@link DRepresentationDescriptor} are updated.
      */
     public void testmultiDRepresentationUpdate() {
-        final IEditorPart editor = DialectUIManager.INSTANCE.openEditor(session, diagram, new NullProgressMonitor());
-        TestsUtil.synchronizationWithUIThread();
-
         String changeIdBeforeModification = new DRepresentationQuery(diagram).getRepresentationDescriptor().getChangeId();
         String changeIdBeforeModification2 = new DRepresentationQuery(diagram2).getRepresentationDescriptor().getChangeId();
 
@@ -217,9 +206,6 @@
      * Tests that when a new {@link DRepresentationDescriptor} is modified, then its change id is not modified.
      */
     public void testDRepresentationDescriptorNoChange() {
-        final IEditorPart editor = DialectUIManager.INSTANCE.openEditor(session, diagram, new NullProgressMonitor());
-        TestsUtil.synchronizationWithUIThread();
-
         String changeIdBeforeModification = new DRepresentationQuery(diagram).getRepresentationDescriptor().getChangeId();
 
         session.getTransactionalEditingDomain().getCommandStack().execute(new RecordingCommand(session.getTransactionalEditingDomain()) {
@@ -242,9 +228,6 @@
      * Tests that when a transient feature related to a representation is modified, then its change id is not modified.
      */
     public void testTransientFeatureNoChange() {
-        final IEditorPart editor = DialectUIManager.INSTANCE.openEditor(session, diagram, new NullProgressMonitor());
-        TestsUtil.synchronizationWithUIThread();
-
         String changeIdBeforeModification = new DRepresentationQuery(diagram).getRepresentationDescriptor().getChangeId();
 
         session.getTransactionalEditingDomain().getCommandStack().execute(new RecordingCommand(session.getTransactionalEditingDomain()) {
@@ -258,9 +241,6 @@
         String changeIdAfterModification = new DRepresentationQuery(diagram).getRepresentationDescriptor().getChangeId();
 
         assertEquals("Change id should not have been changed", changeIdBeforeModification, changeIdAfterModification);
-
-        DialectUIManager.INSTANCE.closeEditor(editor, false);
-        TestsUtil.synchronizationWithUIThread();
     }
 
     private EClass createEClass(final EPackage ePackage) {
@@ -271,7 +251,8 @@
 
     @Override
     protected void tearDown() throws Exception {
-
+        DialectUIManager.INSTANCE.closeEditor(editor, false);
+        TestsUtil.synchronizationWithUIThread();
         diagram = null;
 
         super.tearDown();
diff --git a/plugins/org.eclipse.sirius/src/org/eclipse/sirius/business/api/query/DRepresentationQuery.java b/plugins/org.eclipse.sirius/src/org/eclipse/sirius/business/api/query/DRepresentationQuery.java
index 9ef24b6..6543180 100644
--- a/plugins/org.eclipse.sirius/src/org/eclipse/sirius/business/api/query/DRepresentationQuery.java
+++ b/plugins/org.eclipse.sirius/src/org/eclipse/sirius/business/api/query/DRepresentationQuery.java
@@ -20,6 +20,7 @@
 import org.eclipse.emf.ecore.EStructuralFeature;
 import org.eclipse.sirius.business.api.session.Session;
 import org.eclipse.sirius.business.api.session.SessionManager;
+import org.eclipse.sirius.business.internal.session.danalysis.DAnalysisSessionImpl;
 import org.eclipse.sirius.ext.base.Option;
 import org.eclipse.sirius.ext.base.Options;
 import org.eclipse.sirius.viewpoint.DAnalysis;
@@ -148,27 +149,27 @@
                 session = SessionManager.INSTANCE.getSession(((DSemanticDecorator) representation).getTarget());
             }
             if (session != null) {
-                Collection<EStructuralFeature.Setting> usages = session.getSemanticCrossReferencer().getInverseReferences(representation);
-                for (EStructuralFeature.Setting setting : usages) {
-                    if (ViewpointPackage.Literals.DREPRESENTATION_DESCRIPTOR.isInstance(setting.getEObject())
-                            && setting.getEStructuralFeature() == ViewpointPackage.Literals.DREPRESENTATION_DESCRIPTOR__REPRESENTATION) {
-                        result = (DRepresentationDescriptor) setting.getEObject();
-                    }
-                }
+                result = findDescriptorFromCrossReferencer();
                 if (result == null) {
-                    EObject eObject = session.getAllSessionResources().iterator().next().getContents().get(0);
-                    if (eObject instanceof DAnalysis) {
-                        EList<DView> ownedViews = ((DAnalysis) eObject).getOwnedViews();
-                        for (DView view : ownedViews) {
-                            EList<DRepresentationDescriptor> ownedRepresentationDescriptors = view.getOwnedRepresentationDescriptors();
-                            for (DRepresentationDescriptor descriptor : ownedRepresentationDescriptors) {
-                                DRepresentation representationTemp = descriptor.getRepresentation();
-                                if (representation.equals(representationTemp)) {
-                                    result = descriptor;
-                                    break;
-                                }
-                            }
-                        }
+                    result = findDescriptorFromAnalysis();
+                }
+            }
+        }
+        return result;
+    }
+
+    private DRepresentationDescriptor findDescriptorFromAnalysis() {
+        DRepresentationDescriptor result = null;
+        Collection<DAnalysis> allAnalyses = ((DAnalysisSessionImpl) session).allAnalyses();
+        for (DAnalysis dAnalysis : allAnalyses) {
+            EList<DView> ownedViews = dAnalysis.getOwnedViews();
+            for (DView view : ownedViews) {
+                EList<DRepresentationDescriptor> ownedRepresentationDescriptors = view.getOwnedRepresentationDescriptors();
+                for (DRepresentationDescriptor descriptor : ownedRepresentationDescriptors) {
+                    DRepresentation representationTemp = descriptor.getRepresentation();
+                    if (representation.equals(representationTemp)) {
+                        result = descriptor;
+                        break;
                     }
                 }
             }
@@ -176,4 +177,17 @@
         return result;
     }
 
+    private DRepresentationDescriptor findDescriptorFromCrossReferencer() {
+        DRepresentationDescriptor result = null;
+        Collection<EStructuralFeature.Setting> usages = session.getSemanticCrossReferencer().getInverseReferences(representation);
+        for (EStructuralFeature.Setting setting : usages) {
+            if (ViewpointPackage.Literals.DREPRESENTATION_DESCRIPTOR.isInstance(setting.getEObject())
+                    && setting.getEStructuralFeature() == ViewpointPackage.Literals.DREPRESENTATION_DESCRIPTOR__REPRESENTATION) {
+                result = (DRepresentationDescriptor) setting.getEObject();
+                break;
+            }
+        }
+        return result;
+    }
+
 }
diff --git a/plugins/org.eclipse.sirius/src/org/eclipse/sirius/business/internal/session/danalysis/DAnalysisSessionImpl.java b/plugins/org.eclipse.sirius/src/org/eclipse/sirius/business/internal/session/danalysis/DAnalysisSessionImpl.java
index 2d5fb3f..f698828 100644
--- a/plugins/org.eclipse.sirius/src/org/eclipse/sirius/business/internal/session/danalysis/DAnalysisSessionImpl.java
+++ b/plugins/org.eclipse.sirius/src/org/eclipse/sirius/business/internal/session/danalysis/DAnalysisSessionImpl.java
@@ -121,7 +121,6 @@
 import org.eclipse.sirius.viewpoint.Messages;
 import org.eclipse.sirius.viewpoint.SiriusPlugin;
 import org.eclipse.sirius.viewpoint.ViewpointPackage;
-import org.eclipse.sirius.viewpoint.description.IdentifiedElement;
 import org.eclipse.sirius.viewpoint.description.RepresentationDescription;
 import org.eclipse.sirius.viewpoint.description.Viewpoint;
 import org.eclipse.sirius.viewpoint.impl.DAnalysisSessionEObjectImpl;
@@ -199,6 +198,10 @@
 
     private RepresentationNameListener representationNameListener;
 
+    private DRepresentationChangeListener dRepresentationChangeListener;
+
+    private ChangeIdUpdaterListener changeIdUpdaterListener;
+
     /**
      * Listener that clears the sub diagram decoration descriptors when a {@link DRepresentation} is either created or
      * deleted.
@@ -285,7 +288,7 @@
                 Object notifier = notification.getNotifier();
                 boolean isEObject = notifier instanceof EObject;
                 boolean isTransient = notification.getFeature() instanceof EStructuralFeature && ((EStructuralFeature) notification.getFeature()).isTransient();
-                if (isEObject && !isTransient && notifier instanceof IdentifiedElement) {
+                if (isEObject && !isTransient && ViewpointPackage.Literals.IDENTIFIED_ELEMENT.isInstance(notifier)) {
                     final DRepresentationDescriptor representationDescriptor = getDRepresentationDescriptor((EObject) notifier);
                     if (representationDescriptor != null) {
                         descriptorsToUpdate.add(representationDescriptor);
@@ -372,8 +375,10 @@
         setResourceCollector(new LocalResourceCollector(getTransactionalEditingDomain().getResourceSet()));
         setDeferSaveToPostCommit(true);
         setSaveInExclusiveTransaction(true);
-        getTransactionalEditingDomain().addResourceSetListener(new DRepresentationChangeListener(this));
-        getTransactionalEditingDomain().addResourceSetListener(new ChangeIdUpdaterListener());
+        dRepresentationChangeListener = new DRepresentationChangeListener(this);
+        getTransactionalEditingDomain().addResourceSetListener(dRepresentationChangeListener);
+        changeIdUpdaterListener = new ChangeIdUpdaterListener();
+        getTransactionalEditingDomain().addResourceSetListener(changeIdUpdaterListener);
     }
 
     // *******************
@@ -1452,6 +1457,10 @@
         if (getRefreshEditorsListener() != null) {
             removeListener(getRefreshEditorsListener());
         }
+        getTransactionalEditingDomain().removeResourceSetListener(dRepresentationChangeListener);
+        dRepresentationChangeListener = null;
+        getTransactionalEditingDomain().removeResourceSetListener(changeIdUpdaterListener);
+        changeIdUpdaterListener = null;
         refreshEditorsListeners = null;
         reloadingPolicy = null;
         savingPolicy = null;