Debian Codegraph @

tests/org.eclipse.emf.test.databinding/src/org/eclipse/emf/test/databinding/EMFPropertiesTest.java

raw
1 /**
2 * Copyright (c) 2005 IBM Corporation 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 * IBM - Initial API and implementation
10 */
11 package org.eclipse.emf.test.databinding;
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 import static org.junit.Assert.fail;
21
22 import java.util.Arrays;
23 import java.util.HashMap;
24 import java.util.List;
25 import java.util.Map;
26
27 import org.eclipse.core.databinding.observable.Realm;
28 import org.eclipse.core.databinding.observable.list.IListChangeListener;
29 import org.eclipse.core.databinding.observable.list.IObservableList;
30 import org.eclipse.core.databinding.observable.list.ListChangeEvent;
31 import org.eclipse.core.databinding.observable.list.ListDiff;
32 import org.eclipse.core.databinding.observable.map.IMapChangeListener;
33 import org.eclipse.core.databinding.observable.map.IObservableMap;
34 import org.eclipse.core.databinding.observable.map.MapChangeEvent;
35 import org.eclipse.core.databinding.observable.map.MapDiff;
36 import org.eclipse.core.databinding.observable.set.IObservableSet;
37 import org.eclipse.core.databinding.observable.set.ISetChangeListener;
38 import org.eclipse.core.databinding.observable.set.SetChangeEvent;
39 import org.eclipse.core.databinding.observable.set.SetDiff;
40 import org.eclipse.core.databinding.observable.value.IObservableValue;
41 import org.eclipse.emf.common.util.BasicEList;
42 import org.eclipse.emf.common.util.EList;
43 import org.eclipse.emf.common.util.URI;
44 import org.eclipse.emf.databinding.EMFProperties;
45 import org.eclipse.emf.databinding.FeaturePath;
46 import org.eclipse.emf.databinding.IEMFListProperty;
47 import org.eclipse.emf.databinding.IEMFListProperty.ListElementAccess;
48 import org.eclipse.emf.databinding.IEMFMapProperty;
49 import org.eclipse.emf.databinding.IEMFSetProperty;
50 import org.eclipse.emf.databinding.IEMFValueProperty;
51 import org.eclipse.emf.ecore.resource.Resource;
52 import org.eclipse.emf.ecore.resource.ResourceSet;
53 import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
54 import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl;
55 import org.eclipse.emf.test.common.TestUtil;
56 import org.eclipse.emf.test.databinding.emfdb.A;
57 import org.eclipse.emf.test.databinding.emfdb.B;
58 import org.eclipse.emf.test.databinding.emfdb.EmfdbFactory;
59 import org.eclipse.emf.test.databinding.emfdb.EmfdbPackage;
60 import org.junit.Before;
61 import org.junit.Test;
62
63
64 public class EMFPropertiesTest
65 {
66 private Resource resource;
67
68 private Realm testRealm;
69
70 private SetDiff setDiff;
71
72 private ListDiff listDiff;
73
74 private MapDiff mapDiff;
75
76 @Before
77 public void setUp() throws Exception
78 {
79 ResourceSet resourceSet = new ResourceSetImpl();
80 resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put(Resource.Factory.Registry.DEFAULT_EXTENSION, new XMIResourceFactoryImpl());
81
82 URI uri = URI.createFileURI(TestUtil.getPluginDirectory("org.eclipse.emf.test.databinding") + "/model/A.xmi");
83 resource = resourceSet.getResource(uri, true);
84 testRealm = new Realm()
85 {
86 @Override
87 public boolean isCurrent()
88 {
89 return true;
90 }
91 };
92 }
93
94 @Test
95 public void testFeaturePath()
96 {
97 assertNotNull(FeaturePath.fromList(EmfdbPackage.Literals.A__BLIST));
98 try
99 {
100 FeaturePath.fromList(EmfdbPackage.Literals.A__BLIST, EmfdbPackage.Literals.B__STRING);
101 fail("Traversing list feature");
102 }
103 catch (IllegalArgumentException e)
104 {
105 // Ignore
106 }
107 }
108
109 @Test
110 public void testResourceProperty()
111 {
112 Realm.runWithDefault(testRealm, new Runnable()
113 {
114
115 public void run()
116 {
117 _testResourceProperty();
118 }
119 });
120 }
121
122 private void _testResourceProperty()
123 {
124 IEMFListProperty prop = EMFProperties.resource();
125 IObservableList list = prop.observe(resource);
126 list.addListChangeListener(new IListChangeListener()
127 {
128 public void handleListChange(ListChangeEvent event)
129 {
130 listDiff = event.diff;
131 }
132 });
133 assertNull(listDiff);
134
135 // Adding
136 A a = EmfdbFactory.eINSTANCE.createA();
137 A a1 = EmfdbFactory.eINSTANCE.createA();
138
139 resource.getContents().add(a);
140 assertNotNull(listDiff);
141 assertEquals(1, listDiff.getDifferences().length);
142 assertSame(a, listDiff.getDifferences()[0].getElement());
143 assertEquals(1, listDiff.getDifferences()[0].getPosition());
144 assertTrue(listDiff.getDifferences()[0].isAddition());
145
146 // Moving
147 resource.getContents().move(0, a);
148 assertEquals(2, listDiff.getDifferences().length);
149 assertFalse(listDiff.getDifferences()[0].isAddition()); // Removal
150 assertEquals(1, listDiff.getDifferences()[0].getPosition());
151 assertTrue(listDiff.getDifferences()[1].isAddition()); // Addition
152 assertEquals(0, listDiff.getDifferences()[1].getPosition());
153
154 // Removing
155 resource.getContents().remove(a);
156 assertEquals(1, listDiff.getDifferences().length);
157 assertFalse(listDiff.getDifferences()[0].isAddition()); // Removal
158 assertEquals(0, listDiff.getDifferences()[0].getPosition());
159
160 // Adding many
161 resource.getContents().addAll(Arrays.asList(a, a1));
162 assertEquals(2, listDiff.getDifferences().length);
163
164 // Remove many
165 resource.getContents().removeAll(Arrays.asList(a, a1));
166 assertEquals(2, listDiff.getDifferences().length);
167
168 // =============================================================
169
170 // Changed through IObservableList
171 // Add
172 list.add(a);
173 assertNotNull(listDiff);
174 assertEquals(1, listDiff.getDifferences().length);
175 assertSame(a, listDiff.getDifferences()[0].getElement());
176 assertEquals(1, listDiff.getDifferences()[0].getPosition());
177 assertTrue(listDiff.getDifferences()[0].isAddition());
178
179 // Moving
180 list.move(1, 0);
181 assertEquals(2, listDiff.getDifferences().length);
182 assertFalse(listDiff.getDifferences()[0].isAddition()); // Removal
183 assertEquals(1, listDiff.getDifferences()[0].getPosition());
184 assertTrue(listDiff.getDifferences()[1].isAddition()); // Addition
185 assertEquals(0, listDiff.getDifferences()[1].getPosition());
186
187 // Removing
188 list.remove(a);
189 assertEquals(1, listDiff.getDifferences().length);
190 assertFalse(listDiff.getDifferences()[0].isAddition()); // Removal
191 assertEquals(0, listDiff.getDifferences()[0].getPosition());
192
193 // Adding many
194 list.addAll(Arrays.asList(a, a1));
195 assertEquals(2, listDiff.getDifferences().length);
196
197 // Remove many
198 list.removeAll(Arrays.asList(a, a1));
199 assertEquals(2, listDiff.getDifferences().length);
200 }
201
202 @Test
203 public void testSetProperty()
204 {
205 Realm.runWithDefault(testRealm, new Runnable()
206 {
207 public void run()
208 {
209 _testSetProperty();
210 }
211 });
212 }
213
214 public void _testSetProperty()
215 {
216 A a = (A)resource.getContents().get(0);
217 IEMFSetProperty prop = EMFProperties.set(EmfdbPackage.Literals.A__BLIST);
218 IObservableSet set = prop.observe(a);
219 assertNotNull(set);
220 set.addSetChangeListener(new ISetChangeListener()
221 {
222
223 public void handleSetChange(SetChangeEvent event)
224 {
225 setDiff = event.diff;
226 }
227 });
228 assertNull(setDiff);
229 B b = EmfdbFactory.eINSTANCE.createB();
230 a.getBlist().add(b);
231 assertNotNull(setDiff);
232 assertEquals(1, setDiff.getAdditions().size());
233 assertSame(b, setDiff.getAdditions().iterator().next());
234 }
235
236 @Test
237 public void testListPropertyOnSingleFeature()
238 {
239 Realm.runWithDefault(testRealm, new Runnable()
240 {
241 public void run()
242 {
243 _testListPropertyOnSingleFeature();
244 }
245 });
246 }
247
248 public void _testListPropertyOnSingleFeature()
249 {
250 A a = (A)resource.getContents().get(0);
251 IEMFListProperty prop = EMFProperties.list(EmfdbPackage.Literals.A__STRING);
252 IObservableList l = prop.observe(a);
253 assertEquals(1, l.size());
254 assertEquals("Instance 1", l.get(0));
255 a.setString("Bla");
256 assertEquals("Bla", l.get(0));
257 l.addListChangeListener(new IListChangeListener()
258 {
259
260 public void handleListChange(ListChangeEvent event)
261 {
262 assertEquals(2, event.diff.getDifferences().length);
263
264 assertEquals(0, event.diff.getDifferences()[0].getPosition());
265 assertFalse(event.diff.getDifferences()[0].isAddition());
266 assertEquals("Bla", event.diff.getDifferences()[0].getElement());
267
268 assertEquals(0, event.diff.getDifferences()[1].getPosition());
269 assertTrue(event.diff.getDifferences()[1].isAddition());
270 assertEquals("Instance 1", event.diff.getDifferences()[1].getElement());
271 }
272 });
273 a.setString("Instance 1");
274 }
275
276 @Test
277 public void testListElementProperty()
278 {
279 Realm.runWithDefault(testRealm, new Runnable()
280 {
281
282 public void run()
283 {
284 _testListElementProperty();
285 }
286 });
287 }
288
289 public void _testListElementProperty()
290 {
291 IEMFListProperty prop = EMFProperties.list(EmfdbPackage.Literals.A__BLIST);
292 IEMFValueProperty valueProp = prop.value(new ListElementAccess<Object>()
293 {
294 @Override
295 public int getReadValueIndex(List<Object> list)
296 {
297 return 0;
298 }
299
300 @Override
301 public int getWriteValueIndex(List<Object> list)
302 {
303 return 0;
304 }
305 });
306
307 IEMFValueProperty detailValue = valueProp.value(EmfdbPackage.Literals.B__STRING);
308
309 IObservableValue value = detailValue.observe(resource.getContents().get(0));
310 assertEquals("Instance 1", value.getValue());
311 A a = (A)resource.getContents().get(0);
312 a.getBlist().get(0).setString("Bla Bla");
313 assertEquals("Bla Bla", value.getValue());
314
315 B b = EmfdbFactory.eINSTANCE.createB();
316 b.setString("New Element");
317
318 a.getBlist().add(0, b);
319 assertEquals("New Element", value.getValue());
320
321 b = EmfdbFactory.eINSTANCE.createB();
322 b.setString("New Element 2");
323 IObservableValue aObservable = valueProp.observe(a);
324 try
325 {
326 aObservable.setValue(b);
327 }
328 finally
329 {
330 aObservable.dispose();
331 }
332
333 assertEquals("New Element 2", value.getValue());
334 }
335
336 @Test
337 public void testMapProperty()
338 {
339 Realm.runWithDefault(testRealm, new Runnable()
340 {
341
342 public void run()
343 {
344 _testMapProperty();
345 }
346 });
347 }
348
349 public void _testMapProperty()
350 {
351 A a = (A)resource.getContents().get(0);
352 IEMFMapProperty prop = EMFProperties.map(EmfdbPackage.Literals.A__CMAP);
353 IObservableMap map = prop.observe(a);
354 map.addMapChangeListener(new IMapChangeListener()
355 {
356 public void handleMapChange(MapChangeEvent event)
357 {
358 mapDiff = event.diff;
359 }
360 });
361 assertNull(mapDiff);
362
363 // Changing existing key
364 a.getCmap().put("key 1", "value 1 modified");
365 assertEquals(0, mapDiff.getAddedKeys().size());
366 assertEquals(1, mapDiff.getChangedKeys().size());
367 assertEquals("key 1", mapDiff.getChangedKeys().iterator().next());
368 assertEquals("value 1", mapDiff.getOldValue("key 1"));
369 assertEquals("value 1 modified", mapDiff.getNewValue("key 1"));
370 assertEquals(0, mapDiff.getRemovedKeys().size());
371
372 // Adding new key
373 a.getCmap().put("key 3", "value 3");
374 assertEquals(1, mapDiff.getAddedKeys().size());
375 assertEquals("key 3", mapDiff.getAddedKeys().iterator().next());
376 assertNull(mapDiff.getOldValue("key 3"));
377 assertEquals("value 3", mapDiff.getNewValue("key 3"));
378 assertEquals(0, mapDiff.getChangedKeys().size());
379 assertEquals(0, mapDiff.getRemovedKeys().size());
380
381 // Removing key
382 a.getCmap().removeKey("key 2");
383 assertEquals(0, mapDiff.getAddedKeys().size());
384 assertEquals(0, mapDiff.getChangedKeys().size());
385 assertEquals(1, mapDiff.getRemovedKeys().size());
386 assertEquals("key 2", mapDiff.getRemovedKeys().iterator().next());
387 assertEquals("value 2", mapDiff.getOldValue("key 2"));
388 assertNull(mapDiff.getNewValue("key 2"));
389
390 // putAll is implemented as a series of put calls,
391 // which are already tested above, see BasicEMap.putAll()
392
393 // Removing many keys
394 a.getCmap().clear();
395 assertEquals(0, mapDiff.getAddedKeys().size());
396 assertEquals(0, mapDiff.getChangedKeys().size());
397 assertEquals(2, mapDiff.getRemovedKeys().size());
398 assertTrue(mapDiff.getRemovedKeys().contains("key 1"));
399 assertTrue(mapDiff.getRemovedKeys().contains("key 3"));
400 assertEquals("value 1 modified", mapDiff.getOldValue("key 1"));
401 assertEquals("value 3", mapDiff.getOldValue("key 3"));
402 assertNull(mapDiff.getNewValue("key 1"));
403 assertNull(mapDiff.getNewValue("key 3"));
404
405 // =============================================================
406
407 // Changed through IObservableMap
408 // Adding new key
409 map.put("key 1", "value 1");
410 assertEquals(1, mapDiff.getAddedKeys().size());
411 assertEquals("key 1", mapDiff.getAddedKeys().iterator().next());
412 assertNull(mapDiff.getOldValue("key 1"));
413 assertEquals("value 1", mapDiff.getNewValue("key 1"));
414 assertEquals(0, mapDiff.getChangedKeys().size());
415 assertEquals(0, mapDiff.getRemovedKeys().size());
416
417 // Adding many (including existing) keys
418 Map<String, String> newValues = new HashMap<String, String>();
419 newValues.put("key 1", "value 1 modified");
420 newValues.put("key 2", "value 2");
421 newValues.put("key 3", "value 3");
422 map.putAll(newValues);
423 assertEquals(2, mapDiff.getAddedKeys().size());
424 assertTrue(mapDiff.getAddedKeys().contains("key 2"));
425 assertTrue(mapDiff.getAddedKeys().contains("key 3"));
426 assertNull(mapDiff.getOldValue("key 2"));
427 assertNull(mapDiff.getOldValue("key 3"));
428 assertEquals("value 2", mapDiff.getNewValue("key 2"));
429 assertEquals("value 3", mapDiff.getNewValue("key 3"));
430 assertEquals(1, mapDiff.getChangedKeys().size());
431 assertEquals("key 1", mapDiff.getChangedKeys().iterator().next());
432 assertEquals("value 1", mapDiff.getOldValue("key 1"));
433 assertEquals("value 1 modified", mapDiff.getNewValue("key 1"));
434 assertEquals(0, mapDiff.getRemovedKeys().size());
435
436 // Removing
437 map.remove("key 1");
438 assertEquals(0, mapDiff.getAddedKeys().size());
439 assertEquals(0, mapDiff.getChangedKeys().size());
440 assertEquals(1, mapDiff.getRemovedKeys().size());
441 assertEquals("key 1", mapDiff.getRemovedKeys().iterator().next());
442 assertEquals("value 1 modified", mapDiff.getOldValue("key 1"));
443 assertNull(mapDiff.getNewValue("key 1"));
444
445 // Removing many
446 // The only way to remove many entries from IObservableMap is
447 // to call clear(), but clear removes entries one by one, which
448 // has already been tested above
449 map.clear();
450 }
451
452 @Test
453 public void test_sublistElement()
454 {
455 Realm.runWithDefault(testRealm, new Runnable()
456 {
457
458 public void run()
459 {
460 _test_sublistElement();
461 }
462 });
463 }
464
465 public void _test_sublistElement()
466 {
467 A a = (A)resource.getContents().get(0);
468 IEMFListProperty lProp1 = EMFProperties.list(EmfdbPackage.Literals.A__BLIST);
469 IEMFValueProperty vProp1 = lProp1.value(new ListElementAccess<Object>()
470 {
471 @Override
472 public int getReadValueIndex(List<Object> list)
473 {
474 return 0;
475 }
476
477 @Override
478 public int getWriteValueIndex(List<Object> list)
479 {
480 return WriteData.NO_INDEX;
481 }
482 });
483
484 IEMFValueProperty vProp2 = vProp1.value(EmfdbPackage.Literals.B__D);
485 IEMFListProperty lProp2 = vProp2.list(EmfdbPackage.Literals.D__ELIST);
486
487 IEMFValueProperty vProp3 = lProp2.value(new ListElementAccess<Object>()
488 {
489 @Override
490 public int getReadValueIndex(List<Object> list)
491 {
492 return list.size() - 1;
493 }
494
495 @Override
496 public int getWriteValueIndex(List<Object> list)
497 {
498 return WriteData.NO_INDEX;
499 }
500 });
501
502 IEMFValueProperty detailValue = vProp3.value(EmfdbPackage.Literals.E__NAME);
503 IObservableValue v = detailValue.observe(a);
504 assertEquals(v.getValue(), "Last Element");
505 }
506
507 @Test
508 public void test_attributeListWithDuplicates()
509 {
510 Realm.runWithDefault(testRealm, new Runnable()
511 {
512 public void run()
513 {
514 _test_attributeListWithDuplicates();
515 }
516 });
517 }
518
519 public void _test_attributeListWithDuplicates()
520 {
521 A a = (A)resource.getContents().get(0);
522 EList<String> strings = a.getStrings();
523
524 IEMFListProperty stringsListProperty = EMFProperties.list(EmfdbPackage.Literals.A__STRINGS);
525 IObservableList observableList = stringsListProperty.observe(a);
526 EList<String> normalList = new BasicEList<String>();
527
528 String value1 = "value1";
529 String value1Copy = new String(value1);
530 String value2 = "value2";
531 String value3 = "value3";
532
533 observableList.add(value1);
534 observableList.add(value2);
535 observableList.add(value1Copy);
536 observableList.add(3, value2);
537
538 normalList.add(value1);
539 normalList.add(value2);
540 normalList.add(value1Copy);
541 normalList.add(3, value2);
542
543 assertSameListContents(normalList, observableList);
544 assertSameListContents(normalList, strings);
545
546 observableList.set(2, value3);
547 normalList.set(2, value3);
548
549 assertSameListContents(normalList, observableList);
550 assertSameListContents(normalList, strings);
551
552 observableList.remove(2);
553 normalList.remove(2);
554
555 assertSameListContents(normalList, observableList);
556 assertSameListContents(normalList, strings);
557
558 // See https://bugs.eclipse.org/bugs/show_bug.cgi?id=478053 that
559 // EList.move(int, int) and
560 // IObservableList.move(int, int)
561 // have opposite meaning for source and target.
562 //
563 observableList.move(2, 0);
564 normalList.move(0, 2);
565
566 assertSameListContents(normalList, observableList);
567 assertSameListContents(normalList, strings);
568 }
569
570 private void assertSameListContents(List<?> list1, List<?> list2)
571 {
572 String string1 = list1.toString();
573 String string2 = list2.toString();
574 assertEquals(string1, string2);
575 assertEquals(list1.size(), list2.size());
576 for (int i = 0, size = list1.size(); i < size; ++i)
577 {
578 assertSame("Same object expected at index " + i + " of " + string1, list1.get(i), list2.get(i));
579 }
580 }
581 }