Skip to content

Package: DerivedAttributeAdapter_Test$TestContentAdapter

DerivedAttributeAdapter_Test$TestContentAdapter

nameinstructionbranchcomplexitylinemethod
DerivedAttributeAdapter_Test.TestContentAdapter(EObject, EStructuralFeature)
M: 0 C: 19
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 6
100%
M: 0 C: 1
100%
clearRecordedNotifications()
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
getRecordedNotifications()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
notifyChanged(Notification)
M: 0 C: 21
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 7
100%
M: 0 C: 1
100%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2011-2015 EclipseSource Muenchen GmbH and others.
3: *
4: * All rights reserved. This program and the accompanying materials
5: * are made available under the terms of the Eclipse Public License 2.0
6: * which accompanies this distribution, and is available at
7: * https://www.eclipse.org/legal/epl-2.0/
8: *
9: * SPDX-License-Identifier: EPL-2.0
10: *
11: * Contributors:
12: * Johannes Faltermeier - initial API and implementation
13: ******************************************************************************/
14: package org.eclipse.emf.ecp.common.spi;
15:
16: import static org.junit.Assert.assertEquals;
17: import static org.junit.Assert.assertNotNull;
18: import static org.junit.Assert.assertNull;
19:
20: import java.util.ArrayList;
21: import java.util.Arrays;
22: import java.util.List;
23:
24: import org.eclipse.emf.common.notify.Adapter;
25: import org.eclipse.emf.common.notify.Notification;
26: import org.eclipse.emf.ecore.EObject;
27: import org.eclipse.emf.ecore.EStructuralFeature;
28: import org.eclipse.emf.ecore.util.EContentAdapter;
29: import org.eclipse.emf.ecp.common.spi.DerivedAttributeAdapter.NavigationAdapter;
30: import org.eclipse.emf.ecp.common.test.model.Base;
31: import org.eclipse.emf.ecp.common.test.model.Test1;
32: import org.eclipse.emf.ecp.common.test.model.Test2;
33: import org.eclipse.emf.ecp.common.test.model.Test3;
34: import org.eclipse.emf.ecp.common.test.model.Test4;
35: import org.eclipse.emf.ecp.common.test.model.TestFactory;
36: import org.eclipse.emf.ecp.common.test.model.TestPackage;
37: import org.junit.Test;
38:
39: public class DerivedAttributeAdapter_Test {
40:
41:         private static final String VALUE1 = "value1";
42:         private static final String VALUE2 = "value2";
43:
44:         @Test
45:         public void testLocalDependencySingle() {
46:                 final Test1 test1 = TestFactory.eINSTANCE.createTest1();
47:                 final TestContentAdapter testContentAdapter = new TestContentAdapter(test1,
48:                         TestPackage.eINSTANCE.getTest1_Derived());
49:
50:                 test1.setSingleAttribute(VALUE1);
51:                 assertEquals(1, testContentAdapter.getRecordedNotifications().size());
52:                 assertNotification(Notification.SET, null, VALUE1, testContentAdapter.getRecordedNotifications().get(0));
53:                 testContentAdapter.clearRecordedNotifications();
54:
55:                 test1.setDerived(VALUE2);
56:                 assertEquals(1, testContentAdapter.getRecordedNotifications().size());
57:                 assertNotification(Notification.SET, VALUE1, VALUE2, testContentAdapter.getRecordedNotifications().get(0));
58:         }
59:
60:         @Test
61:         public void testLocalDependencySingleUnsettable() {
62:                 final Test2 test2 = TestFactory.eINSTANCE.createTest2();
63:                 final TestContentAdapter testContentAdapter = new TestContentAdapter(test2,
64:                         TestPackage.eINSTANCE.getTest2_Derived());
65:
66:                 test2.setSingleAttributeUnsettable(VALUE1);
67:                 assertEquals(1, testContentAdapter.getRecordedNotifications().size());
68:                 assertNotification(Notification.SET, null, VALUE1, testContentAdapter.getRecordedNotifications().get(0));
69:                 testContentAdapter.clearRecordedNotifications();
70:
71:                 test2.unsetSingleAttributeUnsettable();
72:                 assertEquals(1, testContentAdapter.getRecordedNotifications().size());
73:                 assertNotification(Notification.UNSET, VALUE1, null, testContentAdapter.getRecordedNotifications().get(0));
74:                 testContentAdapter.clearRecordedNotifications();
75:
76:                 test2.setDerived(VALUE1);
77:                 assertEquals(1, testContentAdapter.getRecordedNotifications().size());
78:                 assertNotification(Notification.SET, null, VALUE1, testContentAdapter.getRecordedNotifications().get(0));
79:                 testContentAdapter.clearRecordedNotifications();
80:
81:                 test2.unsetDerived();
82:                 assertEquals(1, testContentAdapter.getRecordedNotifications().size());
83:                 assertNotification(Notification.UNSET, VALUE1, null, testContentAdapter.getRecordedNotifications().get(0));
84:                 testContentAdapter.clearRecordedNotifications();
85:         }
86:
87:         // BEGIN COMPLEX CODE
88:         @Test
89:         public void testNavigationAdapterAddingAndDisposal() {
90:                 final Test3 test3 = TestFactory.eINSTANCE.createTest3();
91:                 NavigationAdapter navigationAdapter = getNavigationAdapter(test3);
92:                 assertNotNull(navigationAdapter);
93:                 assertEquals(0, navigationAdapter.children().size());
94:                 assertEquals(2, test3.eAdapters().size()); // DAA + DAA.NA
95:
96:                 /* add middle child */
97:                 final Base child11 = TestFactory.eINSTANCE.createBase();
98:                 test3.setChild(child11);
99:
100:                 navigationAdapter = getNavigationAdapter(test3);
101:                 assertNotNull(navigationAdapter);
102:                 assertEquals(1, navigationAdapter.children().size());
103:                 assertEquals(2, test3.eAdapters().size()); // DAA + DAA.NA
104:
105:                 navigationAdapter = getNavigationAdapter(child11);
106:                 assertNotNull(navigationAdapter);
107:                 assertEquals(0, navigationAdapter.children().size());
108:                 assertEquals(1, child11.eAdapters().size()); // DAA.NA
109:
110:                 /* add last child */
111:                 final Base child21 = TestFactory.eINSTANCE.createBase();
112:                 child11.setChild(child21);
113:
114:                 navigationAdapter = getNavigationAdapter(test3);
115:                 assertNotNull(navigationAdapter);
116:                 assertEquals(1, navigationAdapter.children().size());
117:                 assertEquals(2, test3.eAdapters().size()); // DAA + DAA.NA
118:
119:                 navigationAdapter = getNavigationAdapter(child11);
120:                 assertNotNull(navigationAdapter);
121:                 assertEquals(1, navigationAdapter.children().size());
122:                 assertEquals(1, child11.eAdapters().size()); // DAA.NA
123:
124:                 navigationAdapter = getNavigationAdapter(child21);
125:                 assertNotNull(navigationAdapter);
126:                 assertEquals(0, navigationAdapter.children().size());
127:                 assertEquals(1, child21.eAdapters().size()); // DAA.NA
128:
129:                 /* remove last child */
130:                 child11.setChild(null);
131:
132:                 navigationAdapter = getNavigationAdapter(test3);
133:                 assertNotNull(navigationAdapter);
134:                 assertEquals(1, navigationAdapter.children().size());
135:                 assertEquals(2, test3.eAdapters().size()); // DAA + DAA.NA
136:
137:                 navigationAdapter = getNavigationAdapter(child11);
138:                 assertNotNull(navigationAdapter);
139:                 assertEquals(0, navigationAdapter.children().size());
140:                 assertEquals(1, child11.eAdapters().size()); // DAA.NA
141:
142:                 navigationAdapter = getNavigationAdapter(child21);
143:                 assertNull(navigationAdapter);
144:                 assertEquals(0, child21.eAdapters().size());
145:
146:                 /* remove middle child */
147:                 test3.setChild(null);
148:
149:                 navigationAdapter = getNavigationAdapter(test3);
150:                 assertNotNull(navigationAdapter);
151:                 assertEquals(0, navigationAdapter.children().size());
152:                 assertEquals(2, test3.eAdapters().size()); // DAA + DAA.NA
153:
154:                 navigationAdapter = getNavigationAdapter(child11);
155:                 assertNull(navigationAdapter);
156:                 assertEquals(0, child11.eAdapters().size());
157:
158:                 /* add middle AND last child */
159:                 child11.setChild(child21);
160:                 test3.setChild(child11);
161:
162:                 navigationAdapter = getNavigationAdapter(test3);
163:                 assertNotNull(navigationAdapter);
164:                 assertEquals(1, navigationAdapter.children().size());
165:                 assertEquals(2, test3.eAdapters().size()); // DAA + DAA.NA
166:
167:                 navigationAdapter = getNavigationAdapter(child11);
168:                 assertNotNull(navigationAdapter);
169:                 assertEquals(1, navigationAdapter.children().size());
170:                 assertEquals(1, child11.eAdapters().size()); // DAA.NA
171:
172:                 navigationAdapter = getNavigationAdapter(child21);
173:                 assertNotNull(navigationAdapter);
174:                 assertEquals(0, navigationAdapter.children().size());
175:                 assertEquals(1, child21.eAdapters().size()); // DAA.NA
176:
177:                 /* remove middle AND last child */
178:                 test3.setChild(null);
179:
180:                 navigationAdapter = getNavigationAdapter(test3);
181:                 assertNotNull(navigationAdapter);
182:                 assertEquals(0, navigationAdapter.children().size());
183:                 assertEquals(2, test3.eAdapters().size()); // DAA + DAA.NA
184:
185:                 navigationAdapter = getNavigationAdapter(child11);
186:                 assertNull(navigationAdapter);
187:                 assertEquals(0, child11.eAdapters().size());
188:
189:                 navigationAdapter = getNavigationAdapter(child21);
190:                 assertNull(navigationAdapter);
191:                 assertEquals(0, child21.eAdapters().size());
192:         }
193:
194:         // END COMPLEX CODE
195:         @Test
196:         public void testNavigatedSingleUnsettable() {
197:                 final Test3 test3 = TestFactory.eINSTANCE.createTest3();
198:                 final Base child11 = TestFactory.eINSTANCE.createBase();
199:                 final Base child21 = TestFactory.eINSTANCE.createBase();
200:                 child11.setChild(child21);
201:                 test3.setChild(child11);
202:                 final TestContentAdapter testContentAdapter = new TestContentAdapter(test3,
203:                         TestPackage.eINSTANCE.getTest3_Derived());
204:
205:                 /* test set behaviour when full child hierarchy */
206:                 child21.setSingleAttributeUnsettable(VALUE1);
207:                 assertEquals(1, testContentAdapter.getRecordedNotifications().size());
208:                 assertNotification(Notification.SET, null, VALUE1, testContentAdapter.getRecordedNotifications().get(0));
209:                 testContentAdapter.clearRecordedNotifications();
210:
211:                 child21.unsetSingleAttributeUnsettable();
212:                 assertEquals(1, testContentAdapter.getRecordedNotifications().size());
213:                 assertNotification(Notification.UNSET, VALUE1, null, testContentAdapter.getRecordedNotifications().get(0));
214:                 testContentAdapter.clearRecordedNotifications();
215:
216:                 test3.setDerived(VALUE1);
217:                 assertEquals(1, testContentAdapter.getRecordedNotifications().size());
218:                 assertNotification(Notification.SET, null, VALUE1, testContentAdapter.getRecordedNotifications().get(0));
219:                 testContentAdapter.clearRecordedNotifications();
220:
221:                 test3.unsetDerived();
222:                 assertEquals(1, testContentAdapter.getRecordedNotifications().size());
223:                 assertNotification(Notification.UNSET, VALUE1, null, testContentAdapter.getRecordedNotifications().get(0));
224:                 testContentAdapter.clearRecordedNotifications();
225:
226:                 test3.setDerived(VALUE1);
227:                 assertEquals(1, testContentAdapter.getRecordedNotifications().size());
228:                 assertNotification(Notification.SET, null, VALUE1, testContentAdapter.getRecordedNotifications().get(0));
229:                 testContentAdapter.clearRecordedNotifications();
230:
231:                 /* change navigation path last child */
232:                 child11.setChild(null);
233:                 assertEquals(1, testContentAdapter.getRecordedNotifications().size());
234:                 assertNotification(Notification.UNSET, VALUE1, null, testContentAdapter.getRecordedNotifications().get(0));
235:                 testContentAdapter.clearRecordedNotifications();
236:                 assertEquals(0, child21.eAdapters().size());
237:
238:                 final Base child22 = TestFactory.eINSTANCE.createBase();
239:                 child22.setSingleAttributeUnsettable(null);
240:                 child11.setChild(child22);
241:                 assertEquals(1, testContentAdapter.getRecordedNotifications().size());
242:                 assertNotification(Notification.SET, null, null, testContentAdapter.getRecordedNotifications().get(0));
243:                 testContentAdapter.clearRecordedNotifications();
244:                 assertEquals(2, child22.eAdapters().size()); // 2 = content adapter for test + derived adapter
245:
246:                 /* change navigation path middle child */
247:                 final Base child12 = TestFactory.eINSTANCE.createBase();
248:                 final Base child23 = TestFactory.eINSTANCE.createBase();
249:                 child12.setChild(child23);
250:                 test3.setChild(child12);
251:                 assertEquals(1, testContentAdapter.getRecordedNotifications().size());
252:                 assertNotification(Notification.UNSET, null, null, testContentAdapter.getRecordedNotifications().get(0));
253:                 testContentAdapter.clearRecordedNotifications();
254:                 assertEquals(0, child11.eAdapters().size());
255:                 assertEquals(0, child22.eAdapters().size());
256:                 assertEquals(2, child12.eAdapters().size()); // 2 = content adapter for test + derived adapter
257:                 assertEquals(2, child23.eAdapters().size()); // 2 = content adapter for test + derived adapter
258:
259:         }
260:
261:         // BEGIN COMPLEX CODE
262:         @Test
263:         public void testNavigationAdapterMulti() {
264:                 final Test4 test4 = TestFactory.eINSTANCE.createTest4();
265:                 NavigationAdapter navigationAdapter = getNavigationAdapter(test4);
266:                 assertNotNull(navigationAdapter);
267:                 assertEquals(0, navigationAdapter.children().size());
268:                 assertEquals(3, test4.eAdapters().size()); // DAA + 2xDAA.NA
269:
270:                 /* add to list */
271:                 final Base base1 = TestFactory.eINSTANCE.createBase();
272:                 test4.getChildren().add(base1);
273:
274:                 navigationAdapter = getNavigationAdapter(test4);
275:                 assertNotNull(navigationAdapter);
276:                 assertEquals(1, navigationAdapter.children().size());
277:                 assertEquals(3, test4.eAdapters().size()); // DAA + 2xDAA.NA
278:
279:                 navigationAdapter = getNavigationAdapter(base1);
280:                 assertNotNull(navigationAdapter);
281:                 assertEquals(0, navigationAdapter.children().size());
282:                 assertEquals(2, base1.eAdapters().size()); // 2xDAA.NA
283:
284:                 /* Add many to list */
285:                 final Base base2 = TestFactory.eINSTANCE.createBase();
286:                 final Base base3 = TestFactory.eINSTANCE.createBase();
287:                 test4.getChildren().addAll(Arrays.asList(base2, base3));
288:
289:                 navigationAdapter = getNavigationAdapter(test4);
290:                 assertNotNull(navigationAdapter);
291:                 assertEquals(3, navigationAdapter.children().size());
292:                 assertEquals(3, test4.eAdapters().size()); // DAA + 2xDAA.NA
293:
294:                 navigationAdapter = getNavigationAdapter(base1);
295:                 assertNotNull(navigationAdapter);
296:                 assertEquals(0, navigationAdapter.children().size());
297:                 assertEquals(2, base1.eAdapters().size()); // 2xDAA.NA
298:
299:                 navigationAdapter = getNavigationAdapter(base2);
300:                 assertNotNull(navigationAdapter);
301:                 assertEquals(0, navigationAdapter.children().size());
302:                 assertEquals(2, base2.eAdapters().size()); // 2xDAA.NA
303:
304:                 navigationAdapter = getNavigationAdapter(base3);
305:                 assertNotNull(navigationAdapter);
306:                 assertEquals(0, navigationAdapter.children().size());
307:                 assertEquals(2, base3.eAdapters().size()); // 2xDAA.NA
308:
309:                 /* remove from list */
310:                 test4.getChildren().remove(base1);
311:
312:                 navigationAdapter = getNavigationAdapter(test4);
313:                 assertNotNull(navigationAdapter);
314:                 assertEquals(2, navigationAdapter.children().size());
315:                 assertEquals(3, test4.eAdapters().size()); // DAA + 2xDAA.NA
316:
317:                 navigationAdapter = getNavigationAdapter(base1);
318:                 assertNull(navigationAdapter);
319:                 assertEquals(0, base1.eAdapters().size());
320:
321:                 navigationAdapter = getNavigationAdapter(base2);
322:                 assertNotNull(navigationAdapter);
323:                 assertEquals(0, navigationAdapter.children().size());
324:                 assertEquals(2, base2.eAdapters().size()); // 2xDAA.NA
325:
326:                 navigationAdapter = getNavigationAdapter(base3);
327:                 assertNotNull(navigationAdapter);
328:                 assertEquals(0, navigationAdapter.children().size());
329:                 assertEquals(2, base3.eAdapters().size()); // 2xDAA.NA
330:
331:                 /* remove many from list */
332:                 test4.getChildren().removeAll(Arrays.asList(base2, base3));
333:
334:                 navigationAdapter = getNavigationAdapter(test4);
335:                 assertNotNull(navigationAdapter);
336:                 assertEquals(0, navigationAdapter.children().size());
337:                 assertEquals(3, test4.eAdapters().size()); // DAA + 2xDAA.NA
338:
339:                 navigationAdapter = getNavigationAdapter(base1);
340:                 assertNull(navigationAdapter);
341:                 assertEquals(0, base1.eAdapters().size());
342:
343:                 navigationAdapter = getNavigationAdapter(base2);
344:                 assertNull(navigationAdapter);
345:                 assertEquals(0, base2.eAdapters().size());
346:
347:                 navigationAdapter = getNavigationAdapter(base3);
348:                 assertNull(navigationAdapter);
349:                 assertEquals(0, base3.eAdapters().size());
350:         }
351:         // END COMPLEX CODE
352:
353:         @Test
354:         public void testMultiNavigationKeyValueCase() {
355:                 final Test4 test4 = TestFactory.eINSTANCE.createTest4();
356:                 final TestContentAdapter testContentAdapter = new TestContentAdapter(test4,
357:                         TestPackage.eINSTANCE.getTest4_Derived());
358:
359:                 /* add unrelated key */
360:                 final Base keyVal01 = TestFactory.eINSTANCE.createBase();
361:                 test4.getChildren().add(keyVal01);
362:                 assertEquals(2, testContentAdapter.getRecordedNotifications().size());
363:                 assertNotification(Notification.UNSET, null, null, testContentAdapter.getRecordedNotifications().get(0));
364:                 assertNotification(Notification.UNSET, null, null, testContentAdapter.getRecordedNotifications().get(1));
365:                 testContentAdapter.clearRecordedNotifications();
366:
367:                 /* change unrelated value */
368:                 keyVal01.setSingleAttribute(VALUE1);
369:                 assertEquals(1, testContentAdapter.getRecordedNotifications().size());
370:                 assertNotification(Notification.UNSET, null, null, testContentAdapter.getRecordedNotifications().get(0));
371:                 testContentAdapter.clearRecordedNotifications();
372:
373:                 /* change unrelated key to right */
374:                 keyVal01.setSingleAttributeUnsettable(Test4.KEY);
375:                 assertEquals(1, testContentAdapter.getRecordedNotifications().size());
376:                 assertNotification(Notification.SET, null, VALUE1, testContentAdapter.getRecordedNotifications().get(0));
377:                 testContentAdapter.clearRecordedNotifications();
378:
379:                 /* change value */
380:                 keyVal01.setSingleAttribute(VALUE2);
381:                 assertEquals(1, testContentAdapter.getRecordedNotifications().size());
382:                 assertNotification(Notification.SET, VALUE1, VALUE2, testContentAdapter.getRecordedNotifications().get(0));
383:                 testContentAdapter.clearRecordedNotifications();
384:
385:                 /* remove key value */
386:                 test4.getChildren().remove(keyVal01);
387:                 assertEquals(2, testContentAdapter.getRecordedNotifications().size());
388:                 assertNotification(Notification.UNSET, VALUE2, null, testContentAdapter.getRecordedNotifications().get(0));
389:                 assertNotification(Notification.UNSET, null, null, testContentAdapter.getRecordedNotifications().get(1));
390:                 testContentAdapter.clearRecordedNotifications();
391:
392:                 /* set derived */
393:                 test4.setDerived(VALUE1);
394:                 assertEquals(2, testContentAdapter.getRecordedNotifications().size());
395:                 assertNotification(Notification.SET, null, VALUE1, testContentAdapter.getRecordedNotifications().get(0));
396:                 assertNotification(Notification.SET, VALUE1, VALUE1, testContentAdapter.getRecordedNotifications().get(1));
397:                 testContentAdapter.clearRecordedNotifications();
398:
399:         }
400:
401:         private static void assertNotification(int eventType, Object oldValue, Object newValue, Notification notification) {
402:                 assertEquals(eventType, notification.getEventType());
403:                 assertEquals(oldValue, notification.getOldValue());
404:                 assertEquals(newValue, notification.getNewValue());
405:         }
406:
407:         private static DerivedAttributeAdapter.NavigationAdapter getNavigationAdapter(EObject eObject) {
408:                 for (final Adapter adapter : eObject.eAdapters()) {
409:                         if (DerivedAttributeAdapter.NavigationAdapter.class.isInstance(adapter)) {
410:                                 return (NavigationAdapter) adapter;
411:                         }
412:                 }
413:                 return null;
414:         }
415:
416:         private static final class TestContentAdapter extends EContentAdapter {
417:
418:                 private final List<Notification> recordedNotifications = new ArrayList<Notification>();
419:
420:                 private final EObject filterNotifier;
421:                 private final EStructuralFeature filterFeature;
422:
423:                 TestContentAdapter(EObject filterNotifier, EStructuralFeature filterFeature) {
424:                         this.filterNotifier = filterNotifier;
425:                         this.filterFeature = filterFeature;
426:                         filterNotifier.eAdapters().add(this);
427:                 }
428:
429:                 List<Notification> getRecordedNotifications() {
430:                         return recordedNotifications;
431:                 }
432:
433:                 void clearRecordedNotifications() {
434:                         recordedNotifications.clear();
435:                 }
436:
437:                 @Override
438:                 public void notifyChanged(Notification notification) {
439:                         super.notifyChanged(notification);
440:•                        if (filterNotifier != notification.getNotifier()) {
441:                                 return;
442:                         }
443:•                        if (filterFeature != notification.getFeature()) {
444:                                 return;
445:                         }
446:                         recordedNotifications.add(notification);
447:                 }
448:         }
449:
450: }