Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 231adc4f3848d40ea3f5a93a7990369f17ae61f7 (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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/*
 * 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.io.OutputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.net.URL;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Set;

import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
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.resource.impl.ResourceSetImpl;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.emf.ecore.xmi.XMLResource;
import org.eclipse.emf.ecore.xml.type.AnyType;
import org.eclipse.emf.edit.domain.EditingDomain;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.papyrus.infra.core.resource.ModelSet;
import org.eclipse.papyrus.infra.core.resource.sasheditor.DiModel;
import org.eclipse.papyrus.infra.emf.utils.EMFHelper;
import org.eclipse.papyrus.infra.gmfdiag.common.model.NotationModel;
import org.eclipse.papyrus.uml.tools.model.UmlModel;
import org.eclipse.uml2.uml.Package;
import org.eclipse.uml2.uml.Profile;
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;

import com.google.common.base.Predicates;
import com.google.common.collect.Iterables;
import com.google.common.io.ByteStreams;


/**
 * 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 Package} 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 Package model;

	private Class<?> testClass;

	public AbstractModelFixture() {
		super();
	}

	public Statement apply(Statement base, Description description) {
		testClass = description.getTestClass();

		// 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;
	}

	public ResourceSet getResourceSet() {
		EditingDomain domain = getEditingDomain();
		return (domain == null) ? null : domain.getResourceSet();
	}

	/**
	 * 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 Package 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();
		model = (Package)initModelResource(description).getContents().get(0);
	}

	protected Resource initModelResource(Description description) {
		Annotation resourceAnnotation = getResourceAnnotation(description);
		ResourceKind kind = ResourceKind.getResourceKind(resourceAnnotation);

		IPath resourcePath = new Path(kind.getResourcePath(resourceAnnotation));

		String targetResourceName = "model";
		if(isDIModel(resourcePath)) {
			// We will be initializing all three resources, and they have cross-references, so must not change
			// resource name
			targetResourceName = resourcePath.removeFileExtension().lastSegment();
		}

		return initModelResource(targetResourceName, kind, resourcePath.toString());
	}

	protected boolean isDIModel(IPath path) {
		String fileExtension = path.getFileExtension();
		return DiModel.DI_FILE_EXTENSION.equals(fileExtension);
	}

	protected Resource initModelResource(String targetPath, ResourceKind resourceKind, String resourcePath) {
		Resource result;

		ResourceSet resourceSet = getResourceSet();
		final boolean bootstrapResourceSet = resourceSet == null;
		if(bootstrapResourceSet) {
			// Bootstrap the initialization of the test model with a plain resource set
			resourceSet = new ResourceSetImpl();
			resourceSet.getLoadOptions().put(XMLResource.OPTION_RECORD_UNKNOWN_FEATURE, true);
			resourceSet.getLoadOptions().put(XMLResource.OPTION_LAX_FEATURE_PROCESSING, true);
		}

		try {
			IPath resourceIPath = new Path(resourcePath);
			if(isDIModel(resourceIPath)) {
				// Try to initialize the triumvirate of files
				resourceIPath = resourceIPath.removeFileExtension();

				// The UML resource must exist for any sane test
				result = doInitModelResource(resourceSet, targetPath, resourceKind, resourceIPath.addFileExtension(UmlModel.UML_FILE_EXTENSION));

				// Both of these are optional
				IPath notationPath = resourceIPath.addFileExtension(NotationModel.NOTATION_FILE_EXTENSION);
				if(resourceKind.exists(testClass, notationPath)) {
					doInitModelResource(resourceSet, targetPath, resourceKind, notationPath);
				}
				IPath diPath = resourceIPath.addFileExtension(DiModel.DI_FILE_EXTENSION);
				if(resourceKind.exists(testClass, diPath)) {
					doInitModelResource(resourceSet, targetPath, resourceKind, diPath);
				}
			} else {
				result = doInitModelResource(resourceSet, targetPath, resourceKind, resourceIPath);
			}

			// Look for any other dependencies (libraries, profiles, etc.) that also need to be copied
			Queue<Resource> dependents = new LinkedList<Resource>();
			Set<Resource> scanned = new HashSet<Resource>();
			dependents.add(result);
			boolean loadedProfiles = false;
			for(Resource dependent = dependents.poll(); dependent != null; dependent = dependents.poll()) {
				if(scanned.add(dependent)) {
					URI baseURI = result.getURI().trimSegments(1);
					if(!baseURI.isPrefix()) {
						baseURI = baseURI.appendSegment("");
					}

					for(EObject proxy : EcoreUtil.UnresolvedProxyCrossReferencer.find(dependent).keySet()) {
						URI dependencyURI = EcoreUtil.getURI(proxy).trimFragment();
						if(dependencyURI.toString().startsWith(baseURI.toString())) {
							Resource dependency = resourceSet.getResource(dependencyURI, false);
							if((dependency == null) || !dependency.isLoaded() || !dependency.getErrors().isEmpty()) {
								// It should be available in the test bundle. Try to get it
								URI relative = dependencyURI.deresolve(baseURI);
								IPath depPath = resourceIPath.removeLastSegments(1).append(URI.decode(relative.toString()));
								if(resourceKind.exists(testClass, depPath)) {
									if(dependency == null) {
										dependency = resourceSet.createResource(dependencyURI);
									} else {
										dependency.unload();
									}

									dependency = doInitModelResource(resourceSet, URI.decode(relative.toString()), resourceKind, depPath);
									loadedProfiles = loadedProfiles || Iterables.any(dependency.getContents(), Predicates.instanceOf(Profile.class));

									// Enqueue this for recursive dependency processing
									dependents.add(dependency);
								}
							}
						}
					}
				}
			}

			// If we depend on profiles, then we may have stereotype applications that need to resolve against that schema.
			// In such case, re-load the model resource to resolve the stereotype schema
			if(loadedProfiles && Iterables.any(result.getContents(), Predicates.instanceOf(AnyType.class))) {
				try {
					result.unload();
					result.load(null);
				} catch (Exception e) {
					e.printStackTrace();
					fail("Error re-loading resource to resolve stereotype schema: " + e.getLocalizedMessage());
				}
			}
		} finally {
			if(bootstrapResourceSet) {
				EMFHelper.unload(resourceSet);
			}
		}

		return result;
	}

	private Resource doInitModelResource(ResourceSet resourceSet, String targetPath, ResourceKind resourceKind, IPath resourceIPath) {
		IPath targetIPath = new Path(targetPath);
		if(!resourceIPath.getFileExtension().equals(targetIPath.getFileExtension())) {
			targetIPath = targetIPath.addFileExtension(resourceIPath.getFileExtension());
		}

		final URI modelURI = project.getURI(targetIPath);
		Resource result = resourceSet.getResource(modelURI, false);

		if(result == null) {
			result = resourceSet.createResource(modelURI);
		}

		if(!result.isLoaded()) {
			if(resourceSet instanceof ModelSet) {
				ModelSet modelSet = (ModelSet)resourceSet;
				if(modelSet.getURIWithoutExtension() == null) {
					modelSet.getInternal().setPrimaryModelResourceURI(modelURI);
				}
			}

			try {
				InputStream input = resourceKind.getResourceURL(testClass, resourceIPath).openStream();
				OutputStream output = resourceSet.getURIConverter().createOutputStream(result.getURI());

				try {
					ByteStreams.copy(input, output);
				} finally {
					input.close();
					output.close();
				}

				result.load(null);
			} catch (Exception e) {
				e.printStackTrace();
				fail("Failed to load test resource: " + e.getLocalizedMessage());
			}
		}

		return result;
	}

	@Override
	protected void finished(Description description) {
		final ResourceSet resourceSet = getResourceSet();

		model = null;

		if(domain instanceof TransactionalEditingDomain) {
			((TransactionalEditingDomain)domain).dispose();
		}
		domain = null;

		if(resourceSet != null) {
			EMFHelper.unload(resourceSet);
		}
	}

	private Annotation getResourceAnnotation(Description description) {
		Annotation 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 = testMethod.getAnnotation(JavaResource.class);
		} else if(testMethod.isAnnotationPresent(PluginResource.class)) {
			result = testMethod.getAnnotation(PluginResource.class);
		} else {
			// The class must have an annotation
			if(testClass.isAnnotationPresent(JavaResource.class)) {
				result = testClass.getAnnotation(JavaResource.class);
			} else if(testClass.isAnnotationPresent(PluginResource.class)) {
				result = testClass.getAnnotation(PluginResource.class);
			}
		}

		assertThat("No JavaResource or PluginResource annotation found on test.", result, notNullValue());

		return result;
	}

	public static enum ResourceKind {
		JAVA, BUNDLE;

		static ResourceKind getResourceKind(Annotation resourceAnnotation) {
			return (resourceAnnotation instanceof JavaResource) ? JAVA : (resourceAnnotation instanceof PluginResource) ? BUNDLE : null;
		}

		String getResourcePath(Annotation resourceAnnotation) {
			switch(this) {
			case JAVA:
				return ((JavaResource)resourceAnnotation).value();
			case BUNDLE:
				return ((PluginResource)resourceAnnotation).value();
			}

			fail("Not a resource annotation: " + resourceAnnotation);
			return null; // Not reachable
		}

		boolean exists(Class<?> context, IPath path) {
			return getResourceURL(context, path) != null;
		}

		URL getResourceURL(Class<?> context, IPath path) {
			URL result = null;

			switch(this) {
			case JAVA:
				result = context.getResource(path.toString());
				break;
			case BUNDLE:
				result = getBundleURL(context, path);
				break;
			}

			return result;
		}

		private URL getBundleURL(Class<?> testClass, IPath resourcePath) {
			URL result = null;

			String pattern = resourcePath.lastSegment();
			IPath search;
			if(resourcePath.segmentCount() > 1) {
				search = resourcePath.removeLastSegments(1);
			} else {
				search = Path.ROOT;
			}
			Enumeration<URL> urls = FrameworkUtil.getBundle(testClass).findEntries(search.toPortableString(), pattern, false);
			if((urls != null) && urls.hasMoreElements()) {
				result = urls.nextElement();
			}

			return result;
		}
	}
}

Back to the top