Debian Codegraph @

tests/org.eclipse.emf.test.databinding.edit/src/org/eclipse/emf/test/databinding/edit/EMFEditPropertiesTest.java

raw symbols SCIP
1 /**
2 * Copyright (c) 2009 BestSolution and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v10.html
7 *
8 * Contributors:
9 * Tom Schindl - Initial API and implementation
10 */
11 package org.eclipse.emf.test.databinding.edit;
12
13
14 import static org.junit.Assert.assertEquals;
15 import static org.junit.Assert.assertFalse;
16 import static org.junit.Assert.assertNotNull;
17 import static org.junit.Assert.assertNull;
18 import static org.junit.Assert.assertSame;
19 import static org.junit.Assert.assertTrue;
20
21 import java.util.Arrays;
22 import java.util.List;
23
24 import org.eclipse.core.databinding.observable.Realm;
25 import org.eclipse.core.databinding.observable.list.IListChangeListener;
26 import org.eclipse.core.databinding.observable.list.IObservableList;
27 import org.eclipse.core.databinding.observable.list.ListChangeEvent;
28 import org.eclipse.core.databinding.observable.list.ListDiff;
29 import org.eclipse.core.databinding.observable.list.ListDiffEntry;
30 import org.eclipse.core.databinding.observable.set.IObservableSet;
31 import org.eclipse.core.databinding.observable.set.ISetChangeListener;
32 import org.eclipse.core.databinding.observable.set.SetChangeEvent;
33 import org.eclipse.core.databinding.observable.set.SetDiff;
34 import org.eclipse.core.databinding.observable.value.IObservableValue;
35 import org.eclipse.emf.common.command.BasicCommandStack;
36 import org.eclipse.emf.common.command.CompoundCommand;
37 import org.eclipse.emf.common.util.BasicEList;
38 import org.eclipse.emf.common.util.EList;
39 import org.eclipse.emf.common.util.URI;
40 import org.eclipse.emf.databinding.IEMFListProperty;
41 import org.eclipse.emf.databinding.IEMFListProperty.ListElementAccess;
42 import org.eclipse.emf.databinding.edit.EMFEditProperties;
43 import org.eclipse.emf.databinding.edit.IEMFEditListProperty;
44 import org.eclipse.emf.databinding.edit.IEMFEditSetProperty;
45 import org.eclipse.emf.databinding.edit.IEMFEditValueProperty;
46 import org.eclipse.emf.ecore.resource.Resource;
47 import org.eclipse.emf.ecore.resource.ResourceSet;
48 import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
49 import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl;
50 import org.eclipse.emf.edit.command.AddCommand;
51 import org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain;
52 import org.eclipse.emf.edit.domain.EditingDomain;
53 import org.eclipse.emf.edit.provider.ComposedAdapterFactory;
54 import org.eclipse.emf.test.common.TestUtil;
55 import org.eclipse.emf.test.databinding.emfdb.A;
56 import org.eclipse.emf.test.databinding.emfdb.B;
57 import org.eclipse.emf.test.databinding.emfdb.EmfdbFactory;
58 import org.eclipse.emf.test.databinding.emfdb.EmfdbPackage;
59 import org.junit.Before;
60 import org.junit.Test;
61
62
63 public class EMFEditPropertiesTest
64 {
65 private Resource resource;
66
67 private EditingDomain editingDomain;
68
69 private Realm testRealm;
70
71 private boolean flag;
72
73 private ListDiffEntry[] listEntries;
74
75 private ListDiff listDiff;
76
77 private SetDiff diff;
78
79 private BasicCommandStack commandStack;
80
81 @Before
82 public void setUp() throws Exception
83 {
84 ComposedAdapterFactory adapterFactory = new ComposedAdapterFactory(ComposedAdapterFactory.Descriptor.Registry.INSTANCE);
85 ResourceSet resourceSet = new ResourceSetImpl();
86 commandStack = new BasicCommandStack();
87
88 resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put(Resource.Factory.Registry.DEFAULT_EXTENSION, new XMIResourceFactoryImpl());
89 editingDomain = new AdapterFactoryEditingDomain(adapterFactory, commandStack, resourceSet);
90 URI uri = URI.createFileURI(TestUtil.getPluginDirectory("org.eclipse.emf.test.databinding") + "/model/A.xmi");
91 resource = resourceSet.getResource(uri, true);
92 testRealm = new Realm()
93 {
94
95 @Override
96 public boolean isCurrent()
97 {
98 return true;
99 }
100 };
101 flag = false;
102 listEntries = null;
103 }
104
105 // public void testValueEditingDomainEStructuralFeature()
106 // {
107 // fail("Not yet implemented");
108 // }
109 //
110 // public void testValueEditingDomainFeaturePath()
111 // {
112 // fail("Not yet implemented");
113 // }
114 //
115 // public void testValuesEditingDomainEStructuralFeatureArray()
116 // {
117 // fail("Not yet implemented");
118 // }
119 //
120 // public void testValuesEditingDomainFeaturePathArray()
121 // {
122 // fail("Not yet implemented");
123 // }
124
125 @Test
126 public void testListEditingDomainEStructuralFeature()
127 {
128 Realm.runWithDefault(testRealm, new Runnable()
129 {
130 public void run()
131 {
132 _testListEditingDomainEStructuralFeature();
133 }
134 });
135 }
136
137 public void _testListEditingDomainEStructuralFeature()
138 {
139 A a = (A)resource.getContents().get(0);
140 IEMFEditListProperty prop = EMFEditProperties.list(editingDomain, EmfdbPackage.Literals.A__BLIST);
141 IObservableList list = prop.observe(a);
142 list.addListChangeListener(new IListChangeListener()
143 {
144 public void handleListChange(ListChangeEvent event)
145 {
146 flag = true;
147 listEntries = event.diff.getDifferences();
148 }
149 });
150 assertEquals(a.getBlist().size(), list.size());
151 B b = EmfdbFactory.eINSTANCE.createB();
152 list.add(b);
153 // a.getBlist().add(b);
154 assertEquals(a.getBlist().size(), list.size());
155 assertTrue(flag);
156 assertNotNull(listEntries);
157 assertEquals(1, listEntries.length);
158 assertTrue(listEntries[0].isAddition());
159 assertSame(b, listEntries[0].getElement());
160 assertEquals(a.getBlist().size() - 1, listEntries[0].getPosition());
161 assertEquals(list.get(0), a.getBlist().get(0));
162 }
163
164 @Test
165 public void testSetProperty()
166 {
167 Realm.runWithDefault(testRealm, new Runnable()
168 {
169
170 public void run()
171 {
172 _testSetProperty();
173 }
174 });
175 }
176
177 public void _testSetProperty()
178 {
179 A a = (A)resource.getContents().get(0);
180 IEMFEditSetProperty prop = EMFEditProperties.set(editingDomain, EmfdbPackage.Literals.A__BLIST);
181 IObservableSet set = prop.observe(a);
182 assertNotNull(set);
183 set.addSetChangeListener(new ISetChangeListener()
184 {
185 public void handleSetChange(SetChangeEvent event)
186 {
187 diff = event.diff;
188 }
189 });
190 assertNull(diff);
191 assertFalse(commandStack.canUndo());
192 B b = EmfdbFactory.eINSTANCE.createB();
193 // a.getBlist().add(b);
194 set.add(b);
195 assertNotNull(diff);
196 assertEquals(1, diff.getAdditions().size());
197 assertSame(b, diff.getAdditions().iterator().next());
198 assertTrue(commandStack.canUndo());
199 assertFalse(commandStack.canRedo());
200 diff = null;
201 commandStack.undo();
202 assertNotNull(diff);
203 assertEquals(1, diff.getRemovals().size());
204 assertSame(b, diff.getRemovals().iterator().next());
205 assertFalse(commandStack.canUndo());
206 assertTrue(commandStack.canRedo());
207 }
208
209 // public void testListEditingDomainFeaturePath()
210 // {
211 // fail("Not yet implemented");
212 // }
213 //
214 // public void testMultiListEditingDomainEStructuralFeatureArray()
215 // {
216 // fail("Not yet implemented");
217 // }
218 @Test
219 public void testMultiListEditingDomainEStructuralFeatureArray()
220 {
221 Realm.runWithDefault(testRealm, new Runnable()
222 {
223 public void run()
224 {
225 A a = (A)resource.getContents().get(0);
226 IEMFEditListProperty prop = EMFEditProperties.multiList(editingDomain, EmfdbPackage.Literals.A__STRING, EmfdbPackage.Literals.A__BLIST);
227 IObservableList list = prop.observe(a);
228 list.addListChangeListener(new IListChangeListener()
229 {
230
231 public void handleListChange(ListChangeEvent event)
232 {
233 flag = true;
234 listEntries = event.diff.getDifferences();
235 }
236 });
237 assertEquals(a.getString(), list.get(0));
238 assertEquals(a.getBlist().size() + 1, list.size());
239 B b = EmfdbFactory.eINSTANCE.createB();
240 a.getBlist().add(b);
241 assertEquals(a.getBlist().size() + 1, list.size());
242 assertTrue(flag);
243 assertNotNull(listEntries);
244 assertEquals(1, listEntries.length);
245 assertTrue(listEntries[0].isAddition());
246 assertSame(b, listEntries[0].getElement());
247 assertEquals(a.getBlist().size(), listEntries[0].getPosition());
248 assertEquals(list.get(1), a.getBlist().get(0));
249 a.setString(null);
250 assertEquals(a.getBlist().size(), list.size());
251 assertEquals(list.get(0), a.getBlist().get(0));
252 }
253 });
254 }
255
256 @Test
257 public void testResourceProperty()
258 {
259 Realm.runWithDefault(testRealm, new Runnable()
260 {
261
262 public void run()
263 {
264 _testResourceProperty();
265 }
266 });
267 }
268
269 private void _testResourceProperty()
270 {
271 IEMFEditListProperty prop = EMFEditProperties.resource(editingDomain);
272 IObservableList list = prop.observe(resource);
273 list.addListChangeListener(new IListChangeListener()
274 {
275
276 public void handleListChange(ListChangeEvent event)
277 {
278 listDiff = event.diff;
279 }
280 });
281 assertNull(listDiff);
282
283 // Adding
284 A a = EmfdbFactory.eINSTANCE.createA();
285 A a1 = EmfdbFactory.eINSTANCE.createA();
286
287 resource.getContents().add(a);
288 assertNotNull(listDiff);
289 assertEquals(1, listDiff.getDifferences().length);
290 assertSame(a, listDiff.getDifferences()[0].getElement());
291 assertEquals(1, listDiff.getDifferences()[0].getPosition());
292 assertTrue(listDiff.getDifferences()[0].isAddition());
293
294 // Moving
295 resource.getContents().move(0, a);
296 assertEquals(2, listDiff.getDifferences().length);
297 assertFalse(listDiff.getDifferences()[0].isAddition()); // Removal
298 assertEquals(1, listDiff.getDifferences()[0].getPosition());
299 assertTrue(listDiff.getDifferences()[1].isAddition()); // Addition
300 assertEquals(0, listDiff.getDifferences()[1].getPosition());
301
302 // Removing
303 resource.getContents().remove(a);
304 assertEquals(1, listDiff.getDifferences().length);
305 assertFalse(listDiff.getDifferences()[0].isAddition()); // Removal
306 assertEquals(0, listDiff.getDifferences()[0].getPosition());
307
308 // Adding many
309 resource.getContents().addAll(Arrays.asList(a, a1));
310 assertEquals(2, listDiff.getDifferences().length);
311
312 // Remove many
313 resource.getContents().removeAll(Arrays.asList(a, a1));
314 assertEquals(2, listDiff.getDifferences().length);
315
316 // =============================================================
317
318 // Changed through IObservableList
319 // Add
320 assertNull(commandStack.getUndoCommand());
321 list.add(a);
322 assertNotNull(listDiff);
323 assertEquals(1, listDiff.getDifferences().length);
324 assertSame(a, listDiff.getDifferences()[0].getElement());
325 assertEquals(1, listDiff.getDifferences()[0].getPosition());
326 assertTrue(listDiff.getDifferences()[0].isAddition());
327 assertNotNull(commandStack.getUndoCommand());
328 assertTrue(commandStack.canUndo());
329 assertTrue(commandStack.getUndoCommand() instanceof CompoundCommand);
330 CompoundCommand cmd = (CompoundCommand)commandStack.getUndoCommand();
331 assertEquals(1, cmd.getCommandList().size());
332 assertTrue(cmd.getCommandList().get(0) instanceof AddCommand);
333 commandStack.undo();
334 assertFalse(commandStack.canUndo());
335 commandStack.redo();
336 commandStack.flush();
337
338 // Moving
339 list.move(1, 0);
340 assertEquals(2, listDiff.getDifferences().length);
341 assertFalse(listDiff.getDifferences()[0].isAddition()); // Removal
342 assertEquals(1, listDiff.getDifferences()[0].getPosition());
343 assertTrue(listDiff.getDifferences()[1].isAddition()); // Addition
344 assertEquals(0, listDiff.getDifferences()[1].getPosition());
345 assertTrue(commandStack.canUndo());
346 assertTrue(commandStack.getUndoCommand() instanceof CompoundCommand);
347 commandStack.undo();
348 assertFalse(commandStack.canUndo());
349 commandStack.redo();
350 commandStack.flush();
351
352 // Removing
353 list.remove(a);
354 assertEquals(1, listDiff.getDifferences().length);
355 assertFalse(listDiff.getDifferences()[0].isAddition()); // Removal
356 assertEquals(0, listDiff.getDifferences()[0].getPosition());
357 assertTrue(commandStack.canUndo());
358 assertTrue(commandStack.getUndoCommand() instanceof CompoundCommand);
359 commandStack.undo();
360 assertFalse(commandStack.canUndo());
361 commandStack.redo();
362 commandStack.flush();
363
364 // Adding many
365 list.addAll(Arrays.asList(a, a1));
366 assertEquals(2, listDiff.getDifferences().length);
367 assertTrue(commandStack.canUndo());
368 assertTrue(commandStack.getUndoCommand() instanceof CompoundCommand);
369 commandStack.undo();
370 assertFalse(commandStack.canUndo());
371 commandStack.redo();
372 commandStack.flush();
373
374 // Remove many
375 list.removeAll(Arrays.asList(a, a1));
376 assertEquals(2, listDiff.getDifferences().length);
377 assertTrue(commandStack.canUndo());
378 assertTrue(commandStack.getUndoCommand() instanceof CompoundCommand);
379 commandStack.undo();
380 assertFalse(commandStack.canUndo());
381 commandStack.redo();
382 commandStack.flush();
383 }
384
385 @Test
386 public void testListPropertyOnSingleFeature()
387 {
388 Realm.runWithDefault(testRealm, new Runnable()
389 {
390
391 public void run()
392 {
393 _testListPropertyOnSingleFeature();
394 }
395 });
396 }
397
398 public void _testListPropertyOnSingleFeature()
399 {
400 A a = (A)resource.getContents().get(0);
401 IEMFEditListProperty prop = EMFEditProperties.list(editingDomain, EmfdbPackage.Literals.A__STRING);
402 IObservableList l = prop.observe(a);
403 assertEquals(1, l.size());
404 assertEquals("Instance 1", l.get(0));
405 a.setString("Bla");
406 assertEquals("Bla", l.get(0));
407 l.addListChangeListener(new IListChangeListener()
408 {
409
410 public void handleListChange(ListChangeEvent event)
411 {
412 assertEquals(2, event.diff.getDifferences().length);
413
414 assertEquals(0, event.diff.getDifferences()[0].getPosition());
415 assertFalse(event.diff.getDifferences()[0].isAddition());
416 assertEquals("Bla", event.diff.getDifferences()[0].getElement());
417
418 assertEquals(0, event.diff.getDifferences()[1].getPosition());
419 assertTrue(event.diff.getDifferences()[1].isAddition());
420 assertEquals("Instance 1", event.diff.getDifferences()[1].getElement());
421 }
422 });
423 a.setString("Instance 1");
424 }
425
426 @Test
427 public void testListElementProperty()
428 {
429 Realm.runWithDefault(testRealm, new Runnable()
430 {
431
432 public void run()
433 {
434 _testListElementProperty();
435 }
436 });
437 }
438
439 public void _testListElementProperty()
440 {
441 IEMFEditListProperty prop = EMFEditProperties.list(editingDomain, EmfdbPackage.Literals.A__BLIST);
442 IEMFEditValueProperty valueProp = prop.value(new ListElementAccess<Object>()
443 {
444
445 @Override
446 public int getReadValueIndex(List<Object> list)
447 {
448 return 0;
449 }
450
451 @Override
452 public int getWriteValueIndex(List<Object> list)
453 {
454 return 0;
455 }
456 });
457
458 IEMFEditValueProperty detailValue = valueProp.value(EmfdbPackage.Literals.B__STRING);
459
460 IObservableValue value = detailValue.observe(resource.getContents().get(0));
461 assertEquals("Instance 1", value.getValue());
462 A a = (A)resource.getContents().get(0);
463 a.getBlist().get(0).setString("Bla Bla");
464 assertEquals("Bla Bla", value.getValue());
465
466 B b = EmfdbFactory.eINSTANCE.createB();
467 b.setString("New Element");
468
469 a.getBlist().add(0, b);
470 assertEquals("New Element", value.getValue());
471
472 b = EmfdbFactory.eINSTANCE.createB();
473 b.setString("New Element 2");
474 IObservableValue aObservable = valueProp.observe(a);
475 try
476 {
477 aObservable.setValue(b);
478 }
479 finally
480 {
481 aObservable.dispose();
482 }
483 assertEquals("New Element 2", value.getValue());
484 }
485
486 @Test
487 public void test_attributeListWithDuplicates()
488 {
489 Realm.runWithDefault(testRealm, new Runnable()
490 {
491 public void run()
492 {
493 _test_attributeListWithDuplicates();
494 }
495 });
496 }
497
498 public void _test_attributeListWithDuplicates()
499 {
500 A a = (A)resource.getContents().get(0);
501 EList<String> strings = a.getStrings();
502
503 IEMFListProperty stringsListProperty = EMFEditProperties.list(editingDomain, EmfdbPackage.Literals.A__STRINGS);
504 IObservableList observableList = stringsListProperty.observe(a);
505 EList<String> normalList = new BasicEList<String>();
506
507 String value1 = "value1";
508 String value1Copy = new String(value1);
509 String value2 = "value2";
510 String value3 = "value3";
511
512 observableList.add(value1);
513 observableList.add(value2);
514 observableList.add(value1Copy);
515 observableList.add(3, value2);
516
517 normalList.add(value1);
518 normalList.add(value2);
519 normalList.add(value1Copy);
520 normalList.add(3, value2);
521
522 assertSameListContents(normalList, observableList);
523 assertSameListContents(normalList, strings);
524
525 observableList.set(1, value3);
526 normalList.set(1, value3);
527
528 assertSameListContents(normalList, observableList);
529 assertSameListContents(normalList, strings);
530
531 observableList.remove(2);
532 normalList.remove(2);
533
534 assertSameListContents(normalList, observableList);
535 assertSameListContents(normalList, strings);
536
537 observableList.add(value1);
538 normalList.add(value1);
539
540 assertSameListContents(normalList, observableList);
541 assertSameListContents(normalList, strings);
542
543 observableList.remove(observableList.size() - 1);
544 normalList.remove(normalList.size() - 1);
545
546 assertSameListContents(normalList, observableList);
547 assertSameListContents(normalList, strings);
548
549 // See https://bugs.eclipse.org/bugs/show_bug.cgi?id=478053 that
550 // EList.move(int, int) and
551 // IObservableList.move(int, int)
552 // have opposite meaning for source and target.
553 //
554 observableList.move(2, 0);
555 normalList.move(0, 2);
556
557 assertSameListContents(normalList, observableList);
558 assertSameListContents(normalList, strings);
559
560 observableList.add(value1);
561 normalList.add(value1);
562
563 assertSameListContents(normalList, observableList);
564 assertSameListContents(normalList, strings);
565
566 observableList.move(observableList.size() - 1, 0);
567 normalList.move(0, normalList.size() - 1);
568
569 assertSameListContents(normalList, observableList);
570 assertSameListContents(normalList, strings);
571 }
572
573 private void assertSameListContents(List<?> list1, List<?> list2)
574 {
575 String string1 = list1.toString();
576 String string2 = list2.toString();
577 assertEquals(string1, string2);
578 assertEquals(list1.size(), list2.size());
579 for (int i = 0, size = list1.size(); i < size; ++i)
580 {
581 assertSame("Same object expected at index " + i + " of " + string1, list1.get(i), list2.get(i));
582 }
583 }
584
585 @Test
586 public void testMultiValued_NotUnique_Replace_PrimitiveAttributes()
587 {
588 Realm.runWithDefault(testRealm, new Runnable()
589 {
590 public void run()
591 {
592 _testMultiValued_NotUnique_Replace_PrimitiveAttributes();
593 }
594 });
595 }
596
597 private void _testMultiValued_NotUnique_Replace_PrimitiveAttributes()
598 {
599 A a = (A)resource.getContents().get(0);
600 a.getNotUniqueValues().clear();
601 Double d20 = 20.0;
602 Double d30 = 30.0;
603 a.getNotUniqueValues().add(d20);
604 a.getNotUniqueValues().add(d30);
605 a.getNotUniqueValues().add(d20);
606 a.getNotUniqueValues().add(d30);
607 IEMFEditListProperty prop = EMFEditProperties.list(editingDomain, EmfdbPackage.Literals.A__NOT_UNIQUE_VALUES);
608 IObservableList list = prop.observe(a);
609 list.set(1, 100.0);
610 assertEquals(100.0, list.get(1));
611 }
612
613 @Test
614 public void testMultiValued_Unique_Replace_PrimitiveAttributes()
615 {
616 Realm.runWithDefault(testRealm, new Runnable()
617 {
618 public void run()
619 {
620 _testMultiValued_Unique_Replace_PrimitiveAttributes();
621 }
622 });
623 }
624
625 private void _testMultiValued_Unique_Replace_PrimitiveAttributes()
626 {
627 A a = (A)resource.getContents().get(0);
628 IEMFEditListProperty prop = EMFEditProperties.list(editingDomain, EmfdbPackage.Literals.A__PRIMITIVE_VALUES);
629 IObservableList list = prop.observe(a);
630 assertEquals(30.0, list.get(1));
631 list.set(1, 100.0);
632 assertEquals(100.0, list.get(1));
633 }
634
635 public void testMultiValued_NotUnique_Remove_PrimitiveAttributes()
636 {
637 Realm.runWithDefault(testRealm, new Runnable()
638 {
639 public void run()
640 {
641 _testMultiValued_NotUnique_Remove_PrimitiveAttributes();
642 }
643 });
644 }
645
646 private void _testMultiValued_NotUnique_Remove_PrimitiveAttributes()
647 {
648 A a = (A)resource.getContents().get(0);
649 a.getNotUniqueValues().clear();
650 Double d20 = 20.0;
651 Double d30 = 30.0;
652 a.getNotUniqueValues().add(d20);
653 a.getNotUniqueValues().add(d30);
654 a.getNotUniqueValues().add(d20);
655 a.getNotUniqueValues().add(d30);
656 IEMFEditListProperty prop = EMFEditProperties.list(editingDomain, EmfdbPackage.Literals.A__NOT_UNIQUE_VALUES);
657 IObservableList list = prop.observe(a);
658 list.remove(3);
659 assertEquals(20.0, list.get(0));
660 assertEquals(30.0, list.get(1));
661 assertEquals(20.0, list.get(2));
662 }
663
664 public void testMultiValued_Unique_Remove_PrimitiveAttributes()
665 {
666 Realm.runWithDefault(testRealm, new Runnable()
667 {
668 public void run()
669 {
670 _testMultiValued_Unique_Remove_PrimitiveAttributes();
671 }
672 });
673 }
674
675 private void _testMultiValued_Unique_Remove_PrimitiveAttributes()
676 {
677 A a = (A)resource.getContents().get(0);
678 IEMFEditListProperty prop = EMFEditProperties.list(editingDomain, EmfdbPackage.Literals.A__PRIMITIVE_VALUES);
679 IObservableList list = prop.observe(a);
680 list.remove(1);
681 assertEquals(1, list.size());
682 assertEquals(20.0, list.get(0));
683 }
684 }