diff options
author | Esteban Dugueperoux | 2015-10-29 08:03:15 +0000 |
---|---|---|
committer | Esteban Dugueperoux | 2015-11-02 08:41:57 +0000 |
commit | 7bf70fa2cb36dc5555e36dfdd782a84a434d9783 (patch) | |
tree | a3a93bbbdf3414938527a6bd6627374fb8fa5d20 | |
parent | 366827e38895db0dcc2f20c694a07417b5ed509e (diff) | |
download | org.eclipse.sirius-7bf70fa2cb36dc5555e36dfdd782a84a434d9783.tar.gz org.eclipse.sirius-7bf70fa2cb36dc5555e36dfdd782a84a434d9783.tar.xz org.eclipse.sirius-7bf70fa2cb36dc5555e36dfdd782a84a434d9783.zip |
[480773] Add DialectEditorsOpeningWithFailingSessionOpeningTests tests
- Add DialectEditorsOpeningWithFailingSessionOpeningTests JUnit tests
which test diagram, tree, edition table and cross table editors opening
from memento with failing session opening. The opened editors must
display a message about the reason of the failing session opening.
Bug: 480773
Change-Id: Idf9dbe54b141fc6c3f1d266b70f367de9af3daef
Signed-off-by: Esteban Dugueperoux <esteban.dugueperoux@obeo.fr>
5 files changed, 407 insertions, 4 deletions
diff --git a/plugins/org.eclipse.sirius.doc/doc/Release_Notes.html b/plugins/org.eclipse.sirius.doc/doc/Release_Notes.html index a3cfa5b698..8d4d119db6 100644 --- a/plugins/org.eclipse.sirius.doc/doc/Release_Notes.html +++ b/plugins/org.eclipse.sirius.doc/doc/Release_Notes.html @@ -82,7 +82,16 @@ </h4> <ul> <li><span class="label label-success">Added</span> The method - <code>org.eclipse.sirius.ui.business.api.session.SessionEditorInput.getStatus()</code> has been added to have the status about session opening, i.e. if it has been correctly opened, cancelled or an exception has occurred. + <code>org.eclipse.sirius.ui.business.api.session.SessionEditorInput.getStatus()</code> has been added to have the status about session opening, i.e. if it has been correctly opened, cancelled or an exception has occurred. + </li> + </ul> + <h4 id="Changesinorg.eclipse.sirius.tests.junit.support">Changes in + <code>org.eclipse.sirius.tests.junit.support</code> + </h4> + <ul> + <li><span class="label label-success">Added</span> The class + <code>org.eclipse.sirius.tests.support.api.DefaultTestMemento</code> has been created. It help to simulate a real + <code>IMemento</code> for editor restoration. </li> </ul> <h2 id="sirius3.1.0">Changes in Sirius 3.1.0</h2> @@ -529,7 +538,7 @@ <code>RefreshPlan</code> in parameter to have more informations to say if children must be synchronized. </li> </ul> - <h4 id="Changesinorg.eclipse.sirius.tests.junit.support">Changes in + <h4 id="Changesinorg.eclipse.sirius.tests.junit.support2">Changes in <code>org.eclipse.sirius.tests.junit.support</code> </h4> <ul> @@ -1465,7 +1474,7 @@ </ul> </li> </ul> - <h4 id="Changesinorg.eclipse.sirius.tests.junit.support2">Changes in + <h4 id="Changesinorg.eclipse.sirius.tests.junit.support3">Changes in <code>org.eclipse.sirius.tests.junit.support</code> </h4> <ul> diff --git a/plugins/org.eclipse.sirius.doc/doc/Release_Notes.textile b/plugins/org.eclipse.sirius.doc/doc/Release_Notes.textile index a02af930f8..76c9209583 100644 --- a/plugins/org.eclipse.sirius.doc/doc/Release_Notes.textile +++ b/plugins/org.eclipse.sirius.doc/doc/Release_Notes.textile @@ -16,6 +16,10 @@ h4. Changes in @org.eclipse.sirius@ * <span class="label label-success">Added</span> The method @org.eclipse.sirius.ui.business.api.session.SessionEditorInput.getStatus()@ has been added to have the status about session opening, i.e. if it has been correctly opened, cancelled or an exception has occurred. +h4. Changes in @org.eclipse.sirius.tests.junit.support@ + +* <span class="label label-success">Added</span> The class @org.eclipse.sirius.tests.support.api.DefaultTestMemento@ has been created. It help to simulate a real @IMemento@ for editor restoration. + h2(#sirius3.1.0). Changes in Sirius 3.1.0 h3. User-Visible Changes diff --git a/plugins/org.eclipse.sirius.tests.junit.support/src/org/eclipse/sirius/tests/support/api/DefaultTestMemento.java b/plugins/org.eclipse.sirius.tests.junit.support/src/org/eclipse/sirius/tests/support/api/DefaultTestMemento.java new file mode 100644 index 0000000000..849ee67442 --- /dev/null +++ b/plugins/org.eclipse.sirius.tests.junit.support/src/org/eclipse/sirius/tests/support/api/DefaultTestMemento.java @@ -0,0 +1,150 @@ +/******************************************************************************* + * Copyright (c) 2015 THALES GLOBAL SERVICES 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: + * Obeo - initial API and implementation + *******************************************************************************/ +package org.eclipse.sirius.tests.support.api; + +import java.util.HashMap; +import java.util.Map; + +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.Status; +import org.eclipse.sirius.tests.support.internal.SiriusTestsSupportPlugin; +import org.eclipse.ui.IMemento; + +/** + * Default {@link IMemento} implementation to be used as mock. + * + * @author <a href="mailto:esteban.dugueperoux@obeo.fr">Esteban Dugueperoux</a> + */ +public class DefaultTestMemento implements IMemento { + + private Map<String, String> values = new HashMap<String, String>(); + + @Override + public IMemento createChild(String type) { + return null; + } + + @Override + public IMemento createChild(String type, String id) { + return null; + } + + @Override + public IMemento getChild(String type) { + return null; + } + + @Override + public IMemento[] getChildren() { + return null; + } + + @Override + public IMemento[] getChildren(String type) { + return null; + } + + @Override + public Float getFloat(String key) { + Float result = null; + String strValue = values.get(key); + if (strValue == null) { + return null; + } + try { + result = new Float(strValue); + } catch (NumberFormatException e) { + SiriusTestsSupportPlugin.getDefault().getLog().log(new Status(IStatus.ERROR, SiriusTestsSupportPlugin.PLUGIN_ID, "Memento problem - invalid float for key: " + key //$NON-NLS-1$ + + " value: " + strValue, e)); //$NON-NLS-1$ + } + return result; + } + + @Override + public String getType() { + return null; + } + + @Override + public String getID() { + return values.get(TAG_ID); + } + + @Override + public Integer getInteger(String key) { + Integer integer = null; + String strValue = values.get(key); + if (strValue == null) { + return null; + } + try { + integer = new Integer(strValue); + } catch (NumberFormatException e) { + SiriusTestsSupportPlugin.getDefault().getLog().log(new Status(IStatus.ERROR, SiriusTestsSupportPlugin.PLUGIN_ID, "Memento problem - invalid integer for key: " + key //$NON-NLS-1$ + + " value: " + strValue, e)); //$NON-NLS-1$ + } + return integer; + } + + @Override + public String getString(String key) { + return values.get(key); + } + + @Override + public Boolean getBoolean(String key) { + String attr = values.get(key); + if (attr == null) { + return null; + } + return Boolean.valueOf(attr); + } + + @Override + public String getTextData() { + return null; + } + + @Override + public String[] getAttributeKeys() { + return values.keySet().toArray(new String[values.size()]); + } + + @Override + public void putFloat(String key, float value) { + values.put(key, String.valueOf(value)); + } + + @Override + public void putInteger(String key, int value) { + values.put(key, String.valueOf(value)); + } + + @Override + public void putMemento(IMemento memento) { + } + + @Override + public void putString(String key, String value) { + values.put(key, value); + } + + @Override + public void putBoolean(String key, boolean value) { + values.put(key, Boolean.toString(value)); + } + + @Override + public void putTextData(String data) { + + } + +} diff --git a/plugins/org.eclipse.sirius.tests.junit/src/org/eclipse/sirius/tests/suite/common/AllCommonPluginTests.java b/plugins/org.eclipse.sirius.tests.junit/src/org/eclipse/sirius/tests/suite/common/AllCommonPluginTests.java index d14aa010ea..9f97605453 100644 --- a/plugins/org.eclipse.sirius.tests.junit/src/org/eclipse/sirius/tests/suite/common/AllCommonPluginTests.java +++ b/plugins/org.eclipse.sirius.tests.junit/src/org/eclipse/sirius/tests/suite/common/AllCommonPluginTests.java @@ -54,6 +54,7 @@ import org.eclipse.sirius.tests.unit.api.session.SiriusRegistryTests; import org.eclipse.sirius.tests.unit.api.session.ViewpointSelectionTests; import org.eclipse.sirius.tests.unit.api.tools.SiriusControlAndCrossReferenceInMultiSessionTest; import org.eclipse.sirius.tests.unit.api.vsm.edit.SiriusAdapterFactoryRegistryTest; +import org.eclipse.sirius.tests.unit.common.DialectEditorsOpeningWithFailingSessionOpeningTests; import org.eclipse.sirius.tests.unit.common.EclipseUtilTest; import org.eclipse.sirius.tests.unit.common.EqualityHelperTestCase; import org.eclipse.sirius.tests.unit.common.InterpreterVariablesTestCase; @@ -162,7 +163,7 @@ public class AllCommonPluginTests extends TestCase { suite.addTestSuite(EqualityHelperTestCase.class); suite.addTestSuite(WorkspaceResourceSyncTestCase.class); suite.addTestSuite(InterpreterVariablesTestCase.class); - + suite.addTestSuite(DialectEditorsOpeningWithFailingSessionOpeningTests.class); suite.addTestSuite(PreferencesTests.class); suite.addTestSuite(ConvertProjectToModelingProjectTest.class); diff --git a/plugins/org.eclipse.sirius.tests.junit/src/org/eclipse/sirius/tests/unit/common/DialectEditorsOpeningWithFailingSessionOpeningTests.java b/plugins/org.eclipse.sirius.tests.junit/src/org/eclipse/sirius/tests/unit/common/DialectEditorsOpeningWithFailingSessionOpeningTests.java new file mode 100644 index 0000000000..452432ea39 --- /dev/null +++ b/plugins/org.eclipse.sirius.tests.junit/src/org/eclipse/sirius/tests/unit/common/DialectEditorsOpeningWithFailingSessionOpeningTests.java @@ -0,0 +1,239 @@ +/******************************************************************************* + * Copyright (c) 2015 THALES GLOBAL SERVICES 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: + * Obeo - initial API and implementation + *******************************************************************************/ +package org.eclipse.sirius.tests.unit.common; + +import org.eclipse.core.runtime.IAdaptable; +import org.eclipse.core.runtime.OperationCanceledException; +import org.eclipse.draw2d.IFigure; +import org.eclipse.draw2d.Label; +import org.eclipse.emf.common.util.URI; +import org.eclipse.emf.ecore.resource.Resource; +import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl; +import org.eclipse.gef.LayerConstants; +import org.eclipse.gef.RootEditPart; +import org.eclipse.gef.editparts.LayerManager; +import org.eclipse.sirius.business.api.query.URIQuery; +import org.eclipse.sirius.business.api.session.Session; +import org.eclipse.sirius.business.api.session.SessionManager; +import org.eclipse.sirius.business.api.session.SessionManagerListener; +import org.eclipse.sirius.diagram.ui.tools.api.editor.DDiagramEditor; +import org.eclipse.sirius.diagram.ui.tools.internal.editor.DDiagramEditorImpl; +import org.eclipse.sirius.table.ui.tools.internal.editor.DTableCrossEditor; +import org.eclipse.sirius.table.ui.tools.internal.editor.DTableEditionEditor; +import org.eclipse.sirius.tests.support.api.DefaultTestMemento; +import org.eclipse.sirius.tests.support.api.SiriusTestCase; +import org.eclipse.sirius.tests.support.api.TestsUtil; +import org.eclipse.sirius.tools.api.command.ICommandFactory; +import org.eclipse.sirius.tree.ui.tools.internal.editor.DTreeEditor; +import org.eclipse.sirius.ui.business.api.dialect.DialectEditor; +import org.eclipse.sirius.ui.business.api.dialect.DialectUIManager; +import org.eclipse.sirius.ui.business.api.session.SessionEditorInput; +import org.eclipse.sirius.ui.business.api.session.SessionEditorInputFactory; +import org.eclipse.sirius.viewpoint.ViewpointFactory; +import org.eclipse.sirius.viewpoint.provider.SiriusEditPlugin; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Control; +import org.eclipse.ui.IEditorPart; +import org.eclipse.ui.IElementFactory; +import org.eclipse.ui.IMemento; +import org.eclipse.ui.IWorkbenchPage; +import org.eclipse.ui.PlatformUI; +import org.eclipse.ui.ide.IDE; + +/** + * A test class to test that {@link DialectEditor} are opened with a end-user + * message in background and without exceptions when the session opening fails. + * The tests simulates editors opening from memento at Eclipse restart. + * + * @author <a href="mailto:esteban.dugueperoux@obeo.fr">Esteban Dugueperoux</a> + */ +public class DialectEditorsOpeningWithFailingSessionOpeningTests extends SiriusTestCase { + + private IMemento memento; + + private IElementFactory elementFactory; + + private SessionOpeningFailureListener sessionOpeningFailureListener; + + private IEditorPart openedEditor; + + @Override + protected void setUp() throws Exception { + super.setUp(); + closeWelcomePage(); + + String factoryID = SessionEditorInputFactory.class.getName(); + elementFactory = PlatformUI.getWorkbench().getElementFactory(factoryID); + memento = new DefaultTestMemento(); + memento.putString("bundle", SiriusEditPlugin.ID); + memento.putString("class", SessionEditorInput.class.getName()); + URI sessionResourceURI = URI.createURI(URIQuery.INMEMORY_URI_SCHEME + ":/" + TEMPORARY_PROJECT_NAME + "/representationsSet.aird"); + Resource resource = new ResourceSetImpl().createResource(sessionResourceURI); + resource.getContents().add(ViewpointFactory.eINSTANCE.createDAnalysis()); + resource.save(null); + memento.putString("name", "dummyRepName"); + memento.putString("SESSION_RESOURCE_URI", sessionResourceURI.toString()); + memento.putString("uri", sessionResourceURI.appendFragment("dummyFragment").toString()); + sessionOpeningFailureListener = new SessionOpeningFailureListener(); + } + + @Override + protected ICommandFactory getCommandFactory() { + return null; + } + + public void testDiagramEditorOpeningWithFailingSessionOpening() throws Exception { + String exceptionMessage = "FAILING SESSION OPENING MESSAGE ON DIAGRAM EDITOR OPENING"; + testDiagramEditorOpeningOnSessionOpening(exceptionMessage, false, DDiagramEditor.EDITOR_ID); + } + + public void testDiagramEditorOpeningWithCanceledSessionOpening() throws Exception { + String exceptionMessage = "CANCEL SESSION OPENING MESSAGE ON DIAGRAM EDITOR OPENING"; + testDiagramEditorOpeningOnSessionOpening(exceptionMessage, true, DDiagramEditor.EDITOR_ID); + } + + public void testTreeEditorOpeningWithFailingSessionOpening() throws Exception { + String exceptionMessage = "FAILING SESSION OPENING MESSAGE ON TREE EDITOR OPENING"; + testDiagramEditorOpeningOnSessionOpening(exceptionMessage, false, DTreeEditor.ID); + } + + public void testTreeEditorOpeningWithCanceledSessionOpening() throws Exception { + String exceptionMessage = "CANCEL SESSION OPENING MESSAGE ON TREE EDITOR OPENING"; + testDiagramEditorOpeningOnSessionOpening(exceptionMessage, true, DTreeEditor.ID); + } + + public void testEditionTableEditorOpeningWithFailingSessionOpening() throws Exception { + String exceptionMessage = "FAILING SESSION OPENING MESSAGE ON EDITION TABLE EDITOR OPENING"; + testDiagramEditorOpeningOnSessionOpening(exceptionMessage, false, DTableEditionEditor.ID); + } + + public void testEditionTableEditorOpeningWithCanceledSessionOpening() throws Exception { + String exceptionMessage = "CANCEL SESSION OPENING MESSAGE ON EDITION TABLE EDITOR OPENING"; + testDiagramEditorOpeningOnSessionOpening(exceptionMessage, true, DTableEditionEditor.ID); + } + + public void testCrossTableEditorOpeningWithFailingSessionOpening() throws Exception { + String exceptionMessage = "FAILING SESSION OPENING MESSAGE ON CROSS TABLE EDITOR OPENING"; + testDiagramEditorOpeningOnSessionOpening(exceptionMessage, false, DTableCrossEditor.ID); + } + + public void testCrossTableEditorOpeningWithCanceledSessionOpening() throws Exception { + String exceptionMessage = "CANCEL SESSION OPENING MESSAGE ON CROSS TABLE EDITOR OPENING"; + testDiagramEditorOpeningOnSessionOpening(exceptionMessage, true, DTableCrossEditor.ID); + } + + public void testDiagramEditorOpeningOnSessionOpening(String exceptionMessage, boolean cancel, String editorID) throws Exception { + sessionOpeningFailureListener.setThrowOperationCancelExceptionOnSessionOpening(cancel); + sessionOpeningFailureListener.setMessage(exceptionMessage); + + IAdaptable editorInputAdaptable = elementFactory.createElement(memento); + assertTrue("The SessionEditorInputFactory must create a SessionEditorInput, even if session is not opened", editorInputAdaptable instanceof SessionEditorInput); + SessionEditorInput sessionEditorInput = (SessionEditorInput) editorInputAdaptable; + + IWorkbenchPage activePage = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(); + openedEditor = IDE.openEditor(activePage, sessionEditorInput, editorID); + TestsUtil.synchronizationWithUIThread(); + + String displayedMessage = null; + if (editorID == DDiagramEditor.EDITOR_ID) { + assertTrue(openedEditor instanceof DDiagramEditorImpl); + DDiagramEditorImpl dDiagramEditorImpl = (DDiagramEditorImpl) openedEditor; + RootEditPart rootEditPart = dDiagramEditorImpl.getDiagramGraphicalViewer().getRootEditPart(); + assertTrue(rootEditPart instanceof LayerManager); + LayerManager layerManager = (LayerManager) rootEditPart; + IFigure layer = layerManager.getLayer(LayerConstants.PRINTABLE_LAYERS); + assertEquals(3, layer.getChildren().size()); + Object child = layer.getChildren().get(layer.getChildren().size() - 1); + assertTrue(child instanceof Label); + Label statusLabel = (Label) child; + displayedMessage = statusLabel.getText(); + } else if (editorID == DTreeEditor.ID) { + assertTrue(openedEditor instanceof DTreeEditor); + DTreeEditor dTreeEditor = (DTreeEditor) openedEditor; + Control control = dTreeEditor.getControl(); + assertTrue(control instanceof Composite); + Composite composite = (Composite) control; + assertEquals(1, composite.getChildren().length); + Control child = composite.getChildren()[0]; + assertTrue(child instanceof org.eclipse.swt.widgets.Label); + org.eclipse.swt.widgets.Label label = (org.eclipse.swt.widgets.Label) child; + displayedMessage = label.getText(); + } else if (editorID == DTableEditionEditor.ID) { + assertTrue(openedEditor instanceof DTableEditionEditor); + DTableEditionEditor dTableEditionEditor = (DTableEditionEditor) openedEditor; + Control control = dTableEditionEditor.getControl(); + assertTrue(control instanceof Composite); + Composite composite = (Composite) control; + assertEquals(1, composite.getChildren().length); + Control child = composite.getChildren()[0]; + assertTrue(child instanceof org.eclipse.swt.widgets.Label); + org.eclipse.swt.widgets.Label label = (org.eclipse.swt.widgets.Label) child; + displayedMessage = label.getText(); + } else if (editorID == DTableCrossEditor.ID) { + assertTrue(openedEditor instanceof DTableCrossEditor); + DTableCrossEditor dTableCrossEditor = (DTableCrossEditor) openedEditor; + Control control = dTableCrossEditor.getControl(); + assertTrue(control instanceof Composite); + Composite composite = (Composite) control; + assertEquals(1, composite.getChildren().length); + Control child = composite.getChildren()[0]; + assertTrue(child instanceof org.eclipse.swt.widgets.Label); + org.eclipse.swt.widgets.Label label = (org.eclipse.swt.widgets.Label) child; + displayedMessage = label.getText(); + } + assertEquals("The exception message should be displayed in background of opened dialect editor to explain why the session opening has failed", exceptionMessage, displayedMessage); + } + + @Override + protected void tearDown() throws Exception { + if (openedEditor != null) { + DialectUIManager.INSTANCE.closeEditor(openedEditor, false); + TestsUtil.synchronizationWithUIThread(); + } + sessionOpeningFailureListener.dispose(); + sessionOpeningFailureListener = null; + elementFactory = null; + memento = null; + super.tearDown(); + } + + class SessionOpeningFailureListener extends SessionManagerListener.Stub { + + private boolean throwOperationCancelExceptionOnSessionOpening; + + private String message; + + public SessionOpeningFailureListener() { + SessionManager.INSTANCE.addSessionsListener(this); + } + + public void setThrowOperationCancelExceptionOnSessionOpening(boolean throwOperationCancelExceptionOnSessionOpening) { + this.throwOperationCancelExceptionOnSessionOpening = throwOperationCancelExceptionOnSessionOpening; + } + + public void setMessage(String message) { + this.message = message; + } + + @Override + public void notifyAddSession(Session newSession) { + if (throwOperationCancelExceptionOnSessionOpening) { + throw new OperationCanceledException(message); + } else { + throw new RuntimeException(message); + } + } + + public void dispose() { + SessionManager.INSTANCE.removeSessionsListener(this); + } + } +} |