Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: dd9d9861e10bec868d375ce2f016c17a81a63988 (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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
/*****************************************************************************
 * Copyright (c) 2011 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:
 *  Remi Schnekenburger (CEA LIST) remi.schnekenburger@cea.fr - Initial API and implementation
 * 
 * 
 *
 *****************************************************************************/
package org.eclipse.papyrus.sysml.modelexplorer.tests.common;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import org.junit.Assert;

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.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.emf.transaction.RunnableWithResult;
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.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.papyrus.emf.facet.custom.metamodel.v0_2_0.custom.Customization;
import org.eclipse.papyrus.emf.facet.custom.metamodel.v0_2_0.internal.treeproxy.EObjectTreeElement;
import org.eclipse.papyrus.infra.core.resource.ModelSet;
import org.eclipse.papyrus.infra.core.resource.additional.AdditionalResourcesModel;
import org.eclipse.papyrus.sysml.modelexplorer.Activator;
import org.eclipse.papyrus.sysml.modelexplorer.tests.utils.EditorUtils;
import org.eclipse.papyrus.views.modelexplorer.ModelExplorerPageBookView;
import org.eclipse.papyrus.views.modelexplorer.ModelExplorerView;
import org.eclipse.papyrus.views.modelexplorer.NavigatorUtils;
import org.eclipse.papyrus.views.modelexplorer.matching.IMatchingItem;
import org.eclipse.papyrus.views.modelexplorer.matching.LinkItemMatchingItem;
import org.eclipse.papyrus.views.modelexplorer.matching.ModelElementItemMatchingItem;
import org.eclipse.papyrus.views.modelexplorer.matching.ReferencableMatchingItem;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.navigator.CommonViewer;
import org.eclipse.ui.part.FileEditorInput;
import org.eclipse.uml2.uml.Model;
import org.eclipse.uml2.uml.NamedElement;
import org.junit.AfterClass;
import org.junit.BeforeClass;



/**
 * Abstract class for Copy/paste
 */
public abstract class AbstractModelExplorerTest {

	/** boolean to indicate if the test is initialized or not */
	private static boolean isInitialized;

	/** main papyrus editor */
	public static IEditorPart editor = null;

	/** id of the papyrus editor */
	public static String editorID = "org.eclipse.papyrus.infra.core.papyrusEditor"; 

	/** view part: the model explorer */
	protected static IViewPart modelExplorerPart;

	/** generated selectable objects */
	protected static org.eclipse.uml2.uml.Model model_EObject;

	protected static org.eclipse.uml2.uml.Class b1_EObject;

	protected static org.eclipse.uml2.uml.Property pB1P1_B1_EObject;

	protected static org.eclipse.uml2.uml.Property rB2_B1_EObject;

	protected static org.eclipse.uml2.uml.Property pB2_B1_EObject;

	protected static org.eclipse.uml2.uml.Class b2_EObject;

	protected static org.eclipse.uml2.uml.Package p1_EObject;

	protected static org.eclipse.uml2.uml.Class b2P1_P1_EObject;

	protected static org.eclipse.uml2.uml.Class b1P1_P1_EObject;

	protected static org.eclipse.uml2.uml.Property pB1_B1P1_P1_EObject;

	protected static org.eclipse.uml2.uml.Property pB2P1_B1P1_P1_EObject;

	protected static Diagram iBD_B1_Diagram;

	protected static Diagram bDD_Main_Diagram;

	/** end of generated selectable objects */

	/**
	 * Initialization of the test
	 * 
	 * @throws Exception
	 *         thrown when initialization has problem
	 */
	@BeforeClass
	public static void openPapyrusWithAnEmptyProject() throws Exception {
		// Prepare new project for tests
		IProject testProject = ResourcesPlugin.getWorkspace().getRoot().getProject("TestCopyPasteProject");
		if(!testProject.exists()) {
			testProject.create(new NullProgressMonitor());
		}

		if(!testProject.isOpen()) {
			testProject.open(new NullProgressMonitor());
		}

		// Copy EmptyModel from bundle to the test project
		final IFile emptyModel_di = testProject.getFile("ModelWithBDD.di");
		IFile emptyModel_no = testProject.getFile("ModelWithBDD.notation");
		IFile emptyModel_uml = testProject.getFile("ModelWithBDD.uml");

		// isInitialized = isInitialized || emptyModel_di.exists();

		if(!isInitialized) {
			isInitialized = true;
			emptyModel_di.create(Activator.getDefault().getBundle().getResource("/model/ModelWithBDD.di").openStream(), true, new NullProgressMonitor());
			emptyModel_no.create(Activator.getDefault().getBundle().getResource("/model/ModelWithBDD.notation").openStream(), true, new NullProgressMonitor());
			emptyModel_uml.create(Activator.getDefault().getBundle().getResource("/model/ModelWithBDD.uml").openStream(), true, new NullProgressMonitor());
		}

		// Open the EmptyModel.di file with Papyrus (assumed to be the default editor for "di" files here).
		Display.getDefault().syncExec(new Runnable() {

			public void run() {
				try {
					IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
					editor = page.openEditor(new FileEditorInput(emptyModel_di), editorID);

					modelExplorerPart = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().showView(ModelExplorerPageBookView.VIEW_ID);
					modelExplorerPart.setFocus();
				} catch (Exception ex) {
					ex.printStackTrace(System.out);
				}
			}
		});

		Assert.assertNotNull("Model explorer is null", modelExplorerPart);

		prepareTest();
	}

	/**
	 * Prepare the diagram before testing
	 * 
	 * @throws Exception
	 *         exception thrown in case of problem
	 */
	public static void prepareTest() throws Exception {
		DiagramEditPart diagramEditPart = EditorUtils.getDiagramEditPart();
		EObject root = diagramEditPart.getDiagramView().getElement();

		Assert.assertTrue("Impossible to find main model", root instanceof Model); //$NON-NLS-1$

		/** generated selectable objects */
		model_EObject = (org.eclipse.uml2.uml.Model)root;
		b1_EObject = (org.eclipse.uml2.uml.Class)model_EObject.getPackagedElement("B1");
		Assert.assertNotNull("Impossible to find Class B1", b1_EObject); //$NON-NLS-1$
		pB1P1_B1_EObject = b1_EObject.getAttribute("pB1P1", null);
		Assert.assertNotNull("Impossible to find Property pB1P1", pB1P1_B1_EObject); //$NON-NLS-1$
		rB2_B1_EObject = b1_EObject.getAttribute("rB2", null);
		Assert.assertNotNull("Impossible to find Property rB2", rB2_B1_EObject); //$NON-NLS-1$
		pB2_B1_EObject = b1_EObject.getAttribute("pB2", null);
		Assert.assertNotNull("Impossible to find Property pB2", pB2_B1_EObject); //$NON-NLS-1$

		b2_EObject = (org.eclipse.uml2.uml.Class)model_EObject.getPackagedElement("B2");
		Assert.assertNotNull("Impossible to find Class B2", b2_EObject); //$NON-NLS-1$

		p1_EObject = (org.eclipse.uml2.uml.Package)model_EObject.getPackagedElement("P1");
		Assert.assertNotNull("Impossible to find Package P1", p1_EObject); //$NON-NLS-1$
		b2P1_P1_EObject = (org.eclipse.uml2.uml.Class)p1_EObject.getPackagedElement("B2P1");
		Assert.assertNotNull("Impossible to find Class B2P1", b2P1_P1_EObject); //$NON-NLS-1$

		b1P1_P1_EObject = (org.eclipse.uml2.uml.Class)p1_EObject.getPackagedElement("B1P1");
		Assert.assertNotNull("Impossible to find Class B1P1", b1P1_P1_EObject); //$NON-NLS-1$
		pB1_B1P1_P1_EObject = b1P1_P1_EObject.getAttribute("pB1", null);
		Assert.assertNotNull("Impossible to find Property pB1", pB1_B1P1_P1_EObject); //$NON-NLS-1$
		pB2P1_B1P1_P1_EObject = b1P1_P1_EObject.getAttribute("pB2P1", null);
		Assert.assertNotNull("Impossible to find Property pB2P1", pB2P1_B1P1_P1_EObject); //$NON-NLS-1$





		iBD_B1_Diagram = getDiagram("IBD_B1");
		Assert.assertNotNull("Impossible to find IBD_B1", iBD_B1_Diagram); //$NON-NLS-1$
		bDD_Main_Diagram = getDiagram("BDD_Main");
		Assert.assertNotNull("Impossible to find BDD_Main", bDD_Main_Diagram); //$NON-NLS-1$
		List<Customization> appliedCustomizations=org.eclipse.papyrus.views.modelexplorer.Activator.getDefault().getCustomizationManager().getManagedCustomizations();
		Customization SimpleUML=null;
		Iterator<?>iter=appliedCustomizations.iterator();
		while(iter.hasNext()) {
			Customization custo = (Customization)iter.next();
			if( custo.getName().equals("SimpleUML")){
				SimpleUML=custo;
			}
		}
		Assert.assertNotNull("Custom SimpleUML not found", SimpleUML); //$NON-NLS-1$
		org.eclipse.papyrus.views.modelexplorer.Activator.getDefault().getCustomizationManager().getManagedCustomizations().add(0, SimpleUML);



		Assert.assertEquals("bad order of applied Custom", "SimpleUML", appliedCustomizations.get(0).getName()); //$NON-NLS-1$

	/** end of generated selectable objects */
	}

	/**
	 * Close editor
	 * 
	 * @throws Exception
	 *         exception thrown in case of problem
	 */
	@AfterClass
	public static void closePapyrusAndCleanProject() throws Exception {
		// Close the editor without saving content created during tests
		Display.getDefault().syncExec(new Runnable() {

			public void run() {
				IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
				page.closeEditor(editor, false);
			}
		});

		editor = null;
		modelExplorerPart = null;
	}


	/**
	 * Selects and reveal the specified element
	 * 
	 * @param object
	 *        the object to select
	 * @throws Exception
	 *         exception thrown in case element could not be selected
	 */
	public static void selectAndReveal(final EObject object) throws Exception {
		Display.getDefault().syncExec(new Runnable() {

			public void run() {
				try {
					selectAndReveal(Arrays.asList(object));
				} catch (Exception ex) {
					ex.printStackTrace(System.out);
				}
			}
		});
	}

	/**
	 * Selects and reveal the specified list of elements
	 * 
	 * @param newElements
	 *        the list of objects to select
	 * @throws Exception
	 *         exception thrown in case element could not be selected
	 */
	public static void selectAndReveal(List<EObject> newElements) throws Exception {
		// Retrieve model explorer
		ModelExplorerView modelExplorerView = null;

		ModelExplorerPageBookView bookViewPart = (ModelExplorerPageBookView)NavigatorUtils.findViewPart(ModelExplorerPageBookView.VIEW_ID);
		if(bookViewPart != null) {
			modelExplorerView = (ModelExplorerView)bookViewPart.getActiveView();
		}

		// Set selection on new element in the model explorer
		if((modelExplorerView != null) && (newElements != null)) {
			List<EObject> semanticElementList = new ArrayList<EObject>();
			semanticElementList.addAll(newElements);
			modelExplorerView.revealSemanticElement(semanticElementList);
		} else {
			throw new Exception("Impossible to find the model explorer required to select: " + newElements); //$NON-NLS-1$
		}
	}

	/**
	 * Returns the current editing domain
	 * 
	 * @return
	 *         the current editing domain
	 */
	protected TransactionalEditingDomain getEditingDomain() throws Exception {
		return (TransactionalEditingDomain)editor.getAdapter(TransactionalEditingDomain.class);
	}

	/**
	 * Returns <code>true</code> if the current Active editor is dirty.
	 * 
	 * @return <code>true</code> if the current Active editor is dirty
	 * @throws Exception
	 *         exception thrown in case of problem (NPE, etc.)
	 */
	protected boolean isEditorDirty() throws Exception {
		RunnableWithResult<Boolean> runnable;
		Display.getDefault().syncExec(runnable = new RunnableWithResult.Impl<Boolean>() {

			public void run() {
				setResult(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor().isDirty());
			}
		});

		return runnable.getResult();
	}

	/**
	 * Selects and reveal the specified element
	 * 
	 * @param object
	 *        the object to select
	 * @throws Exception
	 *         exception thrown in case element could not be selected
	 */
	public static void selectAndRevealDiagram(final Diagram object) throws Exception {
		Display.getDefault().syncExec(new Runnable() {

			public void run() {
				try {
					selectAndRevealDiagram(Arrays.asList(object));
				} catch (Exception ex) {
					ex.printStackTrace(System.out);
				}
			}
		});
	}

	/**
	 * Selects and reveal the specified list of diagrams
	 * 
	 * @param newElements
	 *        the list of diagrams to select
	 * @throws Exception
	 *         exception thrown in case diagram could not be selected
	 */
	public static void selectAndRevealDiagram(List<Diagram> newDiagrams) throws Exception {
		// Retrieve model explorer
		ModelExplorerView modelExplorerView = null;
		ModelExplorerPageBookView bookViewPart = (ModelExplorerPageBookView)NavigatorUtils.findViewPart(ModelExplorerPageBookView.VIEW_ID);
		if(bookViewPart != null) {
			modelExplorerView = (ModelExplorerView)bookViewPart.getActiveView();
		}
		modelExplorerView.getCommonViewer().expandAll();
		// Set selection on new element in the model explorer
		if((modelExplorerView != null) && (newDiagrams != null)) {
			List<Diagram> semanticElementList = new ArrayList<Diagram>();
			semanticElementList.addAll(newDiagrams);
			// reveal 'container' of the diagram

			modelExplorerView.revealSemanticElement(semanticElementList);
			//reveal(semanticElementList, modelExplorerView.getCommonViewer());
		} else {
			throw new Exception("Impossible to find the model explorer required to select: " + newDiagrams); //$NON-NLS-1$
		}
	}

	/**
	 * Expands the given CommonViewer to reveal the given elements
	 * 
	 * @param elementList
	 *        The elements to reveal
	 * @param commonViewer
	 *        The CommonViewer they are to be revealed in
	 */
	public static void reveal(final Iterable<Diagram> elementList, final CommonViewer commonViewer) {
		ArrayList<IMatchingItem> matchingItemsToSelect = new ArrayList<IMatchingItem>();
		// filter out non EMF objects
		for(Diagram currentEObject : elementList) {
			matchingItemsToSelect.add(new ModelElementItemMatchingItem(currentEObject));
			// the content provider exist?
			if(commonViewer.getContentProvider() != null) {
				// retrieve the ancestors to reveal them
				// and allow the selection of the object
				ArrayList<EObject> parents = new ArrayList<EObject>();
				// retrieve context
				EObject tmp = currentEObject.getElement();
				while(tmp != null) {
					parents.add(tmp);
					tmp = tmp.eContainer();
				}
				List<EObject> reverseParents = new ArrayList<EObject>(parents);
				Collections.reverse(reverseParents);
				// reveal the resource if necessary
				Resource r = null;
				if(!parents.isEmpty()) {
					r = parents.get(parents.size() - 1).eResource();
				} else {
					r = currentEObject.eResource();
				}
				if(r != null) {
					ResourceSet rs = r.getResourceSet();
					if(rs instanceof ModelSet && AdditionalResourcesModel.isAdditionalResource((ModelSet)rs, r.getURI())) {
						commonViewer.expandToLevel(new ReferencableMatchingItem(rs), 1);
						commonViewer.expandToLevel(new ReferencableMatchingItem(r), 1);
					}
				}
				/*
				 * reveal the ancestors tree using expandToLevel on each of them
				 * in the good order. This is a lot faster than going through the whole tree
				 * using getChildren of the ContentProvider since our Viewer uses a Hashtable
				 * to keep track of the revealed elements.
				 * 
				 * However we need to use a dedicated MatchingItem to do the matching,
				 * and a specific comparer in our viewer so than the equals of MatchingItem is
				 * used in priority.
				 * 
				 * Please refer to MatchingItem for more infos.
				 */
				EObject previousParent = null;
				for(EObject parent : reverseParents) {
					if(parent.eContainingFeature() != null && previousParent != null) {
						commonViewer.expandToLevel(new LinkItemMatchingItem(previousParent, parent.eContainmentFeature()), 1);
					}
					commonViewer.expandToLevel(new ModelElementItemMatchingItem(parent), 1);
					previousParent = parent;
				}
				commonViewer.expandToLevel(new LinkItemMatchingItem(currentEObject.eContainer(), currentEObject.eContainmentFeature()), 1);
			}
		}
		commonViewer.setSelection(new StructuredSelection(matchingItemsToSelect), true);
	}

	/**
	 * Returns the diagram with the given name
	 * 
	 * @param name
	 *        the name of the diagram to find
	 * @return the diagram with the given name.
	 * @exception Exception
	 *            exception thrown in case of issue, e.g. diagram was not found
	 */
	protected static Diagram getDiagram(String name) throws Exception {
		Iterator<EObject> it = EditorUtils.getDiagramEditPart().getDiagramView().eResource().getContents().iterator();
		while(it.hasNext()) {
			EObject next = it.next();
			if(next instanceof Diagram) {
				if(name.equals(((Diagram)next).getName())) {
					return (Diagram)next;
				}
			}
		}
		return null;
	}

	/**
	 * Retrieves the Model Element Item for the given EObject
	 * 
	 * @param objectToFind
	 *        object represented by the searched item
	 * @return the {@link ModelElementItem} that corresponds to the diagram
	 * @throws Exception
	 *         exception thrown in case of issue
	 */
	protected EObjectTreeElement findSemanticModelElementItem(EObject objectToFind) throws Exception {
		selectAndReveal(objectToFind);
		IStructuredSelection selection = (IStructuredSelection)modelExplorerPart.getSite().getSelectionProvider().getSelection();
		Assert.assertEquals("one and only one object should be selected", 1, selection.size()); //$NON-NLS-1$
		Object selectedElement = selection.getFirstElement();
		Assert.assertTrue("selection should be a model item element", selectedElement instanceof EObjectTreeElement); //$NON-NLS-1$
		Assert.assertTrue("selection should be linked to a EObject", ((EObjectTreeElement)selectedElement).getEObject() instanceof EObject); //$NON-NLS-1$
		Assert.assertTrue("selection should be linked to the Object: " + objectToFind, ((EObjectTreeElement)selectedElement).getEObject().equals(objectToFind)); //$NON-NLS-1$
		return (EObjectTreeElement)selectedElement;
	}

	/**
	 * Retrieves the Model Element Item for the given Diagram
	 * 
	 * @param diagramToFind
	 *        diagram represented by the searched item
	 * @return the {@link ModelElementItem} that corresponds to the diagram
	 * @throws Exception
	 *         exception thrown in case of issue
	 */
	protected EObjectTreeElement findSemanticModelElementItem(Diagram diagramToFind) throws Exception {

		selectAndRevealDiagram(diagramToFind);
		IStructuredSelection selection = (IStructuredSelection)modelExplorerPart.getSite().getSelectionProvider().getSelection();
		Assert.assertEquals("one and only one diagram should be selected", 1, selection.size()); //$NON-NLS-1$
		Object selectedElement = selection.getFirstElement();
		Assert.assertTrue("selection should be a model item element", selectedElement instanceof EObjectTreeElement); //$NON-NLS-1$
		Assert.assertTrue("selection should be linked to a Diagram", ((EObjectTreeElement)selectedElement).getEObject() instanceof Diagram); //$NON-NLS-1$
		Assert.assertTrue("selection should be linked to the Object: " + diagramToFind, ((EObjectTreeElement)selectedElement).getEObject().equals(diagramToFind)); //$NON-NLS-1$
		return (EObjectTreeElement)selectedElement;
	}

	protected String printElement(EObject object) {
		if(object instanceof Diagram) {
			return printElement((Diagram)object);
		} else if(object instanceof NamedElement) {
			return printElement((NamedElement)object);
		}
		return EcoreUtil.getID(object);
	}

	protected String printElement(NamedElement element) {
		return element.getName() + " <" + element.eClass().getName() + ">";
	}

	protected String printElement(Diagram diagram) {
		return diagram.getName() + " <" + diagram.getType() + ">";
	}

}

Back to the top