Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: dcada7fc60e4fc4577c5819c736c1dcd6014684d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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();
	}

}

Back to the top