Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: d6263afc49860295d3917f6a4ff4cb5cae70d32d (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
/*****************************************************************************
 * Copyright (c) 2018 Christian W. Damus 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 - Initial API and implementation
 *   
 *****************************************************************************/

package org.eclipse.papyrus.uml.diagram.sequence.tests.bug;

import static org.eclipse.papyrus.junit.matchers.MoreMatchers.greaterThan;
import static org.eclipse.papyrus.junit.matchers.MoreMatchers.isEmpty;
import static org.eclipse.papyrus.junit.utils.rules.PapyrusEditorFixture.at;
import static org.eclipse.papyrus.junit.utils.rules.PapyrusEditorFixture.sized;
import static org.hamcrest.CoreMatchers.hasItem;
import static org.hamcrest.CoreMatchers.hasItems;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assume.assumeThat;

import java.util.function.Consumer;

import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.GraphicalEditPart;
import org.eclipse.papyrus.junit.framework.classification.tests.AbstractPapyrusTest;
import org.eclipse.papyrus.junit.matchers.DiagramMatchers;
import org.eclipse.papyrus.junit.utils.rules.ActiveDiagram;
import org.eclipse.papyrus.junit.utils.rules.PapyrusEditorFixture;
import org.eclipse.papyrus.junit.utils.rules.PluginResource;
import org.eclipse.papyrus.uml.diagram.sequence.providers.UMLElementTypes;
import org.eclipse.uml2.uml.CombinedFragment;
import org.eclipse.uml2.uml.ExecutionSpecification;
import org.eclipse.uml2.uml.Interaction;
import org.eclipse.uml2.uml.InteractionFragment;
import org.eclipse.uml2.uml.InteractionOperand;
import org.junit.Rule;
import org.junit.Test;

/**
 * Regression tests specifically for {@link CombinedFragment}s in the sequence diagram
 * editor, especially those tracked under the umbrella of <a href="http://eclip.se/533670">bug 533670</a>.
 * 
 * @author Christian W. Damus
 * @see <a href="http://eclip.se/533670">bug 533670</a>
 */
@ActiveDiagram("sequence")
public class CombinedFragmentRegressionTest extends AbstractPapyrusTest {

	@Rule
	public final PapyrusEditorFixture editor = new PapyrusEditorFixture();

	/**
	 * Initializes me.
	 */
	public CombinedFragmentRegressionTest() {
		super();
	}

	/**
	 * Verify the creation and extent of a default interaction operand in a newly
	 * created combined fragment.
	 */
	@Test
	@PluginResource("resource/bugs/bug533673.di")
	public void defaultInteractionOperand_533673() {
		EditPart interactionEP = editor.findEditPart("DoIt", Interaction.class);
		EditPart interactionCompartment = editor.getShapeCompartment(interactionEP);

		EditPart combinedFragment = editor.createShape(interactionCompartment, UMLElementTypes.CombinedFragment_Shape, at(15, 60), sized(360, 200));

		assertThat(combinedFragment, DiagramMatchers.semanticThat(instanceOf(CombinedFragment.class)));
		CombinedFragment cf = (CombinedFragment) combinedFragment.getAdapter(EObject.class);

		assertThat("No interaction operand", cf.getOperands(), not(isEmpty()));

		InteractionOperand operand = cf.getOperands().get(0);
		EditPart operandEP = editor.requireEditPart(combinedFragment, operand);

		assertThat(operandEP, instanceOf(GraphicalEditPart.class));
		IFigure figure = ((GraphicalEditPart) operandEP).getFigure();

		Consumer<Rectangle> verifyBounds = bounds -> {
			// Account for margins
			assertThat("Width too small", bounds.width, greaterThan(350));
			// Account for some space for the combined fragment operator label
			// and some extra for font size variance across platforms (esp.
			// Linux and Windows, which seem to be bigger than MacOS)
			assertThat("Height too small", bounds.height, greaterThan(160));
		};

		verifyBounds.accept(figure.getBounds());

		editor.undo();

		operandEP = editor.findEditPart(operand);
		assertThat("Operand still present in the diagram", operandEP, nullValue());

		editor.redo();

		operandEP = editor.findEditPart(operand);
		assertThat(operandEP, instanceOf(GraphicalEditPart.class));
		figure = ((GraphicalEditPart) operandEP).getFigure();
		verifyBounds.accept(figure.getBounds());
	}

	/**
	 * Verify that the deletion of an interaction operand does not delete the
	 * interaction fragments that it owns, but transfers them up to the next
	 * fragment container.
	 */
	@Test
	@PluginResource("resource/bugs/bug533682.di")
	public void deleteInteractionOperand() {
		EditPart combinedFragmentEP = editor.findEditPart("cfrag", CombinedFragment.class);
		CombinedFragment cfrag = (CombinedFragment) combinedFragmentEP.getAdapter(EObject.class);
		Interaction interaction = cfrag.getEnclosingInteraction();
		assumeThat("No interaction", interaction, notNullValue());

		InteractionOperand operand = cfrag.getOperands().get(0);
		EditPart operandEP = editor.requireEditPart(combinedFragmentEP, operand);

		// We have at least four message ends and one execution specification
		InteractionFragment[] fragments = operand.getFragments().toArray(new InteractionFragment[0]);
		assumeThat("Lost fragments on opening editor", fragments.length, greaterThan(4));
		ExecutionSpecification exec = (ExecutionSpecification) operand.getFragment("exec");
		assumeThat("No execution specification", exec, notNullValue());

		editor.delete(operandEP);

		assertThat("Interaction fragments not retained", interaction.getFragments(), hasItems(fragments));
		assertThat("Execution specification lost from diagram", editor.findEditPart(exec), notNullValue());

		// Cannot test undo/redo because after Undo, the GridManagementEditPolicy
		// erroneously adds the combined fragment to the fragments of the operand
		// that is restored to its operands list, which is a containment cycle
		// that breaks EMF
	}

	/**
	 * Verify that the deletion of a combined fragment does not delete the
	 * interaction fragments that its operand owns, but transfers them up to the next
	 * fragment container.
	 */
	@Test
	@PluginResource("resource/bugs/bug533682.di")
	public void deleteCombinedFragment() {
		EditPart combinedFragmentEP = editor.findEditPart("cfrag", CombinedFragment.class);
		CombinedFragment cfrag = (CombinedFragment) combinedFragmentEP.getAdapter(EObject.class);
		Interaction interaction = cfrag.getEnclosingInteraction();
		assumeThat("No interaction", interaction, notNullValue());

		InteractionOperand operand = cfrag.getOperands().get(0);

		// We have at least four message ends and one execution specification
		InteractionFragment[] fragments = operand.getFragments().toArray(new InteractionFragment[0]);
		assumeThat("Lost fragments on opening editor", fragments.length, greaterThan(4));
		ExecutionSpecification exec = (ExecutionSpecification) operand.getFragment("exec");
		assumeThat("No execution specification", exec, notNullValue());

		editor.delete(combinedFragmentEP);

		assertThat("Interaction fragments not retained", interaction.getFragments(), hasItems(fragments));
		assertThat("Execution specification lost from diagram", editor.findEditPart(exec), notNullValue());
	}

	/**
	 * Verify that the deletion of an interaction fragment inside of an operand
	 * is not impeded.
	 */
	@Test
	@PluginResource("resource/bugs/bug533682.di")
	public void deleteInteractionFragmentInOperand() {
		EditPart combinedFragmentEP = editor.findEditPart("cfrag", CombinedFragment.class);
		CombinedFragment cfrag = (CombinedFragment) combinedFragmentEP.getAdapter(EObject.class);
		Interaction interaction = cfrag.getEnclosingInteraction();
		assumeThat("No interaction", interaction, notNullValue());

		InteractionOperand operand = cfrag.getOperands().get(0);

		// Find the execution specification
		EditPart execEP = editor.findEditPart("exec", ExecutionSpecification.class);
		ExecutionSpecification exec = (ExecutionSpecification) execEP.getAdapter(EObject.class);

		editor.delete(execEP);

		assertThat("Execution specification not deleted", exec.eResource(), nullValue());
		assertThat("Operand still has the execution specification", operand.getFragments(), not(hasItem(exec)));
		assertThat("Interaction has the execution specification", interaction.getFragments(), not(hasItem(exec)));

		execEP = editor.findEditPart("exec", ExecutionSpecification.class);
		assertThat("Execution specification still presented in diagram", execEP, nullValue());
	}

	/**
	 * Verify that the deletion of the interaction is not impeded.
	 */
	@Test
	@PluginResource("resource/bugs/bug533682.di")
	public void deleteInteraction() {
		EditPart combinedFragmentEP = editor.findEditPart("cfrag", CombinedFragment.class);
		CombinedFragment cfrag = (CombinedFragment) combinedFragmentEP.getAdapter(EObject.class);
		Interaction interaction = cfrag.getEnclosingInteraction();
		assumeThat("No interaction", interaction, notNullValue());

		// Find the execution specification
		EditPart execEP = editor.findEditPart("exec", ExecutionSpecification.class);
		ExecutionSpecification exec = (ExecutionSpecification) execEP.getAdapter(EObject.class);

		EditPart interactionEP = editor.requireEditPart(editor.getActiveDiagram(), interaction);
		editor.delete(interactionEP);

		assertThat("Execution specification not deleted", exec.eResource(), nullValue());
		assertThat("Interaction not deleted", interaction.eResource(), nullValue());
	}

	/**
	 * Verify that the deletion of the only (or last remaining) interaction operand
	 * in a combined fragment results in deletion of the combined fragment, also.
	 * 
	 * @see <a href="http://eclip.se/533683">bug 533683</a>
	 */
	@Test
	@PluginResource("resource/bugs/bug533682.di")
	public void deleteOnlyInteractionOperand() {
		EditPart combinedFragmentEP = editor.findEditPart("cfrag", CombinedFragment.class);
		CombinedFragment cfrag = (CombinedFragment) combinedFragmentEP.getAdapter(EObject.class);
		Interaction interaction = cfrag.getEnclosingInteraction();
		assumeThat("No interaction", interaction, notNullValue());

		InteractionOperand operand = cfrag.getOperands().get(0);
		EditPart operandEP = editor.requireEditPart(combinedFragmentEP, operand);

		editor.delete(operandEP);

		combinedFragmentEP = editor.findEditPart(cfrag);

		assertThat("Combined fragment not deleted", cfrag.eResource(), nullValue());
		assertThat("Combined fragment still presented in diagram", combinedFragmentEP, nullValue());
	}

	/**
	 * Verify that the deletion of an interaction operand that leaves at least one
	 * remaining in a combined fragment does not result in deletion of the combined fragment.
	 * 
	 * @see <a href="http://eclip.se/533683">bug 533683</a>
	 */
	@Test
	@PluginResource("resource/bugs/bug533683.di")
	public void deleteNotOnlyInteractionOperand() {
		EditPart combinedFragmentEP = editor.findEditPart("cfrag", CombinedFragment.class);
		CombinedFragment cfrag = (CombinedFragment) combinedFragmentEP.getAdapter(EObject.class);
		Interaction interaction = cfrag.getEnclosingInteraction();
		assumeThat("No interaction", interaction, notNullValue());

		InteractionOperand operand = cfrag.getOperands().get(1);
		InteractionFragment deleteSend = operand.getFragment("delete-send");
		assumeThat("Lost the delete send event on editor open", deleteSend, notNullValue());
		InteractionFragment deleted = operand.getFragment("deleted");
		assumeThat("Lost the deletion occurrence on editor open", deleted, notNullValue());

		EditPart operandEP = editor.requireEditPart(combinedFragmentEP, operand);

		editor.delete(operandEP);

		combinedFragmentEP = editor.findEditPart(cfrag);

		assertThat("Combined fragment was deleted", cfrag.getEnclosingInteraction(), is(interaction));
		assertThat("Combined fragment no longer presented in diagram", combinedFragmentEP, notNullValue());

		assertThat("Fragments of deleted operand not retained",
				interaction.getFragments(), hasItems(deleteSend, deleted));
	}
}

Back to the top