Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ab885d1afeb2a123f9a8676728ea2fd818bd23c5 (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/*****************************************************************************
 * Copyright (c) 2015 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:
 *   Christian W. Damus - Initial API and implementation
 *   
 *****************************************************************************/

package org.eclipse.papyrus.uml.diagram.tests.canonical;

import static org.junit.Assert.fail;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.emf.common.command.Command;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.emf.transaction.RecordingCommand;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.gmf.runtime.diagram.ui.editparts.DiagramEditPart;
import org.eclipse.gmf.runtime.notation.Diagram;
import org.eclipse.gmf.runtime.notation.NotationPackage;
import org.eclipse.gmf.runtime.notation.StringValueStyle;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.papyrus.commands.ICreationCommand;
import org.eclipse.papyrus.infra.architecture.ArchitectureDescriptionUtils;
import org.eclipse.papyrus.infra.architecture.ArchitectureDomainManager;
import org.eclipse.papyrus.infra.core.architecture.merged.MergedArchitectureContext;
import org.eclipse.papyrus.infra.core.architecture.merged.MergedArchitectureViewpoint;
import org.eclipse.papyrus.infra.core.resource.ModelSet;
import org.eclipse.papyrus.infra.core.services.ExtensionServicesRegistry;
import org.eclipse.papyrus.infra.core.services.ServiceException;
import org.eclipse.papyrus.infra.core.services.ServicesRegistry;
import org.eclipse.papyrus.infra.gmfdiag.common.reconciler.DiagramVersioningUtils;
import org.eclipse.papyrus.infra.ui.editor.IMultiDiagramEditor;
import org.eclipse.papyrus.junit.utils.JUnitUtils;
import org.eclipse.papyrus.junit.utils.rules.HouseKeeper;
import org.eclipse.papyrus.uml.architecture.UMLArchitectureContextIds;
import org.eclipse.papyrus.uml.diagram.common.commands.CreateUMLModelCommand;
import org.eclipse.papyrus.uml.diagram.profile.CreateProfileModelCommand;
import org.eclipse.papyrus.uml.diagram.wizards.command.NewPapyrusModelCommand;
import org.eclipse.papyrus.uml.tools.model.UmlModel;
import org.eclipse.papyrus.uml.tools.utils.CustomUMLUtil;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.intro.IIntroPart;
import org.eclipse.uml2.uml.Package;
import org.eclipse.uml2.uml.UMLPackage;
import org.junit.Assert;
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.Statement;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterators;
import com.google.common.collect.Lists;

/**
 * <p>
 * Shared state for the tests in a generated test suite ("test context"). Manages
 * a single diagram editor to be shared by all of the test cases, with clean-up
 * after each test to allow it to be safely reused by the next.
 * </p>
 * <p>
 * This behavior is factored out as a test rule so as to make as little impact as possible
 * on the structure of the base test framework, which is shared with the non-generated tests.
 * </p>
 */
@StateNotShareable
public class SharedTestSuiteState implements TestRule {
	private final HouseKeeper houseKeeper = new HouseKeeper();

	private boolean canShareState;

	private AbstractPapyrusTestCase testCase;

	private ModelSet modelSet;
	private IProject project;
	private IFile file;
	private IMultiDiagramEditor papyrusEditor;
	private View diagram;
	private List<EObject> preserve;
	private DiagramEditPart diagramEditPart;

	public SharedTestSuiteState() {
		super();
	}

	@Override
	public Statement apply(Statement base, Description description) {
		Statement result;

		if (description.isSuite()) {
			result = applyToTestContext(base, description);
		} else {
			fail("GeneratedTestSuiteStateRule may only be used with @ClassRule");
			result = null; // Unreachable
		}

		return result;
	}

	public HouseKeeper testState() {
		return new HouseKeeper() {

			@Override
			public Statement apply(final Statement base, FrameworkMethod method, final Object target) {
				Statement result;

				if (canShareState) {
					result = applyToTestCase(base, method, target);
				} else {
					result = new Statement() {

						@Override
						public void evaluate() throws Throwable {
							testCase = (AbstractPapyrusTestCase) target;
							try {
								base.evaluate();
							} finally {
								testCase = null;
							}
						}
					};
				}

				return super.apply(result, method, target);
			}
		};
	}

	public IProject getProject() {
		return project;
	}

	protected Statement applyToTestContext(final Statement base, Description description) {
		canShareState = JUnitUtils.getAnnotation(description, StateNotShareable.class) == null;

		return !canShareState
				? base
						: houseKeeper.apply(base, method("apply", Statement.class, Description.class), this); //$NON-NLS-1$
	}

	private FrameworkMethod method(String name, Class<?>... parameterType) {
		try {
			return new FrameworkMethod(SharedTestSuiteState.class.getMethod(name, parameterType));
		} catch (Exception e) {
			fail(e.getMessage());
			return null; // Unreachable
		}
	}

	protected void createProject() throws Exception {
		// Ensure that the intro is not visible
		Runnable closeIntroRunnable = new Runnable() {

			@Override
			public void run() {
				try {
					IIntroPart introPart = PlatformUI.getWorkbench().getIntroManager().getIntro();
					if (introPart != null) {
						PlatformUI.getWorkbench().getIntroManager().closeIntro(introPart);
					}
				} catch (Exception ex) {
					ex.printStackTrace(System.out);
					Assert.fail(ex.getMessage());
				}
			}
		};
		Display.getDefault().syncExec(closeIntroRunnable);

		// Make sure it is nullified for the next test context
		houseKeeper.setField("project", houseKeeper.createProject(testCase.getProjectName())); //$NON-NLS-1$
		houseKeeper.setField("file", project.getFile(testCase.getFileName())); //$NON-NLS-1$

		@SuppressWarnings("deprecation")
		ModelSet _modelSet = houseKeeper.cleanUpLater(new org.eclipse.papyrus.infra.core.utils.DiResourceSet());
		modelSet = _modelSet;
		// at this point, no resources have been created

		if (file.exists()) {
			file.delete(true, null);
		}
		if (!file.exists()) {
			URI fileURI = URI.createPlatformResourceURI(file.getFullPath().toString(), true);
			modelSet.createModels(fileURI);

			final String contextId;
			if (fileURI.lastSegment().matches(".*\\.profile\\.(di|uml)"))
				contextId = UMLArchitectureContextIds.Profile;
			else
				contextId = UMLArchitectureContextIds.UML;

			final List<String> viewpointIds = new ArrayList<String>();
			MergedArchitectureContext context = ArchitectureDomainManager.getInstance().getArchitectureContextById(contextId);
			for (MergedArchitectureViewpoint viewpoint : context.getViewpoints()) {
				viewpointIds.add(viewpoint.getId());
			}

			ArchitectureDescriptionUtils utils = new ArchitectureDescriptionUtils(modelSet);
			Command c = utils.createNewModel(contextId, viewpointIds.toArray(new String[0]));

			TransactionalEditingDomain ted = modelSet.getTransactionalEditingDomain();
			ted.getCommandStack().execute(c);

			ServicesRegistry registry = new ExtensionServicesRegistry(org.eclipse.papyrus.infra.core.Activator.PLUGIN_ID);
			try {
				registry.add(ModelSet.class, Integer.MAX_VALUE, modelSet); // High priority to override all contributions
				registry.startRegistry();
			} catch (ServiceException ex) {
				// Ignore exceptions
			}
		}
	}

	protected void createDiagramEditor() throws Exception {
		// If we're not the first test in the suite, there will already be a diagram
		if (diagram == null) {
			// First, create the diagram
			ICreationCommand command = testCase.getDiagramCommandCreation();
			command.createDiagram(modelSet, null, "DiagramToTest"); //$NON-NLS-1$

			// Must save the model so that the editor may open it
			modelSet.save(new NullProgressMonitor());

			// Forget this resource set; we'll get the editor's to replace it
			modelSet.unload();

			// Then, open the Papyrus editor
			papyrusEditor = houseKeeper.openPapyrusEditor(file);
			Assert.assertNotNull("Failed to open the editor", papyrusEditor); //$NON-NLS-1$

			// Get the new resource set from this editor
			modelSet = papyrusEditor.getServicesRegistry().getService(ModelSet.class);

			// And get the diagram
			diagram = getDiagramEditPart().getDiagramView();

			// Whatever the model and diagram are initialized with, don't destroy any of it
			ImmutableList.Builder<EObject> preserve = ImmutableList.builder();
			preserve.addAll(diagram.eResource().getAllContents());

			UmlModel uml = (UmlModel) modelSet.getModel(UmlModel.MODEL_ID);
			preserve.addAll(uml.getResource().getAllContents());

			houseKeeper.setField("preserve", preserve.build());
		}
	}

	/**
	 * Obtains the root model element of the test suite, that should be preserved when cleaning up
	 * each individual test case for reuse by the next. For example, in the case of behavior diagrams,
	 * this would be the behavior, itself.
	 * 
	 * @param testModel
	 *            the root package of the test model, which is shared by all test cases anyways
	 * 
	 * @return the root model element to share amongst test cases, or {@code null} if none
	 *         (just preserve the test model root package, then)
	 */
	protected EObject getRootModelElement(Package testModel) {
		return (EObject) EcoreUtil.getObjectByType(testModel.eContents(), UMLPackage.Literals.BEHAVIOR);
	}

	/**
	 * Gets the diagram edit part.
	 *
	 * @return the diagram edit part
	 */
	protected DiagramEditPart getDiagramEditPart() {
		if (diagramEditPart == null) {
			IEditorPart diagramEditor = papyrusEditor.getActiveEditor();
			diagramEditPart = papyrusEditor.getAdapter(DiagramEditPart.class);
			Assert.assertNotNull("Cannot find the diagram editor", diagramEditor); //$NON-NLS-1$
			Assert.assertNotNull("Cannot find the Diagram edit part", diagramEditPart); //$NON-NLS-1$
			StringValueStyle style = (StringValueStyle) diagramEditPart.getNotationView().getNamedStyle(NotationPackage.eINSTANCE.getStringValueStyle(), DiagramVersioningUtils.COMPATIBILITY_VERSION);
			Assert.assertNotNull("A version must be associated with every diagram", style); //$NON-NLS-1$
			Assert.assertTrue("The created diagram has not a good version", DiagramVersioningUtils.isOfCurrentPapyrusVersion((Diagram) diagramEditPart.getNotationView())); //$NON-NLS-1$
		}
		return diagramEditPart;
	}

	protected Statement applyToTestCase(final Statement base, FrameworkMethod method, final Object testCase) {
		return !canShareState ? base : new Statement() {

			@Override
			public void evaluate() throws Throwable {
				SharedTestSuiteState.this.testCase = (AbstractPapyrusTestCase) testCase;

				if (project == null) {
					createProject();
				}
				createDiagramEditor();
				setTestState();
				try {
					base.evaluate();
				} finally {
					cleanup();
					SharedTestSuiteState.this.testCase = null;
				}
			}
		};
	}

	protected void cleanup() throws Exception {
		// Delete any model content previously created
		final TransactionalEditingDomain domain = modelSet.getTransactionalEditingDomain();
		domain.getCommandStack().execute(new RecordingCommand(domain) {

			@Override
			protected void doExecute() {
				// Destroy any other diagrams that were created, CSS stylesheets, etc.
				List<EObject> toDestroy = Lists.newArrayList();
				if( diagram.eResource()!=null){
					Iterators.addAll(toDestroy, diagram.eResource().getAllContents());
				}

				UmlModel uml = (UmlModel) modelSet.getModel(UmlModel.MODEL_ID);
				if( uml.getResource()!=null){
				Iterators.addAll(toDestroy, uml.getResource().getAllContents());
				}

				toDestroy.removeAll(preserve);

				CustomUMLUtil.destroyAll(toDestroy);
			}
		});
		domain.getCommandStack().flush();
	}

	private void setTestState() {
		testCase.project = project;
		testCase.file = file;
		testCase.papyrusEditor = papyrusEditor;
		testCase.diResourceSet = modelSet;
	}

	public void setupTest() throws Exception {
		if (!canShareState) {
			testCase.doSetUp();
		}
	}

	void teardownTest() throws Exception {
		if (!canShareState) {
			testCase.doTearDown();
		}
	}
}

Back to the top