diff options
Diffstat (limited to 'tests/junit/plugins')
8 files changed, 392 insertions, 63 deletions
diff --git a/tests/junit/plugins/infra/emf/org.eclipse.papyrus.infra.emf.readonly.tests/src/org/eclipse/papyrus/infra/emf/readonly/Bug323802.uml b/tests/junit/plugins/infra/emf/org.eclipse.papyrus.infra.emf.readonly.tests/src/org/eclipse/papyrus/infra/emf/readonly/Bug323802.uml index cccdefe7e34..6c61fbbcb57 100644 --- a/tests/junit/plugins/infra/emf/org.eclipse.papyrus.infra.emf.readonly.tests/src/org/eclipse/papyrus/infra/emf/readonly/Bug323802.uml +++ b/tests/junit/plugins/infra/emf/org.eclipse.papyrus.infra.emf.readonly.tests/src/org/eclipse/papyrus/infra/emf/readonly/Bug323802.uml @@ -1,5 +1,8 @@ <?xml version="1.0" encoding="UTF-8"?> <uml:Model xmi:version="20110701" xmlns:xmi="http://www.omg.org/spec/XMI/20110701" xmlns:uml="http://www.eclipse.org/uml2/4.0.0/UML" xmi:id="_-3u4cJW3EeOfoK3RwHm3pQ" name="bug323802"> + <packageImport xmi:id="_ycYPAKkTEeOizeYRjNFxJg"> + <importedPackage xmi:type="uml:Model" href="pathmap://UML_LIBRARIES/UMLPrimitiveTypes.library.uml#_0"/> + </packageImport> <packagedElement xmi:type="uml:Class" xmi:id="_C3c2UJW4EeOfoK3RwHm3pQ" name="A" useCase="_0S-V8KYuEeO-ZNuEJamMWA"/> <packagedElement xmi:type="uml:UseCase" xmi:id="_0S-V8KYuEeO-ZNuEJamMWA" name="DoIt" subject="_C3c2UJW4EeOfoK3RwHm3pQ"/> </uml:Model> diff --git a/tests/junit/plugins/infra/emf/org.eclipse.papyrus.infra.emf.readonly.tests/src/org/eclipse/papyrus/infra/emf/readonly/ReadOnlyManagerTest.java b/tests/junit/plugins/infra/emf/org.eclipse.papyrus.infra.emf.readonly.tests/src/org/eclipse/papyrus/infra/emf/readonly/ReadOnlyManagerTest.java index d8d7623db63..82157b058fd 100644 --- a/tests/junit/plugins/infra/emf/org.eclipse.papyrus.infra.emf.readonly.tests/src/org/eclipse/papyrus/infra/emf/readonly/ReadOnlyManagerTest.java +++ b/tests/junit/plugins/infra/emf/org.eclipse.papyrus.infra.emf.readonly.tests/src/org/eclipse/papyrus/infra/emf/readonly/ReadOnlyManagerTest.java @@ -14,21 +14,12 @@ package org.eclipse.papyrus.infra.emf.readonly; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; -import static org.junit.Assert.fail; -import java.io.InputStream; - -import org.eclipse.core.resources.IFile; -import org.eclipse.core.resources.ResourceAttributes; -import org.eclipse.core.runtime.CoreException; import org.eclipse.emf.common.util.URI; -import org.eclipse.emf.ecore.resource.Resource; -import org.eclipse.emf.ecore.resource.ResourceSet; -import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl; -import org.eclipse.emf.transaction.TransactionalEditingDomain; import org.eclipse.papyrus.infra.core.resource.ReadOnlyAxis; +import org.eclipse.papyrus.infra.emf.readonly.tests.PapyrusROEditingDomainFixture; +import org.eclipse.papyrus.junit.utils.rules.JavaResource; import org.eclipse.papyrus.junit.utils.rules.ProjectFixture; -import org.eclipse.uml2.uml.Model; import org.junit.After; import org.junit.Before; import org.junit.Rule; @@ -38,16 +29,15 @@ import org.junit.Test; /** * Test suite for the {@link ReadOnlyManager} class. */ +@JavaResource("Bug323802.uml") public class ReadOnlyManagerTest { @Rule - public final ProjectFixture project = new ProjectFixture(); - - private ReadOnlyManager fixture; + public final PapyrusROEditingDomainFixture domain = new PapyrusROEditingDomainFixture(); - private TransactionalEditingDomain domain; + private final ProjectFixture project = domain.getProject(); - private Model model; + private ReadOnlyManager fixture; public ReadOnlyManagerTest() { super(); @@ -55,27 +45,27 @@ public class ReadOnlyManagerTest { @Test public void testIsReadOnlyEObject() { - setReadOnly(project.getFile(model.eResource().getURI())); - assertThat(fixture.isReadOnly(ReadOnlyAxis.anyAxis(), model).or(false), is(true)); + project.setReadOnly(domain.getModelResource()); + assertThat(fixture.isReadOnly(ReadOnlyAxis.anyAxis(), domain.getModel()).or(false), is(true)); } @Test public void testCanMakeWritableEObject() { - setReadOnly(project.getFile(model.eResource().getURI())); - assertThat(fixture.canMakeWritable(ReadOnlyAxis.anyAxis(), model).or(false), is(true)); + project.setReadOnly(domain.getModelResource()); + assertThat(fixture.canMakeWritable(ReadOnlyAxis.anyAxis(), domain.getModel()).or(false), is(true)); } @Test public void testAnyReadOnlyURIs() { - URI uri = model.eResource().getURI(); - setReadOnly(project.getFile(uri)); + URI uri = domain.getModelResourceURI(); + project.setReadOnly(domain.getModelResource()); assertThat(fixture.anyReadOnly(ReadOnlyAxis.anyAxis(), new URI[]{ uri }).or(false), is(true)); } @Test public void testCanMakeWritableURIs() { - URI uri = model.eResource().getURI(); - setReadOnly(project.getFile(uri)); + URI uri = domain.getModelResourceURI(); + project.setReadOnly(domain.getModelResource()); assertThat(fixture.canMakeWritable(ReadOnlyAxis.anyAxis(), new URI[]{ uri }).or(false), is(true)); } @@ -85,48 +75,11 @@ public class ReadOnlyManagerTest { @Before public void createFixture() throws Exception { - domain = (PapyrusROTransactionalEditingDomain)new PapyrusROTransactionalEditingDomainProvider().createTransactionalEditingDomain(new ResourceSetImpl()); - fixture = new ReadOnlyManager(domain); - - Resource res = domain.getResourceSet().createResource(project.getURI("model.uml")); - InputStream input = ReadOnlyManagerTest.class.getResourceAsStream("Bug323802.uml"); - try { - res.load(input, null); - } finally { - input.close(); - } - res.save(null); // Make sure it exists - model = (Model)res.getContents().get(0); + fixture = new ReadOnlyManager(domain.getEditingDomain()); } @After public void destroyFixture() { - ResourceSet rset = domain.getResourceSet(); - - model = null; - - domain.dispose(); - domain = null; fixture = null; - - for(Resource next : rset.getResources()) { - next.unload(); - next.eAdapters().clear(); - } - - rset.getResources().clear(); - rset.eAdapters().clear(); - } - - void setReadOnly(IFile file) { - ResourceAttributes attr = file.getResourceAttributes(); - attr.setReadOnly(true); - - try { - file.setResourceAttributes(attr); - } catch (CoreException e) { - e.getLocalizedMessage(); - fail("Failed to make workspace file read-only: " + e.getLocalizedMessage()); - } } } diff --git a/tests/junit/plugins/infra/emf/org.eclipse.papyrus.infra.emf.readonly.tests/src/org/eclipse/papyrus/infra/emf/readonly/ReadOnlyTesterTest.java b/tests/junit/plugins/infra/emf/org.eclipse.papyrus.infra.emf.readonly.tests/src/org/eclipse/papyrus/infra/emf/readonly/ReadOnlyTesterTest.java new file mode 100644 index 00000000000..7b1951eea28 --- /dev/null +++ b/tests/junit/plugins/infra/emf/org.eclipse.papyrus.infra.emf.readonly.tests/src/org/eclipse/papyrus/infra/emf/readonly/ReadOnlyTesterTest.java @@ -0,0 +1,88 @@ +/* + * Copyright (c) 2014 CEA 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: + * Christian W. Damus (CEA) - Initial API and implementation + * + */ +package org.eclipse.papyrus.infra.emf.readonly; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; + +import org.eclipse.papyrus.infra.emf.readonly.tests.PapyrusROEditingDomainFixture; +import org.eclipse.papyrus.junit.utils.rules.JavaResource; +import org.eclipse.papyrus.junit.utils.rules.ProjectFixture; +import org.eclipse.uml2.uml.Type; +import org.junit.After; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; + +import com.google.common.collect.Iterators; + + +/** + * This is the ReadOnlyTesterTest type. Enjoy. + */ +@JavaResource("Bug323802.uml") +public class ReadOnlyTesterTest { + + @Rule + public final PapyrusROEditingDomainFixture domain = new PapyrusROEditingDomainFixture(); + + private final ProjectFixture project = domain.getProject(); + + private ReadOnlyTester fixture; + + @Test + public void testAsBoolean() { + assertThat(fixture.asBoolean(true), is(true)); + assertThat(fixture.asBoolean(false), is(false)); + assertThat(fixture.asBoolean("hello"), is(true)); + assertThat(fixture.asBoolean(null), is(true)); + } + + @Test + public void testIsReadOnly() { + assertThat(fixture.testIsReadOnly(Iterators.singletonIterator(domain.getModel()), false), is(true)); + + Type string = domain.getModel().getImportedPackages().get(0).getOwnedType("String"); + assertThat(fixture.testIsReadOnly(Iterators.singletonIterator(string), true), is(true)); + + project.setReadOnly(domain.getModelResource()); + assertThat(fixture.testIsReadOnly(Iterators.singletonIterator(domain.getModel()), true), is(true)); + } + + @Test + public void testCanMakeWritable() { + // If it's already writable, well, then it's trivially easy to make it writable + assertThat(fixture.canMakeWritable(Iterators.singletonIterator(domain.getModel()), true), is(true)); + + Type string = domain.getModel().getImportedPackages().get(0).getOwnedType("String"); + assertThat(fixture.canMakeWritable(Iterators.singletonIterator(string), true), is(false)); + + project.setReadOnly(domain.getModelResource()); + assertThat(fixture.canMakeWritable(Iterators.singletonIterator(domain.getModel()), true), is(true)); + } + + // + // Test framework + // + + @Before + public void createFixture() throws Exception { + fixture = new ReadOnlyTester(); + } + + @After + public void destroyFixture() { + fixture = null; + } + +} diff --git a/tests/junit/plugins/infra/emf/org.eclipse.papyrus.infra.emf.readonly.tests/src/org/eclipse/papyrus/infra/emf/readonly/tests/AllTests.java b/tests/junit/plugins/infra/emf/org.eclipse.papyrus.infra.emf.readonly.tests/src/org/eclipse/papyrus/infra/emf/readonly/tests/AllTests.java index b7d564ad280..12d2f83dd10 100644 --- a/tests/junit/plugins/infra/emf/org.eclipse.papyrus.infra.emf.readonly.tests/src/org/eclipse/papyrus/infra/emf/readonly/tests/AllTests.java +++ b/tests/junit/plugins/infra/emf/org.eclipse.papyrus.infra.emf.readonly.tests/src/org/eclipse/papyrus/infra/emf/readonly/tests/AllTests.java @@ -14,6 +14,7 @@ package org.eclipse.papyrus.infra.emf.readonly.tests; import org.eclipse.papyrus.infra.emf.readonly.PapyrusROTransactionalEditingDomainTest; import org.eclipse.papyrus.infra.emf.readonly.ReadOnlyManagerTest; +import org.eclipse.papyrus.infra.emf.readonly.ReadOnlyTesterTest; import org.eclipse.papyrus.infra.emf.readonly.ReferencedModelReadOnlyHandlerTest; import org.junit.runner.RunWith; import org.junit.runners.Suite; @@ -25,7 +26,7 @@ import org.junit.runners.Suite.SuiteClasses; */ @RunWith(Suite.class) @SuiteClasses({ -PapyrusROTransactionalEditingDomainTest.class, ReferencedModelReadOnlyHandlerTest.class, ReadOnlyManagerTest.class +PapyrusROTransactionalEditingDomainTest.class, ReferencedModelReadOnlyHandlerTest.class, ReadOnlyManagerTest.class, ReadOnlyTesterTest.class }) public class AllTests { diff --git a/tests/junit/plugins/infra/emf/org.eclipse.papyrus.infra.emf.readonly.tests/src/org/eclipse/papyrus/infra/emf/readonly/tests/PapyrusROEditingDomainFixture.java b/tests/junit/plugins/infra/emf/org.eclipse.papyrus.infra.emf.readonly.tests/src/org/eclipse/papyrus/infra/emf/readonly/tests/PapyrusROEditingDomainFixture.java new file mode 100644 index 00000000000..3f1db0e0338 --- /dev/null +++ b/tests/junit/plugins/infra/emf/org.eclipse.papyrus.infra.emf.readonly.tests/src/org/eclipse/papyrus/infra/emf/readonly/tests/PapyrusROEditingDomainFixture.java @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2014 CEA 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: + * Christian W. Damus (CEA) - Initial API and implementation + * + */ +package org.eclipse.papyrus.infra.emf.readonly.tests; + +import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl; +import org.eclipse.papyrus.infra.emf.readonly.PapyrusROTransactionalEditingDomain; +import org.eclipse.papyrus.infra.emf.readonly.PapyrusROTransactionalEditingDomainProvider; +import org.eclipse.papyrus.junit.utils.rules.AbstractModelFixture; + + +/** + * This is the PapyrusROEditingDomainFixture type. Enjoy. + */ +public class PapyrusROEditingDomainFixture extends AbstractModelFixture<PapyrusROTransactionalEditingDomain> { + + public PapyrusROEditingDomainFixture() { + super(); + } + + protected PapyrusROTransactionalEditingDomain createEditingDomain() { + return (PapyrusROTransactionalEditingDomain)new PapyrusROTransactionalEditingDomainProvider().createTransactionalEditingDomain(new ResourceSetImpl()); + } +} diff --git a/tests/junit/plugins/junit/org.eclipse.papyrus.junit.utils/src/org/eclipse/papyrus/junit/utils/rules/AbstractModelFixture.java b/tests/junit/plugins/junit/org.eclipse.papyrus.junit.utils/src/org/eclipse/papyrus/junit/utils/rules/AbstractModelFixture.java new file mode 100644 index 00000000000..25829351a1f --- /dev/null +++ b/tests/junit/plugins/junit/org.eclipse.papyrus.junit.utils/src/org/eclipse/papyrus/junit/utils/rules/AbstractModelFixture.java @@ -0,0 +1,187 @@ +/* + * Copyright (c) 2014 CEA 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: + * Christian W. Damus (CEA) - Initial API and implementation + * + */ +package org.eclipse.papyrus.junit.utils.rules; + +import static org.hamcrest.CoreMatchers.notNullValue; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.fail; + +import java.io.InputStream; +import java.lang.reflect.Method; +import java.net.URL; + +import org.eclipse.emf.common.util.URI; +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.edit.domain.EditingDomain; +import org.eclipse.emf.transaction.TransactionalEditingDomain; +import org.eclipse.papyrus.junit.utils.rules.ProjectFixture; +import org.eclipse.uml2.uml.Model; +import org.junit.Rule; +import org.junit.rules.TestWatcher; +import org.junit.runner.Description; +import org.junit.runners.model.Statement; +import org.osgi.framework.Bundle; +import org.osgi.framework.FrameworkUtil; + + +/** + * Abstract superclass for JUnit test fixture rules that provide: + * <ul> + * <li>an editing domain of some kind (subclasses must create it)</li> + * <li>a test project in the workspace, exposed via a nested {@link ProjectFixture} rule</li> + * <li>a test {@link Model} loaded from a resource in the plug-in and saved as <tt>model.uml</tt> in the test project. This model is specified using + * an annotation on the test, as described below</li> + * </ul> + * The test model template to load into the editing domain and project must be specified by one of the following annotations: + * <ul> + * <li>{@link JavaResource @JavaResource}: specifies the path to a resource to be loaded from the test class's classpath, using the + * {@link Class#getResource(String)} API</li> + * <li>{@link PluginResource @PluginResource}: specifies a path relative to the root of the OSGi bundle containing the test class, to be loaded via + * the {@link Bundle#getEntry(String)} API</li> + * </ul> + * The resource annotation may be specified either on the test method, in which case it applies to that test case, or on the test + * class, in which case it applies to all test methods in the class that do not have a resource annotation of their own (method + * annotations take precedence over the class annotation). + */ +public abstract class AbstractModelFixture<T extends EditingDomain> extends TestWatcher { + + private final ProjectFixture project = new ProjectFixture(); + + private T domain; + + private Model model; + + public AbstractModelFixture() { + super(); + } + + public Statement apply(Statement base, Description description) { + // Wrap myself in the project rule so that the project exists when I start + Statement result = super.apply(base, description); + result = project.apply(result, description); + return result; + } + + /** + * Obtains the nested project fixture rule. If stored in a field of the test class, it must not be annotated as a {@link Rule @Rule} because that + * would result in double initialization of the rule. + * + * @return the nested project fixture + */ + public ProjectFixture getProject() { + return project; + } + + public T getEditingDomain() { + return domain; + } + + /** + * Obtains the test model, which is resident in the <tt>model.uml</tt> file in the test project (as indicated by its + * {@linkplain #getModelResourceURI() URI}). + * + * @return the test model + */ + public Model getModel() { + return model; + } + + public Resource getModelResource() { + return getModel().eResource(); + } + + public URI getModelResourceURI() { + return getModelResource().getURI(); + } + + public URI getModelURI() { + return EcoreUtil.getURI(getModel()); + } + + protected abstract T createEditingDomain(); + + @Override + protected void starting(Description description) { + domain = createEditingDomain(); + + Resource res = domain.getResourceSet().createResource(project.getURI("model.uml")); + + try { + InputStream input = getResourceURL(description).openStream(); + try { + res.load(input, null); + } finally { + input.close(); + } + res.save(null); // Make sure it exists + } catch (Exception e) { + e.printStackTrace(); + fail("Failed to load test resource: " + e.getLocalizedMessage()); + } + + model = (Model)res.getContents().get(0); + } + + @Override + protected void finished(Description description) { + ResourceSet rset = domain.getResourceSet(); + + model = null; + + if(domain instanceof TransactionalEditingDomain) { + ((TransactionalEditingDomain)domain).dispose(); + } + domain = null; + + for(Resource next : rset.getResources()) { + next.unload(); + next.eAdapters().clear(); + } + + rset.getResources().clear(); + rset.eAdapters().clear(); + } + + protected URL getResourceURL(Description description) { + URL result = null; + + Class<?> testClass = description.getTestClass(); + + Method testMethod = null; + try { + testMethod = testClass.getDeclaredMethod(description.getMethodName()); + } catch (Exception e) { + e.printStackTrace(); + fail("Could not access test method via JUnit framework."); + } + + if(testMethod.isAnnotationPresent(JavaResource.class)) { + result = testClass.getResource(testMethod.getAnnotation(JavaResource.class).value()); + } else if(testMethod.isAnnotationPresent(PluginResource.class)) { + result = FrameworkUtil.getBundle(testClass).getEntry(testMethod.getAnnotation(PluginResource.class).value()); + } else { + // The class must have an annotation + if(testClass.isAnnotationPresent(JavaResource.class)) { + result = testClass.getResource(testClass.getAnnotation(JavaResource.class).value()); + } else if(testClass.isAnnotationPresent(PluginResource.class)) { + result = FrameworkUtil.getBundle(testClass).getEntry(testClass.getAnnotation(PluginResource.class).value()); + } + } + + assertThat("No JavaResource or PluginResource annotation found on test.", result, notNullValue()); + + return result; + } +} diff --git a/tests/junit/plugins/junit/org.eclipse.papyrus.junit.utils/src/org/eclipse/papyrus/junit/utils/rules/JavaResource.java b/tests/junit/plugins/junit/org.eclipse.papyrus.junit.utils/src/org/eclipse/papyrus/junit/utils/rules/JavaResource.java new file mode 100644 index 00000000000..d9aa6b14340 --- /dev/null +++ b/tests/junit/plugins/junit/org.eclipse.papyrus.junit.utils/src/org/eclipse/papyrus/junit/utils/rules/JavaResource.java @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2014 CEA 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: + * Christian W. Damus (CEA) - Initial API and implementation + * + */ +package org.eclipse.papyrus.junit.utils.rules; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + + +/** + * Annotation indicating the classpath-relative path to a resource from which to load the test model of an {@link AbstractModelFixture}. + * + * @see AbstractModelFixture + * @see PluginResource + */ +@Target({ ElementType.METHOD, ElementType.TYPE }) +@Retention(RetentionPolicy.RUNTIME) +public @interface JavaResource { + + String value(); +} diff --git a/tests/junit/plugins/junit/org.eclipse.papyrus.junit.utils/src/org/eclipse/papyrus/junit/utils/rules/PluginResource.java b/tests/junit/plugins/junit/org.eclipse.papyrus.junit.utils/src/org/eclipse/papyrus/junit/utils/rules/PluginResource.java new file mode 100644 index 00000000000..dc1e7e6eacd --- /dev/null +++ b/tests/junit/plugins/junit/org.eclipse.papyrus.junit.utils/src/org/eclipse/papyrus/junit/utils/rules/PluginResource.java @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2014 CEA 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: + * Christian W. Damus (CEA) - Initial API and implementation + * + */ +package org.eclipse.papyrus.junit.utils.rules; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + + +/** + * Annotation indicating the bundle-relative path to a resource from which to load the test model of an {@link AbstractModelFixture}. + * + * @see AbstractModelFixture + * @see JavaResource + */ +@Target({ ElementType.METHOD, ElementType.TYPE }) +@Retention(RetentionPolicy.RUNTIME) +public @interface PluginResource { + + String value(); +} |