Skip to content

Package: IndexSegmentStructuralChangeTester_Test$TestNotification

IndexSegmentStructuralChangeTester_Test$TestNotification

nameinstructionbranchcomplexitylinemethod
IndexSegmentStructuralChangeTester_Test.TestNotification(IndexSegmentStructuralChangeTester_Test, int, Object, Object, int, EObject, EStructuralFeature)
M: 0 C: 16
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
getFeature()
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%
getNotifier()
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%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2011-2018 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: * Lucas Koehler - initial API and implementation
13: ******************************************************************************/
14: package org.eclipse.emfforms.internal.core.services.segments.index;
15:
16: import static org.junit.Assert.assertEquals;
17: import static org.junit.Assert.assertFalse;
18: import static org.junit.Assert.assertTrue;
19: import static org.mockito.Matchers.any;
20: import static org.mockito.Mockito.mock;
21: import static org.mockito.Mockito.verify;
22: import static org.mockito.Mockito.when;
23:
24: import java.util.LinkedList;
25:
26: import org.eclipse.emf.common.notify.Notification;
27: import org.eclipse.emf.common.notify.impl.NotificationImpl;
28: import org.eclipse.emf.ecore.EObject;
29: import org.eclipse.emf.ecore.EReference;
30: import org.eclipse.emf.ecore.EStructuralFeature;
31: import org.eclipse.emf.ecp.view.spi.model.ModelChangeNotification;
32: import org.eclipse.emf.ecp.view.spi.model.VFeatureDomainModelReferenceSegment;
33: import org.eclipse.emfforms.core.services.databinding.testmodel.test.model.B;
34: import org.eclipse.emfforms.core.services.databinding.testmodel.test.model.C;
35: import org.eclipse.emfforms.core.services.databinding.testmodel.test.model.TestFactory;
36: import org.eclipse.emfforms.core.services.databinding.testmodel.test.model.TestPackage;
37: import org.eclipse.emfforms.spi.common.report.AbstractReport;
38: import org.eclipse.emfforms.spi.common.report.ReportService;
39: import org.eclipse.emfforms.spi.core.services.databinding.DatabindingFailedException;
40: import org.eclipse.emfforms.spi.core.services.databinding.emf.EMFFormsSegmentResolver;
41: import org.eclipse.emfforms.spi.core.services.structuralchange.StructuralChangeSegmentTester;
42: import org.eclipse.emfforms.spi.view.indexsegment.model.VIndexDomainModelReferenceSegment;
43: import org.eclipse.emfforms.spi.view.indexsegment.model.VIndexsegmentFactory;
44: import org.junit.Before;
45: import org.junit.Test;
46:
47: /**
48: * JUnit tests for {@link IndexSegmentStructuralChangeTester}.
49: *
50: * @author Lucas Koehler
51: *
52: */
53: public class IndexSegmentStructuralChangeTester_Test {
54:
55:         private IndexSegmentStructuralChangeTester changeTester;
56:         private ReportService reportService;
57:         private EMFFormsSegmentResolver segmentResolver;
58:
59:         /**
60:          * Sets up a new index segment structural change tester for every test case.
61:          */
62:         @Before
63:         public void setUp() throws Exception {
64:                 changeTester = new IndexSegmentStructuralChangeTester();
65:                 reportService = mock(ReportService.class);
66:                 changeTester.setReportService(reportService);
67:                 segmentResolver = mock(EMFFormsSegmentResolver.class);
68:                 changeTester.setEMFFormsSegmentResolver(segmentResolver);
69:         }
70:
71:         @Test
72:         public void testIsStructureChangedRemoveIndexedObject() throws DatabindingFailedException {
73:                 final EReference listFeature = TestPackage.eINSTANCE.getB_CList();
74:                 final B domain = TestFactory.eINSTANCE.createB();
75:                 final C c0 = TestFactory.eINSTANCE.createC();
76:                 final C c1 = TestFactory.eINSTANCE.createC();
77:                 domain.getCList().add(c0);
78:
79:                 final Notification notification = new TestNotification(Notification.REMOVE, c1, null, 1, domain, listFeature);
80:                 final ModelChangeNotification mcn = new ModelChangeNotification(notification);
81:
82:                 final IndexedSetting setting = new IndexedSetting(domain, listFeature, 1);
83:                 final VIndexDomainModelReferenceSegment segment = VIndexsegmentFactory.eINSTANCE
84:                         .createIndexDomainModelReferenceSegment();
85:                 segment.setDomainModelFeature(listFeature.getName());
86:                 segment.setIndex(1);
87:                 when(segmentResolver.resolveSegment(segment, domain)).thenReturn(setting);
88:
89:                 final boolean isChanged = changeTester.isStructureChanged(segment, domain, mcn);
90:
91:                 assertTrue(isChanged);
92:         }
93:
94:         /**
95:          * Tests that a change is indicated if a list element with lower index (e.g. 0) than the index of the segment (e.g.
96:          * 1) is removed. This should trigger a change because remaining elements are moving up the list when an element
97:          * with a lower index is removed. This is only true if there was an element at the index's position before the old
98:          * element's removal.
99:          *
100:          * @throws DatabindingFailedException
101:          */
102:         @Test
103:         public void testIsStructureChangedRemoveOnLowerIndexRelevant() throws DatabindingFailedException {
104:                 final EReference listFeature = TestPackage.eINSTANCE.getB_CList();
105:                 final B domain = TestFactory.eINSTANCE.createB();
106:                 final C c0 = TestFactory.eINSTANCE.createC();
107:                 final C c1 = TestFactory.eINSTANCE.createC();
108:                 domain.getCList().add(c0);
109:                 domain.getCList().add(c1);
110:
111:                 final Notification notification = new TestNotification(Notification.REMOVE, c0, null, 0, domain, listFeature);
112:                 final ModelChangeNotification mcn = new ModelChangeNotification(notification);
113:
114:                 final IndexedSetting setting = new IndexedSetting(domain, listFeature, 1);
115:                 final VIndexDomainModelReferenceSegment segment = VIndexsegmentFactory.eINSTANCE
116:                         .createIndexDomainModelReferenceSegment();
117:                 segment.setDomainModelFeature(listFeature.getName());
118:                 segment.setIndex(1);
119:                 when(segmentResolver.resolveSegment(segment, domain)).thenReturn(setting);
120:
121:                 domain.getCList().remove(0);
122:                 final boolean isChanged = changeTester.isStructureChanged(segment, domain, mcn);
123:
124:                 assertTrue(isChanged);
125:         }
126:
127:         /**
128:          * Tests that a change is indicated if a list element with lower index (e.g. 0) than the index of the segment (e.g.
129:          * 1) is removed. This should trigger a change because remaining elements are moving up the list when an element
130:          * with a lower index is removed. This is only true if there was an element at the index's position before the old
131:          * element's removal.
132:          *
133:          * @throws DatabindingFailedException
134:          */
135:         @Test
136:         public void testIsStructureChangedRemoveOnLowerIndexIrrelevant() throws DatabindingFailedException {
137:                 final EReference listFeature = TestPackage.eINSTANCE.getB_CList();
138:                 final B domain = TestFactory.eINSTANCE.createB();
139:                 final C c0 = TestFactory.eINSTANCE.createC();
140:                 final C c1 = TestFactory.eINSTANCE.createC();
141:                 domain.getCList().add(c0);
142:                 domain.getCList().add(c1);
143:
144:                 final Notification notification = new TestNotification(Notification.REMOVE, c0, null, 0, domain, listFeature);
145:                 final ModelChangeNotification mcn = new ModelChangeNotification(notification);
146:
147:                 final IndexedSetting setting = new IndexedSetting(domain, listFeature, 2);
148:                 final VIndexDomainModelReferenceSegment segment = VIndexsegmentFactory.eINSTANCE
149:                         .createIndexDomainModelReferenceSegment();
150:                 segment.setDomainModelFeature(listFeature.getName());
151:                 segment.setIndex(2);
152:                 when(segmentResolver.resolveSegment(segment, domain)).thenReturn(setting);
153:
154:                 domain.getCList().remove(0);
155:                 final boolean isChanged = changeTester.isStructureChanged(segment, domain, mcn);
156:
157:                 assertFalse(isChanged);
158:         }
159:
160:         /**
161:          * Tests that a change is indicated if a list element with lower index (e.g. 0) than the index of the segment (e.g.
162:          * 1) is added. This should trigger a change because the old elements are moving down the list when an element
163:          * with a lower index is added. This is only true, if the list is (now) big enough that there (now) is an element at
164:          * the index's position.
165:          *
166:          * @throws DatabindingFailedException
167:          */
168:         @Test
169:         public void testIsStructureChangedAddOnLowerIndexRelevant() throws DatabindingFailedException {
170:                 final EReference listFeature = TestPackage.eINSTANCE.getB_CList();
171:                 final B domain = TestFactory.eINSTANCE.createB();
172:                 final C c0 = TestFactory.eINSTANCE.createC();
173:                 final C c1 = TestFactory.eINSTANCE.createC();
174:                 final C newC = TestFactory.eINSTANCE.createC();
175:                 domain.getCList().add(c0);
176:                 domain.getCList().add(c1);
177:
178:                 final Notification notification = new TestNotification(Notification.ADD, null, newC, 0, domain, listFeature);
179:                 final ModelChangeNotification mcn = new ModelChangeNotification(notification);
180:
181:                 final IndexedSetting setting = new IndexedSetting(domain, listFeature, 2);
182:                 final VIndexDomainModelReferenceSegment segment = VIndexsegmentFactory.eINSTANCE
183:                         .createIndexDomainModelReferenceSegment();
184:                 segment.setDomainModelFeature(listFeature.getName());
185:                 segment.setIndex(2);
186:                 when(segmentResolver.resolveSegment(segment, domain)).thenReturn(setting);
187:
188:                 domain.getCList().add(0, newC);
189:                 final boolean isChanged = changeTester.isStructureChanged(segment, domain, mcn);
190:
191:                 assertTrue(isChanged);
192:         }
193:
194:         /**
195:          * Tests that a change is indicated if a list element with lower index (e.g. 0) than the index of the segment (e.g.
196:          * 1) is added. This should trigger a change because the old elements are moving down the list when an element
197:          * with a lower index is added. This is only true, if the list is (now) big enough that there (now) is an element at
198:          * the index's position.
199:          *
200:          * @throws DatabindingFailedException
201:          */
202:         @Test
203:         public void testIsStructureChangedAddOnLowerIndexIrrelevant() throws DatabindingFailedException {
204:                 final EReference listFeature = TestPackage.eINSTANCE.getB_CList();
205:                 final B domain = TestFactory.eINSTANCE.createB();
206:                 final C c0 = TestFactory.eINSTANCE.createC();
207:                 final C c1 = TestFactory.eINSTANCE.createC();
208:                 final C newC = TestFactory.eINSTANCE.createC();
209:                 domain.getCList().add(c0);
210:                 domain.getCList().add(c1);
211:
212:                 final Notification notification = new TestNotification(Notification.ADD, null, newC, 0, domain, listFeature);
213:                 final ModelChangeNotification mcn = new ModelChangeNotification(notification);
214:
215:                 final IndexedSetting setting = new IndexedSetting(domain, listFeature, 3);
216:                 final VIndexDomainModelReferenceSegment segment = VIndexsegmentFactory.eINSTANCE
217:                         .createIndexDomainModelReferenceSegment();
218:                 segment.setDomainModelFeature(listFeature.getName());
219:                 segment.setIndex(3);
220:                 when(segmentResolver.resolveSegment(segment, domain)).thenReturn(setting);
221:
222:                 domain.getCList().add(0, newC);
223:                 final boolean isChanged = changeTester.isStructureChanged(segment, domain, mcn);
224:
225:                 assertFalse(isChanged);
226:         }
227:
228:         @Test
229:         public void testIsStructureChangedAddOnIndex() throws DatabindingFailedException {
230:                 final EReference listFeature = TestPackage.eINSTANCE.getB_CList();
231:                 final B domain = TestFactory.eINSTANCE.createB();
232:                 final C c0 = TestFactory.eINSTANCE.createC();
233:                 final C c1 = TestFactory.eINSTANCE.createC();
234:                 domain.getCList().add(c0);
235:                 domain.getCList().add(c1);
236:
237:                 final Notification notification = new TestNotification(Notification.ADD, null, c1, 1, domain, listFeature);
238:                 final ModelChangeNotification mcn = new ModelChangeNotification(notification);
239:
240:                 final IndexedSetting setting = new IndexedSetting(domain, listFeature, 1);
241:                 final VIndexDomainModelReferenceSegment segment = VIndexsegmentFactory.eINSTANCE
242:                         .createIndexDomainModelReferenceSegment();
243:                 segment.setDomainModelFeature(listFeature.getName());
244:                 segment.setIndex(1);
245:                 when(segmentResolver.resolveSegment(segment, domain)).thenReturn(setting);
246:
247:                 final boolean isChanged = changeTester.isStructureChanged(segment, domain, mcn);
248:
249:                 assertTrue(isChanged);
250:         }
251:
252:         @Test
253:         public void testIsStructureChangedTouchAtIndex() throws DatabindingFailedException {
254:                 final EReference listFeature = TestPackage.eINSTANCE.getB_CList();
255:                 final B domain = TestFactory.eINSTANCE.createB();
256:                 final C c0 = TestFactory.eINSTANCE.createC();
257:                 final C c1 = TestFactory.eINSTANCE.createC();
258:                 domain.getCList().add(c0);
259:                 domain.getCList().add(c1);
260:
261:                 final Notification notification = new TestNotification(Notification.RESOLVE, c1, c1, 1, domain, listFeature);
262:                 final ModelChangeNotification mcn = new ModelChangeNotification(notification);
263:
264:                 final IndexedSetting setting = new IndexedSetting(domain, listFeature, 1);
265:                 final VIndexDomainModelReferenceSegment segment = VIndexsegmentFactory.eINSTANCE
266:                         .createIndexDomainModelReferenceSegment();
267:                 segment.setDomainModelFeature(listFeature.getName());
268:                 segment.setIndex(1);
269:                 when(segmentResolver.resolveSegment(segment, domain)).thenReturn(setting);
270:
271:                 final boolean isChanged = changeTester.isStructureChanged(segment, domain, mcn);
272:
273:                 assertFalse(isChanged);
274:         }
275:
276:         @Test
277:         public void testIsStructureChangedAddMany() throws DatabindingFailedException {
278:                 final EReference listFeature = TestPackage.eINSTANCE.getB_CList();
279:                 final B domain = TestFactory.eINSTANCE.createB();
280:                 final C c0 = TestFactory.eINSTANCE.createC();
281:                 final C c1 = TestFactory.eINSTANCE.createC();
282:                 domain.getCList().add(c0);
283:                 domain.getCList().add(c1);
284:
285:                 final LinkedList<C> added = new LinkedList<C>();
286:                 added.add(c0);
287:                 added.add(c1);
288:
289:                 final Notification notification = new TestNotification(Notification.ADD_MANY, null, added,
290:                         Notification.NO_INDEX,
291:                         domain, listFeature);
292:                 final ModelChangeNotification mcn = new ModelChangeNotification(notification);
293:
294:                 final IndexedSetting setting = new IndexedSetting(domain, listFeature, 4);
295:                 final VIndexDomainModelReferenceSegment segment = VIndexsegmentFactory.eINSTANCE
296:                         .createIndexDomainModelReferenceSegment();
297:                 segment.setDomainModelFeature(listFeature.getName());
298:                 segment.setIndex(4);
299:                 when(segmentResolver.resolveSegment(segment, domain)).thenReturn(setting);
300:
301:                 final boolean isChanged = changeTester.isStructureChanged(segment, domain, mcn);
302:
303:                 assertTrue(isChanged);
304:         }
305:
306:         @Test
307:         public void testIsStructureChangedRemoveMany() throws DatabindingFailedException {
308:                 final EReference listFeature = TestPackage.eINSTANCE.getB_CList();
309:                 final B domain = TestFactory.eINSTANCE.createB();
310:                 final C c0 = TestFactory.eINSTANCE.createC();
311:                 final C c1 = TestFactory.eINSTANCE.createC();
312:
313:                 final LinkedList<C> removed = new LinkedList<C>();
314:                 removed.add(c0);
315:                 removed.add(c1);
316:
317:                 final Notification notification = new TestNotification(Notification.REMOVE_MANY, removed, null,
318:                         Notification.NO_INDEX,
319:                         domain, listFeature);
320:                 final ModelChangeNotification mcn = new ModelChangeNotification(notification);
321:
322:                 final IndexedSetting setting = new IndexedSetting(domain, listFeature, 4);
323:                 final VIndexDomainModelReferenceSegment segment = VIndexsegmentFactory.eINSTANCE
324:                         .createIndexDomainModelReferenceSegment();
325:                 segment.setDomainModelFeature(listFeature.getName());
326:                 segment.setIndex(4);
327:                 when(segmentResolver.resolveSegment(segment, domain)).thenReturn(setting);
328:
329:                 final boolean isChanged = changeTester.isStructureChanged(segment, domain, mcn);
330:
331:                 assertTrue(isChanged);
332:         }
333:
334:         @Test
335:         public void testIsStructureChangedMove() throws DatabindingFailedException {
336:                 final EReference listFeature = TestPackage.eINSTANCE.getB_CList();
337:                 final B domain = TestFactory.eINSTANCE.createB();
338:                 final C c0 = TestFactory.eINSTANCE.createC();
339:                 final C c1 = TestFactory.eINSTANCE.createC();
340:
341:                 domain.getCList().add(c0);
342:                 domain.getCList().add(c1);
343:
344:                 final Notification notification = new TestNotification(Notification.MOVE, 1, c1,
345:                         0, domain, listFeature);
346:                 final ModelChangeNotification mcn = new ModelChangeNotification(notification);
347:
348:                 final IndexedSetting setting = new IndexedSetting(domain, listFeature, 4);
349:                 final VIndexDomainModelReferenceSegment segment = VIndexsegmentFactory.eINSTANCE
350:                         .createIndexDomainModelReferenceSegment();
351:                 segment.setDomainModelFeature(listFeature.getName());
352:                 segment.setIndex(4);
353:                 when(segmentResolver.resolveSegment(segment, domain)).thenReturn(setting);
354:
355:                 final boolean isChanged = changeTester.isStructureChanged(segment, domain, mcn);
356:
357:                 assertTrue(isChanged);
358:         }
359:
360:         @Test
361:         public void testIsApplicable() {
362:                 final double score = changeTester.isApplicable(mock(VIndexDomainModelReferenceSegment.class));
363:                 assertEquals(5d, score, 0d);
364:         }
365:
366:         @Test
367:         public void testIsApplicableNullSegment() {
368:                 final double score = changeTester.isApplicable(null);
369:                 assertEquals(StructuralChangeSegmentTester.NOT_APPLICABLE, score, 0d);
370:                 verify(reportService).report(any(AbstractReport.class));
371:         }
372:
373:         @Test
374:         public void testIsApplicableWrongSegmentType() {
375:                 final double score = changeTester.isApplicable(mock(VFeatureDomainModelReferenceSegment.class));
376:                 assertEquals(StructuralChangeSegmentTester.NOT_APPLICABLE, score, 0d);
377:         }
378:
379:         private class TestNotification extends NotificationImpl {
380:                 private final EObject notifier;
381:                 private final EStructuralFeature feature;
382:
383:                 TestNotification(int eventType, Object oldValue, Object newValue, int position, EObject notifier,
384:                         EStructuralFeature feature) {
385:                         super(eventType, oldValue, newValue, position);
386:                         this.notifier = notifier;
387:                         this.feature = feature;
388:                 }
389:
390:                 @Override
391:                 public Object getNotifier() {
392:                         return notifier;
393:                 }
394:
395:                 @Override
396:                 public Object getFeature() {
397:                         return feature;
398:                 }
399:         }
400: }