Skip to content

Package: VDiagnostic_Test$1

VDiagnostic_Test$1

nameinstructionbranchcomplexitylinemethod
equals(Object)
M: 2 C: 37
95%
M: 3 C: 9
75%
M: 3 C: 4
57%
M: 1 C: 6
86%
M: 0 C: 1
100%
hashCode()
M: 0 C: 25
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) 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: * Christian W. Damus - initial API and implementation
13: ******************************************************************************/
14: package org.eclipse.emf.ecp.view.model.test;
15:
16: import static java.util.Collections.emptyList;
17: import static org.hamcrest.CoreMatchers.both;
18: import static org.hamcrest.CoreMatchers.hasItem;
19: import static org.hamcrest.CoreMatchers.hasItems;
20: import static org.hamcrest.CoreMatchers.is;
21: import static org.hamcrest.CoreMatchers.not;
22: import static org.hamcrest.MatcherAssert.assertThat;
23:
24: import java.time.LocalDate;
25: import java.time.ZoneId;
26: import java.util.Arrays;
27: import java.util.Iterator;
28: import java.util.List;
29: import java.util.Objects;
30: import java.util.stream.Stream;
31:
32: import org.eclipse.emf.common.util.BasicDiagnostic;
33: import org.eclipse.emf.common.util.Diagnostic;
34: import org.eclipse.emf.ecore.EObject;
35: import org.eclipse.emf.ecore.EStructuralFeature;
36: import org.eclipse.emf.ecp.view.spi.model.VDiagnostic;
37: import org.eclipse.emf.ecp.view.spi.model.VViewFactory;
38: import org.eclipse.emf.ecp.view.spi.model.impl.VDiagnosticImpl;
39: import org.eclipse.emf.emfstore.bowling.BowlingFactory;
40: import org.eclipse.emf.emfstore.bowling.BowlingPackage;
41: import org.eclipse.emf.emfstore.bowling.Gender;
42: import org.eclipse.emf.emfstore.bowling.League;
43: import org.eclipse.emf.emfstore.bowling.Player;
44: import org.hamcrest.CoreMatchers;
45: import org.hamcrest.Description;
46: import org.hamcrest.FeatureMatcher;
47: import org.hamcrest.Matcher;
48: import org.hamcrest.TypeSafeMatcher;
49: import org.junit.Before;
50: import org.junit.Test;
51:
52: /**
53: * Test cases for the custom code in the {@link VDiagnosticImpl} class.
54: */
55: public class VDiagnostic_Test {
56:
57:         private static final EStructuralFeature NAME = BowlingPackage.Literals.PLAYER__NAME;
58:         private static final EStructuralFeature DOB = BowlingPackage.Literals.PLAYER__DATE_OF_BIRTH;
59:         private static final EStructuralFeature PLAYERS = BowlingPackage.Literals.LEAGUE__PLAYERS;
60:
61:         private final VDiagnostic fixture = VViewFactory.eINSTANCE.createDiagnostic();
62:
63:         private League league;
64:         private Player alice;
65:         private Player brenda;
66:         private Player cathy;
67:
68:         /**
69:          * Test uniqueness of the diagnostic list.
70:          */
71:         @Test
72:         public void getDiagnostics() {
73:                 final List<Object> diagnostics = fixture.getDiagnostics();
74:
75:                 assertThat(diagnostics.size(), is(0));
76:                 diagnostics.add(warning(alice, NAME, "first"));
77:                 diagnostics.add(ok(brenda, NAME, "brenda"));
78:
79:                 assertThat(diagnostics.size(), is(2));
80:
81:                 assertThat(diagnostics.add(warning(cathy, NAME, "second")), is(true));
82:                 assertThat(diagnostics.add(warning(cathy, NAME, "second")), is(false));
83:
84:                 diagnostics.addAll(Arrays.asList(
85:                         warning(alice, NAME, "first"), // dupe
86:                         warning(alice, NAME, "second"), // new
87:                         ok(brenda, NAME, "brenda"), // dupe
88:                         error(brenda, NAME, "brenda"), // new
89:                         warning(cathy, DOB, "second") // new
90:                 ));
91:
92:                 assertThat(diagnostics.size(), is(6));
93:
94:                 assertThat(diagnostics, hasItems(
95:                         warning(alice, NAME, "first"),
96:                         ok(brenda, NAME, "brenda"),
97:                         warning(cathy, NAME, "second"),
98:                         warning(alice, NAME, "second"),
99:                         error(brenda, NAME, "brenda"),
100:                         warning(cathy, DOB, "second")));
101:         }
102:
103:         /**
104:          * Test severity query.
105:          *
106:          * @see VDiagnostic#getHighestSeverity()
107:          */
108:         @Test
109:         public void getHighestSeverity() {
110:                 final List<Object> diagnostics = fixture.getDiagnostics();
111:
112:                 assertThat(fixture.getHighestSeverity(), is(Diagnostic.OK));
113:
114:                 diagnostics.add(ok(brenda, NAME, "first"));
115:                 assertThat(fixture.getHighestSeverity(), is(Diagnostic.OK));
116:
117:                 diagnostics.add(warning(alice, NAME, "first"));
118:                 assertThat(fixture.getHighestSeverity(), is(Diagnostic.WARNING));
119:
120:                 diagnostics.add(error(cathy, NAME, "first"));
121:                 assertThat(fixture.getHighestSeverity(), is(Diagnostic.ERROR));
122:
123:                 diagnostics.remove(2);
124:                 assertThat(fixture.getHighestSeverity(), is(Diagnostic.WARNING));
125:         }
126:
127:         /**
128:          * Test message query.
129:          *
130:          * @see VDiagnostic#getMessage()
131:          */
132:         @Test
133:         public void getMessage() {
134:                 final List<Object> diagnostics = fixture.getDiagnostics();
135:
136:                 assertThat(fixture.getMessage(), is(""));
137:
138:                 diagnostics.add(ok(brenda, NAME, "first"));
139:                 assertThat(fixture.getMessage(), is(""));
140:
141:                 diagnostics.add(warning(alice, NAME, "second"));
142:                 assertThat(fixture.getMessage(), is("second"));
143:
144:                 diagnostics.add(error(cathy, NAME, "third"));
145:                 assertThat(fixture.getMessage(), is("third\nsecond")); // Severity order
146:         }
147:
148:         /**
149:          * Test mappings of diagnostics {@linkplain VDiagnostic#getDiagnostics(EObject) by object}.
150:          *
151:          * @see VDiagnostic#getDiagnostics(EObject)
152:          */
153:         @Test
154:         public void getDiagnostics_EObject() {
155:                 final List<Object> diagnostics = fixture.getDiagnostics();
156:
157:                 assertThat(fixture.getDiagnostics(alice), is(emptyList()));
158:                 assertThat(fixture.getDiagnostics(league), is(emptyList()));
159:
160:                 // OKs are filtered out
161:                 diagnostics.add(ok(alice, NAME, "first"));
162:                 assertThat(fixture.getDiagnostics(alice), is(emptyList()));
163:                 assertThat(fixture.getDiagnostics(league), is(emptyList()));
164:
165:                 diagnostics.add(warning(alice, DOB, "second"));
166:                 diagnostics.add(error(alice, NAME, "third"));
167:                 assertThat(fixture.getDiagnostics(alice), hasInOrder(
168:                         error(alice, NAME, "third"), warning(alice, DOB, "second")));
169:                 assertThat(fixture.getDiagnostics(league), hasInOrder(
170:                         error(alice, NAME, "third"), warning(alice, DOB, "second")));
171:
172:                 diagnostics.add(warning(cathy, DOB, "third"));
173:                 assertThat(fixture.getDiagnostics(alice), hasInOrder(
174:                         error(alice, NAME, "third"), warning(alice, DOB, "second")));
175:                 assertThat(fixture.getDiagnostics(alice), not(hasItem(isAbout(cathy))));
176:                 assertThat(fixture.getDiagnostics(cathy), hasItem(warning(cathy, DOB, "third")));
177:                 assertThat(fixture.getDiagnostics(cathy), not(hasItem(isAbout(alice))));
178:                 assertThat(fixture.getDiagnostics(league), hasInOrder(
179:                         // Warnings are alphabetical by message
180:                         error(alice, NAME, "third"), warning(alice, DOB, "second"), warning(cathy, DOB, "third")));
181:
182:                 diagnostics.add(error(league, PLAYERS, "too few"));
183:                 assertThat(fixture.getDiagnostics(league), hasItem(isAbout(league)));
184:                 assertThat(fixture.getDiagnostics(alice), not(hasItem(isAbout(league))));
185:                 assertThat(fixture.getDiagnostics(cathy), not(hasItem(isAbout(league))));
186:
187:                 // Now remove the error(alice, NAME, "third")
188:                 diagnostics.remove(2);
189:                 assertThat(fixture.getDiagnostics(alice), not(hasItem(error(alice, NAME, "third"))));
190:                 assertThat(fixture.getDiagnostics(league), not(hasItem(error(alice, NAME, "third"))));
191:
192:                 // And replace warning(alice, DOB, "second")
193:                 diagnostics.set(1, error(brenda, NAME, "new"));
194:                 assertThat(fixture.getDiagnostics(alice), not(hasItem(warning(alice, DOB, "second"))));
195:                 assertThat(fixture.getDiagnostics(league), both(hasItem(error(brenda, NAME, "new"))).and(
196:                         not(hasItem(warning(alice, DOB, "second")))));
197:         }
198:
199:         /**
200:          * Test mappings of diagnostics {@linkplain VDiagnostic#getDiagnostics(EObject) by object and feature}.
201:          *
202:          * @see VDiagnostic#getDiagnostic(EObject, EStructuralFeature)
203:          */
204:         @Test
205:         public void getDiagnostic_EObject_EStructuralFeature() {
206:                 final List<Object> diagnostics = fixture.getDiagnostics();
207:
208:                 assertThat(fixture.getDiagnostic(cathy, NAME), is(emptyList()));
209:                 assertThat(fixture.getDiagnostic(league, NAME), is(emptyList()));
210:
211:                 // OKs are filtered out
212:                 diagnostics.add(ok(cathy, NAME, "first"));
213:                 assertThat(fixture.getDiagnostic(cathy, NAME), is(emptyList()));
214:                 assertThat(fixture.getDiagnostic(league, NAME), is(emptyList()));
215:
216:                 diagnostics.add(warning(cathy, NAME, "second"));
217:                 diagnostics.add(error(cathy, DOB, "third"));
218:                 assertThat(fixture.getDiagnostic(cathy, NAME), hasItem(
219:                         warning(cathy, NAME, "second")));
220:                 assertThat(fixture.getDiagnostic(cathy, NAME), not(hasItem(hasFeature(DOB))));
221:                 assertThat(fixture.getDiagnostic(league, NAME), hasItem(
222:                         warning(cathy, NAME, "second")));
223:                 assertThat(fixture.getDiagnostic(league, NAME), not(hasItem(hasFeature(DOB))));
224:
225:                 diagnostics.add(error(cathy, NAME, "third"));
226:                 assertThat(fixture.getDiagnostic(cathy, NAME), hasInOrder(
227:                         error(cathy, NAME, "third"), warning(cathy, NAME, "second")));
228:         }
229:
230:         //
231:         // Test framework
232:         //
233:
234:         @Before
235:         public void createModel() {
236:                 league = BowlingFactory.eINSTANCE.createLeague();
237:                 league.setName("Bantam A");
238:
239:                 alice = player("Alice");
240:                 brenda = player("Brenda");
241:                 cathy = player("Cathy");
242:         }
243:
244:         private Player player(String name) {
245:                 final Player result = BowlingFactory.eINSTANCE.createPlayer();
246:                 result.setName(name);
247:                 result.setGender(Gender.FEMALE);
248:                 result.setDateOfBirth(
249:                         java.util.Date.from(LocalDate.now().minusYears(23).atStartOfDay(ZoneId.systemDefault()).toInstant()));
250:                 league.getPlayers().add(result);
251:                 return result;
252:         }
253:
254:         Diagnostic error(EObject subject, EStructuralFeature feature, String message) {
255:                 return problem(Diagnostic.ERROR, subject, feature, message);
256:         }
257:
258:         Diagnostic warning(EObject subject, EStructuralFeature feature, String message) {
259:                 return problem(Diagnostic.WARNING, subject, feature, message);
260:         }
261:
262:         Diagnostic ok(EObject subject, EStructuralFeature feature, String message) {
263:                 return problem(Diagnostic.OK, subject, feature, message);
264:         }
265:
266:         Diagnostic problem(int severity, EObject subject, EStructuralFeature feature, String message) {
267:                 return new BasicDiagnostic(severity, "source", 0, message, new Object[] { subject, feature }) {
268:                         // Implement equals() and hashcode() for convenience of testing uniqueness constraints
269:
270:                         @Override
271:                         public int hashCode() {
272:                                 return Objects.hash(getSeverity(), getSource(), getMessage(), getData());
273:                         }
274:
275:                         @Override
276:                         public boolean equals(Object obj) {
277:•                                if (obj == null || obj.getClass() != getClass()) {
278:                                         return false;
279:                                 }
280:                                 final Diagnostic other = (Diagnostic) obj;
281:•                                return getSeverity() == other.getSeverity()
282:•                                        && Objects.equals(getSource(), other.getSource())
283:•                                        && Objects.equals(getMessage(), other.getMessage())
284:•                                        && Objects.equals(getData(), other.getData());
285:                         }
286:                 };
287:         }
288:
289:         static Matcher<Diagnostic> isAbout(Matcher<? super EObject> subjectMatcher) {
290:                 return new FeatureMatcher<Diagnostic, EObject>(subjectMatcher, "data[0] as EObject", "subject") {
291:                         @Override
292:                         protected EObject featureValueOf(Diagnostic actual) {
293:                                 final List<?> data = actual.getData();
294:                                 return data.isEmpty() || !(data.get(0) instanceof EObject)
295:                                         ? null
296:                                         : (EObject) data.get(0);
297:                         }
298:                 };
299:         }
300:
301:         static Matcher<Diagnostic> isAbout(EObject subject) {
302:                 return isAbout(is(subject));
303:         }
304:
305:         static Matcher<Diagnostic> hasFeature(Matcher<? super EStructuralFeature> featureMatcher) {
306:                 return new FeatureMatcher<Diagnostic, EStructuralFeature>(featureMatcher, "data[1] as EStructuralFeature",
307:                         "feature") {
308:                         @Override
309:                         protected EStructuralFeature featureValueOf(Diagnostic actual) {
310:                                 final List<?> data = actual.getData();
311:                                 return data.size() < 2 || !(data.get(1) instanceof EStructuralFeature)
312:                                         ? null
313:                                         : (EStructuralFeature) data.get(1);
314:                         }
315:                 };
316:         }
317:
318:         static Matcher<Diagnostic> hasFeature(EStructuralFeature feature) {
319:                 return hasFeature(is(feature));
320:         }
321:
322:         @SafeVarargs
323:         static <T> Matcher<Iterable<T>> hasInOrder(Matcher<? super T>... matchers) {
324:                 return new InOrderMatcher<>(matchers);
325:         }
326:
327:         private static final class InOrderMatcher<T> extends TypeSafeMatcher<Iterable<T>> {
328:                 private final List<Matcher<? super T>> matchers;
329:
330:                 @SafeVarargs
331:                 InOrderMatcher(Matcher<? super T>... matchers) {
332:                         super();
333:
334:                         this.matchers = Arrays.asList(matchers);
335:                 }
336:
337:                 @Override
338:                 public void describeTo(Description description) {
339:                         description.appendText("matches in order ");
340:                         matchers.forEach(description::appendDescriptionOf);
341:                 }
342:
343:                 @Override
344:                 protected boolean matchesSafely(Iterable<T> item) {
345:                         boolean result = true;
346:                         final Iterator<T> iter = item.iterator();
347:                         final Iterator<Matcher<? super T>> mIter = matchers.iterator();
348:                         Matcher<? super T> m = null;
349:
350:                         while (iter.hasNext()) {
351:                                 if (m == null) {
352:                                         if (!mIter.hasNext()) {
353:                                                 break; // All matched
354:                                         }
355:                                         m = mIter.next();
356:                                 }
357:
358:                                 final T next = iter.next();
359:                                 if (m.matches(next)) {
360:                                         // Match the next
361:                                         m = null;
362:                                 }
363:                         }
364:
365:                         if (mIter.hasNext()) {
366:                                 // Not all matched
367:                                 result = false;
368:                         }
369:
370:                         return result;
371:                 }
372:         }
373:
374:         @SuppressWarnings("unchecked")
375:         static <T> Matcher<Iterable<T>> hasInOrder(T... items) {
376:                 return hasInOrder(Stream.of(items).map(CoreMatchers::is).toArray(Matcher[]::new));
377:         }
378:
379: }