Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorCamille Letavernier2014-10-22 13:57:10 +0000
committerCamille Letavernier2014-10-22 14:58:56 +0000
commit27f05ae2e8877dd129c07d025eccd5abd009fc3d (patch)
tree2847b2c88243fcbaf2d0790efb7bbb32255ae501 /tests/junit/plugins/junit
parent97b039fb883348839be5585518dcc7e3e7047434 (diff)
downloadorg.eclipse.papyrus-27f05ae2e8877dd129c07d025eccd5abd009fc3d.tar.gz
org.eclipse.papyrus-27f05ae2e8877dd129c07d025eccd5abd009fc3d.tar.xz
org.eclipse.papyrus-27f05ae2e8877dd129c07d025eccd5abd009fc3d.zip
448389: [JUnit Rules] Add a JUnit Rule for bypassing the Transactions in
Tests https://bugs.eclipse.org/bugs/show_bug.cgi?id=448389
Diffstat (limited to 'tests/junit/plugins/junit')
-rw-r--r--tests/junit/plugins/junit/org.eclipse.papyrus.junit.utils/src/org/eclipse/papyrus/junit/utils/rules/NoTransactionFixture.java95
-rw-r--r--tests/junit/plugins/junit/org.eclipse.papyrus.junit.utils/src/org/eclipse/papyrus/junit/utils/rules/NoTransactionRule.java59
2 files changed, 154 insertions, 0 deletions
diff --git a/tests/junit/plugins/junit/org.eclipse.papyrus.junit.utils/src/org/eclipse/papyrus/junit/utils/rules/NoTransactionFixture.java b/tests/junit/plugins/junit/org.eclipse.papyrus.junit.utils/src/org/eclipse/papyrus/junit/utils/rules/NoTransactionFixture.java
new file mode 100644
index 00000000000..1f4cefd6ae8
--- /dev/null
+++ b/tests/junit/plugins/junit/org.eclipse.papyrus.junit.utils/src/org/eclipse/papyrus/junit/utils/rules/NoTransactionFixture.java
@@ -0,0 +1,95 @@
+/*****************************************************************************
+ * Copyright (c) 2014 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
+ *
+ *****************************************************************************/
+
+package org.eclipse.papyrus.junit.utils.rules;
+
+import java.util.concurrent.atomic.AtomicReference;
+
+import org.eclipse.emf.common.command.AbstractCommand;
+import org.eclipse.emf.ecore.resource.ResourceSet;
+import org.junit.rules.TestRule;
+import org.junit.runner.Description;
+import org.junit.runners.model.Statement;
+
+/**
+ * This rule provides a ResourceSet with a TransactionalEditingDomain
+ *
+ * All test methods are executed within a Transaction (Which means test method
+ * do not need to care about transactions)
+ *
+ * This fixture is meant to be used through {@link NoTransactionRule}
+ *
+ * @author Camille Letavernier
+ *
+ * @see {@link NoTransactionRule}
+ */
+public class NoTransactionFixture implements TestRule {
+
+
+ private final ModelSetFixture modelSet;
+
+ public NoTransactionFixture(ModelSetFixture modelSet) {
+ this.modelSet = modelSet;
+ }
+
+ /**
+ * @see org.junit.rules.TestRule#apply(org.junit.runners.model.Statement, org.junit.runner.Description)
+ *
+ * @param arg0
+ * @param arg1
+ * @return
+ */
+ public Statement apply(final Statement base, final Description description) {
+ return new Statement() {
+ @Override
+ public void evaluate() throws Throwable {
+
+ final AtomicReference<Throwable> throwable = new AtomicReference<Throwable>();
+ modelSet.getEditingDomain().getCommandStack().execute(new AbstractCommand() {
+
+ public void execute() {
+ try {
+ base.evaluate();
+ } catch (Throwable t) {
+ throwable.set(t);
+ }
+ }
+
+ public void redo() {
+ // Nothing
+ }
+
+ /**
+ * @see org.eclipse.emf.common.command.AbstractCommand#prepare()
+ *
+ * @return
+ */
+ @Override
+ protected boolean prepare() {
+ return true;
+ }
+
+ });
+
+ if (throwable.get() != null) {
+ throw throwable.get();
+ }
+ }
+ };
+ }
+
+ public ResourceSet getResourceSet() {
+ return modelSet.getResourceSet();
+ }
+
+}
diff --git a/tests/junit/plugins/junit/org.eclipse.papyrus.junit.utils/src/org/eclipse/papyrus/junit/utils/rules/NoTransactionRule.java b/tests/junit/plugins/junit/org.eclipse.papyrus.junit.utils/src/org/eclipse/papyrus/junit/utils/rules/NoTransactionRule.java
new file mode 100644
index 00000000000..46797e4a31b
--- /dev/null
+++ b/tests/junit/plugins/junit/org.eclipse.papyrus.junit.utils/src/org/eclipse/papyrus/junit/utils/rules/NoTransactionRule.java
@@ -0,0 +1,59 @@
+/*****************************************************************************
+ * Copyright (c) 2014 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
+ *
+ *****************************************************************************/
+
+package org.eclipse.papyrus.junit.utils.rules;
+
+import org.eclipse.emf.ecore.resource.Resource;
+import org.eclipse.emf.ecore.resource.ResourceSet;
+import org.junit.rules.RuleChain;
+import org.junit.rules.TestRule;
+import org.junit.runner.Description;
+import org.junit.runners.model.Statement;
+
+/**
+ * This rule provides a ResourceSet with a TransactionalEditingDomain
+ *
+ * All test methods are executed within a Transaction (Which means test method
+ * do not need to care about transactions)
+ *
+ * Usage:
+ *
+ * <pre>
+ * &#064;Rule
+ * public NoTransactionRule noTransaction = new NoTransactionRule();
+ * </pre>
+ *
+ * @author Camille Letavernier
+ */
+public class NoTransactionRule implements TestRule {
+
+ private final ModelSetFixture modelSet = new ModelSetFixture();
+
+ private final NoTransactionFixture noTransaction = new NoTransactionFixture(modelSet);
+
+ public RuleChain getRuleChain() {
+ return RuleChain.outerRule(modelSet).around(noTransaction);
+ }
+
+ public Statement apply(Statement base, Description description) {
+ return getRuleChain().apply(base, description);
+ }
+
+ public ResourceSet getResourceSet() {
+ return modelSet.getResourceSet();
+ }
+
+ public Resource getModelResource() {
+ return modelSet.getModelResource();
+ }
+}

Back to the top