Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 92fb543dc50eb3ef1bfb3ace25e81f45670b2ea8 (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
/*****************************************************************************
 * Copyright (c) 2013-2016 CEA
 *
 *
 * 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:
 *   Soyatec - Initial API and implementation
 *   Benoit Maggi (benoit.maggi@cea.fr) - #487997 remove dependency to Sys ML
 *****************************************************************************/
package org.eclipse.papyrus.uml.diagram.sequence.tests.bug.pro20130916;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.util.Iterator;
import java.util.List;

import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.text.TextFlow;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.transaction.RecordingCommand;
import org.eclipse.gef.EditPart;
import org.eclipse.gmf.runtime.diagram.core.util.ViewUtil;
import org.eclipse.gmf.runtime.diagram.ui.editparts.ConnectionNodeEditPart;
import org.eclipse.gmf.runtime.diagram.ui.editparts.DiagramEditPart;
import org.eclipse.gmf.runtime.diagram.ui.editparts.GraphicalEditPart;
import org.eclipse.gmf.runtime.draw2d.ui.figures.WrappingLabel;
import org.eclipse.gmf.runtime.notation.Edge;
import org.eclipse.gmf.runtime.notation.Node;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.papyrus.infra.gmfdiag.common.editpart.IPapyrusEditPart;
import org.eclipse.papyrus.uml.diagram.common.Activator;
import org.eclipse.papyrus.uml.diagram.common.figure.node.ILabelFigure;
import org.eclipse.papyrus.uml.diagram.common.figure.node.IPapyrusNodeUMLElementFigure;
import org.eclipse.papyrus.uml.diagram.common.stereotype.display.helper.StereotypeDisplayCommandExecution;
import org.eclipse.papyrus.uml.diagram.common.stereotype.display.helper.StereotypeDisplayUtil;
import org.eclipse.papyrus.uml.diagram.sequence.tests.bug.m7.AbstractNodeTest;
import org.eclipse.papyrus.uml.diagram.sequence.util.SequenceUtil;
import org.eclipse.papyrus.uml.tools.utils.PackageUtil;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.Model;
import org.eclipse.uml2.uml.PackageableElement;
import org.eclipse.uml2.uml.Profile;
import org.eclipse.uml2.uml.Property;
import org.eclipse.uml2.uml.Stereotype;


/**
 * @author Jin Liu (jin.liu@soyatec.com)
 */
public class BaseStereotypesTest extends AbstractNodeTest {

	/**
	 * 
	 */
	private static final String STEREOTYPE1 = "Stereotype1";

	/**
	 * 
	 */
	private static final String TESTPROFILE_STEREOTYPE1 = "testProfile::Stereotype1";

	private static final URI TEST_PROFILE_URI = URI.createURI("platform:/plugin/org.eclipse.papyrus.uml.diagram.stereotypeproperty.tests/resource/testProfile.profile.uml");
	
	
	
	private StereotypeDisplayUtil helper = StereotypeDisplayUtil.getInstance();

	protected void prepareStereotypes(EditPart editPart) {
		assertNotNull("editPart", editPart);
		View view = getDiagramElement(editPart);
		assertNotNull("view", view);
		Element element = getElement(editPart);
		assertNotNull("element", element);
		Stereotype appliedStereotype = applyStereotype(editPart);
		assertNotNull("appliedStereotype", appliedStereotype);
		waitForComplete();
	}

	protected void doTestDisplayStereotypeInCompartment(EditPart editPart, EditPart stereotypeEditPart) {

		// Check Label Figure
		if (stereotypeEditPart == null) {
			stereotypeEditPart = editPart;
		}

		prepareStereotypes(editPart);

		final View view = ((GraphicalEditPart) editPart).getNotationView();
		final View stereotypeLabelView = ((GraphicalEditPart) stereotypeEditPart).getNotationView();

		String labelName = "";
		if (helper.getStereotypeLabel(stereotypeLabelView, TESTPROFILE_STEREOTYPE1) != null) {
			labelName = helper.getStereotypeTextToDisplay(stereotypeLabelView);
		} else if (helper.getStereotypeLabel(view, TESTPROFILE_STEREOTYPE1) != null) {
			labelName = helper.getStereotypeTextToDisplay(view);

		}


		assertTrue("Label Name", labelName.equals(Activator.ST_LEFT + STEREOTYPE1 + Activator.ST_RIGHT) || labelName.equals(Activator.ST_LEFT + TESTPROFILE_STEREOTYPE1 + Activator.ST_RIGHT));


		IFigure stereotypeFigure = null;
		IFigure primaryShape = null;
		if (stereotypeEditPart instanceof IPapyrusEditPart) {
			primaryShape = ((IPapyrusEditPart) stereotypeEditPart).getPrimaryShape();
		} else if (stereotypeEditPart instanceof org.eclipse.gef.GraphicalEditPart) {
			primaryShape = ((org.eclipse.gef.GraphicalEditPart) stereotypeEditPart).getFigure();
		}
		if (primaryShape instanceof IPapyrusNodeUMLElementFigure) {
			stereotypeFigure = ((IPapyrusNodeUMLElementFigure) primaryShape).getStereotypesLabel();
		}
		assertNotNull(stereotypeFigure);
		String actualValue = getFigureText(stereotypeFigure);
		assertEquals("stereotype displayed in Compartment", labelName, actualValue);



		// Check Compartment Figure
		Stereotype stereotype = getElement(editPart).getAppliedStereotype(TESTPROFILE_STEREOTYPE1);
		assertNotNull(stereotype);
		View compartment = helper.getStereotypeCompartment(view, stereotype);

		List children = editPart.getChildren();
		EditPart compartmentEditPart = null;
		for (Object child : children) {
			if (compartment != null) {
				if (compartment.equals(((EditPart) child).getModel())) {
					compartmentEditPart = (EditPart) child;
				}
			}
		}

		if (compartmentEditPart != null) {
			IFigure figure = findLabelFigure(getFigure(compartmentEditPart), labelName);
			assertNotNull(figure);
		}


	}

	protected String getFigureText(IFigure figure) {
		if (figure instanceof Label) {
			return ((Label) figure).getText();
		} else if (figure instanceof WrappingLabel) {
			return ((WrappingLabel) figure).getText();
		} else if (figure instanceof ILabelFigure) {
			return ((ILabelFigure) figure).getText();
		} else if (figure instanceof TextFlow) {
			return ((TextFlow) figure).getText();
		}
		return null;
	}

	protected void doTestDisplayStereotypeWithBrace(EditPart editPart, EditPart stereotypeEditPart) {
		if (stereotypeEditPart == null) {
			stereotypeEditPart = editPart;
		}
		prepareStereotypes(editPart);
		final View diagramElement = getDiagramElement(stereotypeEditPart);
		View brace = null;
		// doCheck
		StereotypeDisplayUtil helper = StereotypeDisplayUtil.getInstance();
		for (Object child : diagramElement.getChildren()) {
			if (helper.isStereotypeBrace(child)) {
				brace = (View) child;
			}
		}


		assertNotNull("Brace Compartment", brace);
	}

	private IFigure findLabelFigure(IFigure figure, String labelText) {
		if (figure == null || labelText == null) {
			return null;
		}
		String value = getFigureText(figure);
		if (labelText.equals(value)) {
			return figure;
		}
		List children = figure.getChildren();
		for (Object object : children) {
			IFigure child = (IFigure) object;
			IFigure fig = findLabelFigure(child, labelText);
			if (fig != null) {
				return fig;
			}
		}
		return null;
	}

	private IFigure getFigure(EditPart editPart) {
		if (editPart instanceof org.eclipse.gef.GraphicalEditPart) {
			return ((org.eclipse.gef.GraphicalEditPart) editPart).getFigure();
		}
		return null;
	}

	protected void doTestDisplayStereotypeComment(EditPart editPart) {
		prepareStereotypes(editPart);
		final View diagramElement = getDiagramElement(editPart);

		// doCheck
		waitForComplete();
		Node commentNode = getAppliedStereotypeCommentNode(diagramElement);
		assertNotNull("commentNode", commentNode);
	}

	protected Node getAppliedStereotypeCommentNode(View semanticView) {
		if (semanticView == null) {
			return null;
		}
		Edge appliedStereotypeLink = null;
		// look for all links with the id AppliedStereotypesCommentLinkEditPart.ID
		@SuppressWarnings("unchecked")
		Iterator<Edge> edgeIterator = semanticView.getSourceEdges().iterator();
		while (edgeIterator.hasNext()) {
			Edge edge = edgeIterator.next();
			if (edge.getType().equals("StereotypeCommentLink")) {
				appliedStereotypeLink = edge;
			}

		}
		if (appliedStereotypeLink == null) {
			return null;
		}
		return (Node) appliedStereotypeLink.getTarget();

	}

	protected Stereotype applyStereotype(EditPart editPart) {
		assertNotNull("editPart", editPart);
		final Element element = getElement(editPart);
		assertNotNull("element", element);
		Model model = getModel(element);
		assertNotNull("model", model);
		Profile profile = applyProfile(model);
		assertNotNull("profile", profile);
		final View diagramElement = getDiagramElement(editPart);
		EList<PackageableElement> packagedElements = profile.getPackagedElements();
		for (PackageableElement elt : packagedElements) {
			if (elt instanceof Stereotype) {
				final Stereotype stereotype = (Stereotype) elt;
				if (!element.isStereotypeApplicable(stereotype)) {
					continue;
				}
				Stereotype appliedStereotype = element.getAppliedStereotype(stereotype.getQualifiedName());
				if (appliedStereotype == null) {
					// Apply the stereotype if possible.
					getCommandStack().execute(new RecordingCommand(getEditingDomain()) {

						@Override
						protected void doExecute() {
							element.applyStereotype(stereotype);
						}
					});
					appliedStereotype = element.getAppliedStereotype(stereotype.getQualifiedName());
					// Display current stereotype
					StereotypeDisplayUtil helper = StereotypeDisplayUtil.getInstance();
					View stereotypeLabel = helper.getStereotypeLabel(diagramElement, appliedStereotype);
					StereotypeDisplayCommandExecution.getInstance().setPersistency(getEditingDomain(), stereotypeLabel, false);
					StereotypeDisplayCommandExecution.getInstance().setVisibility(getEditingDomain(), stereotypeLabel, true, false);
					if (appliedStereotype != null) {
						// // Display all properties of it.
						Iterator<Property> propIt = appliedStereotype.getAllAttributes().iterator();
						while (propIt.hasNext()) {
							final Property currentProp = propIt.next();
							View propertyView = helper.getStereotypeProperty(diagramElement, appliedStereotype, currentProp);
							boolean adding = false;
							if (currentProp.getAssociation() != null) {
								if (!currentProp.getName().startsWith("base_")) {
									adding = true;
								}
							} else {
								adding = true;
							}
							if (adding) {
								StereotypeDisplayCommandExecution.getInstance().setPersistency(getEditingDomain(), propertyView, false);
								StereotypeDisplayCommandExecution.getInstance().setVisibility(getEditingDomain(), propertyView, true, false);
							}
						}
					}


				}
				return appliedStereotype;
			}
		}
		return null;
	}
	
	
	protected Profile applyProfile(final Model model) {
		final Profile testProfile = (Profile) PackageUtil.loadPackage(TEST_PROFILE_URI, model.eResource().getResourceSet());
		assertNotNull("profileToApply", testProfile);
		getCommandStack().execute(new RecordingCommand(getEditingDomain()) {

			@Override
			protected void doExecute() {
				PackageUtil.applyProfile(model, testProfile, true);
			}
		});
		return testProfile;
	}

	protected View getDiagramElement(EditPart editPart) {
		if (editPart == null) {
			return null;
		}
		Object model = editPart.getModel();
		if (model instanceof View) {
			return (View) model;
		}
		return null;
	}

	protected Element getElement(EditPart editPart) {
		View diagramElement = getDiagramElement(editPart);
		if (diagramElement == null) {
			return null;
		}
		return (Element) ViewUtil.resolveSemanticElement(diagramElement);
	}

	protected Model getModel(EObject obj) {
		if (obj instanceof Model) {
			return (Model) obj;
		}
		EObject parent = obj.eContainer();
		while (parent != null && !(parent instanceof Model)) {
			parent = parent.eContainer();
		}
		return (Model) parent;
	}



	/**
	 * test apply profile to model and apply stereotype to a EditPart.
	 */
	protected void innerTestApplyProfile() {
		DiagramEditPart diagram = getDiagramEditPart();
		assertNotNull("diagram", diagram);
		Stereotype stereotype = applyStereotype(diagram);
		assertNotNull("appliedStereotype", stereotype);
	}

	public void checkConnectionHorizontally(ConnectionNodeEditPart conn, int tolerance) {
		assertNotNull("connection", conn);
		Point start = SequenceUtil.getAbsoluteEdgeExtremity(conn, true);
		Point end = SequenceUtil.getAbsoluteEdgeExtremity(conn, false);
		int distance = Math.abs(start.y - end.y);
		assertTrue("connection horizontally (" + distance + ")", distance <= tolerance);
	}

}

Back to the top