Skip to content

Package: GenerateTableColumnSegmentDmrsHandler_Test$1

GenerateTableColumnSegmentDmrsHandler_Test$1

nameinstructionbranchcomplexitylinemethod
getColumnDmrRootEClass(EClass)
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
{...}
M: 0 C: 11
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-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.emf.ecp.view.internal.editor.handler;
15:
16: import static org.junit.Assert.assertEquals;
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.mockito.Matchers.any;
21: import static org.mockito.Matchers.same;
22: import static org.mockito.Mockito.mock;
23: import static org.mockito.Mockito.never;
24: import static org.mockito.Mockito.times;
25: import static org.mockito.Mockito.verify;
26: import static org.mockito.Mockito.when;
27:
28: import java.util.Optional;
29:
30: import org.eclipse.emf.common.command.CommandStack;
31: import org.eclipse.emf.common.util.EList;
32: import org.eclipse.emf.databinding.IEMFValueProperty;
33: import org.eclipse.emf.ecore.EAttribute;
34: import org.eclipse.emf.ecore.EClass;
35: import org.eclipse.emf.ecore.EPackage;
36: import org.eclipse.emf.ecore.EReference;
37: import org.eclipse.emf.ecore.EStructuralFeature;
38: import org.eclipse.emf.ecore.EcoreFactory;
39: import org.eclipse.emf.ecore.resource.Resource;
40: import org.eclipse.emf.ecp.test.common.TestUtil;
41: import org.eclipse.emf.ecp.view.spi.model.VDomainModelReference;
42: import org.eclipse.emf.ecp.view.spi.model.VFeatureDomainModelReferenceSegment;
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.table.model.VTableControl;
46: import org.eclipse.emf.ecp.view.spi.table.model.VTableFactory;
47: import org.eclipse.emf.edit.command.AddCommand;
48: import org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain;
49: import org.eclipse.emfforms.spi.common.report.ReportService;
50: import org.eclipse.emfforms.spi.core.services.databinding.DatabindingFailedException;
51: import org.eclipse.emfforms.spi.core.services.databinding.emf.EMFFormsDatabindingEMF;
52: import org.eclipse.emfforms.view.spi.multisegment.model.VMultiDomainModelReferenceSegment;
53: import org.eclipse.emfforms.view.spi.multisegment.model.VMultisegmentFactory;
54: import org.eclipse.emfforms.view.spi.multisegment.model.VMultisegmentPackage;
55: import org.junit.Before;
56: import org.junit.Test;
57:
58: /**
59: * Unit tests for the {@link GenerateTableColumnSegmentDmrsHandler}.
60: *
61: * @author Lucas Koehler
62: *
63: */
64: public class GenerateTableColumnSegmentDmrsHandler_Test {
65:
66:         private GenerateTableColumnSegmentDmrsHandler generator;
67:         private EMFFormsDatabindingEMF databinding;
68:         private ReportService reportService;
69:
70:         private Resource resource;
71:         private VView view;
72:         private VTableControl tableControl;
73:         private EClass eClass;
74:         private EAttribute att1;
75:         private EAttribute att2;
76:         private EAttribute att3;
77:
78:         @Before
79:         public void setUp() {
80:                 databinding = mock(EMFFormsDatabindingEMF.class);
81:                 reportService = mock(ReportService.class);
82:                 generator = new GenerateTableColumnSegmentDmrsHandler(databinding, reportService);
83:                 resource = TestUtil.createResourceWithEditingDomain();
84:                 view = VViewFactory.eINSTANCE.createView();
85:                 tableControl = VTableFactory.eINSTANCE.createTableControl();
86:                 view.getChildren().add(tableControl);
87:                 resource.getContents().add(view);
88:
89:                 eClass = EcoreFactory.eINSTANCE.createEClass();
90:                 eClass.setName("A");
91:
92:                 att1 = EcoreFactory.eINSTANCE.createEAttribute();
93:                 att1.setName("a");
94:                 att2 = EcoreFactory.eINSTANCE.createEAttribute();
95:                 att2.setName("b");
96:                 att3 = EcoreFactory.eINSTANCE.createEAttribute();
97:                 att3.setName("c");
98:                 final EReference ref = EcoreFactory.eINSTANCE.createEReference();
99:                 ref.setName("ref");
100:
101:                 eClass.getEStructuralFeatures().add(att1);
102:                 eClass.getEStructuralFeatures().add(att2);
103:                 eClass.getEStructuralFeatures().add(att3);
104:                 eClass.getEStructuralFeatures().add(ref);
105:
106:                 final EPackage p = EcoreFactory.eINSTANCE.createEPackage();
107:                 p.getEClassifiers().add(eClass);
108:         }
109:
110:         @Test
111:         public void generateChildDmrs() throws DatabindingFailedException {
112:                 final EClass viewRoot = EcoreFactory.eINSTANCE.createEClass();
113:                 view.setRootEClass(viewRoot);
114:
115:                 final VDomainModelReference multiDmr = VViewFactory.eINSTANCE.createDomainModelReference();
116:                 final VMultiDomainModelReferenceSegment multiSegment = VMultisegmentFactory.eINSTANCE
117:                         .createMultiDomainModelReferenceSegment();
118:                 multiDmr.getSegments().add(multiSegment);
119:                 final VDomainModelReference existingChildDmr = VViewFactory.eINSTANCE.createDomainModelReference();
120:                 multiSegment.getChildDomainModelReferences().add(existingChildDmr);
121:                 tableControl.setDomainModelReference(multiDmr);
122:
123:                 // mock multi dmr databinding
124:                 final EReference tableReference = mock(EReference.class);
125:                 when(tableReference.getEReferenceType()).thenReturn(eClass);
126:                 mockDmrDatabinding(multiDmr, viewRoot, tableReference);
127:
128:                 // mock existing dmr databinding
129:                 mockDmrDatabinding(existingChildDmr, eClass, att1);
130:
131:                 generator.execute(tableControl);
132:
133:                 verify(reportService, never()).report(any());
134:                 final EList<VDomainModelReference> childDmrs = multiSegment.getChildDomainModelReferences();
135:                 assertEquals(3, childDmrs.size());
136:                 assertEquals(childDmrs.get(0), existingChildDmr);
137:                 assertChildDmr(childDmrs.get(1), att2);
138:                 assertChildDmr(childDmrs.get(2), att3);
139:
140:                 final CommandStack commandStack = AdapterFactoryEditingDomain.getEditingDomainFor(multiSegment)
141:                         .getCommandStack();
142:                 assertTrue(commandStack.canUndo());
143:                 assertTrue(commandStack.getMostRecentCommand() instanceof AddCommand);
144:                 final AddCommand command = (AddCommand) commandStack.getMostRecentCommand();
145:                 assertSame(VMultisegmentPackage.Literals.MULTI_DOMAIN_MODEL_REFERENCE_SEGMENT__CHILD_DOMAIN_MODEL_REFERENCES,
146:                         command.getFeature());
147:         }
148:
149:         @Test
150:         public void generateChildDmrs_noRootEClass() throws DatabindingFailedException {
151:                 final VDomainModelReference multiDmr = VViewFactory.eINSTANCE.createDomainModelReference();
152:                 final VMultiDomainModelReferenceSegment multiSegment = VMultisegmentFactory.eINSTANCE
153:                         .createMultiDomainModelReferenceSegment();
154:                 multiDmr.getSegments().add(multiSegment);
155:                 final VDomainModelReference existingChildDmr = VViewFactory.eINSTANCE.createDomainModelReference();
156:                 multiSegment.getChildDomainModelReferences().add(existingChildDmr);
157:                 tableControl.setDomainModelReference(multiDmr);
158:
159:                 generator.execute(tableControl);
160:
161:                 verify(reportService, times(1)).report(any());
162:                 // Check that nothing was done and the existing child dmr is still present
163:                 final EList<VDomainModelReference> childDmrs = multiSegment.getChildDomainModelReferences();
164:                 assertEquals(1, childDmrs.size());
165:                 assertEquals(childDmrs.get(0), existingChildDmr);
166:                 final CommandStack commandStack = AdapterFactoryEditingDomain.getEditingDomainFor(multiSegment)
167:                         .getCommandStack();
168:                 assertNull(commandStack.getMostRecentCommand());
169:         }
170:
171:         @Test
172:         public void generateChildDmrs_noMultiDmr() throws DatabindingFailedException {
173:                 final EClass viewRoot = EcoreFactory.eINSTANCE.createEClass();
174:                 view.setRootEClass(viewRoot);
175:
176:                 generator.execute(tableControl);
177:
178:                 // Check that something was reported
179:                 verify(reportService, times(1)).report(any());
180:         }
181:
182:         @Test
183:         public void generateChildDmrs_noMultiSegment() throws DatabindingFailedException {
184:                 final EClass viewRoot = EcoreFactory.eINSTANCE.createEClass();
185:                 view.setRootEClass(viewRoot);
186:
187:                 final VDomainModelReference multiDmr = VViewFactory.eINSTANCE.createDomainModelReference();
188:                 tableControl.setDomainModelReference(multiDmr);
189:
190:                 generator.execute(tableControl);
191:
192:                 // Check that something was reported
193:                 verify(reportService, times(1)).report(any());
194:         }
195:
196:         @Test
197:         public void generateChildDmrs_multiDmrResolvesToAttribute() throws DatabindingFailedException {
198:                 final EClass viewRoot = EcoreFactory.eINSTANCE.createEClass();
199:                 view.setRootEClass(viewRoot);
200:
201:                 final VDomainModelReference multiDmr = VViewFactory.eINSTANCE.createDomainModelReference();
202:                 final VMultiDomainModelReferenceSegment multiSegment = VMultisegmentFactory.eINSTANCE
203:                         .createMultiDomainModelReferenceSegment();
204:                 multiDmr.getSegments().add(multiSegment);
205:                 final VDomainModelReference existingChildDmr = VViewFactory.eINSTANCE.createDomainModelReference();
206:                 multiSegment.getChildDomainModelReferences().add(existingChildDmr);
207:                 tableControl.setDomainModelReference(multiDmr);
208:
209:                 // mock multi dmr databinding to resolve to EAttribute
210:                 final EAttribute eAttribute = mock(EAttribute.class);
211:                 mockDmrDatabinding(multiDmr, viewRoot, eAttribute);
212:
213:                 generator.execute(tableControl);
214:
215:                 verify(reportService, times(1)).report(any());
216:                 // Check that nothing was done and the existing child dmr is still present
217:                 final EList<VDomainModelReference> childDmrs = multiSegment.getChildDomainModelReferences();
218:                 assertEquals(1, childDmrs.size());
219:                 assertEquals(childDmrs.get(0), existingChildDmr);
220:                 final CommandStack commandStack = AdapterFactoryEditingDomain.getEditingDomainFor(multiSegment)
221:                         .getCommandStack();
222:                 assertNull(commandStack.getMostRecentCommand());
223:         }
224:
225:         @SuppressWarnings("unchecked")
226:         @Test
227:         public void generateChildDmrs_multiDmrDatabindingFailed() throws DatabindingFailedException {
228:                 final EClass viewRoot = EcoreFactory.eINSTANCE.createEClass();
229:                 view.setRootEClass(viewRoot);
230:
231:                 final VDomainModelReference multiDmr = VViewFactory.eINSTANCE.createDomainModelReference();
232:                 final VMultiDomainModelReferenceSegment multiSegment = VMultisegmentFactory.eINSTANCE
233:                         .createMultiDomainModelReferenceSegment();
234:                 multiDmr.getSegments().add(multiSegment);
235:                 final VDomainModelReference existingChildDmr = VViewFactory.eINSTANCE.createDomainModelReference();
236:                 multiSegment.getChildDomainModelReferences().add(existingChildDmr);
237:                 tableControl.setDomainModelReference(multiDmr);
238:
239:                 when(databinding.getValueProperty(same(multiDmr), any(EClass.class)))
240:                         .thenThrow(DatabindingFailedException.class);
241:
242:                 generator.execute(tableControl);
243:
244:                 verify(reportService, times(1)).report(any());
245:                 // Check that nothing was done and the existing child dmr is still present
246:                 final EList<VDomainModelReference> childDmrs = multiSegment.getChildDomainModelReferences();
247:                 assertEquals(1, childDmrs.size());
248:                 assertEquals(childDmrs.get(0), existingChildDmr);
249:                 final CommandStack commandStack = AdapterFactoryEditingDomain.getEditingDomainFor(multiSegment)
250:                         .getCommandStack();
251:                 assertNull(commandStack.getMostRecentCommand());
252:         }
253:
254:         /** Tests that child dmrs are still generated even if the databinding for an existing child dmr fails. */
255:         @SuppressWarnings("unchecked")
256:         @Test
257:         public void generateChildDmrs_existingDmrDatabindingFailed() throws DatabindingFailedException {
258:                 final EClass viewRoot = EcoreFactory.eINSTANCE.createEClass();
259:                 view.setRootEClass(viewRoot);
260:
261:                 final VDomainModelReference multiDmr = VViewFactory.eINSTANCE.createDomainModelReference();
262:                 final VMultiDomainModelReferenceSegment multiSegment = VMultisegmentFactory.eINSTANCE
263:                         .createMultiDomainModelReferenceSegment();
264:                 multiDmr.getSegments().add(multiSegment);
265:                 final VDomainModelReference existingChildDmr = VViewFactory.eINSTANCE.createDomainModelReference();
266:                 multiSegment.getChildDomainModelReferences().add(existingChildDmr);
267:                 tableControl.setDomainModelReference(multiDmr);
268:
269:                 // mock multi dmr databinding
270:                 final EReference tableReference = mock(EReference.class);
271:                 when(tableReference.getEReferenceType()).thenReturn(eClass);
272:                 mockDmrDatabinding(multiDmr, viewRoot, tableReference);
273:
274:                 // mock existing dmr databinding to throw exception
275:                 when(databinding.getValueProperty(same(existingChildDmr), any(EClass.class)))
276:                         .thenThrow(DatabindingFailedException.class);
277:
278:                 generator.execute(tableControl);
279:
280:                 verify(reportService, times(1)).report(any()); // for failed child dmr databinding
281:                 final EList<VDomainModelReference> childDmrs = multiSegment.getChildDomainModelReferences();
282:
283:                 assertEquals(4, childDmrs.size());
284:                 assertEquals(childDmrs.get(0), existingChildDmr);
285:                 assertChildDmr(childDmrs.get(1), att1);
286:                 assertChildDmr(childDmrs.get(2), att2);
287:                 assertChildDmr(childDmrs.get(3), att3);
288:
289:                 final CommandStack commandStack = AdapterFactoryEditingDomain.getEditingDomainFor(multiSegment)
290:                         .getCommandStack();
291:                 assertTrue(commandStack.canUndo());
292:                 assertTrue(commandStack.getMostRecentCommand() instanceof AddCommand);
293:                 final AddCommand command = (AddCommand) commandStack.getMostRecentCommand();
294:                 assertSame(VMultisegmentPackage.Literals.MULTI_DOMAIN_MODEL_REFERENCE_SEGMENT__CHILD_DOMAIN_MODEL_REFERENCES,
295:                         command.getFeature());
296:         }
297:
298:         /**
299:          * Tests that DMRs for all EAttributes for a sub class of the base column root are generated.
300:          */
301:         @Test
302:         public void generateChildDmrs_subClass() throws DatabindingFailedException {
303:                 final EAttribute subAtt = EcoreFactory.eINSTANCE.createEAttribute();
304:                 subAtt.setName("subAtt");
305:                 final EClass subClass = EcoreFactory.eINSTANCE.createEClass();
306:                 subClass.setName("SubClass");
307:                 subClass.getEStructuralFeatures().add(subAtt);
308:                 subClass.getESuperTypes().add(eClass);
309:
310:                 // Create generator that returns sub class
311:                 generator = new GenerateTableColumnSegmentDmrsHandler(databinding, reportService) {
312:
313:                         @Override
314:                         protected Optional<EClass> getColumnDmrRootEClass(EClass baseEClass) {
315:                                 return Optional.of(subClass);
316:                         }
317:                 };
318:
319:                 final EClass viewRoot = EcoreFactory.eINSTANCE.createEClass();
320:                 view.setRootEClass(viewRoot);
321:
322:                 final VDomainModelReference multiDmr = VViewFactory.eINSTANCE.createDomainModelReference();
323:                 final VMultiDomainModelReferenceSegment multiSegment = VMultisegmentFactory.eINSTANCE
324:                         .createMultiDomainModelReferenceSegment();
325:                 multiDmr.getSegments().add(multiSegment);
326:                 tableControl.setDomainModelReference(multiDmr);
327:
328:                 // mock multi dmr databinding
329:                 final EReference tableReference = mock(EReference.class);
330:                 when(tableReference.getEReferenceType()).thenReturn(eClass);
331:                 mockDmrDatabinding(multiDmr, viewRoot, tableReference);
332:
333:                 generator.execute(tableControl);
334:
335:                 verify(reportService, never()).report(any());
336:                 final EList<VDomainModelReference> childDmrs = multiSegment.getChildDomainModelReferences();
337:                 assertEquals(4, childDmrs.size());
338:                 assertChildDmr(childDmrs.get(0), att1);
339:                 assertChildDmr(childDmrs.get(1), att2);
340:                 assertChildDmr(childDmrs.get(2), att3);
341:                 assertChildDmr(childDmrs.get(3), subAtt);
342:
343:                 final CommandStack commandStack = AdapterFactoryEditingDomain.getEditingDomainFor(multiSegment)
344:                         .getCommandStack();
345:                 assertTrue(commandStack.canUndo());
346:                 assertTrue(commandStack.getMostRecentCommand() instanceof AddCommand);
347:                 final AddCommand command = (AddCommand) commandStack.getMostRecentCommand();
348:                 assertSame(VMultisegmentPackage.Literals.MULTI_DOMAIN_MODEL_REFERENCE_SEGMENT__CHILD_DOMAIN_MODEL_REFERENCES,
349:                         command.getFeature());
350:         }
351:
352:         /**
353:          * Tests that nothing happens if no subclass (not even the base column root) is used
354:          */
355:         @Test
356:         public void generateChildDmrs_noSubClass() throws DatabindingFailedException {
357:                 // Create generator that returns sub class
358:                 generator = new GenerateTableColumnSegmentDmrsHandler(databinding, reportService) {
359:
360:                         @Override
361:                         protected Optional<EClass> getColumnDmrRootEClass(EClass baseEClass) {
362:                                 return Optional.empty();
363:                         }
364:                 };
365:
366:                 final EClass viewRoot = EcoreFactory.eINSTANCE.createEClass();
367:                 view.setRootEClass(viewRoot);
368:
369:                 final VDomainModelReference multiDmr = VViewFactory.eINSTANCE.createDomainModelReference();
370:                 final VMultiDomainModelReferenceSegment multiSegment = VMultisegmentFactory.eINSTANCE
371:                         .createMultiDomainModelReferenceSegment();
372:                 multiDmr.getSegments().add(multiSegment);
373:                 tableControl.setDomainModelReference(multiDmr);
374:
375:                 // mock multi dmr databinding
376:                 final EReference tableReference = mock(EReference.class);
377:                 when(tableReference.getEReferenceType()).thenReturn(eClass);
378:                 mockDmrDatabinding(multiDmr, viewRoot, tableReference);
379:
380:                 generator.execute(tableControl);
381:
382:                 verify(reportService, never()).report(any());
383:                 final EList<VDomainModelReference> childDmrs = multiSegment.getChildDomainModelReferences();
384:                 assertEquals(0, childDmrs.size());
385:
386:                 final CommandStack commandStack = AdapterFactoryEditingDomain.getEditingDomainFor(multiSegment)
387:                         .getCommandStack();
388:                 assertNull(commandStack.getMostRecentCommand());
389:         }
390:         // ------------------------
391:         // TEST HELPERS
392:         // ------------------------
393:
394:         private void mockDmrDatabinding(VDomainModelReference dmr, EClass dmrRoot, EStructuralFeature dmrTarget)
395:                 throws DatabindingFailedException {
396:                 final IEMFValueProperty property = mock(IEMFValueProperty.class);
397:                 when(property.getStructuralFeature()).thenReturn(dmrTarget);
398:                 when(databinding.getValueProperty(dmr, dmrRoot)).thenReturn(property);
399:         }
400:
401:         private void assertChildDmr(VDomainModelReference childDmr, EStructuralFeature targetFeature) {
402:                 assertEquals(childDmr.getSegments().size(), 1);
403:                 assertTrue("Child dmr segment is a feature segment",
404:                         childDmr.getSegments().get(0) instanceof VFeatureDomainModelReferenceSegment);
405:                 final VFeatureDomainModelReferenceSegment featureSegment = (VFeatureDomainModelReferenceSegment) childDmr
406:                         .getSegments().get(0);
407:                 assertEquals(targetFeature.getName(), featureSegment.getDomainModelFeature());
408:         }
409: }