Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: d3c434da94251cd4db743df9921f6c99685c7317 (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
/*****************************************************************************
 * Copyright (c) 2010, 2014 Atos Origin, 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:
 *  Emilien Perico (Atos Origin) emilien.perico@atosorigin.com - Initial API and implementation
 *  Christian W. Damus (CEA) - Work around regression in URI parsing in EMF 2.9
 *  Christian W. Damus (CEA) - bug 437217 - control-mode strategy changes interfere with later tests
 *
 *****************************************************************************/
package org.eclipse.papyrus.infra.services.resourceloading.tests.testModel1;

import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.gmf.runtime.notation.Node;
import org.eclipse.papyrus.infra.core.resource.ModelSet;
import org.eclipse.papyrus.infra.services.resourceloading.OnDemandLoadingModelSetServiceFactory;
import org.eclipse.papyrus.infra.services.resourceloading.tests.StrategyChooserFixture;
import org.eclipse.papyrus.infra.services.resourceloading.tests.testModel2.ITestConstants;
import org.eclipse.papyrus.junit.framework.classification.tests.AbstractPapyrusTest;
import org.eclipse.uml2.uml.Property;
import org.eclipse.uml2.uml.Type;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;


/**
 * Abstract class to test all the strategies with TestModel2
 * - Check that a model resource is loaded when a model contains reference to it.
 * - Check that a notation resource is loaded when a diagram contains reference to it.
 *
 * @author eperico
 *
 */
public abstract class AbstractResourceLoadingTestModel2 extends AbstractPapyrusTest {

	public static final String INITIAL_PATH = "resources/TestModel2/";

	public static final String RESOURCE_URI = ITestConstants.FRAGMENT_ID + "/" + INITIAL_PATH;

	private String[] resources = new String[]{ "model1", "Package0" };

	private String[] extensions = new String[]{ ".di", ".notation", ".uml" };

	@Rule
	public final HouseKeeper houseKeeper = new HouseKeeper();

	protected ModelSet modelSet;

	private IFile resourceLoaded;

	/**
	 * {@inheritDoc}
	 */
	@Before
	public void setUp() throws Exception {
		houseKeeper.cleanUpLater(new StrategyChooserFixture(getStrategy()));

		// first we need to create the test project from the plugin to the workspace test platform
		IProject project = copyTestModelToThePlatform();
		modelSet = houseKeeper.cleanUpLater((ModelSet)new OnDemandLoadingModelSetServiceFactory().createServiceInstance());
		if(project != null) {
			resourceLoaded = getResourceToLoad(project);
			modelSet.loadModels(resourceLoaded);
		}
	}

	/**
	 * Copy test model from the test plugin to the platform where the tests are executed
	 *
	 * @return the project in the runtime platform
	 * @throws Exception
	 */
	private IProject copyTestModelToThePlatform() throws Exception {
		IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(ITestConstants.FRAGMENT_ID);
		IProgressMonitor monitor = new NullProgressMonitor();

		if(project != null && !project.exists()) {
			project.create(monitor);
		}
		project.open(monitor);
		for(String res : resources) {
			for(String s : extensions) {
				IFile file = project.getFile(INITIAL_PATH + res + s);
				// link all the models resources
				if(!file.exists()) {
					createFolder(project, "resources/");
					createFolder(project, INITIAL_PATH);
					URL url = FileLocator.find(Platform.getBundle(ITestConstants.FRAGMENT_ID), new Path(INITIAL_PATH + res + s), null);
					URL newFile = FileLocator.resolve(url);

					// encode the URI for spaces in the path
					file.createLink(new URL(newFile.toString().replaceAll(" ", "%20")).toURI(), IResource.REPLACE, monitor);
				}
			}
		}
		project.refreshLocal(IResource.DEPTH_INFINITE, monitor);
		return project;
	}

	/**
	 * Gets the current resource loading strategy.
	 *
	 * @return the strategy
	 */
	public abstract int getStrategy();

	/**
	 * Gets the resource to load, the one it is opened with the papyrus editor
	 *
	 * @param project
	 *        the project in which the resources should be
	 *
	 * @return the resource to load
	 */
	public abstract IFile getResourceToLoad(IProject project);

	/**
	 * Gets an object (Class0) from a reference (type of property) to the high level resource (model1)
	 */
	@Test
	public void testGetReferenceInControlledRessource() {
		URI uriProperty0 = URI.createPlatformResourceURI(RESOURCE_URI + "Package0.uml", false).appendFragment("_57LlkIRSEd-ZSb15jhF0Qw");
		EObject property0 = modelSet.getEObject(uriProperty0, true);
		Type type = null;
		if(property0 instanceof Property) {
			type = ((Property)property0).getType();
			assertTestGetDanglingReferenceFromParentResource("Get type from controlled resource is resolved", type);
		}

		URI uriClass0 = URI.createPlatformResourceURI(RESOURCE_URI + "model1.uml", false).appendFragment("_1766sIRSEd-ZSb15jhF0Qw");
		EObject class0 = modelSet.getEObject(uriClass0, true);
		assertTestGetReferenceInControlledRessource("Type of property is resolved ? :", type, class0);
	}

	private void assertTestGetDanglingReferenceFromParentResource(String message, EObject eObject) {
		switch(getStrategy()) {
		case 0:
			// Load all the needed resources
			assertTrue(message, !eObject.eIsProxy());
			break;
		case 1:
			// Load the additional resources (profile and pathmap). Controlled resources are not loaded
			assertTrue(message, eObject.eIsProxy());
			break;
		case 2:
			// Load the additional resources (profile and pathmap) and the needed controlled resources
			if(resourceLoaded != null && resourceLoaded.getName().contains("model1")) {
				assertTrue(message, !eObject.eIsProxy());
			} else {
				// when Package0 is opened, model1 is not loaded because it is not a controlled resource
				assertTrue(message, eObject.eIsProxy());
			}
			break;
		default:
			break;
		}
	}

	private void assertTestGetReferenceInControlledRessource(String message, EObject eObject1, EObject eObject2) {
		switch(getStrategy()) {
		case 0:
			// Load all the needed resources
			assertSame(message, eObject1, eObject2);
			break;
		case 1:
			// Load the additional resources (profile and pathmap). Controlled resources are not loaded
			assertNotSame(message, eObject1, eObject2);
			break;
		case 2:
			// Load the additional resources (profile and pathmap) and the needed controlled resources
			if(resourceLoaded != null && resourceLoaded.getName().contains("model1")) {
				assertSame(message, eObject1, eObject2);
			} else {
				// when Package0 is opened, model1 is not loaded because it is not a controlled resource
				assertNotSame(message, eObject1, eObject2);
			}
			break;
		default:
			break;
		}
	}

	/**
	 * Gets a figure (figure of Class0) contains in the high level resource (model1) from a diagram in controlled resource (Package0)
	 */
	@Test
	public void testGetFigureInControlledRessource() {
		URI uriFigurePackage0 = URI.createPlatformResourceURI(RESOURCE_URI + "Package0.notation", false).appendFragment("_-ig9EIRSEd-ZSb15jhF0Qw");
		EObject figurePackage0 = modelSet.getEObject(uriFigurePackage0, true);
		assertTestGetFigureInControlledRessource1("Get figure in Package0 resource", figurePackage0);
		EObject element = null;
		if(figurePackage0 instanceof Node) {
			Node node = (Node)figurePackage0;
			element = node.getElement();
		}
		URI uriClass0 = URI.createPlatformResourceURI(RESOURCE_URI + "model1.uml", false).appendFragment("_1766sIRSEd-ZSb15jhF0Qw");
		EObject class0 = modelSet.getEObject(uriClass0, true);
		assertTestGetFigureInControlledRessource2("Load figure from high level resource", class0, element);
	}

	private void assertTestGetFigureInControlledRessource1(String message, EObject eObject) {
		switch(getStrategy()) {
		case 0:
			// Load all the needed resources
			assertTrue(message, !eObject.eIsProxy());
			break;
		case 1:
			// Load the additional resources (profile and pathmap). Controlled resources are not loaded
			if(resourceLoaded != null && resourceLoaded.getName().contains("model1")) {
				//assertTrue(message, eObject.eIsProxy());
				// eObject is null, not a proxy
				assertNull(message, eObject);
			} else {
				// when Package0 is opened, model1 is not loaded because it is not a controlled resource
				assertTrue(message, !eObject.eIsProxy());
			}
			break;
		case 2:
			// Load the additional resources (profile and pathmap) and the needed controlled resources
			assertTrue(message, !eObject.eIsProxy());
			break;
		default:
			break;
		}
	}

	private void assertTestGetFigureInControlledRessource2(String message, EObject eObject1, EObject eObject2) {
		switch(getStrategy()) {
		case 0:
			// Load all the needed resources
			assertSame(message, eObject1, eObject2);
			break;
		case 1:
			// Load the additional resources (profile and pathmap). Controlled resources are not loaded
			assertNotSame(message, eObject1, eObject2);
			break;
		case 2:
			// Load the additional resources (profile and pathmap) and the needed controlled resources
			if(resourceLoaded != null && resourceLoaded.getName().contains("model1")) {
				assertSame(message, eObject1, eObject2);
			} else {
				// when Package0 is opened, model1 is not loaded because it is not a controlled resource
				assertNotSame(message, eObject1, eObject2);
			}
			break;
		default:
			break;
		}
	}

	/**
	 * {@inheritDoc}
	 */
	@After
	public void tearDown() throws Exception {
		// Unload models
		List<Resource> resources = new ArrayList<Resource>(modelSet.getResources());
		for(Resource r : resources) {
			try {
				r.unload();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * Creates the folder name in the specified project
	 *
	 * @param project
	 * @param name
	 * @throws CoreException
	 */
	private void createFolder(IProject project, String name) throws CoreException {
		IFolder parent = project.getFolder(name);
		if(!parent.exists()) {
			parent.create(true, true, new NullProgressMonitor());
		}
		assert (parent.exists());
	}

}

Back to the top