Skip to content

Package: FeaturePathDMRValidation_Test$DiagnosticInfo

FeaturePathDMRValidation_Test$DiagnosticInfo

nameinstructionbranchcomplexitylinemethod
FeaturePathDMRValidation_Test.DiagnosticInfo(FeaturePathDMRValidation_Test, int, EObject, EStructuralFeature)
M: 0 C: 15
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
sameData(Diagnostic)
M: 4 C: 25
86%
M: 2 C: 4
67%
M: 2 C: 2
50%
M: 2 C: 5
71%
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: * jfaltermeier - initial API and implementation
13: * Christian W. Damus - Bug 543160
14: ******************************************************************************/
15: package org.eclipse.emf.ecp.view.model.test;
16:
17: import static org.junit.Assert.assertEquals;
18: import static org.junit.Assert.assertFalse;
19: import static org.junit.Assert.assertTrue;
20: import static org.junit.Assert.fail;
21:
22: import java.util.ArrayList;
23: import java.util.Arrays;
24: import java.util.Collection;
25: import java.util.Iterator;
26: import java.util.LinkedHashMap;
27: import java.util.LinkedHashSet;
28: import java.util.List;
29: import java.util.Set;
30:
31: import org.eclipse.emf.common.util.BasicDiagnostic;
32: import org.eclipse.emf.common.util.Diagnostic;
33: import org.eclipse.emf.ecore.EObject;
34: import org.eclipse.emf.ecore.EStructuralFeature;
35: import org.eclipse.emf.ecore.EValidator;
36: import org.eclipse.emf.ecp.view.spi.model.VControl;
37: import org.eclipse.emf.ecp.view.spi.model.VFeaturePathDomainModelReference;
38: import org.eclipse.emf.ecp.view.spi.model.VView;
39: import org.eclipse.emf.ecp.view.spi.model.VViewFactory;
40: import org.eclipse.emf.ecp.view.spi.model.VViewPackage;
41: import org.eclipse.emf.ecp.view.spi.model.util.ViewValidator;
42: import org.eclipse.emf.emfstore.bowling.BowlingPackage;
43: import org.junit.Before;
44: import org.junit.Test;
45: import org.junit.runner.RunWith;
46: import org.junit.runners.Parameterized;
47: import org.junit.runners.Parameterized.Parameters;
48:
49: /**
50: * @author jfaltermeier
51: *
52: */
53: @RunWith(Parameterized.class)
54: public class FeaturePathDMRValidation_Test {
55:
56:         private VView view;
57:         private VControl control;
58:         private VFeaturePathDomainModelReference featurePath;
59:
60:         private EValidator validator;
61:         private BasicDiagnostic chain;
62:         private LinkedHashMap<Object, Object> context;
63:         private final Boolean createChain;
64:
65:         public FeaturePathDMRValidation_Test(Boolean createChain) {
66:                 this.createChain = createChain;
67:         }
68:
69:         @Parameters
70:         public static Collection<Object[]> data() {
71:                 // run all tests once with a diagnostic chain and once without
72:                 final List<Object[]> parameters = new ArrayList<Object[]>();
73:                 parameters.add(new Object[] { true });
74:                 parameters.add(new Object[] { false });
75:                 return parameters;
76:         }
77:
78:         @Before
79:         public void before() {
80:                 view = VViewFactory.eINSTANCE.createView();
81:                 view.setRootEClass(BowlingPackage.eINSTANCE.getFan());
82:                 control = VViewFactory.eINSTANCE.createControl();
83:                 view.getChildren().add(control);
84:                 featurePath = VViewFactory.eINSTANCE.createFeaturePathDomainModelReference();
85:                 control.setDomainModelReference(featurePath);
86:
87:                 validator = ViewValidator.INSTANCE;
88:                 context = new LinkedHashMap<Object, Object>();
89:                 if (createChain) {
90:                         chain = new BasicDiagnostic();
91:                 } else {
92:                         chain = null;
93:                 }
94:         }
95:
96:         private boolean validate() {
97:                 return validator.validate(featurePath, chain, context);
98:         }
99:
100:         private DiagnosticInfo controlDMR() {
101:                 return new DiagnosticInfo(Diagnostic.ERROR, control, VViewPackage.eINSTANCE.getControl_DomainModelReference());
102:         }
103:
104:         private DiagnosticInfo controlDMRWarning() {
105:                 return new DiagnosticInfo(Diagnostic.WARNING, control,
106:                         VViewPackage.eINSTANCE.getControl_DomainModelReference());
107:         }
108:
109:         private DiagnosticInfo featurePathEFeature() {
110:                 return new DiagnosticInfo(Diagnostic.ERROR, featurePath, VViewPackage.eINSTANCE
111:                         .getFeaturePathDomainModelReference_DomainModelEFeature());
112:         }
113:
114:         private DiagnosticInfo featurePathPath() {
115:                 return new DiagnosticInfo(Diagnostic.ERROR, featurePath, VViewPackage.eINSTANCE
116:                         .getFeaturePathDomainModelReference_DomainModelEReferencePath());
117:         }
118:
119:         @Test
120:         public void testNoEFeature() {
121:                 assertFalse(validate());
122:                 if (createChain) {
123:                         assertEquals(Diagnostic.ERROR, chain.getSeverity());
124:                         assertChain(controlDMR(), featurePathEFeature());
125:                 }
126:         }
127:
128:         @Test
129:         public void testNoEFeatureNoContainer() {
130:                 control.eUnset(VViewPackage.eINSTANCE.getControl_DomainModelReference());
131:                 assertFalse(validate());
132:                 if (createChain) {
133:                         assertEquals(Diagnostic.ERROR, chain.getSeverity());
134:                         assertChain(featurePathEFeature());
135:                 }
136:         }
137:
138:         @Test
139:         public void testErrorOnPath() {
140:                 featurePath.getDomainModelEReferencePath().add(BowlingPackage.eINSTANCE.getReferee_League());
141:                 featurePath.setDomainModelEFeature(BowlingPackage.eINSTANCE.getLeague_Name());
142:                 assertFalse(validate());
143:                 if (createChain) {
144:                         assertEquals(Diagnostic.ERROR, chain.getSeverity());
145:                         assertChain(controlDMR(), featurePathPath());
146:                 }
147:         }
148:
149:         @Test
150:         public void testErrorOnFeature() {
151:                 featurePath.getDomainModelEReferencePath().add(BowlingPackage.eINSTANCE.getFan_FavouritePlayer());
152:                 featurePath.setDomainModelEFeature(BowlingPackage.eINSTANCE.getFan_Name());
153:                 assertFalse(validate());
154:                 if (createChain) {
155:                         assertEquals(Diagnostic.ERROR, chain.getSeverity());
156:                         assertChain(controlDMR(), featurePathEFeature());
157:                 }
158:         }
159:
160:         @Test
161:         public void testOkPath() {
162:                 featurePath.getDomainModelEReferencePath().add(BowlingPackage.eINSTANCE.getFan_FavouritePlayer());
163:                 featurePath.setDomainModelEFeature(BowlingPackage.eINSTANCE.getPlayer_Name());
164:                 assertTrue(validate());
165:                 if (createChain) {
166:                         assertEquals(Diagnostic.OK, chain.getSeverity());
167:                         assertChain();
168:                 }
169:         }
170:
171:         @Test
172:         public void testBadRootEClass() {
173:                 view.setRootEClass(BowlingPackage.eINSTANCE.getPlayer());
174:                 featurePath.getDomainModelEReferencePath().add(BowlingPackage.eINSTANCE.getFan_FavouritePlayer());
175:                 featurePath.setDomainModelEFeature(BowlingPackage.eINSTANCE.getPlayer_Name());
176:                 assertFalse(validate());
177:                 if (createChain) {
178:                         assertEquals(Diagnostic.ERROR, chain.getSeverity());
179:                         assertChain(controlDMR(), featurePathPath());
180:                 }
181:         }
182:
183:         @Test
184:         public void testBadRootEClassOkRootEClassInContext() {
185:                 context.put(ViewValidator.ECLASS_KEY, BowlingPackage.eINSTANCE.getFan());
186:                 view.setRootEClass(BowlingPackage.eINSTANCE.getPlayer());
187:                 featurePath.getDomainModelEReferencePath().add(BowlingPackage.eINSTANCE.getFan_FavouritePlayer());
188:                 featurePath.setDomainModelEFeature(BowlingPackage.eINSTANCE.getPlayer_Name());
189:                 assertTrue(validate());
190:                 if (createChain) {
191:                         assertEquals(Diagnostic.OK, chain.getSeverity());
192:                 }
193:         }
194:
195:         @Test
196:         public void testOkPathNoRootEClassOnContainer() {
197:                 view.setRootEClass(null);
198:                 featurePath.getDomainModelEReferencePath().add(BowlingPackage.eINSTANCE.getFan_FavouritePlayer());
199:                 featurePath.setDomainModelEFeature(BowlingPackage.eINSTANCE.getPlayer_Name());
200:                 assertTrue(validate());
201:                 if (createChain) {
202:                         assertEquals(Diagnostic.WARNING, chain.getSeverity());
203:                         assertChain(controlDMRWarning());
204:                 }
205:         }
206:
207:         @Test
208:         public void testOkPathNotInView() {
209:                 control.eUnset(VViewPackage.eINSTANCE.getControl_DomainModelReference());
210:                 featurePath.getDomainModelEReferencePath().add(BowlingPackage.eINSTANCE.getFan_FavouritePlayer());
211:                 featurePath.setDomainModelEFeature(BowlingPackage.eINSTANCE.getPlayer_Name());
212:                 assertTrue(validate());
213:                 if (createChain) {
214:                         assertEquals(Diagnostic.OK, chain.getSeverity());
215:                         assertChain();
216:                 }
217:         }
218:
219:         @Test
220:         public void testBadEFeatureNotInView() {
221:                 control.eUnset(VViewPackage.eINSTANCE.getControl_DomainModelReference());
222:                 featurePath.getDomainModelEReferencePath().add(BowlingPackage.eINSTANCE.getFan_FavouritePlayer());
223:                 featurePath.setDomainModelEFeature(BowlingPackage.eINSTANCE.getFan_Name());
224:                 assertFalse(validate());
225:                 if (createChain) {
226:                         assertEquals(Diagnostic.ERROR, chain.getSeverity());
227:                         assertChain(featurePathEFeature());
228:                 }
229:         }
230:
231:         @Test
232:         public void testBadPathNotInView() {
233:                 control.eUnset(VViewPackage.eINSTANCE.getControl_DomainModelReference());
234:                 featurePath.getDomainModelEReferencePath().add(BowlingPackage.eINSTANCE.getFan_FavouritePlayer());
235:                 featurePath.getDomainModelEReferencePath().add(BowlingPackage.eINSTANCE.getFan_FavouritePlayer());
236:                 featurePath.setDomainModelEFeature(BowlingPackage.eINSTANCE.getFan_Name());
237:                 assertFalse(validate());
238:                 if (createChain) {
239:                         assertEquals(Diagnostic.ERROR, chain.getSeverity());
240:                         assertChain(featurePathPath());
241:                 }
242:         }
243:
244:         @Test
245:         public void testOnlyEFeatureNotInView() {
246:                 control.eUnset(VViewPackage.eINSTANCE.getControl_DomainModelReference());
247:                 featurePath.setDomainModelEFeature(BowlingPackage.eINSTANCE.getPlayer_Name());
248:                 assertTrue(validate());
249:                 if (createChain) {
250:                         assertEquals(Diagnostic.OK, chain.getSeverity());
251:                         assertChain();
252:                 }
253:         }
254:
255:         private void assertChain(DiagnosticInfo... infos) {
256:                 final Set<DiagnosticInfo> infoSet = new LinkedHashSet<DiagnosticInfo>(Arrays.asList(infos));
257:                 assertEquals(infos.length, chain.getChildren().size());
258:                 for (final Diagnostic child : chain.getChildren()) {
259:                         boolean found = false;
260:                         final Iterator<DiagnosticInfo> iterator = infoSet.iterator();
261:                         while (iterator.hasNext()) {
262:                                 final DiagnosticInfo next = iterator.next();
263:                                 if (next.sameData(child)) {
264:                                         found = true;
265:                                         iterator.remove();
266:                                         break;
267:                                 }
268:                         }
269:                         if (!found) {
270:                                 fail("Chain is missing child diagnostic.");
271:                         }
272:                 }
273:         }
274:
275:         private class DiagnosticInfo {
276:                 private final int severity;
277:                 private final EObject object;
278:                 private final EStructuralFeature feature;
279:
280:                 DiagnosticInfo(int severity, EObject object, EStructuralFeature feature) {
281:                         this.severity = severity;
282:                         this.object = object;
283:                         this.feature = feature;
284:                 }
285:
286:                 public boolean sameData(Diagnostic diagnostic) {
287:•                        if (diagnostic.getSeverity() != severity) {
288:                                 return false;
289:                         }
290:•                        if (!object.equals(diagnostic.getData().get(0))) {
291:                                 return false;
292:                         }
293:•                        if (!feature.equals(diagnostic.getData().get(1))) {
294:                                 return false;
295:                         }
296:                         return true;
297:                 }
298:
299:         }
300: }