Skip to content

Package: DmrToSegmentsMigratorImpl_Test

DmrToSegmentsMigratorImpl_Test

nameinstructionbranchcomplexitylinemethod
DmrToSegmentsMigratorImpl_Test()
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%
createViewResource()
M: 0 C: 20
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
lambda$0(VDomainModelReferenceSegment, VDomainModelReference, VDomainModelReference)
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
lambda$1(VDomainModelReferenceSegment, VDomainModelReference, VDomainModelReference)
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
needsMigration_legacyOnly()
M: 0 C: 30
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 9
100%
M: 0 C: 1
100%
needsMigration_mixed()
M: 0 C: 44
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 13
100%
M: 0 C: 1
100%
needsMigration_segmentDmrsOnly()
M: 0 C: 30
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 8
100%
M: 0 C: 1
100%
performMigration()
M: 0 C: 210
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 44
100%
M: 0 C: 1
100%
performMigration_emptyResource()
M: 0 C: 13
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
performMigration_mixed()
M: 0 C: 113
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 27
100%
M: 0 C: 1
100%
performMigration_preReplaceProcessor()
M: 0 C: 122
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 29
100%
M: 0 C: 1
100%
performMigration_tableWithCrossReference()
M: 0 C: 130
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 34
100%
M: 0 C: 1
100%
setUp()
M: 0 C: 30
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 7
100%
M: 0 C: 1
100%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2011-2019 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.ide.view.segments;
15:
16: import static org.junit.Assert.assertEquals;
17: import static org.junit.Assert.assertFalse;
18: import static org.junit.Assert.assertNotNull;
19: import static org.junit.Assert.assertNotSame;
20: import static org.junit.Assert.assertSame;
21: import static org.junit.Assert.assertTrue;
22: import static org.mockito.Matchers.any;
23: import static org.mockito.Mockito.mock;
24: import static org.mockito.Mockito.never;
25: import static org.mockito.Mockito.times;
26: import static org.mockito.Mockito.verify;
27: import static org.mockito.Mockito.when;
28:
29: import java.util.Arrays;
30: import java.util.Collections;
31:
32: import org.eclipse.emf.common.util.URI;
33: import org.eclipse.emf.ecore.resource.Resource;
34: import org.eclipse.emf.ecore.resource.ResourceSet;
35: import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
36: import org.eclipse.emf.ecp.view.spi.group.model.VGroup;
37: import org.eclipse.emf.ecp.view.spi.group.model.VGroupFactory;
38: import org.eclipse.emf.ecp.view.spi.model.VControl;
39: import org.eclipse.emf.ecp.view.spi.model.VDomainModelReference;
40: import org.eclipse.emf.ecp.view.spi.model.VDomainModelReferenceSegment;
41: import org.eclipse.emf.ecp.view.spi.model.VFeatureDomainModelReferenceSegment;
42: import org.eclipse.emf.ecp.view.spi.model.VFeaturePathDomainModelReference;
43: import org.eclipse.emf.ecp.view.spi.model.VView;
44: import org.eclipse.emf.ecp.view.spi.model.VViewFactory;
45: import org.eclipse.emf.ecp.view.spi.model.VViewPackage;
46: import org.eclipse.emf.ecp.view.spi.model.util.VViewResourceFactoryImpl;
47: import org.eclipse.emf.ecp.view.spi.table.model.VTableControl;
48: import org.eclipse.emf.ecp.view.spi.table.model.VTableDomainModelReference;
49: import org.eclipse.emf.ecp.view.spi.table.model.VTableFactory;
50: import org.eclipse.emf.ecp.view.spi.table.model.VWidthConfiguration;
51: import org.eclipse.emfforms.spi.common.report.ReportService;
52: import org.eclipse.emfforms.spi.core.services.segments.EMFFormsSegmentGenerator;
53: import org.eclipse.emfforms.spi.ide.view.segments.DmrToSegmentsMigrationException;
54: import org.eclipse.emfforms.spi.ide.view.segments.DmrToSegmentsMigrator.PreReplaceProcessor;
55: import org.eclipse.emfforms.view.spi.multisegment.model.VMultiDomainModelReferenceSegment;
56: import org.eclipse.emfforms.view.spi.multisegment.model.VMultisegmentFactory;
57: import org.junit.Before;
58: import org.junit.Test;
59:
60: /**
61: * Unit tests for {@link DmrToSegmentsMigratorImpl}. These tests test the migration behavior based on a mocked
62: * {@link EMFFormsSegmentGenerator} and with an already loaded Resource.
63: * <p>
64: * For a "realistic" integration test see {@link DmrToSegmentsMigratorImpl_ITest}.
65: *
66: * @author Lucas Koehler
67: *
68: */
69: public class DmrToSegmentsMigratorImpl_Test {
70:
71:         private DmrToSegmentsMigratorImpl migrator;
72:         private ReportService reportService;
73:         private EMFFormsSegmentGenerator segmentGenerator;
74:         private Resource resource;
75:
76:         @Before
77:         public void setUp() throws Exception {
78:                 reportService = mock(ReportService.class);
79:                 segmentGenerator = mock(EMFFormsSegmentGenerator.class);
80:                 migrator = new DmrToSegmentsMigratorImpl();
81:                 migrator.setReportService(reportService);
82:                 migrator.setEMFFormsSegmentGenerator(segmentGenerator);
83:                 resource = createViewResource();
84:         }
85:
86:         @Test
87:         public void performMigration() throws DmrToSegmentsMigrationException {
88:                 final VView view = VViewFactory.eINSTANCE.createView();
89:                 final VControl control1 = VViewFactory.eINSTANCE.createControl();
90:                 final VDomainModelReference featureDmr1 = VViewFactory.eINSTANCE.createFeaturePathDomainModelReference();
91:                 control1.setDomainModelReference(featureDmr1);
92:                 view.getChildren().add(control1);
93:
94:                 final VControl control2 = VViewFactory.eINSTANCE.createControl();
95:                 final VDomainModelReference featureDmr2 = VViewFactory.eINSTANCE.createFeaturePathDomainModelReference();
96:                 control2.setDomainModelReference(featureDmr2);
97:                 final VGroup group = VGroupFactory.eINSTANCE.createGroup();
98:                 group.getChildren().add(control2);
99:                 view.getChildren().add(group);
100:
101:                 // mock segment generation
102:                 final VFeatureDomainModelReferenceSegment segment11 = VViewFactory.eINSTANCE
103:                         .createFeatureDomainModelReferenceSegment();
104:                 final VFeatureDomainModelReferenceSegment segment12 = VViewFactory.eINSTANCE
105:                         .createFeatureDomainModelReferenceSegment();
106:                 when(segmentGenerator.generateSegments(featureDmr1)).thenReturn(Arrays.asList(segment11, segment12));
107:                 final VFeatureDomainModelReferenceSegment segment2 = VViewFactory.eINSTANCE
108:                         .createFeatureDomainModelReferenceSegment();
109:                 when(segmentGenerator.generateSegments(featureDmr2)).thenReturn(Arrays.asList(segment2));
110:
111:                 resource.getContents().add(view);
112:
113:                 migrator.performMigration(resource);
114:
115:                 assertEquals(1, resource.getContents().size());
116:                 assertSame(view, resource.getContents().get(0));
117:                 assertEquals(2, view.getChildren().size());
118:                 assertSame(control1, view.getChildren().get(0));
119:
120:                 // check migrated dmr of control1
121:                 final VDomainModelReference migratedDmr1 = control1.getDomainModelReference();
122:                 assertNotNull(migratedDmr1);
123:                 assertNotSame(featureDmr1, migratedDmr1);
124:                 assertSame(VViewPackage.Literals.DOMAIN_MODEL_REFERENCE, migratedDmr1.eClass());
125:                 assertEquals(2, migratedDmr1.getSegments().size());
126:                 assertSame(segment11, migratedDmr1.getSegments().get(0));
127:                 assertSame(segment12, migratedDmr1.getSegments().get(1));
128:
129:                 // check layout and migrated dmr of control2
130:                 assertSame(group, view.getChildren().get(1));
131:                 assertEquals(1, group.getChildren().size());
132:                 assertSame(control2, group.getChildren().get(0));
133:                 final VDomainModelReference migratedDmr2 = control2.getDomainModelReference();
134:                 assertNotNull(migratedDmr2);
135:                 assertNotSame(featureDmr2, migratedDmr2);
136:                 assertSame(VViewPackage.Literals.DOMAIN_MODEL_REFERENCE, migratedDmr2.eClass());
137:                 assertEquals(1, migratedDmr2.getSegments().size());
138:                 assertSame(segment2, migratedDmr2.getSegments().get(0));
139:
140:                 verify(segmentGenerator, times(2)).generateSegments(any());
141:                 verify(reportService, never()).report(any());
142:         }
143:
144:         @Test
145:         public void performMigration_tableWithCrossReference() throws DmrToSegmentsMigrationException {
146:                 final VView view = VViewFactory.eINSTANCE.createView();
147:                 final VTableControl table = VTableFactory.eINSTANCE.createTableControl();
148:                 view.getChildren().add(table);
149:                 final VTableDomainModelReference tableDmr = VTableFactory.eINSTANCE.createTableDomainModelReference();
150:                 table.setDomainModelReference(tableDmr);
151:
152:                 final VFeaturePathDomainModelReference columnDmr1 = VViewFactory.eINSTANCE
153:                         .createFeaturePathDomainModelReference();
154:                 final VFeaturePathDomainModelReference columnDmr2 = VViewFactory.eINSTANCE
155:                         .createFeaturePathDomainModelReference();
156:                 tableDmr.getColumnDomainModelReferences().add(columnDmr1);
157:                 tableDmr.getColumnDomainModelReferences().add(columnDmr2);
158:
159:                 final VWidthConfiguration widthConfig = VTableFactory.eINSTANCE.createWidthConfiguration();
160:                 widthConfig.setMinWidth(100);
161:                 widthConfig.setColumnDomainReference(columnDmr2);
162:                 table.getColumnConfigurations().add(widthConfig);
163:
164:                 final VMultiDomainModelReferenceSegment multiSegment = VMultisegmentFactory.eINSTANCE
165:                         .createMultiDomainModelReferenceSegment();
166:                 final VDomainModelReference childDmr1 = VViewFactory.eINSTANCE.createDomainModelReference();
167:                 final VDomainModelReference childDmr2 = VViewFactory.eINSTANCE.createDomainModelReference();
168:                 multiSegment.getChildDomainModelReferences().add(childDmr1);
169:                 multiSegment.getChildDomainModelReferences().add(childDmr2);
170:
171:                 when(segmentGenerator.generateSegments(tableDmr)).thenReturn(Collections.singletonList(multiSegment));
172:
173:                 resource.getContents().add(view);
174:                 migrator.performMigration(resource);
175:
176:                 assertSame(resource, table.eResource());
177:                 final VDomainModelReference newTableDmr = table.getDomainModelReference();
178:                 assertNotNull(newTableDmr);
179:                 assertSame(VViewPackage.Literals.DOMAIN_MODEL_REFERENCE, newTableDmr.eClass());
180:                 assertEquals(1, newTableDmr.getSegments().size());
181:                 assertSame(multiSegment, newTableDmr.getSegments().get(0));
182:                 assertSame(widthConfig, table.getColumnConfigurations().get(0));
183:                 assertSame(childDmr2, widthConfig.getColumnDomainReference());
184:                 assertEquals(100, widthConfig.getMinWidth());
185:         }
186:
187:         /**
188:          * Tests migration for a view with a legacy and a segment dmr.
189:          *
190:          * @throws DmrToSegmentsMigrationException
191:          */
192:         @Test
193:         public void performMigration_mixed() throws DmrToSegmentsMigrationException {
194:                 final VView view = VViewFactory.eINSTANCE.createView();
195:                 final VControl legacyControl = VViewFactory.eINSTANCE.createControl();
196:                 final VControl segmentControl = VViewFactory.eINSTANCE.createControl();
197:
198:                 final VFeaturePathDomainModelReference legacyDmr = VViewFactory.eINSTANCE
199:                         .createFeaturePathDomainModelReference();
200:                 legacyControl.setDomainModelReference(legacyDmr);
201:                 final VDomainModelReference segmentDmr = VViewFactory.eINSTANCE.createDomainModelReference();
202:                 segmentControl.setDomainModelReference(segmentDmr);
203:
204:                 view.getChildren().add(legacyControl);
205:                 view.getChildren().add(segmentControl);
206:
207:                 final VFeatureDomainModelReferenceSegment featureSegment = VViewFactory.eINSTANCE
208:                         .createFeatureDomainModelReferenceSegment();
209:                 when(segmentGenerator.generateSegments(legacyDmr)).thenReturn(Collections.singletonList(featureSegment));
210:
211:                 resource.getContents().add(view);
212:                 migrator.performMigration(resource);
213:
214:                 assertSame(resource, legacyControl.eResource());
215:                 final VDomainModelReference dmr = legacyControl.getDomainModelReference();
216:                 assertNotNull(dmr);
217:                 assertNotSame(legacyDmr, dmr);
218:                 assertSame(VViewPackage.Literals.DOMAIN_MODEL_REFERENCE, dmr.eClass());
219:                 assertEquals(1, dmr.getSegments().size());
220:
221:                 assertSame(featureSegment, dmr.getSegments().get(0));
222:
223:                 assertSame(resource, segmentControl.eResource());
224:                 assertSame(segmentDmr, segmentControl.getDomainModelReference());
225:                 verify(reportService, never()).report(any());
226:                 verify(segmentGenerator, times(1)).generateSegments(any());
227:         }
228:
229:         /**
230:          * Tests that the migration applies given {@link PreReplaceProcessor}s and does so in the correct order.
231:          *
232:          * @throws DmrToSegmentsMigrationException
233:          */
234:         @Test
235:         public void performMigration_preReplaceProcessor() throws DmrToSegmentsMigrationException {
236:                 final VView view = VViewFactory.eINSTANCE.createView();
237:                 final VControl legacyControl = VViewFactory.eINSTANCE.createControl();
238:
239:                 final VFeaturePathDomainModelReference legacyDmr = VViewFactory.eINSTANCE
240:                         .createFeaturePathDomainModelReference();
241:                 legacyControl.setDomainModelReference(legacyDmr);
242:
243:                 view.getChildren().add(legacyControl);
244:                 resource.getContents().add(view);
245:
246:                 final VFeatureDomainModelReferenceSegment featureSegment = VViewFactory.eINSTANCE
247:                         .createFeatureDomainModelReferenceSegment();
248:                 when(segmentGenerator.generateSegments(legacyDmr)).thenReturn(Collections.singletonList(featureSegment));
249:
250:                 // mock pre replace processors
251:                 final VDomainModelReferenceSegment addedByProcessor1 = VViewFactory.eINSTANCE
252:                         .createFeatureDomainModelReferenceSegment();
253:                 final VDomainModelReferenceSegment addedByProcessor2 = VViewFactory.eINSTANCE
254:                         .createFeatureDomainModelReferenceSegment();
255:                 final PreReplaceProcessor processor1 = (legacy, segment) -> {
256:                         segment.getSegments().add(addedByProcessor1);
257:                 };
258:                 final PreReplaceProcessor processor2 = (legacy, segment) -> {
259:                         segment.getSegments().add(addedByProcessor2);
260:                 };
261:
262:                 migrator.performMigration(resource, processor1, processor2);
263:
264:                 assertSame(resource, legacyControl.eResource());
265:                 final VDomainModelReference dmr = legacyControl.getDomainModelReference();
266:                 assertNotNull(dmr);
267:                 assertNotSame(legacyDmr, dmr);
268:                 assertSame(VViewPackage.Literals.DOMAIN_MODEL_REFERENCE, dmr.eClass());
269:                 assertEquals(3, dmr.getSegments().size());
270:
271:                 assertSame(featureSegment, dmr.getSegments().get(0));
272:                 assertSame(addedByProcessor1, dmr.getSegments().get(1));
273:                 assertSame(addedByProcessor2, dmr.getSegments().get(2));
274:
275:                 verify(reportService, never()).report(any());
276:                 verify(segmentGenerator, times(1)).generateSegments(any());
277:         }
278:
279:         @Test
280:         public void performMigration_emptyResource() throws DmrToSegmentsMigrationException {
281:                 migrator.performMigration(resource);
282:
283:                 assertTrue(resource.getContents().isEmpty());
284:         }
285:
286:         @Test
287:         public void needsMigration_mixed() {
288:                 final VView view = VViewFactory.eINSTANCE.createView();
289:                 final VControl legacyControl = VViewFactory.eINSTANCE.createControl();
290:                 final VControl segmentControl = VViewFactory.eINSTANCE.createControl();
291:
292:                 final VFeaturePathDomainModelReference legacyDmr = VViewFactory.eINSTANCE
293:                         .createFeaturePathDomainModelReference();
294:                 legacyControl.setDomainModelReference(legacyDmr);
295:                 final VDomainModelReference segmentDmr = VViewFactory.eINSTANCE.createDomainModelReference();
296:                 segmentControl.setDomainModelReference(segmentDmr);
297:
298:                 view.getChildren().add(legacyControl);
299:                 view.getChildren().add(segmentControl);
300:
301:                 resource.getContents().add(view);
302:                 assertTrue(migrator.needsMigration(resource));
303:         }
304:
305:         @Test
306:         public void needsMigration_legacyOnly() {
307:                 final VView view = VViewFactory.eINSTANCE.createView();
308:                 final VControl legacyControl = VViewFactory.eINSTANCE.createControl();
309:
310:                 final VFeaturePathDomainModelReference legacyDmr = VViewFactory.eINSTANCE
311:                         .createFeaturePathDomainModelReference();
312:                 legacyControl.setDomainModelReference(legacyDmr);
313:
314:                 view.getChildren().add(legacyControl);
315:
316:                 resource.getContents().add(view);
317:                 assertTrue(migrator.needsMigration(resource));
318:         }
319:
320:         @Test
321:         public void needsMigration_segmentDmrsOnly() {
322:                 final VView view = VViewFactory.eINSTANCE.createView();
323:                 final VControl segmentControl = VViewFactory.eINSTANCE.createControl();
324:
325:                 final VDomainModelReference segmentDmr = VViewFactory.eINSTANCE.createDomainModelReference();
326:                 segmentControl.setDomainModelReference(segmentDmr);
327:
328:                 view.getChildren().add(segmentControl);
329:
330:                 resource.getContents().add(view);
331:                 assertFalse(migrator.needsMigration(resource));
332:         }
333:
334:         private Resource createViewResource() {
335:                 final ResourceSet rs = new ResourceSetImpl();
336:                 rs.getResourceFactoryRegistry().getExtensionToFactoryMap().put(Resource.Factory.Registry.DEFAULT_EXTENSION,
337:                         new VViewResourceFactoryImpl());
338:                 final Resource resource = rs.createResource(URI.createURI("VIRTUAL-RESOURCE")); //$NON-NLS-1$
339:                 return resource;
340:         }
341: }