Skip to content

Package: MappingSegmentExpander_Test

MappingSegmentExpander_Test

nameinstructionbranchcomplexitylinemethod
MappingSegmentExpander_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%
setUp()
M: 0 C: 16
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
testIsApplicable()
M: 0 C: 12
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
testIsApplicableNull()
M: 0 C: 19
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
testIsApplicableWrongSegmentType()
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%
testPrepareDomainObjectBothNull()
M: 7 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
testPrepareDomainObjectEObjectNull()
M: 9 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
testPrepareDomainObjectKeyAndValuePresent()
M: 0 C: 53
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 15
100%
M: 0 C: 1
100%
testPrepareDomainObjectKeyNotPresent()
M: 0 C: 49
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 14
100%
M: 0 C: 1
100%
testPrepareDomainObjectKeyPresentValueNull()
M: 0 C: 54
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 15
100%
M: 0 C: 1
100%
testPrepareDomainObjectMapValueAttribute()
M: 7 C: 19
73%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 2 C: 7
78%
M: 0 C: 1
100%
testPrepareDomainObjectNoMap()
M: 7 C: 19
73%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 2 C: 7
78%
M: 0 C: 1
100%
testPrepareDomainObjectSegmentNull()
M: 9 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
testPrepareDomainObjectValueRefAbstractKeyAndValueNotPresent()
M: 7 C: 19
73%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 2 C: 7
78%
M: 0 C: 1
100%
testPrepareDomainObjectValueRefAbstractKeyAndValuePresent()
M: 0 C: 53
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 15
100%
M: 0 C: 1
100%
testPrepareDomainObjectValueRefAbstractValueNotPresent()
M: 7 C: 29
81%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 2 C: 9
82%
M: 0 C: 1
100%
testPrepareDomainObjectValueRefAbstractWithConcreteMappedClass()
M: 0 C: 45
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 12
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.assertTrue;
18: import static org.mockito.Matchers.any;
19: import static org.mockito.Mockito.mock;
20: import static org.mockito.Mockito.verify;
21:
22: import java.util.Optional;
23:
24: import org.eclipse.emf.common.util.EMap;
25: import org.eclipse.emf.ecore.EClass;
26: import org.eclipse.emf.ecore.EObject;
27: import org.eclipse.emf.ecore.EReference;
28: import org.eclipse.emf.ecore.EcoreFactory;
29: import org.eclipse.emf.ecp.view.spi.model.VDomainModelReferenceSegment;
30: import org.eclipse.emfforms.core.services.databinding.testmodel.test.model.A;
31: import org.eclipse.emfforms.core.services.databinding.testmodel.test.model.B;
32: import org.eclipse.emfforms.core.services.databinding.testmodel.test.model.C;
33: import org.eclipse.emfforms.core.services.databinding.testmodel.test.model.E;
34: import org.eclipse.emfforms.core.services.databinding.testmodel.test.model.TestFactory;
35: import org.eclipse.emfforms.core.services.databinding.testmodel.test.model.TestPackage;
36: import org.eclipse.emfforms.spi.common.report.AbstractReport;
37: import org.eclipse.emfforms.spi.common.report.ReportService;
38: import org.eclipse.emfforms.spi.core.services.domainexpander.EMFFormsDMRSegmentExpander;
39: import org.eclipse.emfforms.spi.core.services.domainexpander.EMFFormsExpandingFailedException;
40: import org.eclipse.emfforms.spi.view.mappingsegment.model.VMappingDomainModelReferenceSegment;
41: import org.eclipse.emfforms.spi.view.mappingsegment.model.VMappingsegmentFactory;
42: import org.junit.Before;
43: import org.junit.Test;
44:
45: /**
46: * JUnit tests for {@link MappingSegmentExpander}.
47: *
48: * @author Lucas Koehler
49: *
50: */
51: public class MappingSegmentExpander_Test {
52:
53:         private MappingSegmentExpander expander;
54:         private ReportService reportService;
55:
56:         /**
57:          * Instantiates a new mapping expander for every test case.
58:          */
59:         @Before
60:         public void setUp() {
61:                 expander = new MappingSegmentExpander();
62:                 reportService = mock(ReportService.class);
63:                 expander.setReportService(reportService);
64:         }
65:
66:         @Test
67:         public void testIsApplicable() {
68:                 final double result = expander.isApplicable(mock(VMappingDomainModelReferenceSegment.class));
69:                 assertEquals(5d, result, 0d);
70:         }
71:
72:         @Test
73:         public void testIsApplicableNull() {
74:                 final double result = expander.isApplicable(null);
75:                 assertEquals(EMFFormsDMRSegmentExpander.NOT_APPLICABLE, result, 0d);
76:                 verify(reportService).report(any(AbstractReport.class));
77:         }
78:
79:         @Test
80:         public void testIsApplicableWrongSegmentType() {
81:                 final double result = expander.isApplicable(mock(VDomainModelReferenceSegment.class));
82:                 assertEquals(EMFFormsDMRSegmentExpander.NOT_APPLICABLE, result, 0d);
83:         }
84:
85:         @Test(expected = IllegalArgumentException.class)
86:         public void testPrepareDomainObjectSegmentNull() throws EMFFormsExpandingFailedException {
87:                 expander.prepareDomainObject(null, mock(EObject.class));
88:         }
89:
90:         @Test(expected = IllegalArgumentException.class)
91:         public void testPrepareDomainObjectEObjectNull() throws EMFFormsExpandingFailedException {
92:                 expander.prepareDomainObject(mock(VMappingDomainModelReferenceSegment.class), null);
93:         }
94:
95:         @Test(expected = IllegalArgumentException.class)
96:         public void testPrepareDomainObjectBothNull() throws EMFFormsExpandingFailedException {
97:                 expander.prepareDomainObject(null, null);
98:         }
99:
100:         @Test(expected = EMFFormsExpandingFailedException.class)
101:         public void testPrepareDomainObjectNoMap() throws EMFFormsExpandingFailedException {
102:                 final B domain = TestFactory.eINSTANCE.createB();
103:                 final EClass key = EcoreFactory.eINSTANCE.createEClass();
104:                 final EReference structuralFeature = TestPackage.eINSTANCE.getB_CList();
105:                 final VMappingDomainModelReferenceSegment mappingSegment = VMappingsegmentFactory.eINSTANCE
106:                         .createMappingDomainModelReferenceSegment();
107:                 mappingSegment.setDomainModelFeature(structuralFeature.getName());
108:                 mappingSegment.setMappedClass(key);
109:
110:                 expander.prepareDomainObject(mappingSegment, domain);
111:         }
112:
113:         @Test(expected = EMFFormsExpandingFailedException.class)
114:         public void testPrepareDomainObjectMapValueAttribute() throws EMFFormsExpandingFailedException {
115:                 final C domain = TestFactory.eINSTANCE.createC();
116:                 final EClass key = EcoreFactory.eINSTANCE.createEClass();
117:                 final EReference structuralFeature = TestPackage.eINSTANCE.getC_EClassToString();
118:                 final VMappingDomainModelReferenceSegment mappingSegment = VMappingsegmentFactory.eINSTANCE
119:                         .createMappingDomainModelReferenceSegment();
120:                 mappingSegment.setDomainModelFeature(structuralFeature.getName());
121:                 mappingSegment.setMappedClass(key);
122:
123:                 expander.prepareDomainObject(mappingSegment, domain);
124:         }
125:
126:         @SuppressWarnings("unchecked")
127:         @Test
128:         public void testPrepareDomainObjectKeyAndValuePresent() throws EMFFormsExpandingFailedException {
129:                 final EReference structuralFeature = TestPackage.eINSTANCE.getC_EClassToA();
130:                 final C domain = TestFactory.eINSTANCE.createC();
131:                 final EClass key = EcoreFactory.eINSTANCE.createEClass();
132:                 final A value = TestFactory.eINSTANCE.createA();
133:                 final EMap<EClass, A> map = (EMap<EClass, A>) domain.eGet(structuralFeature);
134:                 map.put(key, value);
135:
136:                 final VMappingDomainModelReferenceSegment mappingSegment = VMappingsegmentFactory.eINSTANCE
137:                         .createMappingDomainModelReferenceSegment();
138:                 mappingSegment.setDomainModelFeature(structuralFeature.getName());
139:                 mappingSegment.setMappedClass(key);
140:
141:                 final Optional<EObject> result = expander.prepareDomainObject(mappingSegment, domain);
142:
143:                 assertEquals(value, result.get());
144:                 assertEquals(value, map.get(key));
145:                 assertEquals(1, map.size());
146:         }
147:
148:         @SuppressWarnings("unchecked")
149:         @Test
150:         public void testPrepareDomainObjectKeyPresentValueNull() throws EMFFormsExpandingFailedException {
151:                 final EReference structuralFeature = TestPackage.eINSTANCE.getC_EClassToA();
152:                 final C domain = TestFactory.eINSTANCE.createC();
153:                 final EClass key = EcoreFactory.eINSTANCE.createEClass();
154:                 final EMap<EClass, A> map = (EMap<EClass, A>) domain.eGet(structuralFeature);
155:                 map.put(key, null);
156:
157:                 final VMappingDomainModelReferenceSegment mappingSegment = VMappingsegmentFactory.eINSTANCE
158:                         .createMappingDomainModelReferenceSegment();
159:                 mappingSegment.setDomainModelFeature(structuralFeature.getName());
160:                 mappingSegment.setMappedClass(key);
161:
162:                 final Optional<EObject> result = expander.prepareDomainObject(mappingSegment, domain);
163:
164:                 assertTrue(result.isPresent());
165:                 assertTrue(result.get() instanceof A);
166:                 assertEquals(result.get(), map.get(key));
167:                 assertEquals(1, map.size());
168:         }
169:
170:         @SuppressWarnings("unchecked")
171:         @Test
172:         public void testPrepareDomainObjectKeyNotPresent() throws EMFFormsExpandingFailedException {
173:                 final EReference structuralFeature = TestPackage.eINSTANCE.getC_EClassToA();
174:                 final C domain = TestFactory.eINSTANCE.createC();
175:                 final EClass key = EcoreFactory.eINSTANCE.createEClass();
176:                 final EMap<EClass, A> map = (EMap<EClass, A>) domain.eGet(structuralFeature);
177:
178:                 final VMappingDomainModelReferenceSegment mappingSegment = VMappingsegmentFactory.eINSTANCE
179:                         .createMappingDomainModelReferenceSegment();
180:                 mappingSegment.setDomainModelFeature(structuralFeature.getName());
181:                 mappingSegment.setMappedClass(key);
182:
183:                 final Optional<EObject> result = expander.prepareDomainObject(mappingSegment, domain);
184:
185:                 assertTrue(result.isPresent());
186:                 assertTrue(result.get() instanceof A);
187:                 assertEquals(result.get(), map.get(key));
188:                 assertEquals(1, map.size());
189:         }
190:
191:         @SuppressWarnings("unchecked")
192:         @Test
193:         public void testPrepareDomainObjectValueRefAbstractKeyAndValuePresent() throws EMFFormsExpandingFailedException {
194:                 final EReference structuralFeature = TestPackage.eINSTANCE.getC_EClassToE();
195:                 final C domain = TestFactory.eINSTANCE.createC();
196:                 final EClass key = EcoreFactory.eINSTANCE.createEClass();
197:                 final A value = TestFactory.eINSTANCE.createA();
198:                 final EMap<EClass, E> map = (EMap<EClass, E>) domain.eGet(structuralFeature);
199:                 map.put(key, value);
200:
201:                 final VMappingDomainModelReferenceSegment mappingSegment = VMappingsegmentFactory.eINSTANCE
202:                         .createMappingDomainModelReferenceSegment();
203:                 mappingSegment.setDomainModelFeature(structuralFeature.getName());
204:                 mappingSegment.setMappedClass(key);
205:
206:                 final Optional<EObject> result = expander.prepareDomainObject(mappingSegment, domain);
207:
208:                 assertEquals(value, result.get());
209:                 assertEquals(value, map.get(key));
210:                 assertEquals(1, map.size());
211:         }
212:
213:         @SuppressWarnings("unchecked")
214:         @Test(expected = EMFFormsExpandingFailedException.class)
215:         public void testPrepareDomainObjectValueRefAbstractValueNotPresent() throws EMFFormsExpandingFailedException {
216:                 final EReference structuralFeature = TestPackage.eINSTANCE.getC_EClassToE();
217:                 final C domain = TestFactory.eINSTANCE.createC();
218:                 final EClass key = EcoreFactory.eINSTANCE.createEClass();
219:                 final EMap<EClass, E> map = (EMap<EClass, E>) domain.eGet(structuralFeature);
220:                 map.put(key, null);
221:
222:                 final VMappingDomainModelReferenceSegment mappingSegment = VMappingsegmentFactory.eINSTANCE
223:                         .createMappingDomainModelReferenceSegment();
224:                 mappingSegment.setDomainModelFeature(structuralFeature.getName());
225:                 mappingSegment.setMappedClass(key);
226:
227:                 expander.prepareDomainObject(mappingSegment, domain);
228:         }
229:
230:         @Test(expected = EMFFormsExpandingFailedException.class)
231:         public void testPrepareDomainObjectValueRefAbstractKeyAndValueNotPresent() throws EMFFormsExpandingFailedException {
232:                 final EReference structuralFeature = TestPackage.eINSTANCE.getC_EClassToE();
233:                 final C domain = TestFactory.eINSTANCE.createC();
234:                 final EClass key = EcoreFactory.eINSTANCE.createEClass();
235:
236:                 final VMappingDomainModelReferenceSegment mappingSegment = VMappingsegmentFactory.eINSTANCE
237:                         .createMappingDomainModelReferenceSegment();
238:                 mappingSegment.setDomainModelFeature(structuralFeature.getName());
239:                 mappingSegment.setMappedClass(key);
240:
241:                 expander.prepareDomainObject(mappingSegment, domain);
242:         }
243:
244:         @SuppressWarnings("unchecked")
245:         @Test
246:         public void testPrepareDomainObjectValueRefAbstractWithConcreteMappedClass()
247:                 throws EMFFormsExpandingFailedException {
248:                 final EReference structuralFeature = TestPackage.eINSTANCE.getC_EClassToE();
249:                 final C domain = TestFactory.eINSTANCE.createC();
250:                 final EClass key = TestPackage.eINSTANCE.getA();
251:                 final EMap<EClass, E> map = (EMap<EClass, E>) domain.eGet(structuralFeature);
252:                 map.put(key, null);
253:
254:                 final VMappingDomainModelReferenceSegment mappingSegment = VMappingsegmentFactory.eINSTANCE
255:                         .createMappingDomainModelReferenceSegment();
256:                 mappingSegment.setDomainModelFeature(structuralFeature.getName());
257:                 mappingSegment.setMappedClass(key);
258:
259:                 expander.prepareDomainObject(mappingSegment, domain);
260:
261:                 assertEquals(TestPackage.eINSTANCE.getA(), domain.getEClassToE().get(key).eClass());
262:         }
263: }