diff options
Diffstat (limited to 'plugins/cdo/tests/org.eclipse.papyrus.cdo.ui.tests/src/org/eclipse/papyrus/cdo')
7 files changed, 1292 insertions, 0 deletions
diff --git a/plugins/cdo/tests/org.eclipse.papyrus.cdo.ui.tests/src/org/eclipse/papyrus/cdo/internal/ui/editors/tests/BasicEditorTest.java b/plugins/cdo/tests/org.eclipse.papyrus.cdo.ui.tests/src/org/eclipse/papyrus/cdo/internal/ui/editors/tests/BasicEditorTest.java new file mode 100755 index 00000000..7ee0a805 --- /dev/null +++ b/plugins/cdo/tests/org.eclipse.papyrus.cdo.ui.tests/src/org/eclipse/papyrus/cdo/internal/ui/editors/tests/BasicEditorTest.java @@ -0,0 +1,225 @@ +/***************************************************************************** + * Copyright (c) 2013, 2017 CEA LIST 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: + * CEA LIST - Initial API and implementation + * Christian W. Damus (CEA) - bug 422257 + * + *****************************************************************************/ +package org.eclipse.papyrus.cdo.internal.ui.editors.tests; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.notNullValue; +import static org.junit.Assert.assertThat; + +import org.eclipse.emf.cdo.CDOObject; +import org.eclipse.emf.cdo.transaction.CDOTransaction; +import org.eclipse.emf.cdo.util.CDOUtil; +import org.eclipse.emf.cdo.util.CommitException; +import org.eclipse.emf.cdo.util.ConcurrentAccessException; +import org.eclipse.emf.common.util.URI; +import org.eclipse.emf.ecore.EObject; +import org.eclipse.emf.ecore.util.EcoreUtil; +import org.eclipse.gef.EditPart; +import org.eclipse.net4j.util.lifecycle.LifecycleUtil; +import org.eclipse.papyrus.cdo.internal.core.CDOUtils; +import org.eclipse.papyrus.cdo.internal.ui.expressions.CDOObjectPropertyTester; +import org.eclipse.papyrus.cdo.ui.tests.AbstractPapyrusCDOUITest; +import org.eclipse.papyrus.infra.core.sasheditor.editor.IComponentPage; +import org.eclipse.papyrus.infra.core.sasheditor.editor.IEditorPage; +import org.eclipse.papyrus.infra.core.sasheditor.editor.IPageVisitor; +import org.eclipse.papyrus.infra.core.sasheditor.editor.ISashWindowsContainer; +import org.eclipse.papyrus.infra.core.utils.AdapterUtils; +import org.eclipse.papyrus.junit.framework.classification.rules.MemoryLeakRule; +import org.eclipse.ui.IEditorPart; +import org.eclipse.uml2.uml.Class; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Rule; +import org.junit.Test; + +import com.google.common.base.Optional; + +/** + * This is the BasicEditorTest type. Enjoy. + */ +public class BasicEditorTest extends AbstractPapyrusCDOUITest { + + private static final Object[] NO_OBJECTS = {}; + + @Rule + public final MemoryLeakRule memory = new MemoryLeakRule(); + + + @BeforeClass + public static void beforeClass() { + System.setProperty("cdo.interactive.conflict.resolution", "false"); + } + + @AfterClass + public static void afterClass() { + System.clearProperty("cdo.interactive.conflict.resolution"); + } + + public BasicEditorTest() { + super(); + } + + @Test + public void editorOpens() { + openEditor(); + + Class foo = expectElement("Foo", Class.class); + EditPart editPart = expectEditPart(foo); + + assertThat(editPart.isActive(), is(true)); + } + + @Test + public void canLock() { + openEditor(); + + Class foo = expectElement("Foo", Class.class); + EditPart editPart = expectEditPart(foo); + + CDOObject cdoObject = (CDOObject) editPart.getAdapter(CDOObject.class); + boolean canLock = new CDOObjectPropertyTester().test(cdoObject, CDOObjectPropertyTester.CAN_LOCK, NO_OBJECTS, true); + assertThat(canLock, is(true)); + } + + @Test + public void isLockedLocally() { + openEditor(); + + Class foo = expectElement("Foo", Class.class); + EditPart editPart = expectEditPart(foo); + + getDawnEditorSupport().lockObject(editPart); + + CDOObject cdoObject = (CDOObject) editPart.getAdapter(CDOObject.class); + boolean isLocked = new CDOObjectPropertyTester().test(cdoObject, CDOObjectPropertyTester.IS_LOCKED_LOCALLY, NO_OBJECTS, true); + assertThat(isLocked, is(true)); + } + + @Test + public void isLockedRemotely() { + openEditor(); + + Class foo = expectElement("Foo", Class.class); + EditPart editPart = expectEditPart(foo); + + URI uri = EcoreUtil.getURI(foo); + + // open another transaction in which to lock the element + CDOTransaction transaction = createTransaction(); + EObject remote = transaction.getResourceSet().getEObject(uri, true); + assertThat(remote, notNullValue()); + CDOUtils.lock(CDOUtil.getCDOObject(remote)); + + // give the lock a chance to propagate + sleep(3); + + CDOObject cdoObject = (CDOObject) editPart.getAdapter(CDOObject.class); + boolean isLocked = new CDOObjectPropertyTester().test(cdoObject, CDOObjectPropertyTester.IS_LOCKED_REMOTELY, NO_OBJECTS, true); + assertThat(isLocked, is(true)); + } + + @Test + public void isConflicting() throws Exception { + openEditor(); + + final Class foo = expectElement("Foo", Class.class); + EditPart editPart = expectEditPart(foo); + executeEdit(new Runnable() { + + @Override + public void run() { + foo.setName("Foo1"); + } + }); + + URI uri = EcoreUtil.getURI(foo); + + // open another transaction in which to modify the element + CDOTransaction transaction = createTransaction(); + Class remote = (Class) transaction.getResourceSet().getEObject(uri, true); + assertThat(remote, notNullValue()); + remote.setName("Foo2"); + transaction.commit(); + + // give the lock a chance to propagate + sleep(3); + + CDOObject cdoObject = (CDOObject) editPart.getAdapter(CDOObject.class); + boolean isConflicting = new CDOObjectPropertyTester().test(cdoObject, CDOObjectPropertyTester.IS_CONFLICTED, NO_OBJECTS, true); + assertThat(isConflicting, is(true)); + } + + // TODO Re-enable when interactive conflict resolution is in place and modal dialog can be tested. + // @Test + public void handlesConflict() throws Exception { + IEditorPart editor = openEditor(); + CDOTransaction transaction = null; + + try { + final Class foo = expectElement("Foo", Class.class); + EditPart editPart = expectEditPart(foo); + executeEdit(new Runnable() { + + @Override + public void run() { + foo.setName("Foo1"); + } + }); + + URI uri = EcoreUtil.getURI(foo); + + // open another transaction in which to modify the element + transaction = createTransaction(); + Class remote = (Class) transaction.getResourceSet().getEObject(uri, true); + assertThat(remote, notNullValue()); + remote.setName("Foo2"); + transaction.commit(); + transaction.close(); + + // give the lock a chance to propagate + sleep(3); + + CDOObject cdoObject = (CDOObject) editPart.getAdapter(CDOObject.class); + boolean isConflicting = new CDOObjectPropertyTester().test(cdoObject, CDOObjectPropertyTester.IS_CONFLICTED, NO_OBJECTS, true); + assertThat(isConflicting, is(true)); + + editor.doSave(null); + } finally { + LifecycleUtil.deactivate(transaction); + closeEditor(); + } + } + + @Test + public void editorDoesNotLeakWhenClosed() throws Exception { + Optional<ISashWindowsContainer> sashContainer = AdapterUtils.adapt(openEditor(), ISashWindowsContainer.class); + sashContainer.get().visit(new IPageVisitor() { + + @Override + public void accept(IEditorPage page) { + memory.add(page.getIEditorPart()); + } + + @Override + public void accept(IComponentPage page) { + // Pass + } + }); + + // Close the editor and open a new one so that the Outline View page + // may be replaced and reclaimed by GC + closeEditor(); + openEditor(); + } +} diff --git a/plugins/cdo/tests/org.eclipse.papyrus.cdo.ui.tests/src/org/eclipse/papyrus/cdo/internal/ui/editors/tests/DawnEditorAdapterTest.java b/plugins/cdo/tests/org.eclipse.papyrus.cdo.ui.tests/src/org/eclipse/papyrus/cdo/internal/ui/editors/tests/DawnEditorAdapterTest.java new file mode 100755 index 00000000..42d061cd --- /dev/null +++ b/plugins/cdo/tests/org.eclipse.papyrus.cdo.ui.tests/src/org/eclipse/papyrus/cdo/internal/ui/editors/tests/DawnEditorAdapterTest.java @@ -0,0 +1,269 @@ +/***************************************************************************** + * Copyright (c) 2013, 2017 CEA LIST. + * + * 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: + * CEA LIST - Initial API and implementation + * Christian W. Damus (CEA) - bug 430023 + * + *****************************************************************************/ +package org.eclipse.papyrus.cdo.internal.ui.editors.tests; + +import static org.hamcrest.CoreMatchers.instanceOf; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.not; +import static org.hamcrest.CoreMatchers.notNullValue; +import static org.hamcrest.CoreMatchers.sameInstance; +import static org.junit.Assert.assertThat; + +import org.eclipse.core.runtime.NullProgressMonitor; +import org.eclipse.emf.cdo.dawn.editors.IDawnEditor; +import org.eclipse.emf.cdo.transaction.CDOTransaction; +import org.eclipse.emf.cdo.transaction.CDOTransactionFinishedEvent; +import org.eclipse.emf.cdo.view.CDOView; +import org.eclipse.gmf.runtime.diagram.ui.parts.IDiagramEditDomain; +import org.eclipse.net4j.util.event.IEvent; +import org.eclipse.net4j.util.event.IListener; +import org.eclipse.papyrus.cdo.internal.ui.editors.DawnEditorAdapter; +import org.eclipse.papyrus.cdo.internal.ui.editors.PapyrusGMFEditorSupport; +import org.eclipse.papyrus.cdo.ui.tests.AbstractPapyrusCDOUITest; +import org.eclipse.papyrus.junit.framework.classification.rules.Condition; +import org.eclipse.papyrus.junit.framework.classification.rules.Conditional; +import org.eclipse.swt.graphics.Image; +import org.eclipse.ui.IEditorInput; +import org.eclipse.ui.IEditorPart; +import org.eclipse.ui.IEditorSite; +import org.eclipse.ui.IPropertyListener; +import org.eclipse.ui.IWorkbenchPart; +import org.eclipse.ui.IWorkbenchPartSite; +import org.eclipse.ui.IWorkbenchWindow; +import org.eclipse.ui.PlatformUI; +import org.eclipse.uml2.uml.Class; +import org.junit.Before; +import org.junit.Test; + +/** + * This is the DawnEditorAdapterTest type. Enjoy. + */ +public class DawnEditorAdapterTest extends AbstractPapyrusCDOUITest { + + private IDawnEditor fixture; + + @Test + public void testGetDawnEditor() { + assertThat(fixture, notNullValue()); + } + + @Test + public void testGetView() { + CDOTransaction transaction = getTransaction(getModelSet()); + assertThat(fixture.getView(), sameInstance((CDOView)transaction)); + } + + @Test + public void testGetContributorID() { + assertThat(fixture.getContributorID(), is("org.eclipse.papyrus.uml.diagram.clazz.part.UMLDiagramEditorID")); + } + + @Test + public void testSetDirty() { + getEditor().doSave(new NullProgressMonitor()); + assertThat(getEditor().isDirty(), is(false)); + + TestPropertyListener test = new TestPropertyListener(); + getDiagramEditor().addPropertyListener(test); + + fixture.setDirty(); + + assertThat(test.getAndClearPropertyID(), is(IEditorPart.PROP_DIRTY)); + } + + @Test + public void testGetAdapter() { + Object expected = getDiagramEditor().getAdapter(IDiagramEditDomain.class); + assertThat(expected, notNullValue()); + + Object adapter = fixture.getAdapter(IDiagramEditDomain.class); + assertThat(adapter, is(expected)); + } + + @Test + public void testIsDirty() { + final Class foo = expectElement("Foo", Class.class); + executeEdit(new Runnable() { + + @Override + public void run() { + foo.setName("Foo1"); + } + }); + + assertThat(fixture.isDirty(), is(true)); + + getEditor().doSave(new NullProgressMonitor()); + + assertThat(fixture.isDirty(), is(false)); + } + + @Test + public void testGetDawnEditorSupport() { + assertThat(fixture.getDawnEditorSupport(), instanceOf(PapyrusGMFEditorSupport.class)); + } + + @Test + public void testGetEditorInput() { + IEditorInput input = fixture.getEditorInput(); + assertThat(input, notNullValue()); + assertThat(input, is(getDiagramEditor().getEditorInput())); + } + + @Test + public void testGetEditorSite() { + IEditorSite site = fixture.getEditorSite(); + assertThat(site, notNullValue()); + assertThat(site, is(getDiagramEditor().getEditorSite())); + } + + @Test + public void testAddRemovePropertyListener() { + TestPropertyListener test = new TestPropertyListener(); + fixture.addPropertyListener(test); + + getEditor().doSave(new NullProgressMonitor()); + fixture.setDirty(); + + fixture.removePropertyListener(test); + + assertThat(test.getAndClearPropertyID(), is(IEditorPart.PROP_DIRTY)); + + getEditor().doSave(new NullProgressMonitor()); + fixture.setDirty(); + + assertThat(test.getAndClearPropertyID(), is(-1)); + } + + @Test + public void testGetSite() { + IWorkbenchPartSite site = fixture.getSite(); + assertThat(site, notNullValue()); + assertThat(site, is(getDiagramEditor().getSite())); + } + + @Test + public void testGetTitle() { + String title = fixture.getTitle(); + assertThat(title, notNullValue()); + assertThat(title, is(getDiagramEditor().getTitle())); + } + + @Test + public void testGetTitleImage() { + Image image = fixture.getTitleImage(); + assertThat(image, notNullValue()); + assertThat(image, is(getDiagramEditor().getTitleImage())); + } + + @Test + public void testGetTitleToolTip() { + String title = fixture.getTitleToolTip(); + assertThat(title, notNullValue()); + assertThat(title, is(getDiagramEditor().getTitleToolTip())); + } + + @Test + @Conditional(key = "workbenchIsActive") + public void testSetFocus() { + getRepositoryExplorer().setFocus(); + flushDisplayEvents(); + + assertThat(getWorkbenchPage().getActivePart(), not((IWorkbenchPart)getEditor())); + + fixture.setFocus(); + flushDisplayEvents(); + + assertThat(getWorkbenchPage().getActivePart(), is((IWorkbenchPart)getEditor())); + } + + @Test + public void testDoSave() { + final Class foo = expectElement("Foo", Class.class); + executeEdit(new Runnable() { + + @Override + public void run() { + foo.setName("Foo1"); + } + }); + + class Test implements IListener { + + private volatile boolean committed; + + @Override + public void notifyEvent(IEvent event) { + if(event instanceof CDOTransactionFinishedEvent) { + committed = true; + } + } + + boolean getAndClearCommitted() { + boolean result = committed; + committed = false; + return result; + } + } + + Test test = new Test(); + getTransaction(getModelSet()).addListener(test); + + fixture.doSave(new NullProgressMonitor()); + sleep(3); // give the transaction some time to notify + + assertThat(test.getAndClearCommitted(), is(true)); + } + + // + // Test framework + // + + @Before + public void prepareEditor() { + openEditor(); + + fixture = DawnEditorAdapter.getDawnEditor(getDiagramEditor()); + } + + /** + * when running the tests while using some other application, the workbench + * window doesn't have focus, so other focus changes don't work and we + * get spurious test failures. + */ + @Condition + public boolean workbenchIsActive() { + IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); + if(window == null) { + window = PlatformUI.getWorkbench().getWorkbenchWindows()[0]; + } + return window.getShell().getDisplay().getActiveShell() == window.getShell(); + } + + private static class TestPropertyListener implements IPropertyListener { + + private int propertyID = -1; + + @Override + public void propertyChanged(Object source, int propId) { + propertyID = propId; + } + + int getAndClearPropertyID() { + int result = propertyID; + propertyID = -1; + return result; + } + } +} diff --git a/plugins/cdo/tests/org.eclipse.papyrus.cdo.ui.tests/src/org/eclipse/papyrus/cdo/internal/ui/markers/tests/CDOPapyrusMarkerTest.java b/plugins/cdo/tests/org.eclipse.papyrus.cdo.ui.tests/src/org/eclipse/papyrus/cdo/internal/ui/markers/tests/CDOPapyrusMarkerTest.java new file mode 100755 index 00000000..89417ea7 --- /dev/null +++ b/plugins/cdo/tests/org.eclipse.papyrus.cdo.ui.tests/src/org/eclipse/papyrus/cdo/internal/ui/markers/tests/CDOPapyrusMarkerTest.java @@ -0,0 +1,148 @@ +/***************************************************************************** + * Copyright (c) 2013, 2017 CEA LIST. + * + * 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: + * CEA LIST - Initial API and implementation + *****************************************************************************/ +package org.eclipse.papyrus.cdo.internal.ui.markers.tests; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; + +import java.util.Map; + +import org.eclipse.core.resources.IMarker; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.emf.ecore.EValidator; +import org.eclipse.emf.ecore.EcorePackage; +import org.eclipse.emf.ecore.util.EcoreUtil; +import org.eclipse.emf.edit.provider.ComposedAdapterFactory; +import org.eclipse.papyrus.cdo.core.tests.AbstractPapyrusCDOTest; +import org.eclipse.papyrus.cdo.internal.ui.Activator; +import org.eclipse.papyrus.cdo.internal.ui.markers.CDOPapyrusMarker; +import org.eclipse.papyrus.cdo.validation.problems.EProblem; +import org.eclipse.papyrus.cdo.validation.problems.ESeverity; +import org.eclipse.papyrus.cdo.validation.problems.ProblemsFactory; +import org.eclipse.papyrus.cdo.validation.problems.edit.ProblemEditUtil; +import org.eclipse.papyrus.infra.services.markerlistener.IPapyrusMarker; +import org.junit.Before; +import org.junit.Test; + +/** + * This is the CDOPapyrusMarkerTest type. Enjoy. + */ +public class CDOPapyrusMarkerTest extends AbstractPapyrusCDOTest { + + private static final String URI = EcoreUtil.getURI(EcorePackage.Literals.ECLASS).toString(); + + private static final String MESSAGE = "Hello, world"; + + private static final int SEVERITY = IMarker.SEVERITY_WARNING; + + private static final String ATTR_BOOLEAN = "aBoolean"; + + private static final boolean ATTR_BOOLEAN_VALUE = true; + + private static final String ATTR_INTEGER = "anInteger"; + + private static final int ATTR_INTEGER_VALUE = 42; + + private static final String ATTR_STRING = "aString"; + + private static final String ATTR_STRING_VALUE = "this is a string"; + + private CDOPapyrusMarker fixture; + + public CDOPapyrusMarkerTest() { + super(); + } + + @Test + public void testGetURI() throws CoreException { + assertThat(fixture.getAttribute(EValidator.URI_ATTRIBUTE), is((Object)URI)); + assertThat(fixture.getAttribute(EValidator.URI_ATTRIBUTE, ""), is(URI)); + } + + @Test + public void testGetMessage() throws CoreException { + assertThat(fixture.getAttribute(IPapyrusMarker.MESSAGE), is((Object)MESSAGE)); + assertThat(fixture.getAttribute(IPapyrusMarker.MESSAGE, ""), is(MESSAGE)); + } + + @Test + public void testGetSeverity() throws CoreException { + assertThat(fixture.getAttribute(IPapyrusMarker.SEVERITY), is((Object)SEVERITY)); + assertThat(fixture.getAttribute(IPapyrusMarker.SEVERITY, -1), is(SEVERITY)); + } + + @Test + public void testGetBoolean() throws CoreException { + assertThat(fixture.getAttribute(ATTR_BOOLEAN), is((Object)ATTR_BOOLEAN_VALUE)); + assertThat(fixture.getAttribute(ATTR_BOOLEAN, false), is(ATTR_BOOLEAN_VALUE)); + } + + @Test + public void testGetInteger() throws CoreException { + assertThat(fixture.getAttribute(ATTR_INTEGER), is((Object)ATTR_INTEGER_VALUE)); + assertThat(fixture.getAttribute(ATTR_INTEGER, 0), is(ATTR_INTEGER_VALUE)); + } + + @Test + public void testGetString() throws CoreException { + assertThat(fixture.getAttribute(ATTR_STRING), is((Object)ATTR_STRING_VALUE)); + assertThat(fixture.getAttribute(ATTR_STRING, ""), is(ATTR_STRING_VALUE)); + } + + @Test + public void testGetAttributes() throws CoreException { + Map<String, ?> attributes = fixture.getAttributes(); + + assertThat(attributes.get(EValidator.URI_ATTRIBUTE), is((Object)URI)); + assertThat(attributes.get(IPapyrusMarker.MESSAGE), is((Object)MESSAGE)); + assertThat(attributes.get(IPapyrusMarker.SEVERITY), is((Object)SEVERITY)); + + assertThat(attributes.get(ATTR_BOOLEAN), is((Object)ATTR_BOOLEAN_VALUE)); + assertThat(attributes.get(ATTR_INTEGER), is((Object)ATTR_INTEGER_VALUE)); + assertThat(attributes.get(ATTR_STRING), is((Object)ATTR_STRING_VALUE)); + } + + @Test(expected = IllegalArgumentException.class) + public void testGetBooleanWrong() throws CoreException { + fixture.getAttribute(ATTR_INTEGER, false); + } + + @Test(expected = IllegalArgumentException.class) + public void testGetIntegerWrong() throws CoreException { + fixture.getAttribute(ATTR_STRING, 0); + } + + @Test(expected = IllegalArgumentException.class) + public void testGetStringWrong() throws CoreException { + fixture.getAttribute(ATTR_BOOLEAN, ""); + } + + // + // Test framework + // + + @Before + public void createFixture() throws CoreException { + EProblem problem = ProblemsFactory.eINSTANCE.createEProblem(); + problem.setElement(EcorePackage.Literals.ECLASS); + problem.setMessage(MESSAGE); + problem.setSeverity(ESeverity.WARNING); + problem.setSource(Activator.PLUGIN_ID); + + fixture = CDOPapyrusMarker.wrap(new ProblemEditUtil(new ComposedAdapterFactory(ComposedAdapterFactory.Descriptor.Registry.INSTANCE))).apply(problem); + + fixture.setAttribute(ATTR_BOOLEAN, ATTR_BOOLEAN_VALUE); + fixture.setAttribute(ATTR_INTEGER, ATTR_INTEGER_VALUE); + fixture.setAttribute(ATTR_STRING, ATTR_STRING_VALUE); + } + +} diff --git a/plugins/cdo/tests/org.eclipse.papyrus.cdo.ui.tests/src/org/eclipse/papyrus/cdo/internal/ui/markers/tests/ModelValidationMarkersTest.java b/plugins/cdo/tests/org.eclipse.papyrus.cdo.ui.tests/src/org/eclipse/papyrus/cdo/internal/ui/markers/tests/ModelValidationMarkersTest.java new file mode 100755 index 00000000..8963e461 --- /dev/null +++ b/plugins/cdo/tests/org.eclipse.papyrus.cdo.ui.tests/src/org/eclipse/papyrus/cdo/internal/ui/markers/tests/ModelValidationMarkersTest.java @@ -0,0 +1,183 @@ +/***************************************************************************** + * Copyright (c) 2013, 2017 CEA LIST 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: + * CEA LIST - Initial API and implementation + * Christian W. Damus (CEA) - bug 422257 + * + *****************************************************************************/ +package org.eclipse.papyrus.cdo.internal.ui.markers.tests; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.notNullValue; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.fail; + +import java.lang.reflect.Field; +import java.util.List; + +import org.eclipse.core.commands.ExecutionException; +import org.eclipse.core.commands.operations.IUndoableOperation; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.NullProgressMonitor; +import org.eclipse.papyrus.cdo.ui.tests.AbstractPapyrusCDOUITest; +import org.eclipse.papyrus.cdo.validation.problems.EProblem; +import org.eclipse.papyrus.infra.core.services.ServiceException; +import org.eclipse.papyrus.infra.core.utils.ServiceUtils; +import org.eclipse.papyrus.infra.services.markerlistener.IPapyrusMarker; +import org.eclipse.papyrus.infra.services.markerlistener.MarkersMonitorService; +import org.eclipse.papyrus.infra.services.validation.commands.ValidateDelMarkersFromModelCommand; +import org.eclipse.papyrus.infra.services.validation.commands.ValidateModelCommand; +import org.eclipse.papyrus.junit.framework.classification.rules.MemoryLeakRule; +import org.eclipse.ui.PartInitException; +import org.junit.After; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; + +import com.google.common.collect.Iterables; +import com.google.common.collect.Lists; + +/** + * This is the ModelValidationMarkersTest type. Enjoy. + */ +public class ModelValidationMarkersTest extends AbstractPapyrusCDOUITest { + + @Rule + public final MemoryLeakRule memory = new MemoryLeakRule(); + + public ModelValidationMarkersTest() { + super(); + } + + @Test + public void testCreateMarkers() { + validateModel(); + + // get some marker + IPapyrusMarker marker = null; + try { + marker = Iterables.getFirst(getMarkersMonitorService().getMarkers(getUMLModel().eResource(), null, true), null); + assertThat("Did not find a marker.", marker, notNullValue()); + } catch (CoreException e) { + e.printStackTrace(); + fail("Could not get problem markers."); + } + } + + @Test + public void testDeleteMarkers() { + validateModel(); + + // get the markers + List<IPapyrusMarker> markers = null; + try { + markers = Lists.newArrayList(getMarkersMonitorService().getMarkers(getUMLModel().eResource(), null, true)); + assertThat("Did not find any markers.", markers.isEmpty(), is(false)); + } catch (CoreException e) { + e.printStackTrace(); + fail("Could not get problem markers."); + } + + for(IPapyrusMarker next : markers) { + try { + next.delete(); + } catch (CoreException e) { + e.printStackTrace(); + fail("Could not delete problem marker."); + } + } + + try { + assertThat("Found markers.", getMarkersMonitorService().getMarkers(getUMLModel().eResource(), null, true).isEmpty(), is(true)); + } catch (CoreException e) { + e.printStackTrace(); + fail("Could not get problem markers."); + } + } + + @Test + public void testMemoryLeaksInValidation() throws InterruptedException { + validateModel(); + + // get some marker + IPapyrusMarker marker = null; + try { + marker = Iterables.getFirst(getMarkersMonitorService().getMarkers(getUMLModel().eResource(), null, true), null); + assertThat("Did not find a marker.", marker, notNullValue()); + } catch (CoreException e) { + e.printStackTrace(); + fail("Could not get problem markers."); + } + + memory.add(marker); + memory.add(getEProblem(marker)); + } + + // + // Test framework + // + + @Before + public void ensureValidationView() throws PartInitException { + getWorkbenchPage().showView("org.eclipse.papyrus.views.validation.ModelValidationView"); + openEditor(); + } + + @Override + @After + public void closeEditor() { + super.closeEditor(); + } + + void execute(IUndoableOperation operation) { + try { + getWorkbenchPage().getWorkbenchWindow().getWorkbench().getOperationSupport().getOperationHistory().execute(operation, new NullProgressMonitor(), null); + } catch (ExecutionException e) { + e.printStackTrace(); + fail("Failed to execute operation: " + operation); + } + } + + void validateModel() { + execute(new ValidateModelCommand(getUMLModel())); + flushDisplayEvents(); + } + + void deleteMarkers() { + execute(new ValidateDelMarkersFromModelCommand(getUMLModel())); + } + + MarkersMonitorService getMarkersMonitorService() { + MarkersMonitorService result = null; + + try { + result = ServiceUtils.getInstance().getService(MarkersMonitorService.class, getEditor().getServicesRegistry()); + } catch (ServiceException e) { + e.printStackTrace(); + fail("Failed to get MarkersMonitorService."); + } + + return result; + } + + EProblem getEProblem(IPapyrusMarker marker) { + EProblem result = null; + + try { + Field eProblemField = marker.getClass().getDeclaredField("problem"); + eProblemField.setAccessible(true); + result = (EProblem)eProblemField.get(marker); + } catch (Exception e) { + e.printStackTrace(); + fail("Failed to get EProblem wrapped by IPapyrusMarker."); + } + + return result; + } +} diff --git a/plugins/cdo/tests/org.eclipse.papyrus.cdo.ui.tests/src/org/eclipse/papyrus/cdo/internal/ui/views/tests/ItemProviderFilterRegistryTest.java b/plugins/cdo/tests/org.eclipse.papyrus.cdo.ui.tests/src/org/eclipse/papyrus/cdo/internal/ui/views/tests/ItemProviderFilterRegistryTest.java new file mode 100755 index 00000000..c405ff5e --- /dev/null +++ b/plugins/cdo/tests/org.eclipse.papyrus.cdo.ui.tests/src/org/eclipse/papyrus/cdo/internal/ui/views/tests/ItemProviderFilterRegistryTest.java @@ -0,0 +1,66 @@ +/***************************************************************************** + * Copyright (c) 2014, 2017 CEA LIST 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: + * CEA LIST - Initial API and implementation + * Eike Stepper (CEA) - bug 466520 + * + *****************************************************************************/ + +package org.eclipse.papyrus.cdo.internal.ui.views.tests; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; + +import java.util.Arrays; +import java.util.Collections; + +import org.eclipse.emf.cdo.explorer.CDOExplorerUtil; +import org.eclipse.papyrus.cdo.core.tests.AbstractPapyrusCDOTest; +import org.eclipse.papyrus.cdo.internal.ui.views.CheckoutItemProvider; +import org.junit.After; +import org.junit.Test; + +import com.google.common.base.Predicate; + +/** + * Tests the {@code ItemProviderFilterRegistry} class. + */ +public class ItemProviderFilterRegistryTest extends AbstractPapyrusCDOTest { + + public ItemProviderFilterRegistryTest() { + super(); + } + + @Test + public void testPredicateFilter() { + TestFilter.banned = getCheckout(); + CheckoutItemProvider itemProvider = new CheckoutItemProvider(null); + itemProvider.inputChanged(null, null, CDOExplorerUtil.getCheckoutManager()); + + assertThat(Arrays.asList(itemProvider.getChildren(CDOExplorerUtil.getCheckoutManager())), is(Collections.EMPTY_LIST)); + } + + // + // Test framework + // + + @After + public void tearDown() { + TestFilter.banned = null; + } + + public static final class TestFilter implements Predicate<Object> { + static Object banned; + + @Override + public boolean apply(Object input) { + return (banned != null) && (input == banned); + } + } +} diff --git a/plugins/cdo/tests/org.eclipse.papyrus.cdo.ui.tests/src/org/eclipse/papyrus/cdo/ui/tests/AbstractPapyrusCDOUITest.java b/plugins/cdo/tests/org.eclipse.papyrus.cdo.ui.tests/src/org/eclipse/papyrus/cdo/ui/tests/AbstractPapyrusCDOUITest.java new file mode 100755 index 00000000..f88d229c --- /dev/null +++ b/plugins/cdo/tests/org.eclipse.papyrus.cdo.ui.tests/src/org/eclipse/papyrus/cdo/ui/tests/AbstractPapyrusCDOUITest.java @@ -0,0 +1,363 @@ +/***************************************************************************** + * Copyright (c) 2013, 2017 CEA LIST, Christian W. Damus, 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: + * CEA LIST - Initial API and implementation + * Christian W. Damus (CEA) - bug 422257 + * Christian W. Damus (CEA) - bug 432813 + * Eike Stepper (CEA) - bug 466520 + * Christian W. Damus - bug 434983 + * + *****************************************************************************/ +package org.eclipse.papyrus.cdo.ui.tests; + +import static org.hamcrest.CoreMatchers.instanceOf; +import static org.hamcrest.CoreMatchers.notNullValue; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.fail; + +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +import org.eclipse.emf.cdo.dawn.editors.IDawnEditor; +import org.eclipse.emf.cdo.dawn.editors.IDawnEditorSupport; +import org.eclipse.emf.cdo.dawn.gmf.util.DawnDiagramUpdater; +import org.eclipse.emf.cdo.transaction.CDOTransaction; +import org.eclipse.emf.common.util.ECollections; +import org.eclipse.emf.common.util.URI; +import org.eclipse.emf.ecore.EObject; +import org.eclipse.emf.ecore.resource.Resource; +import org.eclipse.emf.ecore.resource.ResourceSet; +import org.eclipse.emf.ecore.util.EcoreUtil; +import org.eclipse.emf.transaction.RecordingCommand; +import org.eclipse.emf.transaction.TransactionalEditingDomain; +import org.eclipse.gef.EditPart; +import org.eclipse.gmf.runtime.diagram.ui.editparts.DiagramEditPart; +import org.eclipse.gmf.runtime.diagram.ui.resources.editor.parts.DiagramDocumentEditor; +import org.eclipse.gmf.runtime.notation.Diagram; +import org.eclipse.gmf.runtime.notation.View; +import org.eclipse.papyrus.cdo.core.tests.AbstractPapyrusCDOTest; +import org.eclipse.papyrus.cdo.internal.ui.editors.PapyrusCDOEditorManager; +import org.eclipse.papyrus.infra.core.resource.ModelSet; +import org.eclipse.papyrus.infra.ui.editor.IMultiDiagramEditor; +import org.eclipse.swt.widgets.Display; +import org.eclipse.ui.IEditorPart; +import org.eclipse.ui.IPerspectiveDescriptor; +import org.eclipse.ui.IViewPart; +import org.eclipse.ui.IViewReference; +import org.eclipse.ui.IWorkbenchPage; +import org.eclipse.ui.PlatformUI; +import org.eclipse.uml2.uml.NamedElement; +import org.eclipse.uml2.uml.Namespace; +import org.eclipse.uml2.uml.Package; +import org.eclipse.uml2.uml.UMLPackage; +import org.junit.After; +import org.junit.Before; + +import com.google.common.base.Splitter; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; + +/** + * This is the AbstractPapyrusCDOUITest type. Enjoy. + */ +public abstract class AbstractPapyrusCDOUITest extends AbstractPapyrusCDOTest { + + public static final String PLUGIN_ID = "org.eclipse.papyrus.cdo.ui.tests"; + + protected static final String TEST_MODEL_NAME = "model.di"; + + protected static final String TEST_UML_NAME = "model.uml"; + + protected static final String TEST_NOTATION_NAME = "model.notation"; + + private IWorkbenchPage page; + + private List<IEditorPart> openedEditors = Lists.newArrayList(); + + private IMultiDiagramEditor lastEditor; + + private DiagramDocumentEditor lastDiagramEditor; + + @SuppressWarnings("restriction") + private org.eclipse.papyrus.infra.ui.internal.preferences.YesNo initialSashStoragePreference; + + public AbstractPapyrusCDOUITest() { + super(); + } + + @Before + public void initWorkbench() throws Exception { + + page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(); + + // ensure the Papyrus perspective + IPerspectiveDescriptor perspective = page.getWorkbenchWindow().getWorkbench().getPerspectiveRegistry().findPerspectiveWithId("org.eclipse.papyrus.infra.core.perspective"); + if (!perspective.getId().equals(page.getPerspective().getId())) { + page.setPerspective(perspective); + } + + // minimize the Welcome view + for (IViewReference next : page.getViewReferences()) { + if ("org.eclipse.ui.internal.introview".equals(next.getId())) { + page.setPartState(next, IWorkbenchPage.STATE_MINIMIZED); + break; + } + } + + // bring the Repository Explorer forward + IViewPart reposView = page.showView("org.eclipse.emf.cdo.explorer.ui.CDORepositoriesView"); + page.activate(reposView); + + page.setEditorAreaVisible(true); + + flushDisplayEvents(); + } + + @Before + public void importTestModel() throws Exception { + + CDOTransaction transaction = createTransaction(); + Map<Resource, Resource> importMap = Maps.newHashMap(); + + importResource(transaction, TEST_MODEL_NAME, TEST_MODEL_NAME, importMap); + importResource(transaction, TEST_UML_NAME, TEST_UML_NAME, importMap); + importResource(transaction, TEST_NOTATION_NAME, TEST_NOTATION_NAME, importMap); + + for (Map.Entry<Resource, Resource> next : importMap.entrySet()) { + Resource xml = next.getKey(); + Resource cdo = next.getValue(); + ECollections.setEList(cdo.getContents(), ImmutableList.copyOf(xml.getContents())); + xml.unload(); + xml.eAdapters().clear(); + xml.getResourceSet().getResources().remove(xml); + } + + commit(transaction.getResourceSet()); + close(getCheckout(), transaction); + } + + @SuppressWarnings("restriction") + @Before + public void suppressSashLayoutMigrationDialog() { + initialSashStoragePreference = org.eclipse.papyrus.infra.ui.internal.preferences.EditorPreferences.getInstance().getConvertSharedPageLayoutToPrivate(); + org.eclipse.papyrus.infra.ui.internal.preferences.EditorPreferences.getInstance().setConvertSharedPageLayoutToPrivate(org.eclipse.papyrus.infra.ui.internal.preferences.YesNo.NO); + } + + @After + public void closeEditors() { + flushDisplayEvents(); + + for (IEditorPart next : openedEditors) { + try { + page.closeEditor(next, false); + } catch (Exception e) { + e.printStackTrace(); + } + + flushDisplayEvents(); + } + + openedEditors.clear(); + + lastEditor = null; + lastDiagramEditor = null; + + flushDisplayEvents(); + } + + @SuppressWarnings("restriction") + @After + public void restoreSashLayoutMigrationDialog() { + org.eclipse.papyrus.infra.ui.internal.preferences.EditorPreferences.getInstance().setConvertSharedPageLayoutToPrivate(initialSashStoragePreference); + } + + private void importResource(CDOTransaction transaction, String srcPath, String dstPath, Map<? super Resource, ? super Resource> importMap) { + ResourceSet rset = transaction.getResourceSet(); + + Resource xml = rset.getResource(URI.createPlatformPluginURI(PLUGIN_ID + "/resources/" + srcPath, true), true); + + EcoreUtil.resolveAll(xml); + + Resource cdo = transaction.getOrCreateResource(getResourcePath(dstPath)); + + importMap.put(xml, cdo); + } + + protected IWorkbenchPage getWorkbenchPage() { + return page; + } + + protected IViewPart getRepositoryExplorer() { + return page.findView("org.eclipse.emf.cdo.explorer.ui.CDORepositoriesView"); + } + + protected IEditorPart openEditor() { + URI uri = getTestResourceURI(TEST_MODEL_NAME); + + try { + IEditorPart result = PapyrusCDOEditorManager.INSTANCE.openEditor(getWorkbenchPage(), uri, TEST_MODEL_NAME); + + openedEditors.add(result); + + assertThat(result, instanceOf(IMultiDiagramEditor.class)); + + lastEditor = (IMultiDiagramEditor) result; + if (lastEditor.getActiveEditor() instanceof DiagramDocumentEditor) { + lastDiagramEditor = (DiagramDocumentEditor) lastEditor.getActiveEditor(); + } + + flushDisplayEvents(); + + return result; + } catch (Exception e) { + e.printStackTrace(); + fail("Failed to open editor on test model."); + return null; // make the compiler happy + } + } + + protected void closeEditor() { + closeEditor(lastEditor); + + lastDiagramEditor = null; + lastEditor = null; + } + + protected void closeEditor(IEditorPart editor) { + page.closeEditor(editor, false); + openedEditors.remove(editor); + + flushDisplayEvents(); + } + + protected void flushDisplayEvents() { + while (Display.getCurrent().readAndDispatch()) { + // pass + } + } + + protected void sleep(int seconds) { + for (int i = 0; i < seconds; i++) { + try { + Thread.sleep(1000); + } catch (Exception e) { + // fine. So, we don't sleep so long + } + + flushDisplayEvents(); + } + } + + protected IDawnEditor getDawnEditor() { + return lastEditor.getAdapter(IDawnEditor.class); + } + + protected IDawnEditorSupport getDawnEditorSupport() { + return getDawnEditor().getDawnEditorSupport(); + } + + protected void executeEdit(final Runnable edit) { + TransactionalEditingDomain domain = getModelSet().getTransactionalEditingDomain(); + + domain.getCommandStack().execute(new RecordingCommand(domain, "Edit") { + + @Override + protected void doExecute() { + edit.run(); + } + }); + } + + protected ModelSet getModelSet() { + ModelSet result = null; + + try { + result = lastEditor.getServicesRegistry().getService(ModelSet.class); + } catch (Exception e) { + e.printStackTrace(); + fail("Failed to get ModelSet from current editor."); + } + + return result; + } + + protected Package getUMLModel() { + Resource resource = getModelSet().getResource(getTestResourceURI(TEST_UML_NAME), true); + assertThat("UML model not found.", resource, notNullValue()); + return (Package) EcoreUtil.getObjectByType(resource.getContents(), UMLPackage.Literals.PACKAGE); + } + + protected <N extends NamedElement> N findElement(String relativeName, Class<N> type) { + + N result = null; + Namespace namespace = getUMLModel(); + + for (Iterator<String> iter = Splitter.on(NamedElement.SEPARATOR).split(relativeName).iterator(); iter.hasNext();) { + + NamedElement next = namespace.getMember(iter.next()); + if (next instanceof Namespace) { + namespace = (Namespace) next; + } + + if (!iter.hasNext()) { + if (type.isInstance(next)) { + result = type.cast(next); + } + } + } + + return result; + } + + protected <N extends NamedElement> N expectElement(String relativeName, Class<N> type) { + + N result = findElement(relativeName, type); + assertThat("UML element not found.", result, notNullValue()); + return result; + } + + protected IMultiDiagramEditor getEditor() { + return lastEditor; + } + + protected DiagramDocumentEditor getDiagramEditor() { + return lastDiagramEditor; + } + + protected DiagramEditPart getDiagramEditPart() { + return getDiagramEditor().getDiagramEditPart(); + } + + protected Diagram getDiagram() { + return getDiagramEditPart().getDiagramView(); + } + + protected View findView(EObject element) { + return DawnDiagramUpdater.findViewForModel(element, getDiagramEditor()); + } + + protected View expectView(EObject element) { + View result = findView(element); + assertThat("Required view not found.", result, notNullValue()); + return result; + } + + protected EditPart findEditPart(EObject element) { + View view = findView(element); + return (view == null) ? null : DawnDiagramUpdater.findEditPart(view, getDiagramEditor()); + } + + protected EditPart expectEditPart(EObject element) { + EditPart result = findEditPart(element); + assertThat("Required edit-part not found.", result, notNullValue()); + return result; + } +} diff --git a/plugins/cdo/tests/org.eclipse.papyrus.cdo.ui.tests/src/org/eclipse/papyrus/cdo/ui/tests/AllTests.java b/plugins/cdo/tests/org.eclipse.papyrus.cdo.ui.tests/src/org/eclipse/papyrus/cdo/ui/tests/AllTests.java new file mode 100755 index 00000000..4636b777 --- /dev/null +++ b/plugins/cdo/tests/org.eclipse.papyrus.cdo.ui.tests/src/org/eclipse/papyrus/cdo/ui/tests/AllTests.java @@ -0,0 +1,38 @@ +/***************************************************************************** + * Copyright (c) 2013, 2017 CEA LIST 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: + * CEA LIST - Initial API and implementation + * Christian W. Damus (CEA) - bug 443830 + * + *****************************************************************************/ +package org.eclipse.papyrus.cdo.ui.tests; + +import org.eclipse.papyrus.cdo.internal.ui.editors.tests.BasicEditorTest; +import org.eclipse.papyrus.cdo.internal.ui.editors.tests.DawnEditorAdapterTest; +import org.eclipse.papyrus.cdo.internal.ui.markers.tests.CDOPapyrusMarkerTest; +import org.eclipse.papyrus.cdo.internal.ui.markers.tests.ModelValidationMarkersTest; +import org.eclipse.papyrus.cdo.internal.ui.views.tests.ItemProviderFilterRegistryTest; +import org.junit.runner.RunWith; +import org.junit.runners.Suite; +import org.junit.runners.Suite.SuiteClasses; + +/** + * This is the AllTests type. Enjoy. + */ +@RunWith(Suite.class) +@SuiteClasses({ BasicEditorTest.class, DawnEditorAdapterTest.class, // + ModelValidationMarkersTest.class, CDOPapyrusMarkerTest.class, // + ItemProviderFilterRegistryTest.class }) +public class AllTests { + + public AllTests() { + super(); + } + +} |