Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c64b9923fb77daefb8a48f290e90a047d31418c4 (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
/*
 * 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.core.resource;

import static org.hamcrest.CoreMatchers.both;
import static org.hamcrest.CoreMatchers.hasItem;
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.CoreMatchers.sameInstance;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.fail;

import java.net.URL;
import java.util.concurrent.Callable;

import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.emf.common.command.Command;
import org.eclipse.emf.common.command.CommandStack;
import org.eclipse.emf.common.notify.AdapterFactory;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EAttribute;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.ENamedElement;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.EcoreFactory;
import org.eclipse.emf.ecore.EcorePackage;
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.edit.provider.ComposedAdapterFactory;
import org.eclipse.emf.transaction.RecordingCommand;
import org.eclipse.emf.transaction.TransactionalCommandStack;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.emf.transaction.impl.TransactionalEditingDomainImpl;
import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.google.common.base.Objects;


/**
 * Test suite for the {@link NestingTransactionalCommandStack} class.
 */
public class NestingTransactionalCommandStackTest {

	// No API signatures but the most basic are required for nesting
	private CommandStack fixture;

	private TransactionalEditingDomain domain;

	private ResourceSet rset;

	private EPackage testPackage;

	private EClass foo;

	public NestingTransactionalCommandStackTest() {
		super();
	}

	//
	// Test cases
	//

	@Test
	public void testCompleteUnnestedCommand() {
		Callable<EAttribute> createAttribute = createAttribute();

		int oldFeatureCount = foo.getEStructuralFeatures().size();

		// Create two attributes
		EAttribute attr1 = execute(createAttribute);
		EAttribute attr2 = execute(createAttribute);
		assertThat(foo.getEStructuralFeatures().size(), is(oldFeatureCount + 2));
		assertThat(foo.getEStructuralFeatures(), hasItem(attr1));
		assertThat(foo.getEStructuralFeatures(), hasItem(attr2));

		// Undo only undoes one of them
		Command undone = undo();
		assertThat(foo.getEStructuralFeatures().size(), is(oldFeatureCount + 1));
		assertThat(foo.getEStructuralFeatures(), hasItem(attr1));
		assertThat(foo.getEStructuralFeatures(), not(hasItem(attr2)));

		// Redo is sane
		Command redone = redo();
		assertThat(redone, sameInstance(undone));
		assertThat(foo.getEStructuralFeatures().size(), is(oldFeatureCount + 2));
		assertThat(foo.getEStructuralFeatures(), hasItem(attr1));
		assertThat(foo.getEStructuralFeatures(), hasItem(attr2));
	}

	@Test
	public void testCancelUnnestedCommand() {
		Callable<EAttribute> createAttribute = createAttribute(2);

		int oldFeatureCount = foo.getEStructuralFeatures().size();

		// Try to create two attributes (second one cancels)
		EAttribute attr1 = execute(createAttribute);
		EAttribute attr2 = execute(createAttribute);
		assertThat(attr2, nullValue());
		assertThat(foo.getEStructuralFeatures().size(), is(oldFeatureCount + 1));
		assertThat(foo.getEStructuralFeatures(), hasItem(attr1));
		assertThat(foo.getEStructuralFeature("attribute2"), nullValue()); //$NON-NLS-1$

		// Undo is sane
		Command undone = undo();
		assertThat(foo.getEStructuralFeatures().size(), is(oldFeatureCount));
		assertThat(foo.getEStructuralFeatures(), not(hasItem(attr1)));
		assertThat(foo.getEStructuralFeature("attribute2"), nullValue()); //$NON-NLS-1$

		// Redo is sane
		Command redone = redo();
		assertThat(redone, sameInstance(undone));
		assertThat(foo.getEStructuralFeatures().size(), is(oldFeatureCount + 1));
		assertThat(foo.getEStructuralFeatures(), hasItem(attr1));
		assertThat(foo.getEStructuralFeature("attribute2"), nullValue()); //$NON-NLS-1$
	}

	@Test
	public void testCompleteNestedCommand() {
		final int FEATURES = 2;
		Callable<EClass> createClass = createClass(FEATURES);

		int oldClassifierCount = testPackage.getEClassifiers().size();

		EClass clas = execute(createClass);
		assertThat(testPackage.getEClassifiers().size(), is(oldClassifierCount + 1));
		assertThat(testPackage.getEClassifiers(), hasItem(clas));
		assertThat(clas.getEStructuralFeatures().size(), is(FEATURES));
		assertThat(clas.getEStructuralFeature("attribute1"), notNullValue()); //$NON-NLS-1$
		assertThat(clas.getEStructuralFeature("attribute2"), notNullValue()); //$NON-NLS-1$

		// Undo is sane and there is only one command to undo
		Command undone = undo();
		assertThat(fixture.canUndo(), is(false));
		assertThat(testPackage.getEClassifiers().size(), is(oldClassifierCount));
		assertThat(testPackage.getEClassifiers(), not(hasItem(clas)));

		// Redo is sane
		Command redone = redo();
		assertThat(redone, sameInstance(undone));
		assertThat(testPackage.getEClassifiers().size(), is(oldClassifierCount + 1));
		assertThat(testPackage.getEClassifiers(), hasItem(clas));
		assertThat(clas.getEStructuralFeatures().size(), is(FEATURES));
		assertThat(clas.getEStructuralFeature("attribute1"), notNullValue()); //$NON-NLS-1$
		assertThat(clas.getEStructuralFeature("attribute2"), notNullValue()); //$NON-NLS-1$
	}

	@Test
	public void testCancelNestedCommand() {
		final int FEATURES = 2;
		Callable<EClass> createClass = createClass(FEATURES, 2, FEATURES);

		int oldClassifierCount = testPackage.getEClassifiers().size();

		// Try to create two classes (second one cancels)
		EClass class1 = execute(createClass);
		EClass class2 = execute(createClass);
		assertThat(class2, nullValue());
		assertThat(testPackage.getEClassifiers().size(), is(oldClassifierCount + 1));
		assertThat(testPackage.getEClassifiers(), hasItem(class1));
		assertThat(class1.getEStructuralFeatures().size(), is(FEATURES));
		assertThat(class1.getEStructuralFeature("attribute1"), notNullValue()); //$NON-NLS-1$
		assertThat(class1.getEStructuralFeature("attribute2"), notNullValue()); //$NON-NLS-1$

		// Undo is sane and there is only one command to undo
		Command undone = undo();
		assertThat(fixture.canUndo(), is(false));
		assertThat(testPackage.getEClassifiers().size(), is(oldClassifierCount));
		assertThat(testPackage.getEClassifiers(), not(hasItem(class1)));

		// Redo is sane
		Command redone = redo();
		assertThat(redone, sameInstance(undone));
		assertThat(testPackage.getEClassifiers().size(), is(oldClassifierCount + 1));
		assertThat(testPackage.getEClassifiers(), hasItem(class1));
		assertThat(class1.getEStructuralFeatures().size(), is(FEATURES));
		assertThat(class1.getEStructuralFeature("attribute1"), notNullValue()); //$NON-NLS-1$
		assertThat(class1.getEStructuralFeature("attribute2"), notNullValue()); //$NON-NLS-1$
	}

	@Test
	public void testCompleteTripleNestedCommand() {
		final int CLASSES = 2;
		final int FEATURES = 2;

		Callable<EPackage> createPackage = createPackage(CLASSES, FEATURES);

		int oldPackageCount = testPackage.getESubpackages().size();

		EPackage pkg = execute(createPackage);
		assertThat(testPackage.getESubpackages().size(), is(oldPackageCount + 1));
		assertThat(testPackage.getESubpackages(), hasItem(pkg));
		assertThat(pkg.getEClassifiers().size(), is(CLASSES));
		assertThat(pkg.getEClassifiers(), hasItem(both(featureCount(FEATURES)).and(named("Class1")).and(hasFeature("attribute1")).and(hasFeature("attribute2")))); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
		assertThat(pkg.getEClassifiers(), hasItem(both(featureCount(FEATURES)).and(named("Class2")).and(hasFeature("attribute1")).and(hasFeature("attribute2")))); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$

		// Undo is sane and there is only one command to undo
		Command undone = undo();
		assertThat(fixture.canUndo(), is(false));
		assertThat(testPackage.getESubpackages().size(), is(oldPackageCount));
		assertThat(testPackage.getESubpackages(), not(hasItem(pkg)));
		assertThat(pkg.getName(), nullValue());
		assertThat(pkg.getEClassifiers().size(), is(0));
		assertThat(pkg.getEClassifiers().size(), is(0));

		// Redo is sane
		Command redone = redo();
		assertThat(redone, sameInstance(undone));
		assertThat(fixture.canRedo(), is(false));
		assertThat(testPackage.getESubpackages().size(), is(oldPackageCount + 1));
		assertThat(testPackage.getESubpackages(), hasItem(pkg));
		assertThat(pkg, named("package1")); //$NON-NLS-1$
		assertThat(pkg.getEClassifiers().size(), is(CLASSES));
		assertThat(pkg.getEClassifiers(), hasItem(both(featureCount(FEATURES)).and(named("Class1")).and(hasFeature("attribute1")).and(hasFeature("attribute2")))); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
		assertThat(pkg.getEClassifiers(), hasItem(both(featureCount(FEATURES)).and(named("Class2")).and(hasFeature("attribute1")).and(hasFeature("attribute2")))); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
	}

	@Test
	public void testCancelTripleNestedCommand() {
		final int CLASSES = 2;
		final int FEATURES = 2;

		Callable<EPackage> createPackage = createPackage(CLASSES, FEATURES, 2, CLASSES, FEATURES);

		int oldPackageCount = testPackage.getESubpackages().size();

		// Try to create two packages (second one cancels)
		EPackage pkg1 = execute(createPackage);
		EPackage pkg2 = execute(createPackage);

		// the first package is complete
		assertThat(testPackage.getESubpackages().size(), is(oldPackageCount + 1));
		assertThat(testPackage.getESubpackages(), hasItem(pkg1));
		assertThat(pkg1, named("package1")); //$NON-NLS-1$
		assertThat(pkg1.getEClassifiers().size(), is(CLASSES));
		assertThat(pkg1.getEClassifiers(), hasItem(both(featureCount(FEATURES)).and(named("Class1")).and(hasFeature("attribute1")).and(hasFeature("attribute2")))); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
		assertThat(pkg1.getEClassifiers(), hasItem(both(featureCount(FEATURES)).and(named("Class2")).and(hasFeature("attribute1")).and(hasFeature("attribute2")))); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$

		// there is no second package
		assertThat(pkg2, nullValue());

		// Undo is sane and there is only one command to undo
		Command undone = undo();
		assertThat(fixture.canUndo(), is(false));
		assertThat(testPackage.getESubpackages().size(), is(oldPackageCount));
		assertThat(testPackage.getESubpackages(), not(hasItem(pkg1)));
		assertThat(pkg1.getName(), nullValue());
		assertThat(pkg1.getEClassifiers().size(), is(0));

		// Redo is sane
		Command redone = redo();
		assertThat(fixture.canRedo(), is(false));
		assertThat(redone, sameInstance(undone));
		assertThat(testPackage.getESubpackages().size(), is(oldPackageCount + 1));
		assertThat(testPackage.getESubpackages(), hasItem(pkg1));
		assertThat(pkg1, named("package1")); //$NON-NLS-1$
		assertThat(pkg1.getEClassifiers().size(), is(CLASSES));
		assertThat(pkg1.getEClassifiers(), hasItem(both(featureCount(FEATURES)).and(named("Class1")).and(hasFeature("attribute1")).and(hasFeature("attribute2")))); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
		assertThat(pkg1.getEClassifiers(), hasItem(both(featureCount(FEATURES)).and(named("Class2")).and(hasFeature("attribute1")).and(hasFeature("attribute2")))); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
	}

	//
	// Test framework
	//

	@Before
	public void createFixture() {
		rset = new ResourceSetImpl();
		fixture = new NestingTransactionalCommandStack();
		AdapterFactory adapterFactory = new ComposedAdapterFactory(ComposedAdapterFactory.Descriptor.Registry.INSTANCE);
		domain = new TransactionalEditingDomainImpl(adapterFactory, (TransactionalCommandStack)fixture, rset);

		URL testModelURL = getClass().getResource("bug402525.ecore"); //$NON-NLS-1$
		Resource testModel = rset.getResource(URI.createURI(testModelURL.toExternalForm(), true), true);
		testPackage = (EPackage)testModel.getContents().get(0);
		foo = (EClass)testPackage.getEClassifier("Foo"); //$NON-NLS-1$
	}

	@After
	public void destroyFixture() {
		// This disposes the command stack for us
		domain.dispose();
		domain = null;
		fixture = null;

		dispose(rset);
		rset = null;
	}

	void dispose(ResourceSet rset) {
		for(Resource next : rset.getResources()) {
			next.unload();
			next.eAdapters().clear();
		}

		rset.getResources().clear();
		rset.eAdapters().clear();
	}

	<V> V execute(final Callable<V> operation) {
		class TestCommand extends RecordingCommand {

			V result;

			TestCommand() {
				super(domain);
			}

			@Override
			protected void doExecute() {
				try {
					result = operation.call();
				} catch (OperationCanceledException e) {
					// Pass it on
					throw e;
				} catch (Exception e) {
					e.printStackTrace();
					fail("Uncaught exception in operation: " + e.getLocalizedMessage()); //$NON-NLS-1$
				}
			}
		}

		TestCommand command = new TestCommand();
		fixture.execute(command);
		return command.result;
	}

	Command undo() {
		assertThat("Cannot undo", fixture.canUndo()); //$NON-NLS-1$
		Command result = fixture.getUndoCommand();
		fixture.undo();
		return result;
	}

	Command redo() {
		assertThat("Cannot redo", fixture.canRedo()); //$NON-NLS-1$
		Command result = fixture.getRedoCommand();
		fixture.redo();
		return result;
	}

	Callable<EAttribute> createAttribute() {
		return createAttribute(0);
	}

	Callable<EAttribute> createAttribute(int cancelOn) {
		return createAttribute(foo, cancelOn);
	}

	Callable<EAttribute> createAttribute(final EClass owner, final int cancelOn) {
		return new Callable<EAttribute>() {

			int i = 0;

			public EAttribute call() throws Exception {
				String name = nextName();

				EAttribute attr = EcoreFactory.eINSTANCE.createEAttribute();
				owner.getEStructuralFeatures().add(attr);
				attr.setName(name);
				attr.setEType(EcorePackage.Literals.ESTRING);

				checkCancel();

				return attr;
			}

			private String nextName() {
				i = i + 1;
				return "attribute" + i; //$NON-NLS-1$
			}

			private void checkCancel() {
				if(i == cancelOn) {
					throw new OperationCanceledException();
				}
			}
		};
	}

	Callable<EClass> createClass(int attributes) {
		return createClass(attributes, 0, 0);
	}

	Callable<EClass> createClass(int attributes, int cancelOn, int cancelAttributesOn) {
		return createClass(testPackage, attributes, cancelOn, cancelAttributesOn);
	}

	Callable<EClass> createClass(final EPackage owner, final int attributes, final int cancelOn, final int cancelAttributesOn) {
		return new Callable<EClass>() {

			int i = 0;

			public EClass call() throws Exception {
				String name = nextName();

				EClass clas = EcoreFactory.eINSTANCE.createEClass();
				owner.getEClassifiers().add(clas);
				clas.setName(name);

				Callable<?> createAttribute = createAttribute(clas, checkCancelAttributes());
				for(int i = 0; i < attributes; i++) {
					// Nested command
					execute(createAttribute);
				}

				return clas;
			}

			private String nextName() {
				i = i + 1;
				return "Class" + i; //$NON-NLS-1$
			}

			private int checkCancelAttributes() {
				return (i == cancelOn) ? cancelAttributesOn : 0;
			}
		};
	}

	Callable<EPackage> createPackage(int classes, int attributes) {
		return createPackage(classes, attributes, 0, 0, 0);
	}

	Callable<EPackage> createPackage(final int classes, final int attributes, final int cancelOn, final int cancelClassesOn, final int cancelAttributesOn) {
		return new Callable<EPackage>() {

			int i = 0;

			public EPackage call() throws Exception {
				String name = nextName();

				EPackage pkg = EcoreFactory.eINSTANCE.createEPackage();
				testPackage.getESubpackages().add(pkg);
				pkg.setName(name);

				Callable<?> createClass = createClass(pkg, attributes, checkCancelClasses(), cancelAttributesOn);
				for(int i = 0; i < classes; i++) {
					// Nested command
					execute(createClass);
				}

				return pkg;
			}

			private String nextName() {
				i = i + 1;
				return "package" + i; //$NON-NLS-1$
			}

			private int checkCancelClasses() {
				return (i == cancelOn) ? cancelClassesOn : 0;
			}
		};
	}

	Matcher<ENamedElement> named(final String name) {
		return new BaseMatcher<ENamedElement>() {

			public void describeTo(Description desc) {
				desc.appendText("is named \"").appendValue(name).appendText("\""); //$NON-NLS-1$ //$NON-NLS-2$
			}

			public boolean matches(Object o) {
				return (o instanceof ENamedElement) && Objects.equal(((ENamedElement)o).getName(), name);
			}
		};
	}

	Matcher<EClass> featureCount(final int count) {
		return new BaseMatcher<EClass>() {

			public void describeTo(Description desc) {
				desc.appendText("has ").appendValue(count).appendText(" features"); //$NON-NLS-1$ //$NON-NLS-2$
			}

			public boolean matches(Object o) {
				return (o instanceof EClass) && (((EClass)o).getEStructuralFeatures().size() == count);
			}
		};
	}

	Matcher<EClass> hasFeature(final String name) {
		return new BaseMatcher<EClass>() {

			public void describeTo(Description desc) {
				desc.appendText("has a \"").appendValue(name).appendText("\" feature"); //$NON-NLS-1$ //$NON-NLS-2$
			}

			public boolean matches(Object o) {
				return (o instanceof EClass) && (((EClass)o).getEStructuralFeature(name) != null);
			}
		};
	}
}

Back to the top