Skip to main content
summaryrefslogtreecommitdiffstats
blob: 121de8e2a30111e3035ce9a973e883ab0c4889b3 (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
/*****************************************************************************
 * Copyright (c) 2014 CEA LIST.
 *
 * 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:
 * 	Thibault Le Ouay (Sherpa Engineering) t.leouay@sherpa-eng.com - Initial API and implementation
 * 	Camille Letavernier (CEA LIST) camille.letavernier@cea.fr
 *****************************************************************************/
package org.eclipse.papyrus.migration.rsa.tests.qvt;

import java.net.URL;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.emf.common.command.Command;
import org.eclipse.emf.common.util.EList;
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.util.EcoreUtil;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.gef.EditPart;
import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart;
import org.eclipse.gmf.runtime.diagram.ui.parts.IDiagramWorkbenchPart;
import org.eclipse.gmf.runtime.notation.Diagram;
import org.eclipse.papyrus.commands.OpenDiagramCommand;
import org.eclipse.papyrus.commands.wrappers.GMFtoEMFCommandWrapper;
import org.eclipse.papyrus.infra.core.editor.IMultiDiagramEditor;
import org.eclipse.papyrus.infra.core.resource.ModelMultiException;
import org.eclipse.papyrus.infra.core.resource.ModelSet;
import org.eclipse.papyrus.infra.core.resource.ModelsReader;
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.model.NotationUtils;
import org.eclipse.papyrus.junit.framework.classification.tests.AbstractPapyrusTest;
import org.eclipse.papyrus.junit.utils.EditorUtils;
import org.eclipse.papyrus.junit.utils.FilesUtils;
import org.eclipse.papyrus.junit.utils.ProjectUtils;
import org.eclipse.papyrus.migration.rsa.tests.Activator;
import org.eclipse.papyrus.migration.rsa.transformation.ImportTransformation;
import org.eclipse.papyrus.uml.diagram.common.util.DiagramEditPartsUtil;
import org.eclipse.papyrus.uml.tools.commands.ApplyProfileCommand;
import org.eclipse.papyrus.uml.tools.model.UmlUtils;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.uml2.uml.Profile;
import org.eclipse.uml2.uml.UMLPackage;
import org.eclipse.uml2.uml.resource.UMLResource;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;

//resources/*.e*x
public class ImportDiagramTest extends AbstractPapyrusTest {

	private static IProject targetProject;

	protected static IMultiDiagramEditor editor;

	protected static URI fileDiagram, fileProfile;


	@AfterClass
	public static void dispose() throws Exception {

		IWorkbench wb = PlatformUI.getWorkbench();
		IWorkbenchPage page = wb.getActiveWorkbenchWindow().getActivePage();
		page.closeAllEditors(false);
		targetProject.delete(true, new NullProgressMonitor());
	}


	@BeforeClass
	public static void init() throws Exception {
		IWorkbench workbench = PlatformUI.getWorkbench();
		IWorkbenchWindow window = workbench.getActiveWorkbenchWindow();
		while (window == null) {
			Display.getCurrent().readAndDispatch();
			window = workbench.getActiveWorkbenchWindow();
		}

		targetProject = ProjectUtils.createProject(Activator.PLUGIN_ID + ".testProject");
		URI rsaDiagramModelUri = URI.createPlatformPluginURI(Activator.PLUGIN_ID + "/resources/ModelTestClass.emx", true);
		URI rsaProfileModelUri = URI.createPlatformPluginURI(Activator.PLUGIN_ID + "/resources/Profile.epx", true);

		FilesUtils.copyFiles(targetProject, "ModelTestClass.emx", new URL(rsaDiagramModelUri.toString()));
		FilesUtils.copyFiles(targetProject, "Profile.epx", new URL(rsaProfileModelUri.toString()));

		fileDiagram = URI.createPlatformResourceURI(Activator.PLUGIN_ID + ".testProject" + "/ModelTestClass.emx", true);
		ImportTransformation transfoDiagram = new ImportTransformation(fileDiagram);

		fileProfile = URI.createPlatformResourceURI(Activator.PLUGIN_ID + ".testProject" + "/Profile.epx", true);
		ImportTransformation transfoProfile = new ImportTransformation(fileProfile);

		transfoDiagram.run(false);
		transfoProfile.run(false);

		transfoDiagram.waitForCompletion();
		transfoProfile.waitForCompletion();
	}



	// Test should be more generic

	@Test
	public void openModelTest() throws Exception {
		ModelSet modelSet = new ModelSet();
		ModelsReader reader = new ModelsReader();
		reader.readModel(modelSet);
		try {
			modelSet.loadModels(fileDiagram.trimFileExtension().appendFileExtension("di"));
		} catch (ModelMultiException e) {
			e.printStackTrace();
		}
		final ServicesRegistry registry = new ExtensionServicesRegistry(org.eclipse.papyrus.infra.core.Activator.PLUGIN_ID);
		try {
			registry.add(ModelSet.class, Integer.MAX_VALUE, modelSet);
			registry.startRegistry();
		} catch (ServiceException ex) {
			// Ignored: we don't need all services
		}



		TransactionalEditingDomain editingDomain = modelSet.getTransactionalEditingDomain();
		Resource umlResource = UmlUtils.getUmlResource(modelSet);

		// Profile should be applied during the transformation

		if (umlResource instanceof UMLResource) {
			EList<EObject> umlresourceContent = umlResource.getContents();
			Iterator<EObject> umlite = umlresourceContent.iterator();
			while (umlite.hasNext()) {
				Object currentobject = umlite.next();
				if (currentobject instanceof org.eclipse.uml2.uml.Package) {
					URI uri = URI.createURI(UMLResource.STANDARD_PROFILE_URI, true);
					Resource resource = modelSet.getResource(uri, true);
					Profile profile = (Profile) EcoreUtil.getObjectByType(resource.getContents(), UMLPackage.Literals.PROFILE);
					Command command = new ApplyProfileCommand((org.eclipse.uml2.uml.Package) currentobject, profile, editingDomain);
					try {
						editingDomain.getCommandStack().execute(command);
					} catch (Exception ex) {
						ex.printStackTrace();
					}

				}
			}
		}

		final AtomicReference<Exception> exception = new AtomicReference<Exception>();
		Display.getDefault().syncExec(new Runnable() {

			public void run() {
				try {
					IFile fileToOpen = (IFile) ResourcesPlugin.getWorkspace().getRoot().findMember(fileDiagram.trimFileExtension().appendFileExtension("di").toPlatformString(true));
					editor = EditorUtils.openPapyrusEditor(fileToOpen);
				} catch (Exception ex) {
					exception.set(ex);
				}
			}
		});

		// Throw exception to get a proper JUnit report
		if (exception.get() != null) {
			throw exception.get();
		}

		Assert.assertNotNull("Cannot open the Papyrus editor", editor);

		Resource notationResource = NotationUtils.getNotationModel(editor.getServicesRegistry().getService(ModelSet.class)).getResource();
		List<EObject> listcontent = notationResource.getContents();

		// Expected is 7 valid migrated diagrams.
		// FIXME: Sequence Diagram import is currently disabled, so 6 is the new expected number
		Assert.assertEquals(6, listcontent.size()); // Expected is 7 valid migrated diagrams

		for (EObject cont : listcontent) {
			Assert.assertTrue(cont instanceof Diagram);

			Diagram currentDia = (Diagram) cont;
			editingDomain.getCommandStack().execute(new GMFtoEMFCommandWrapper(new OpenDiagramCommand(editingDomain, currentDia)));
			IEditorPart currentEditor = editor.getActiveEditor();
			Assert.assertNotNull("could not open the  diagram editor", currentEditor);
			Assert.assertTrue("The active editor should be a GMF Diagram Editor. Received " + editor.getActiveEditor().getClass().getCanonicalName(), editor.getActiveEditor() instanceof IDiagramWorkbenchPart);
			Object temp = currentEditor.getAdapter(EditPart.class);
			Assert.assertNotNull(temp);
			EditPart editPart = (EditPart) temp;
			List<IGraphicalEditPart> listEditPart = DiagramEditPartsUtil.getAllEditParts(editPart);
			for (EditPart currentEditPart : listEditPart) {
				Assert.assertNotNull("should not be invalid view", currentEditPart);
			}
		}

		try {
			registry.disposeRegistry();
		} catch (ServiceException ex) {
			// Ignore
		}
	}

	@Test
	public void openProfileTest() throws Exception {
		ModelSet modelSet = new ModelSet();
		ModelsReader reader = new ModelsReader();
		reader.readModel(modelSet);
		try {
			modelSet.loadModels(fileProfile.trimFileExtension().appendFileExtension("profile.di"));
		} catch (ModelMultiException e) {
			e.printStackTrace();
		}
		final ServicesRegistry registry = new ExtensionServicesRegistry(org.eclipse.papyrus.infra.core.Activator.PLUGIN_ID);
		try {
			registry.add(ModelSet.class, Integer.MAX_VALUE, modelSet);
			registry.startRegistry();
		} catch (ServiceException ex) {
			// Ignored: we don't need all services
		}

		TransactionalEditingDomain editingDomain = modelSet.getTransactionalEditingDomain();
		Resource umlResource = UmlUtils.getUmlResource(modelSet);

		// Profile should be applied during the transformation

		if (umlResource instanceof UMLResource) {
			EList<EObject> umlresourceContent = umlResource.getContents();
			Iterator<EObject> umlite = umlresourceContent.iterator();
			while (umlite.hasNext()) {
				Object currentobject = umlite.next();
				if (currentobject instanceof org.eclipse.uml2.uml.Package) {
					URI uri = URI.createURI(UMLResource.STANDARD_PROFILE_URI, true);
					Resource resource = modelSet.getResource(uri, true);
					Profile profile = (Profile) EcoreUtil.getObjectByType(resource.getContents(), UMLPackage.Literals.PROFILE);
					Command command = new ApplyProfileCommand((org.eclipse.uml2.uml.Package) currentobject, profile, editingDomain);
					try {
						editingDomain.getCommandStack().execute(command);
					} catch (Exception ex) {
						ex.printStackTrace();
					}

				}
			}
		}

		Display.getDefault().syncExec(new Runnable() {

			public void run() {
				try {
					IFile fileToOpen = (IFile) ResourcesPlugin.getWorkspace().getRoot().findMember(fileProfile.trimFileExtension().appendFileExtension("profile.di").toPlatformString(true));
					editor = EditorUtils.openPapyrusEditor(fileToOpen);
				} catch (PartInitException ex) {
					Activator.log.error(ex);
					Assert.fail(ex.getMessage());
				}
			}
		});
		Assert.assertNotNull("Cannot open the Papyrus editor", editor);

		Resource notationResource = NotationUtils.getNotationModel(editor.getServicesRegistry().getService(ModelSet.class)).getResource();
		List<EObject> listcontent = notationResource.getContents();
		Assert.assertEquals(1, listcontent.size());
		for (EObject cont : listcontent) {
			Assert.assertTrue(cont instanceof Diagram);

			Diagram currentDia = (Diagram) cont;
			editingDomain.getCommandStack().execute(new GMFtoEMFCommandWrapper(new OpenDiagramCommand(editingDomain, currentDia)));
			IEditorPart currentEditor = editor.getActiveEditor();
			Assert.assertNotNull("could not open the  diagram editor", currentEditor);
			Assert.assertTrue("The active editor should be a GMF Diagram Editor", editor.getActiveEditor() instanceof IDiagramWorkbenchPart);
			Object temp = currentEditor.getAdapter(EditPart.class);
			Assert.assertNotNull(temp);
			EditPart editPart = (EditPart) temp;
			List<IGraphicalEditPart> listEditPart = DiagramEditPartsUtil.getAllEditParts(editPart);
			for (EditPart currentEditPart : listEditPart) {
				Assert.assertNotNull("should not be invalid view", currentEditPart);
			}
		}

		try {
			registry.disposeRegistry();
		} catch (ServiceException ex) {
			// Ignore
		}
	}

}

Back to the top