Skip to content

Package: MappingSegmentStructuralChangeTester_Test$5

MappingSegmentStructuralChangeTester_Test$5

nameinstructionbranchcomplexitylinemethod
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: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
{...}
M: 0 C: 16
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.mapping;
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: import java.util.List;
26: import java.util.Map;
27:
28: import org.eclipse.emf.common.notify.Notification;
29: import org.eclipse.emf.common.notify.impl.NotificationImpl;
30: import org.eclipse.emf.common.util.EMap;
31: import org.eclipse.emf.ecore.EClass;
32: import org.eclipse.emf.ecore.EReference;
33: import org.eclipse.emf.ecore.EcoreFactory;
34: import org.eclipse.emf.ecp.view.spi.model.ModelChangeNotification;
35: import org.eclipse.emf.ecp.view.spi.model.VFeatureDomainModelReferenceSegment;
36: import org.eclipse.emfforms.core.services.databinding.testmodel.test.model.A;
37: import org.eclipse.emfforms.core.services.databinding.testmodel.test.model.C;
38: import org.eclipse.emfforms.core.services.databinding.testmodel.test.model.TestFactory;
39: import org.eclipse.emfforms.core.services.databinding.testmodel.test.model.TestPackage;
40: import org.eclipse.emfforms.spi.common.report.AbstractReport;
41: import org.eclipse.emfforms.spi.common.report.ReportService;
42: import org.eclipse.emfforms.spi.core.services.databinding.DatabindingFailedException;
43: import org.eclipse.emfforms.spi.core.services.databinding.emf.EMFFormsSegmentResolver;
44: import org.eclipse.emfforms.spi.core.services.structuralchange.StructuralChangeSegmentTester;
45: import org.eclipse.emfforms.spi.view.mappingsegment.model.VMappingDomainModelReferenceSegment;
46: import org.eclipse.emfforms.spi.view.mappingsegment.model.VMappingsegmentFactory;
47: import org.junit.Before;
48: import org.junit.Test;
49:
50: /**
51: * JUnit test cases for {@link MappingSegmentStructuralChangeTester}.
52: *
53: * @author Lucas Koehler
54: *
55: */
56: public class MappingSegmentStructuralChangeTester_Test {
57:
58:         private MappingSegmentStructuralChangeTester changeTester;
59:         private ReportService reportService;
60:         private EMFFormsSegmentResolver segmentResolver;
61:
62:         /**
63:          * Sets up a new mapping change tester for every test case.
64:          */
65:         @Before
66:         public void setUp() throws Exception {
67:                 changeTester = new MappingSegmentStructuralChangeTester();
68:                 reportService = mock(ReportService.class);
69:                 changeTester.setReportService(reportService);
70:                 segmentResolver = mock(EMFFormsSegmentResolver.class);
71:                 changeTester.setEMFFormsSegmentResolver(segmentResolver);
72:         }
73:
74:         @Test
75:         public void testIsApplicable() {
76:                 final double score = changeTester.isApplicable(mock(VMappingDomainModelReferenceSegment.class));
77:                 assertEquals(5d, score, 0d);
78:         }
79:
80:         @Test
81:         public void testIsApplicableSegmentNull() {
82:                 final double score = changeTester.isApplicable(null);
83:                 assertEquals(StructuralChangeSegmentTester.NOT_APPLICABLE, score, 0d);
84:                 verify(reportService).report(any(AbstractReport.class));
85:         }
86:
87:         @Test
88:         public void testIsApplicableWrongSegmentType() {
89:                 final double score = changeTester.isApplicable(mock(VFeatureDomainModelReferenceSegment.class));
90:                 assertEquals(StructuralChangeSegmentTester.NOT_APPLICABLE, score, 0d);
91:         }
92:
93:         @Test
94:         public void testIsStructureChangedRemove() throws DatabindingFailedException {
95:                 final int index = 1;
96:                 final C domain = TestFactory.eINSTANCE.createC();
97:                 final EClass key0 = EcoreFactory.eINSTANCE.createEClass();
98:                 final EClass key1 = EcoreFactory.eINSTANCE.createEClass();
99:                 final A value0 = TestFactory.eINSTANCE.createA();
100:                 final A value1 = TestFactory.eINSTANCE.createA();
101:                 final EMap<EClass, A> map = domain.getEClassToA();
102:                 final EReference mapFeature = TestPackage.eINSTANCE.getC_EClassToA();
103:
104:                 map.put(key0, value0);
105:                 map.put(key1, value1);
106:
107:                 final Notification notification = new NotificationImpl(Notification.REMOVE, map.get(index), null, index) {
108:
109:                         @Override
110:                         public Object getNotifier() {
111:                                 return domain;
112:                         }
113:
114:                         @Override
115:                         public Object getFeature() {
116:                                 return mapFeature;
117:                         }
118:
119:                 };
120:                 final ModelChangeNotification mcn = new ModelChangeNotification(notification);
121:
122:                 final VMappingDomainModelReferenceSegment mappingSegment = VMappingsegmentFactory.eINSTANCE
123:                         .createMappingDomainModelReferenceSegment();
124:                 mappingSegment.setMappedClass(map.get(index).getKey());
125:                 mappingSegment.setDomainModelFeature(mapFeature.getName());
126:
127:                 final MappedSetting mappedSetting = new MappedSetting(domain, mapFeature, map.get(index).getKey());
128:                 when(segmentResolver.resolveSegment(mappingSegment, domain)).thenReturn(mappedSetting);
129:
130:                 map.remove(index);
131:
132:                 final boolean isChanged = changeTester.isStructureChanged(mappingSegment, domain, mcn);
133:
134:                 assertTrue(isChanged);
135:                 assertEquals(1, map.size());
136:         }
137:
138:         @Test
139:         public void testIsStructureChangedRemoveNoChange() throws DatabindingFailedException {
140:                 final int index = 0;
141:                 final C domain = TestFactory.eINSTANCE.createC();
142:                 final EClass key0 = EcoreFactory.eINSTANCE.createEClass();
143:                 final EClass key1 = EcoreFactory.eINSTANCE.createEClass();
144:                 final A value0 = TestFactory.eINSTANCE.createA();
145:                 final A value1 = TestFactory.eINSTANCE.createA();
146:                 final EMap<EClass, A> map = domain.getEClassToA();
147:                 final EReference mapFeature = TestPackage.eINSTANCE.getC_EClassToA();
148:
149:                 map.put(key0, value0);
150:                 map.put(key1, value1);
151:
152:                 final Notification notification = new NotificationImpl(Notification.REMOVE, map.get(index), null, index) {
153:
154:                         @Override
155:                         public Object getNotifier() {
156:                                 return domain;
157:                         }
158:
159:                         @Override
160:                         public Object getFeature() {
161:                                 return mapFeature;
162:                         }
163:
164:                 };
165:                 final ModelChangeNotification mcn = new ModelChangeNotification(notification);
166:
167:                 final VMappingDomainModelReferenceSegment mappingSegment = VMappingsegmentFactory.eINSTANCE
168:                         .createMappingDomainModelReferenceSegment();
169:                 mappingSegment.setMappedClass(key1);
170:                 mappingSegment.setDomainModelFeature(mapFeature.getName());
171:
172:                 final MappedSetting mappedSetting = new MappedSetting(domain, mapFeature, key1);
173:                 when(segmentResolver.resolveSegment(mappingSegment, domain)).thenReturn(mappedSetting);
174:
175:                 map.remove(index);
176:
177:                 final boolean isChanged = changeTester.isStructureChanged(mappingSegment, domain, mcn);
178:
179:                 assertFalse(isChanged);
180:                 assertEquals(1, map.size());
181:         }
182:
183:         @Test
184:         public void testIsStructureChangedAdd() throws DatabindingFailedException {
185:                 final int index = 1;
186:                 final C domain = TestFactory.eINSTANCE.createC();
187:                 final EClass key0 = EcoreFactory.eINSTANCE.createEClass();
188:                 final EClass key1 = EcoreFactory.eINSTANCE.createEClass();
189:                 final A value0 = TestFactory.eINSTANCE.createA();
190:                 final A value1 = TestFactory.eINSTANCE.createA();
191:                 final EMap<EClass, A> map = domain.getEClassToA();
192:                 final EReference mapFeature = TestPackage.eINSTANCE.getC_EClassToA();
193:
194:                 map.put(key0, value0);
195:                 map.put(key1, value1);
196:
197:                 final Notification notification = new NotificationImpl(Notification.ADD, null, map.get(index), index) {
198:
199:                         @Override
200:                         public Object getNotifier() {
201:                                 return domain;
202:                         }
203:
204:                         @Override
205:                         public Object getFeature() {
206:                                 return mapFeature;
207:                         }
208:
209:                 };
210:                 final ModelChangeNotification mcn = new ModelChangeNotification(notification);
211:
212:                 final VMappingDomainModelReferenceSegment mappingSegment = VMappingsegmentFactory.eINSTANCE
213:                         .createMappingDomainModelReferenceSegment();
214:                 mappingSegment.setMappedClass(map.get(index).getKey());
215:                 mappingSegment.setDomainModelFeature(mapFeature.getName());
216:
217:                 final MappedSetting mappedSetting = new MappedSetting(domain, mapFeature, map.get(index).getKey());
218:                 when(segmentResolver.resolveSegment(mappingSegment, domain)).thenReturn(mappedSetting);
219:
220:                 final boolean isChanged = changeTester.isStructureChanged(mappingSegment, domain, mcn);
221:
222:                 assertTrue(isChanged);
223:                 assertEquals(2, map.size());
224:         }
225:
226:         @Test
227:         public void testIsStructureChangedAddNoChange() throws DatabindingFailedException {
228:                 final int index = 1;
229:                 final C domain = TestFactory.eINSTANCE.createC();
230:                 final EClass key0 = EcoreFactory.eINSTANCE.createEClass();
231:                 final EClass key1 = EcoreFactory.eINSTANCE.createEClass();
232:                 final A value0 = TestFactory.eINSTANCE.createA();
233:                 final A value1 = TestFactory.eINSTANCE.createA();
234:                 final EMap<EClass, A> map = domain.getEClassToA();
235:                 final EReference mapFeature = TestPackage.eINSTANCE.getC_EClassToA();
236:
237:                 map.put(key0, value0);
238:                 map.put(key1, value1);
239:
240:                 final Notification notification = new NotificationImpl(Notification.ADD, null, map.get(index), index) {
241:
242:                         @Override
243:                         public Object getNotifier() {
244:                                 return domain;
245:                         }
246:
247:                         @Override
248:                         public Object getFeature() {
249:                                 return mapFeature;
250:                         }
251:
252:                 };
253:                 final ModelChangeNotification mcn = new ModelChangeNotification(notification);
254:
255:                 final VMappingDomainModelReferenceSegment mappingSegment = VMappingsegmentFactory.eINSTANCE
256:                         .createMappingDomainModelReferenceSegment();
257:                 mappingSegment.setMappedClass(key0);
258:                 mappingSegment.setDomainModelFeature(mapFeature.getName());
259:
260:                 final MappedSetting mappedSetting = new MappedSetting(domain, mapFeature, key0);
261:                 when(segmentResolver.resolveSegment(mappingSegment, domain)).thenReturn(mappedSetting);
262:
263:                 final boolean isChanged = changeTester.isStructureChanged(mappingSegment, domain, mcn);
264:
265:                 assertFalse(isChanged);
266:                 assertEquals(2, map.size());
267:         }
268:
269:         @Test
270:         public void testIsStructureChangedWrongFeature() throws DatabindingFailedException {
271:                 final int index = 0;
272:                 final C domain = TestFactory.eINSTANCE.createC();
273:                 final EClass key0 = EcoreFactory.eINSTANCE.createEClass();
274:                 final A value0 = TestFactory.eINSTANCE.createA();
275:                 final EMap<EClass, A> map = domain.getEClassToA();
276:                 final EReference mapFeature = TestPackage.eINSTANCE.getC_EClassToA();
277:                 final EReference notificationFeature = TestPackage.eINSTANCE.getC_EClassToString();
278:
279:                 map.put(key0, value0);
280:
281:                 final Notification notification = new NotificationImpl(Notification.ADD, null, map.get(index), index) {
282:
283:                         @Override
284:                         public Object getNotifier() {
285:                                 return domain;
286:                         }
287:
288:                         @Override
289:                         public Object getFeature() {
290:                                 return notificationFeature;
291:                         }
292:
293:                 };
294:                 final ModelChangeNotification mcn = new ModelChangeNotification(notification);
295:
296:                 final VMappingDomainModelReferenceSegment mappingSegment = VMappingsegmentFactory.eINSTANCE
297:                         .createMappingDomainModelReferenceSegment();
298:                 mappingSegment.setMappedClass(map.get(index).getKey());
299:                 mappingSegment.setDomainModelFeature(mapFeature.getName());
300:
301:                 final MappedSetting mappedSetting = new MappedSetting(domain, mapFeature, map.get(index).getKey());
302:                 when(segmentResolver.resolveSegment(mappingSegment, domain)).thenReturn(mappedSetting);
303:
304:                 final boolean isChanged = changeTester.isStructureChanged(mappingSegment, domain, mcn);
305:
306:                 assertFalse(isChanged);
307:                 assertEquals(1, map.size());
308:         }
309:
310:         @Test
311:         public void testIsStructureChangedAddMany() throws DatabindingFailedException {
312:                 final C domain = TestFactory.eINSTANCE.createC();
313:                 final EClass key0 = EcoreFactory.eINSTANCE.createEClass();
314:                 final EClass key1 = EcoreFactory.eINSTANCE.createEClass();
315:                 final A value0 = TestFactory.eINSTANCE.createA();
316:                 final A value1 = TestFactory.eINSTANCE.createA();
317:                 final EMap<EClass, A> map = domain.getEClassToA();
318:                 final EReference mapFeature = TestPackage.eINSTANCE.getC_EClassToA();
319:
320:                 map.put(key0, value0);
321:                 map.put(key1, value1);
322:
323:                 final List<Map.Entry<EClass, A>> newObjects = new LinkedList<Map.Entry<EClass, A>>();
324:                 newObjects.add(map.get(0));
325:                 newObjects.add(map.get(1));
326:
327:                 final Notification notification = new NotificationImpl(Notification.ADD_MANY, null, newObjects) {
328:
329:                         @Override
330:                         public Object getNotifier() {
331:                                 return domain;
332:                         }
333:
334:                         @Override
335:                         public Object getFeature() {
336:                                 return mapFeature;
337:                         }
338:
339:                 };
340:                 final ModelChangeNotification mcn = new ModelChangeNotification(notification);
341:
342:                 final VMappingDomainModelReferenceSegment mappingSegment = VMappingsegmentFactory.eINSTANCE
343:                         .createMappingDomainModelReferenceSegment();
344:                 mappingSegment.setMappedClass(key0);
345:                 mappingSegment.setDomainModelFeature(mapFeature.getName());
346:
347:                 final MappedSetting mappedSetting = new MappedSetting(domain, mapFeature, key0);
348:                 when(segmentResolver.resolveSegment(mappingSegment, domain)).thenReturn(mappedSetting);
349:
350:                 final boolean isChanged = changeTester.isStructureChanged(mappingSegment, domain, mcn);
351:
352:                 assertTrue(isChanged);
353:                 assertEquals(2, map.size());
354:         }
355:
356:         @Test
357:         public void testIsStructureChangedRemoveMany() throws DatabindingFailedException {
358:                 final C domain = TestFactory.eINSTANCE.createC();
359:                 final EClass key0 = EcoreFactory.eINSTANCE.createEClass();
360:                 final EClass key1 = EcoreFactory.eINSTANCE.createEClass();
361:                 final A value0 = TestFactory.eINSTANCE.createA();
362:                 final A value1 = TestFactory.eINSTANCE.createA();
363:                 final EMap<EClass, A> map = domain.getEClassToA();
364:                 final EReference mapFeature = TestPackage.eINSTANCE.getC_EClassToA();
365:
366:                 map.put(key0, value0);
367:                 map.put(key1, value1);
368:
369:                 final List<Map.Entry<EClass, A>> oldObjects = new LinkedList<Map.Entry<EClass, A>>();
370:                 oldObjects.add(map.get(0));
371:                 oldObjects.add(map.get(1));
372:
373:                 final Notification notification = new NotificationImpl(Notification.REMOVE_MANY, oldObjects, null) {
374:
375:                         @Override
376:                         public Object getNotifier() {
377:                                 return domain;
378:                         }
379:
380:                         @Override
381:                         public Object getFeature() {
382:                                 return mapFeature;
383:                         }
384:
385:                 };
386:                 final ModelChangeNotification mcn = new ModelChangeNotification(notification);
387:
388:                 final VMappingDomainModelReferenceSegment mappingSegment = VMappingsegmentFactory.eINSTANCE
389:                         .createMappingDomainModelReferenceSegment();
390:                 mappingSegment.setMappedClass(key0);
391:                 mappingSegment.setDomainModelFeature(mapFeature.getName());
392:
393:                 final MappedSetting mappedSetting = new MappedSetting(domain, mapFeature, key0);
394:                 when(segmentResolver.resolveSegment(mappingSegment, domain)).thenReturn(mappedSetting);
395:
396:                 map.remove(1);
397:                 map.remove(0);
398:                 final boolean isChanged = changeTester.isStructureChanged(mappingSegment, domain, mcn);
399:
400:                 assertTrue(isChanged);
401:                 assertEquals(0, map.size());
402:         }
403:
404:         @Test
405:         public void testIsStructureChangedTouch() throws DatabindingFailedException {
406:                 final int index = 0;
407:                 final C domain = TestFactory.eINSTANCE.createC();
408:                 final EClass key0 = EcoreFactory.eINSTANCE.createEClass();
409:                 final A value0 = TestFactory.eINSTANCE.createA();
410:                 final EMap<EClass, A> map = domain.getEClassToA();
411:                 final EReference mapFeature = TestPackage.eINSTANCE.getC_EClassToA();
412:
413:                 map.put(key0, value0);
414:
415:                 final Notification notification = new NotificationImpl(Notification.RESOLVE, map.get(index), map.get(index),
416:                         index) {
417:
418:                         @Override
419:                         public Object getNotifier() {
420:                                 return domain;
421:                         }
422:
423:                         @Override
424:                         public Object getFeature() {
425:                                 return mapFeature;
426:                         }
427:
428:                 };
429:                 final ModelChangeNotification mcn = new ModelChangeNotification(notification);
430:
431:                 final VMappingDomainModelReferenceSegment mappingSegment = VMappingsegmentFactory.eINSTANCE
432:                         .createMappingDomainModelReferenceSegment();
433:                 mappingSegment.setMappedClass(map.get(index).getKey());
434:                 mappingSegment.setDomainModelFeature(mapFeature.getName());
435:
436:                 final MappedSetting mappedSetting = new MappedSetting(domain, mapFeature, map.get(index).getKey());
437:                 when(segmentResolver.resolveSegment(mappingSegment, domain)).thenReturn(mappedSetting);
438:
439:                 final boolean isChanged = changeTester.isStructureChanged(mappingSegment, domain, mcn);
440:
441:                 assertFalse(isChanged);
442:                 assertEquals(1, map.size());
443:         }
444: }