Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: f256e6679fc6113f44579ae4516a28d1e91891fa (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
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
/*
 * Copyright (c) 2014 CEA 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 (CEA) - Initial API and implementation
 *
 */
package org.eclipse.papyrus.infra.emf.advice;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.fail;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import org.eclipse.core.commands.operations.IOperationHistory;
import org.eclipse.core.commands.operations.IUndoContext;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IAdapterFactory;
import org.eclipse.core.runtime.Platform;
import org.eclipse.emf.common.command.Command;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.common.util.UniqueEList;
import org.eclipse.emf.ecore.EClass;
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.resource.impl.ResourceSetImpl;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain;
import org.eclipse.emf.edit.domain.EditingDomain;
import org.eclipse.emf.transaction.RecordingCommand;
import org.eclipse.emf.transaction.RollbackException;
import org.eclipse.emf.transaction.Transaction;
import org.eclipse.emf.transaction.TransactionalCommandStack;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.emf.workspace.IWorkspaceCommandStack;
import org.eclipse.emf.workspace.ResourceUndoContext;
import org.eclipse.emf.workspace.WorkspaceEditingDomainFactory;
import org.eclipse.gmf.runtime.common.core.command.ICommand;
import org.eclipse.gmf.runtime.emf.type.core.ClientContextManager;
import org.eclipse.gmf.runtime.emf.type.core.ElementTypeRegistry;
import org.eclipse.gmf.runtime.emf.type.core.IClientContext;
import org.eclipse.gmf.runtime.emf.type.core.IElementType;
import org.eclipse.gmf.runtime.emf.type.core.edithelper.IEditHelper;
import org.eclipse.gmf.runtime.emf.type.core.requests.CreateElementRequest;
import org.eclipse.gmf.runtime.emf.type.core.requests.CreateRelationshipRequest;
import org.eclipse.gmf.runtime.emf.type.core.requests.DestroyElementRequest;
import org.eclipse.gmf.runtime.emf.type.core.requests.DestroyReferenceRequest;
import org.eclipse.gmf.runtime.emf.type.core.requests.DuplicateElementsRequest;
import org.eclipse.gmf.runtime.emf.type.core.requests.IEditCommandRequest;
import org.eclipse.gmf.runtime.emf.type.core.requests.MoveRequest;
import org.eclipse.gmf.runtime.emf.type.core.requests.ReorientReferenceRelationshipRequest;
import org.eclipse.gmf.runtime.emf.type.core.requests.ReorientRelationshipRequest;
import org.eclipse.gmf.runtime.emf.type.core.requests.ReorientRequest;
import org.eclipse.gmf.runtime.emf.type.core.requests.SetRequest;
import org.eclipse.papyrus.infra.emf.utils.EMFHelper;
import org.eclipse.papyrus.junit.utils.rules.Condition;
import org.eclipse.papyrus.junit.utils.rules.ConditionRule;
import org.eclipse.papyrus.junit.utils.rules.Conditional;
import org.eclipse.papyrus.junit.utils.rules.HouseKeeper;
import org.eclipse.uml2.uml.Class;
import org.eclipse.uml2.uml.Comment;
import org.eclipse.uml2.uml.Dependency;
import org.eclipse.uml2.uml.NamedElement;
import org.eclipse.uml2.uml.Package;
import org.eclipse.uml2.uml.Type;
import org.eclipse.uml2.uml.UMLPackage;
import org.eclipse.uml2.uml.Usage;
import org.eclipse.uml2.uml.UseCase;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameter;
import org.junit.runners.Parameterized.Parameters;

import com.google.common.base.Function;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;


/**
 * Test suite for the {@link ReadOnlyObjectEditAdvice} class.
 */
@RunWith(Parameterized.class)
public class ReadOnlyObjectEditAdviceTest {

	private static final String TEST_BUNDLE = "org.eclipse.papyrus.infra.emf.tests"; //$NON-NLS-1$

	private static final IClientContext PAPYRUS_CONTEXT = ClientContextManager.getInstance().getClientContext("org.eclipse.papyrus.infra.services.edit.TypeContext"); //$NON-NLS-1$

	private static IAdapterFactory readOnlyHandlerAdapterFactory;

	@Parameter
	public ResourceMode resourceMode;

	@Rule
	public final ConditionRule condition = new ConditionRule();

	@Rule
	public final HouseKeeper houseKeeper = new HouseKeeper();

	@Rule
	public final TemporaryFolder tmp = new TemporaryFolder();

	private IProject project;

	private IFile workspaceFile;

	private TransactionalEditingDomain domain;

	private Package writablePackage;

	private Package readOnlyPackage;

	private Class classA;

	private Class classB;

	public ReadOnlyObjectEditAdviceTest() {
		super();
	}

	/**
	 * Test that setting an attribute of a read-only object is not permitted.
	 */
	@Test
	public void testSetCommand_attribute() {
		ICommand command = getEditCommand(classB, new SetRequest(classB, UMLPackage.Literals.NAMED_ELEMENT__NAME, "BB")); //$NON-NLS-1$
		assertExecutability(command);
	}

	/**
	 * Test that adding a read-only object to a multi-valued reference that has an opposite is not permitted.
	 */
	@Test
	public void testSetCommand_referenceOppositeAdd() {
		UseCase doIt = classA.getUseCase("DoIt"); //$NON-NLS-1$
		ICommand command = getEditCommand(doIt, new SetRequest(doIt, UMLPackage.Literals.USE_CASE__SUBJECT, classB)); //$NON-NLS-1$
		assertExecutability(command);
	}

	/**
	 * Test that removing a read-only object from a multi-valued reference that has an opposite is not permitted.
	 */
	@Test
	public void testSetCommand_referenceOppositeRemove() {
		final UseCase doIt = classA.getUseCase("DoIt"); //$NON-NLS-1$

		// By-pass edit-helpers to set up the dependency
		executeUnprotected(new RecordingCommand(domain) {

			@Override
			protected void doExecute() {
				doIt.getSubjects().clear();
				doIt.getSubjects().add(classB); // the read-only object
			}
		});

		ICommand command = getEditCommand(doIt, new SetRequest(doIt, UMLPackage.Literals.USE_CASE__SUBJECT, ImmutableList.of(classA))); //$NON-NLS-1$
		assertExecutability(command);
	}

	/**
	 * Test that adding a read-only object to a new container by means of setting a containment reference is forbidden (as the side-effect is to
	 * remove it from the originating read-only containment context).
	 */
	@Test
	public void testSetCommand_containmentOldContainerReadonly() {
		ICommand command = getEditCommand(writablePackage, new SetRequest(writablePackage, UMLPackage.Literals.PACKAGE__PACKAGED_ELEMENT, classB));
		assertExecutability(command);
	}

	/**
	 * Test that adding a read-only object to a new container by means of setting a containment reference to a new list is forbidden (as the
	 * side-effect is to remove it from the originating read-only containment context).
	 */
	@Test
	public void testSetCommand_containmentOldContainerReadonlyMany() {
		List<EObject> newList = new UniqueEList<EObject>(writablePackage.getPackagedElements());
		newList.add(classB);
		ICommand command = getEditCommand(writablePackage, new SetRequest(writablePackage, UMLPackage.Literals.PACKAGE__PACKAGED_ELEMENT, newList));
		assertExecutability(command);
	}

	@Test
	public void testCreateCommand() {
		IElementType propertyType = getElementType(UMLPackage.Literals.PROPERTY);
		ICommand command = getEditCommand(classB, new CreateElementRequest(classB, propertyType, UMLPackage.Literals.STRUCTURED_CLASSIFIER__OWNED_ATTRIBUTE));
		assertExecutability(command);
	}

	@Test
	public void testCreateRelationshipCommand() {
		IElementType usageType = getElementType(UMLPackage.Literals.USAGE);
		// Create the Usage in the writable package to remove that concern
		ICommand command = getEditCommand(writablePackage, new CreateRelationshipRequest(writablePackage, classB, classA, usageType));
		assertExecutability(command);
	}

	@Test
	public void testDestroyElementCommand() {
		ICommand command = getEditCommand(readOnlyPackage, new DestroyElementRequest(classB, false));
		assertExecutability(command);
	}

	@Test
	public void testDestroyReferenceCommand_owner() {
		final Usage[] usage = { null };
		final Type classD = writablePackage.getOwnedType("D"); //$NON-NLS-1$

		// By-pass edit-helpers to set up the relationship
		executeUnprotected(new RecordingCommand(domain) {

			@Override
			protected void doExecute() {
				usage[0] = (Usage)writablePackage.createPackagedElement("B-->D", UMLPackage.Literals.USAGE); //$NON-NLS-1$
				usage[0].getClients().add(classB);
				usage[0].getSuppliers().add(classD);
			}
		});

		ICommand command = getEditCommand(writablePackage, new DestroyReferenceRequest(classB, UMLPackage.Literals.NAMED_ELEMENT__CLIENT_DEPENDENCY, usage[0], false)); //$NON-NLS-1$
		assertExecutability(command);
	}

	@Test
	public void testDestroyReferenceCommand_referencedOpposite() {
		final UseCase doIt = classA.getUseCase("DoIt"); //$NON-NLS-1$

		// By-pass edit-helpers to set up the relationship
		executeUnprotected(new RecordingCommand(domain) {

			@Override
			protected void doExecute() {
				doIt.getSubjects().add(classB);
			}
		});

		ICommand command = getEditCommand(doIt, new DestroyReferenceRequest(doIt, UMLPackage.Literals.USE_CASE__SUBJECT, classB, false)); //$NON-NLS-1$
		assertExecutability(command);
	}

	@Test
	@Conditional(key = "adviceEnabled")
	public void testDuplicateCommand() {
		// We don't have any edit helpers suitable for this use case
		assertAdvice(new DuplicateElementsRequest(Collections.singletonList(classB)));
	}

	@Test
	public void testMoveCommand_fromReadOnly() {
		ICommand command = getEditCommand(readOnlyPackage, new MoveRequest(writablePackage, UMLPackage.Literals.PACKAGE__PACKAGED_ELEMENT, classB));
		assertExecutability(command);
	}

	/**
	 * Test the special case of a root element of a read-only resource being moved to a writable resource. Roots do not have containers (read-only
	 * or otherwise) so we need to check the originating containing resource for read-only state.
	 */
	@Test
	public void testMoveCommand_rootFromReadOnly() {
		ICommand command = getEditCommand(readOnlyPackage, new MoveRequest(writablePackage, UMLPackage.Literals.PACKAGE__PACKAGED_ELEMENT, readOnlyPackage));
		assertExecutability(command);
	}

	@Test
	public void testMoveCommand_toReadOnly() {
		final Type classD = writablePackage.getOwnedType("D"); //$NON-NLS-1$
		ICommand command = getEditCommand(writablePackage, new MoveRequest(readOnlyPackage, UMLPackage.Literals.PACKAGE__PACKAGED_ELEMENT, classD));
		assertExecutability(command);
	}

	@Test
	public void testReorientRelationshipCommand_targetOldReadonly() {
		final Dependency dep = classA.getClientDependency("A-->D"); //$NON-NLS-1$
		final NamedElement classD = dep.getSupplier("D"); //$NON-NLS-1$
		assertThat("Wrong initial supplier in test model dependency", classD, notNullValue()); //$NON-NLS-1$

		// By-pass edit-helpers to set up the relationship
		executeUnprotected(new RecordingCommand(domain) {

			@Override
			protected void doExecute() {
				dep.getSuppliers().clear();
				dep.getSuppliers().add(classB);
			}
		});

		ICommand command = getEditCommand(dep, new ReorientRelationshipRequest(dep, classD, classB, ReorientRequest.REORIENT_TARGET));
		// Reorienting the target is always permitted because there is no inverse reference
		assertThat("Command should be executable", command.canExecute(), is(true)); //$NON-NLS-1$
	}

	@Test
	public void testReorientRelationshipCommand_targetNewReadOnly() {
		final Dependency dep = classA.getClientDependency("A-->D"); //$NON-NLS-1$
		final NamedElement classD = dep.getSupplier("D"); //$NON-NLS-1$
		assertThat("Wrong supplier in test model dependency", classD, notNullValue()); //$NON-NLS-1$

		ICommand command = getEditCommand(dep, new ReorientRelationshipRequest(dep, classB, classD, ReorientRequest.REORIENT_TARGET));
		// Reorienting the target is always permitted because there is no inverse reference
		assertThat("Command should be executable", command.canExecute(), is(true)); //$NON-NLS-1$
	}

	@Test
	public void testReorientRelationshipCommand_sourceNewReadOnly() {
		final Dependency dep = classA.getClientDependency("A-->D"); //$NON-NLS-1$

		ICommand command = getEditCommand(dep, new ReorientRelationshipRequest(dep, classB, classA, ReorientRequest.REORIENT_SOURCE));
		assertExecutability(command);
	}

	@Test
	public void testReorientRelationshipCommand_sourceOldReadOnly() {
		final Dependency dep = classA.getClientDependency("A-->D"); //$NON-NLS-1$

		// By-pass edit-helpers to set up the relationship
		executeUnprotected(new RecordingCommand(domain) {

			@Override
			protected void doExecute() {
				dep.getClients().clear();
				dep.getClients().add(classB);
			}
		});

		ICommand command = getEditCommand(dep, new ReorientRelationshipRequest(dep, classA, classB, ReorientRequest.REORIENT_SOURCE));
		assertExecutability(command);
	}

	@Test
	@Conditional(key = "adviceEnabled")
	public void testReorientReferenceCommand_targetOldReadonly() {
		final Comment[] comment = { null };

		// By-pass edit-helpers to set up the reference
		executeUnprotected(new RecordingCommand(domain) {

			@Override
			protected void doExecute() {
				comment[0] = writablePackage.createOwnedComment();
				comment[0].getAnnotatedElements().add(classB);
			}
		});

		// We don't have any edit helpers suitable for this use case.
		// Reorienting the target is always permitted because there is no inverse reference
		assertThat(new ReadOnlyObjectEditAdvice().getBeforeEditCommand(new ReorientReferenceRelationshipRequest(comment[0], classA, classB, ReorientRequest.REORIENT_TARGET)), nullValue());
	}

	@Test
	@Conditional(key = "adviceEnabled")
	public void testReorientReferenceCommand_targetNewReadonly() {
		final Comment[] comment = { null };

		// By-pass edit-helpers to set up the reference
		executeUnprotected(new RecordingCommand(domain) {

			@Override
			protected void doExecute() {
				comment[0] = writablePackage.createOwnedComment();
				comment[0].getAnnotatedElements().add(classA);
			}
		});

		// We don't have any edit helpers suitable for this use case.
		// Reorienting the target is always permitted because there is no inverse reference
		assertThat(new ReadOnlyObjectEditAdvice().getBeforeEditCommand(new ReorientReferenceRelationshipRequest(comment[0], classB, classA, ReorientRequest.REORIENT_TARGET)), nullValue());
	}

	@Test
	@Conditional(key = "adviceEnabled")
	public void testReorientReferenceCommand_sourceOldReadOnly() {
		final Comment[] comment = { null, null };

		// By-pass edit-helpers to set up the reference
		executeUnprotected(new RecordingCommand(domain) {

			@Override
			protected void doExecute() {
				comment[0] = readOnlyPackage.createOwnedComment();
				comment[1] = writablePackage.createOwnedComment();
				comment[0].getAnnotatedElements().add(classA);
			}
		});

		// We don't have any edit helpers suitable for this use case
		assertAdvice(new ReorientReferenceRelationshipRequest(comment[0], comment[1], comment[0], ReorientRequest.REORIENT_SOURCE));
	}

	@Test
	@Conditional(key = "adviceEnabled")
	public void testReorientReferenceCommand_sourceNewReadonly() {
		final Comment[] comment = { null, null };

		// By-pass edit-helpers to set up the reference
		executeUnprotected(new RecordingCommand(domain) {

			@Override
			protected void doExecute() {
				comment[0] = writablePackage.createOwnedComment();
				comment[1] = readOnlyPackage.createOwnedComment();
				comment[0].getAnnotatedElements().add(classA);
			}
		});

		// We don't have any edit helpers suitable for this use case
		assertAdvice(new ReorientReferenceRelationshipRequest(comment[0], comment[1], comment[0], ReorientRequest.REORIENT_SOURCE));
	}

	//
	// Test framework
	//

	@Parameters(name = "model is {0}")
	public static Collection<Object[]> parameters() {
		return ImmutableList.copyOf(Iterables.transform(Arrays.asList(ResourceMode.values()), new Function<Object, Object[]>() {

			public Object[] apply(Object input) {
				return new Object[]{ input };
			}
		}));
	}

	@Condition
	public boolean adviceEnabled() {
		return resourceMode.isAdviceEnabled();
	}

	@BeforeClass
	public static void registerReadOnlyHandlerAdapterFactory() throws Exception {
		// Cannot add dependency on read-only plug-in because that would induce a cycle
		readOnlyHandlerAdapterFactory = (IAdapterFactory)Platform.getBundle("org.eclipse.papyrus.infra.emf.readonly").loadClass("org.eclipse.papyrus.infra.emf.readonly.ReadOnlyAdapterFactory").newInstance();
		Platform.getAdapterManager().registerAdapters(readOnlyHandlerAdapterFactory, EditingDomain.class);
	}

	@AfterClass
	public static void deregisterReadOnlyHandlerAdapterFactory() {
		Platform.getAdapterManager().unregisterAdapters(readOnlyHandlerAdapterFactory, EditingDomain.class);
		readOnlyHandlerAdapterFactory = null;
	}

	@Before
	public void createFixture() throws Exception {
		project = houseKeeper.createProject(String.format("%s_%d", getClass().getSimpleName(), resourceMode.ordinal())); //$NON-NLS-1$

		IFile writableFile = project.getFile("writable.uml"); //$NON-NLS-1$
		writablePackage = loadPackage(URI.createPlatformResourceURI(writableFile.getFullPath().toString(), true));

		URI readOnlyURI;

		switch(resourceMode) {
		case WORKSPACE_WRITEABLE:
		case WORKSPACE_READONLY:
			workspaceFile = project.getFile("readonly.uml"); //$NON-NLS-1$
			readOnlyURI = URI.createPlatformResourceURI(workspaceFile.getFullPath().toString(), true);
			break;
		default: // plug-in resource
			readOnlyURI = URI.createPlatformPluginURI(String.format("%s/resources/readonly.uml", TEST_BUNDLE), true); //$NON-NLS-1$
			break;
		}

		readOnlyPackage = loadPackage(readOnlyURI);

		switch(resourceMode) {
		case WORKSPACE_WRITEABLE:
			// File needs to exist to determine that it is writable
			readOnlyPackage.eResource().save(null);
			break;
		case WORKSPACE_READONLY:
			// Must first create the file, then make it read-only
			readOnlyPackage.eResource().save(null);
			break;
		default:
			// Nothing to do
			break;
		}

		// Refresh to pick up and read-only/persisted state changes in test resources
		project.refreshLocal(IResource.DEPTH_INFINITE, null);

		// Pick out some model elements
		classA = (Class)writablePackage.getOwnedType("A"); //$NON-NLS-1$
		classB = (Class)readOnlyPackage.getOwnedType("B"); //$NON-NLS-1$
	}

	private Package loadPackage(URI uri) throws IOException {
		Package result = null;

		if(domain == null) {
			// Use an editing domain that doesn't implement its own read-only checking in order not
			// to interfere with the advice's read-only check
			domain = houseKeeper.cleanUpLater(WorkspaceEditingDomainFactory.INSTANCE.createEditingDomain(new ResourceSetImpl()), new HouseKeeper.Disposer<TransactionalEditingDomain>() {

				public void dispose(TransactionalEditingDomain object) throws Exception {
					// Make sure that we flush undo contexts so that our commands and the resources they retain may be reclaimed
					ResourceSet rset = object.getResourceSet();
					IOperationHistory history = ((IWorkspaceCommandStack)object.getCommandStack()).getOperationHistory();

					for(Resource next : rset.getResources()) {
						// Purge the resource undo context
						IUndoContext resourceContext = new ResourceUndoContext(object, next);
						history.dispose(resourceContext, true, true, true);
					}

					object.dispose();
					EMFHelper.unload(rset);
				}
			});

			// Disable the EMF default read-only tracking
			((AdapterFactoryEditingDomain)domain).setResourceToReadOnlyMap(null);
		}

		Resource res = domain.getResourceSet().createResource(uri);
		URL url = Platform.getBundle(TEST_BUNDLE).getEntry(String.format("resources/%s", uri.lastSegment())); //$NON-NLS-1$

		InputStream input = null;
		try {
			input = url.openStream();
			res.load(input, null);
			result = (Package)EcoreUtil.getObjectByType(res.getContents(), UMLPackage.Literals.PACKAGE);
		} finally {
			if(input != null) {
				try {
					input.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

		return result;
	}

	@After
	public void destroyFixture() {
		// Purge the default undo context
		if(domain.getCommandStack() instanceof IWorkspaceCommandStack) {
			IWorkspaceCommandStack stack = (IWorkspaceCommandStack)domain.getCommandStack();
			stack.getOperationHistory().dispose(stack.getDefaultUndoContext(), true, true, true);
		}
	}

	IClientContext getClientContext() {
		switch(resourceMode) {
		case PLUGIN_NOADVICE:
			return ClientContextManager.getDefaultClientContext();
		default:
			return PAPYRUS_CONTEXT;
		}
	}

	IEditHelper getEditHelper(EObject object) {
		// Use the Papyrus context to get an UML-aware edit helper
		return ElementTypeRegistry.getInstance().getElementType(object, PAPYRUS_CONTEXT).getEditHelper();
	}

	<R extends IEditCommandRequest> R prepareRequest(R request) {
		request.setClientContext(getClientContext());
		return request;
	}

	ICommand getEditCommand(EObject subject, IEditCommandRequest request) {
		return getEditHelper(subject).getEditCommand(prepareRequest(request));
	}

	IElementType getElementType(EClass eclass) {
		// Use the Papyrus context to get an UML-aware edit helper
		return getElementType(eclass, PAPYRUS_CONTEXT);
	}

	IElementType getElementType(EClass eclass, IClientContext context) {
		return ElementTypeRegistry.getInstance().getElementType(eclass, context);
	}

	void assertExecutability(ICommand command) {
		switch(resourceMode) {
		case WORKSPACE_WRITEABLE:
		case WORKSPACE_READONLY:
		case PLUGIN_NOADVICE:
			// The edit-helper can return null if there is no executable command to be provided
			assertThat("Command should be executable", (command != null) && command.canExecute(), is(true)); //$NON-NLS-1$
			break;
		default:
			// The edit-helper can return null if there is no executable command to be provided
			assertThat("Command should not be executable", (command != null) && command.canExecute(), is(false)); //$NON-NLS-1$
			break;
		}
	}

	void assertAdvice(IEditCommandRequest request) {
		ICommand command = new ReadOnlyObjectEditAdvice().getBeforeEditCommand(request);

		switch(resourceMode) {
		case WORKSPACE_WRITEABLE:
		case WORKSPACE_READONLY:
		case PLUGIN_NOADVICE:
			// The advice can return null if there is no need to decorate the operation
			assertThat("Advice should be executable", (command == null) || command.canExecute(), is(true)); //$NON-NLS-1$
			break;
		default:
			// The advice can return null if there is no need to decorate the operation
			assertThat("Advice should not be executable", (command == null) || command.canExecute(), is(false)); //$NON-NLS-1$
			break;
		}
	}

	void executeUnprotected(Command command) {
		try {
			((TransactionalCommandStack)domain.getCommandStack()).execute(command, Collections.singletonMap(Transaction.OPTION_UNPROTECTED, true));
		} catch (InterruptedException e) {
			fail("Unprotected command execution interrupted.");
		} catch (RollbackException e) {
			e.printStackTrace();
			fail("Unprotected command execution rolled back: " + e.getLocalizedMessage());
		}
	}

	enum ResourceMode {
		WORKSPACE_WRITEABLE("writable in workspace"), //$NON-NLS-1$
		WORKSPACE_READONLY("read-only in workspace"), //$NON-NLS-1$
		PLUGIN_NOADVICE("deployed in plug-in, advice disabled"), //$NON-NLS-1$
		PLUGIN("deployed in plug-in"); //$NON-NLS-1$

		private final String displayName;

		ResourceMode(String displayName) {
			this.displayName = displayName;
		}

		boolean isAdviceEnabled() {
			return this != PLUGIN_NOADVICE;
		}

		@Override
		public String toString() {
			return displayName;
		}
	}
}

Back to the top