Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ac0399f566d6f582f9a254d9cdf598309bd6652b (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
/*****************************************************************************
 * Copyright (c) 2018 CEA LIST 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:
 *   CEA LIST - Initial API and implementation
 *   
 *****************************************************************************/

package org.eclipse.papyrus.uml.service.types.tests.deletion;

import static org.eclipse.papyrus.junit.matchers.MoreMatchers.greaterThan;
import static org.hamcrest.CoreMatchers.everyItem;
import static org.hamcrest.CoreMatchers.hasItem;
import static org.hamcrest.CoreMatchers.hasItems;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.fail;
import static org.junit.Assume.assumeThat;

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.transaction.RollbackException;
import org.eclipse.gmf.runtime.common.core.command.CompositeCommand;
import org.eclipse.gmf.runtime.common.core.command.ICommand;
import org.eclipse.gmf.runtime.common.core.command.IdentityCommand;
import org.eclipse.gmf.runtime.emf.type.core.requests.DestroyElementRequest;
import org.eclipse.gmf.runtime.emf.type.core.requests.IEditCommandRequest;
import org.eclipse.papyrus.infra.emf.gmf.util.GMFUnsafe;
import org.eclipse.papyrus.infra.services.edit.service.ElementEditServiceUtils;
import org.eclipse.papyrus.infra.services.edit.service.IElementEditService;
import org.eclipse.papyrus.infra.types.core.registries.ElementTypeSetConfigurationRegistry;
import org.eclipse.papyrus.junit.utils.rules.ModelSetFixture;
import org.eclipse.papyrus.junit.utils.rules.PluginResource;
import org.eclipse.uml2.common.util.UML2Util;
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.eclipse.uml2.uml.InteractionOperatorKind;
import org.eclipse.uml2.uml.NamedElement;
import org.eclipse.uml2.uml.UMLFactory;
import org.hamcrest.CustomTypeSafeMatcher;
import org.hamcrest.Matcher;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;

import com.google.common.collect.Iterators;

/**
 * @author damus
 *
 */
public class InteractionDeletionTest {

	@Rule
	public final ModelSetFixture model = new ModelSetFixture();

	/**
	 * Constructor.
	 *
	 */
	public InteractionDeletionTest() {
		super();
	}

	/**
	 * 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/interactions/bug533682.uml")
	public void deleteCombinedFragment() {
		CombinedFragment cfrag = findElement("cfrag", CombinedFragment.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());

		delete(cfrag);

		assertThat("Interaction fragments not retained", interaction.getFragments(), hasItems(fragments));
	}

	/**
	 * A variant in which the contents of a combined fragment that is deleted end up in
	 * a nesting fragment, not the interaction.
	 */
	@Test
	@PluginResource("resource/interactions/bug533682.uml")
	public void deleteCombinedFragmentNested() {
		CombinedFragment cfrag = findElement("cfrag", CombinedFragment.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());

		// Insert a layer of nesting
		CombinedFragment newCFrag = insertContainingCombinedFragment(cfrag);
		InteractionOperand intermediate = newCFrag.getOperands().get(0);

		delete(cfrag);

		assertThat("Interaction fragments not retained by parent operand", intermediate.getFragments(), hasItems(fragments));
	}

	/**
	 * A variant in which the content of a combined fragment that is deleted is just another
	 * combined fragment, not more primitive fragments.
	 */
	@Test
	@PluginResource("resource/interactions/bug533682.uml")
	public void deleteCombinedFragmentDeeplyNested() {
		CombinedFragment cfrag = findElement("cfrag", CombinedFragment.class);
		Interaction interaction = cfrag.getEnclosingInteraction();
		assumeThat("No interaction", interaction, notNullValue());

		// Insert two layers of nesting
		CombinedFragment toDelete = insertContainingCombinedFragment(cfrag);
		CombinedFragment newCFrag = insertContainingCombinedFragment(toDelete);
		InteractionOperand intermediate = newCFrag.getOperands().get(0);

		delete(toDelete);

		assertThat("Interaction fragments not retained by grandparent operand", intermediate.getFragments(), hasItem(cfrag));
	}

	/**
	 * Verify that the deletion of an interaction fragment inside of an operand is not impeded.
	 */
	@Test
	@PluginResource("resource/interactions/bug533682.uml")
	public void deleteInteractionFragmentInOperand() {
		CombinedFragment cfrag = findElement("cfrag", CombinedFragment.class);
		Interaction interaction = cfrag.getEnclosingInteraction();
		assumeThat("No interaction", interaction, notNullValue());

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

		ExecutionSpecification exec = findElement("exec", ExecutionSpecification.class);

		delete(exec);

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

	/**
	 * Verify that the deletion of the interaction is not impeded.
	 */
	@Test
	@PluginResource("resource/interactions/bug533682.uml")
	public void deleteInteraction() {
		CombinedFragment cfrag = findElement("cfrag", CombinedFragment.class);
		Interaction interaction = cfrag.getEnclosingInteraction();
		assumeThat("No interaction", interaction, notNullValue());

		ExecutionSpecification exec = findElement("exec", ExecutionSpecification.class);

		delete(interaction);

		assertThat("Execution specification not deleted", exec, isDeleted());
		assertThat("Interaction not deleted", interaction, isDeleted());
	}

	@Test
	@PluginResource("resource/interactions/bug533682.uml")
	public void deleteInteractionWithNesting() {
		CombinedFragment cfrag = findElement("cfrag", CombinedFragment.class);
		Interaction interaction = cfrag.getEnclosingInteraction();
		assumeThat("No interaction", interaction, notNullValue());

		ExecutionSpecification exec = findElement("exec", ExecutionSpecification.class);

		insertContainingCombinedFragment(cfrag);

		delete(interaction);

		assertThat("Execution specification not deleted", exec, isDeleted());
		assertThat("Interaction not deleted", interaction, isDeleted());
	}

	/**
	 * Verify that the deletion of the last operand of a combined fragment results in the
	 * combined fragment also being deleted.
	 */
	@Test
	@PluginResource("resource/interactions/bug533683.uml")
	public void deleteOperandsOfCombinedFragment() {
		CombinedFragment cfrag = findElement("cfrag", CombinedFragment.class);
		Interaction interaction = cfrag.getEnclosingInteraction();
		assumeThat("No interaction", interaction, notNullValue());

		InteractionOperand alt1 = cfrag.getOperand("alt1");
		InteractionOperand alt2 = cfrag.getOperand("alt2");

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

		delete(alt2);

		assertThat(cfrag.getOperands(), is(Collections.singletonList(alt1)));

		assertThat("Interaction fragments were moved or deleted", alt1.getFragments(), hasItems(fragments));

		delete(alt1);

		assertThat("Combined fragment not deleted", cfrag, isDeleted());

		assertThat("Interaction fragments not retained", interaction.getFragments(), hasItems(fragments));
	}

	/**
	 * Verify that the simultaneous deletion of all operands of a combined fragment results in the
	 * combined fragment also being deleted.
	 */
	@Test
	@PluginResource("resource/interactions/bug533683.uml")
	public void deleteAllOperandsOfCombinedFragment() {
		CombinedFragment cfrag = findElement("cfrag", CombinedFragment.class);
		Interaction interaction = cfrag.getEnclosingInteraction();
		assumeThat("No interaction", interaction, notNullValue());

		InteractionOperand alt1 = cfrag.getOperand("alt1");
		InteractionOperand alt2 = cfrag.getOperand("alt2");

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

		delete(Arrays.asList(alt1, alt2));
		assertThat(Arrays.asList(alt1, alt2), everyItem(isDeleted()));

		assertThat("Combined fragment not deleted", cfrag, isDeleted());
		assertThat("Interaction fragments not retained", interaction.getFragments(), hasItems(fragments));
	}

	//
	// Test framework
	//

	@BeforeClass
	public static void ensureModeledEditHelperAdvice() {
		ElementTypeSetConfigurationRegistry.getInstance().getElementTypeSetConfigurations();
	}

	<T extends NamedElement> T findElement(String name, Class<T> type) {
		Iterator<? extends EObject> eObjects = Iterators.filter(model.getResourceSet().getAllContents(), EObject.class);
		return type.cast(UML2Util.findEObject(eObjects, eObject -> type.isInstance(eObject) && name.equals(((NamedElement) eObject).getName())));
	}

	void delete(EObject object) {
		DestroyElementRequest request = new DestroyElementRequest(model.getEditingDomain(), object, false);
		model.execute(command(object.eContainer(), request));
	}

	void delete(Collection<? extends EObject> objects) {
		DestroyElementRequest request = new DestroyElementRequest(model.getEditingDomain(), false);
		Map<String, Object> parameters = new HashMap<>();

		@SuppressWarnings("unchecked")
		ICommand command = objects.stream().reduce((ICommand) IdentityCommand.INSTANCE,
				(cmd, object) -> {
					request.setElementToDestroy(object);
					request.addParameters(parameters);
					ICommand delete = command(object.eContainer(), request);
					parameters.clear();
					parameters.putAll(request.getParameters());
					return cmd.compose(delete);
				},
				CompositeCommand::compose);

		model.execute(command.reduce());
	}

	ICommand command(EObject object, IEditCommandRequest request) {
		IElementEditService edit = ElementEditServiceUtils.getCommandProvider(object);
		assertThat("No edit command provider", edit, notNullValue());
		ICommand result = edit.getEditCommand(request);
		assertThat("No command provided for " + request.getClass().getSimpleName(), result, notNullValue());
		return result;
	}

	/**
	 * Sneak a combined fragment into the model as parent of the given combined fragment,
	 * to add a level of nesting.
	 * 
	 * @param cfrag
	 *            the combined fragment to nested into another new one
	 * @return the new nesting combined fragment
	 */
	CombinedFragment insertContainingCombinedFragment(CombinedFragment cfrag) {
		CombinedFragment[] result = { null };

		try {
			GMFUnsafe.write(model.getEditingDomain(), () -> {
				EList<InteractionFragment> fragments = (cfrag.getEnclosingInteraction() == null)
						? cfrag.getEnclosingOperand().getFragments()
						: cfrag.getEnclosingInteraction().getFragments();

				int index = fragments.indexOf(cfrag);
				CombinedFragment newCFrag = UMLFactory.eINSTANCE.createCombinedFragment();
				newCFrag.setInteractionOperator(InteractionOperatorKind.OPT_LITERAL);
				InteractionOperand newOperand = newCFrag.createOperand(null);
				fragments.add(index, newCFrag);
				newOperand.getFragments().add(cfrag);

				result[0] = newCFrag;
			});
		} catch (InterruptedException e) {
			fail("Combined fragment insertion transaction interrupted");
		} catch (RollbackException e) {
			e.printStackTrace();
			fail("Combined fragment insertion transaction rolled back");
		}

		return result[0];
	}

	static Matcher<EObject> isDeleted() {
		return new CustomTypeSafeMatcher<EObject>("is deleted") {
			@Override
			protected boolean matchesSafely(EObject item) {
				return item.eResource() == null;
			}
		};
	}
}

Back to the top