Skip to content

Package: DiagnosticFrequencyMap_Test$Limited

DiagnosticFrequencyMap_Test$Limited

nameinstructionbranchcomplexitylinemethod
DiagnosticFrequencyMap_Test.Limited()
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
addDiagnosticFilter()
M: 0 C: 162
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 23
100%
M: 0 C: 1
100%
appendTo()
M: 0 C: 121
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 21
100%
M: 0 C: 1
100%
clear()
M: 0 C: 79
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 9
100%
M: 0 C: 1
100%
getDiscardedSeverity()
M: 0 C: 97
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 10
100%
M: 0 C: 1
100%
isEmpty()
M: 0 C: 32
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
isFull()
M: 0 C: 45
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
iterator()
M: 0 C: 114
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 16
100%
M: 0 C: 1
100%
lambda$30(Set, Diagnostic)
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
lambda$31(Diagnostic)
M: 0 C: 8
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
size()
M: 0 C: 51
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 6
100%
M: 0 C: 1
100%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2019 Christian W. Damus 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.emfforms.spi.common.validation;
15:
16: import static java.util.stream.Collectors.toList;
17: import static java.util.stream.Collectors.toSet;
18: import static org.hamcrest.CoreMatchers.anything;
19: import static org.hamcrest.CoreMatchers.everyItem;
20: import static org.hamcrest.CoreMatchers.hasItem;
21: import static org.hamcrest.CoreMatchers.hasItems;
22: import static org.hamcrest.CoreMatchers.is;
23: import static org.hamcrest.CoreMatchers.not;
24: import static org.junit.Assert.assertThat;
25: import static org.junit.Assume.assumeThat;
26:
27: import java.util.ArrayList;
28: import java.util.List;
29: import java.util.Set;
30: import java.util.function.Predicate;
31: import java.util.stream.Stream;
32:
33: import org.eclipse.emf.common.util.BasicDiagnostic;
34: import org.eclipse.emf.common.util.Diagnostic;
35: import org.hamcrest.Description;
36: import org.hamcrest.Matcher;
37: import org.hamcrest.TypeSafeDiagnosingMatcher;
38: import org.junit.Test;
39: import org.junit.experimental.runners.Enclosed;
40: import org.junit.runner.RunWith;
41: import org.junit.runners.JUnit4;
42:
43: /**
44: * Test cases for the {@link DiagnosticFrequencyMap} type.
45: */
46: @RunWith(Enclosed.class)
47: @SuppressWarnings("nls")
48: public class DiagnosticFrequencyMap_Test {
49:
50:         private final DiagnosticFrequencyMap fixture;
51:
52:         /**
53:          * Initializes me with my fixture.
54:          *
55:          * @param fixture my fixture
56:          */
57:         DiagnosticFrequencyMap_Test(DiagnosticFrequencyMap fixture) {
58:                 super();
59:
60:                 this.fixture = fixture;
61:         }
62:
63:         @RunWith(JUnit4.class)
64:         public static class Limited extends DiagnosticFrequencyMap_Test {
65:
66:                 /**
67:                  * Initializes me.
68:                  */
69:                 public Limited() {
70:                         super(DiagnosticFrequencyMap.limitedTo(5));
71:                 }
72:
73:                 @Test
74:                 public void isEmpty() {
75:                         assertThat("should be empty", fixture().isEmpty(), is(true));
76:                         charStream("abcdefghijklmnop").map(this::error).forEach(fixture()::add);
77:                         assertThat("should not be empty", fixture().isEmpty(), is(false));
78:                 }
79:
80:                 @Test
81:                 public void isFull() {
82:                         charStream("abcd").map(this::error).forEach(fixture()::add);
83:                         assertThat("limited map should not be full", fixture().isFull(), is(false));
84:                         charStream("efghijklmnop").map(this::error).forEach(fixture()::add);
85:                         assertThat("limited map should be full", fixture().isFull(), is(true));
86:                 }
87:
88:                 @Test
89:                 public void size() {
90:                         assertThat(fixture().size(), is(0));
91:                         charStream("abcd").map(this::warning).forEach(fixture()::add);
92:                         assertThat(fixture().size(), is(4));
93:                         charStream("efghijklmnop").map(this::error).forEach(fixture()::add);
94:                         assertThat(fixture().size(), is(5)); // 5 is the limit
95:                 }
96:
97:                 @Test
98:                 public void getDiscardedSeverity() {
99:                         // Initial conditions
100:                         assertThat(fixture().getDiscardedSeverity(), is(Diagnostic.OK));
101:
102:                         charStream("abcde").map(this::info).forEach(fixture()::add);
103:                         assertThat("nothing should be discarded, yet", fixture().getDiscardedSeverity(), is(Diagnostic.OK));
104:
105:                         charStream("fghij").map(this::warning).forEach(fixture()::add);
106:                         assertThat("infos should be discarded", fixture().getDiscardedSeverity(), is(Diagnostic.INFO));
107:
108:                         charStream("klmno").map(this::error).forEach(fixture()::add);
109:                         assertThat("warnings should be discarded", fixture().getDiscardedSeverity(), is(Diagnostic.WARNING));
110:
111:                         charStream("p").map(this::error).forEach(fixture()::add);
112:                         assertThat("an error should have been discarded", fixture().getDiscardedSeverity(), is(Diagnostic.ERROR));
113:                 }
114:
115:                 @Test
116:                 public void iterator() {
117:                         // Initial conditions
118:                         List<Diagnostic> diagnostics = iterate(fixture());
119:                         assertThat(diagnostics, not(hasItem(anything())));
120:
121:                         fixture().addAll(charStream("abcde").map(this::info).collect(toList()));
122:                         diagnostics = iterate(fixture());
123:                         assertThat(diagnostics, everyItem(matches(Diagnostic.INFO, substringOf("abcde"))));
124:
125:                         fixture().addAll(charStream("fghij").map(this::warning).collect(toList()));
126:                         diagnostics = iterate(fixture());
127:                         assertThat(diagnostics, everyItem(matches(Diagnostic.WARNING, substringOf("fghij"))));
128:
129:                         fixture().addAll(charStream("klmno").map(this::error).collect(toList()));
130:                         diagnostics = iterate(fixture());
131:                         assertThat(diagnostics, everyItem(matches(Diagnostic.ERROR, substringOf("klmno"))));
132:
133:                         fixture().add(error("p"));
134:                         diagnostics = iterate(fixture());
135:                         assertThat("should have discarded new error", diagnostics,
136:                                 everyItem(matches(Diagnostic.ERROR, substringOf("klmno"))));
137:                 }
138:
139:                 @Test
140:                 public void clear() {
141:                         charStream("abc").map(this::info).forEach(fixture()::add);
142:                         charStream("def").map(this::warning).forEach(fixture()::add);
143:                         charStream("ghi").map(this::error).forEach(fixture()::add);
144:
145:                         fixture().clear();
146:                         assertThat("not empty", fixture().isEmpty(), is(true));
147:                         assertThat("somehow full", fixture().isFull(), is(false));
148:                         assertThat("has size", fixture().size(), is(0));
149:                         assertThat("something discarded from nothing", fixture().getDiscardedSeverity(), is(Diagnostic.OK));
150:                 }
151:
152:                 @Test
153:                 public void appendTo() {
154:                         // Initial conditions
155:                         final List<Diagnostic> diagnostics = new ArrayList<>();
156:                         fixture().appendTo(diagnostics);
157:                         assertThat(diagnostics, not(hasItem(anything())));
158:
159:                         fixture().addAll(charStream("abcde").map(this::info).collect(toList()));
160:                         diagnostics.clear();
161:                         fixture().appendTo(diagnostics);
162:                         assertThat(diagnostics, everyItem(matches(Diagnostic.INFO, substringOf("abcde"))));
163:
164:                         fixture().addAll(charStream("fghij").map(this::warning).collect(toList()));
165:                         diagnostics.clear();
166:                         fixture().appendTo(diagnostics);
167:                         assertThat(diagnostics, everyItem(matches(Diagnostic.WARNING, substringOf("fghij"))));
168:
169:                         fixture().addAll(charStream("klmno").map(this::error).collect(toList()));
170:                         diagnostics.clear();
171:                         fixture().appendTo(diagnostics);
172:                         assertThat(diagnostics, everyItem(matches(Diagnostic.ERROR, substringOf("klmno"))));
173:
174:                         fixture().add(error("p"));
175:                         diagnostics.clear();
176:                         fixture().appendTo(diagnostics);
177:                         assertThat("should have discarded new error", diagnostics,
178:                                 everyItem(matches(Diagnostic.ERROR, substringOf("klmno"))));
179:                 }
180:
181:                 @SuppressWarnings("unchecked")
182:                 @Test
183:                 public void addDiagnosticFilter() {
184:                         final Set<String> vowels = charStream("aeiou").collect(toSet());
185:                         final Predicate<Diagnostic> isVowel = d -> vowels.contains(d.getMessage());
186:•                        final Predicate<Diagnostic> isProblem = d -> d.getSeverity() > Diagnostic.INFO;
187:
188:                         fixture().addDiagnosticFilter(isVowel);
189:                         fixture().addDiagnosticFilter(isProblem);
190:
191:                         // initial conditions
192:                         assumeThat(fixture().size(), is(0));
193:                         assumeThat(fixture().getDiscardedSeverity(), is(Diagnostic.OK));
194:
195:                         charStream("abcd").map(this::info).forEach(fixture()::add);
196:                         assertThat(fixture().size(), is(0));
197:                         assertThat(fixture().getDiscardedSeverity(), is(Diagnostic.INFO));
198:                         charStream("efghijklmnop").map(this::warning).forEach(fixture()::add);
199:                         assertThat(fixture().size(), is(3));
200:                         assertThat(fixture().getDiscardedSeverity(), is(Diagnostic.WARNING));
201:                         charStream("qrstuvwxyz").map(this::error).forEach(fixture()::add);
202:                         assertThat(fixture().size(), is(4));
203:                         assertThat(fixture().getDiscardedSeverity(), is(Diagnostic.ERROR));
204:
205:                         final List<Diagnostic> collected = iterate(fixture());
206:                         assertThat(collected, hasItems(
207:                                 matches(Diagnostic.WARNING, "e"),
208:                                 matches(Diagnostic.WARNING, "i"),
209:                                 matches(Diagnostic.WARNING, "o"),
210:                                 matches(Diagnostic.ERROR, "u")));
211:                 }
212:
213:         }
214:
215:         @RunWith(JUnit4.class)
216:         public static class Unlimited extends DiagnosticFrequencyMap_Test {
217:
218:                 /**
219:                  * Initializes me.
220:                  */
221:                 public Unlimited() {
222:                         super(DiagnosticFrequencyMap.unlimited());
223:                 }
224:
225:                 @Test
226:                 public void isEmpty() {
227:                         assertThat("should be empty", fixture().isEmpty(), is(true));
228:                         charStream("abcdefghijklmnop").map(this::error).forEach(fixture()::add);
229:                         assertThat("should not be empty", fixture().isEmpty(), is(false));
230:                 }
231:
232:                 @Test
233:                 public void isFull() {
234:                         charStream("abcdefghijklmnop").map(this::error).forEach(fixture()::add);
235:                         assertThat("unlimited map should never be full", fixture().isFull(), is(false));
236:                 }
237:
238:                 @Test
239:                 public void size() {
240:                         assertThat(fixture().size(), is(0));
241:                         charStream("abcdefghijklmnop").map(this::error).forEach(fixture()::add);
242:                         assertThat(fixture().size(), is(16));
243:                 }
244:
245:                 @Test
246:                 public void getDiscardedSeverity() {
247:                         // Initial conditions
248:                         assertThat(fixture().getDiscardedSeverity(), is(Diagnostic.OK));
249:
250:                         charStream("abcde").map(this::info).forEach(fixture()::add);
251:                         assertThat("nothing should ever be discarded", fixture().getDiscardedSeverity(), is(Diagnostic.OK));
252:
253:                         charStream("fghij").map(this::warning).forEach(fixture()::add);
254:                         assertThat("nothing should ever be discarded", fixture().getDiscardedSeverity(), is(Diagnostic.OK));
255:
256:                         charStream("klmno").map(this::error).forEach(fixture()::add);
257:                         assertThat("nothing should ever be discarded", fixture().getDiscardedSeverity(), is(Diagnostic.OK));
258:
259:                         charStream("p").map(this::error).forEach(fixture()::add);
260:                         assertThat("nothing should ever be discarded", fixture().getDiscardedSeverity(), is(Diagnostic.OK));
261:                 }
262:
263:                 @Test
264:                 public void appendTo() {
265:                         // Initial conditions
266:                         final List<Diagnostic> diagnostics = new ArrayList<>();
267:                         fixture().appendTo(diagnostics);
268:                         assertThat(diagnostics, not(hasItem(anything())));
269:
270:                         fixture().addAll(charStream("abcde").map(this::info).collect(toList()));
271:                         diagnostics.clear();
272:                         fixture().appendTo(diagnostics);
273:                         assertThat(diagnostics, everyItem(matches(Diagnostic.INFO, substringOf("abcde"))));
274:
275:                         fixture().addAll(charStream("fghij").map(this::warning).collect(toList()));
276:                         diagnostics.clear();
277:                         fixture().appendTo(diagnostics);
278:                         assertThat(diagnostics.subList(0, 5), everyItem(matches(Diagnostic.WARNING, substringOf("fghij"))));
279:                         assertThat(diagnostics.subList(5, 10), everyItem(matches(Diagnostic.INFO, substringOf("abcde"))));
280:
281:                         fixture().addAll(charStream("klmno").map(this::error).collect(toList()));
282:                         diagnostics.clear();
283:                         fixture().appendTo(diagnostics);
284:                         assertThat(diagnostics.subList(0, 5), everyItem(matches(Diagnostic.ERROR, substringOf("klmno"))));
285:                         assertThat(diagnostics.subList(5, 10), everyItem(matches(Diagnostic.WARNING, substringOf("fghij"))));
286:                         assertThat(diagnostics.subList(10, 15), everyItem(matches(Diagnostic.INFO, substringOf("abcde"))));
287:
288:                         fixture().add(error("p"));
289:                         diagnostics.clear();
290:                         fixture().appendTo(diagnostics);
291:                         assertThat("Sixth diagnostic should be the error added to the earlier five",
292:                                 diagnostics.get(5), matches(Diagnostic.ERROR, "p"));
293:                 }
294:
295:                 @Test
296:                 public void iterator() {
297:                         // Initial conditions
298:                         List<Diagnostic> diagnostics = iterate(fixture());
299:                         assertThat(diagnostics, not(hasItem(anything())));
300:
301:                         fixture().addAll(charStream("abcde").map(this::info).collect(toList()));
302:                         diagnostics = iterate(fixture());
303:                         assertThat(diagnostics, everyItem(matches(Diagnostic.INFO, substringOf("abcde"))));
304:
305:                         fixture().addAll(charStream("fghij").map(this::warning).collect(toList()));
306:                         diagnostics = iterate(fixture());
307:                         assertThat(diagnostics.subList(0, 5), everyItem(matches(Diagnostic.WARNING, substringOf("fghij"))));
308:                         assertThat(diagnostics.subList(5, 10), everyItem(matches(Diagnostic.INFO, substringOf("abcde"))));
309:
310:                         fixture().addAll(charStream("klmno").map(this::error).collect(toList()));
311:                         diagnostics = iterate(fixture());
312:                         assertThat(diagnostics.subList(0, 5), everyItem(matches(Diagnostic.ERROR, substringOf("klmno"))));
313:                         assertThat(diagnostics.subList(5, 10), everyItem(matches(Diagnostic.WARNING, substringOf("fghij"))));
314:                         assertThat(diagnostics.subList(10, 15), everyItem(matches(Diagnostic.INFO, substringOf("abcde"))));
315:
316:                         fixture().add(error("p"));
317:                         diagnostics = iterate(fixture());
318:                         assertThat("Sixth diagnostic should be the error added to the earlier five",
319:                                 diagnostics.get(5), matches(Diagnostic.ERROR, "p"));
320:                 }
321:
322:                 @Test
323:                 public void clear() {
324:                         charStream("abc").map(this::info).forEach(fixture()::add);
325:                         charStream("def").map(this::warning).forEach(fixture()::add);
326:                         charStream("ghi").map(this::error).forEach(fixture()::add);
327:
328:                         fixture().clear();
329:                         assertThat("not empty", fixture().isEmpty(), is(true));
330:                         assertThat("somehow full", fixture().isFull(), is(false));
331:                         assertThat("has size", fixture().size(), is(0));
332:                         assertThat("something discarded from nothing", fixture().getDiscardedSeverity(), is(Diagnostic.OK));
333:                 }
334:
335:                 @SuppressWarnings("unchecked")
336:                 @Test
337:                 public void addDiagnosticFilter() {
338:                         final Set<String> vowels = charStream("aeiou").collect(toSet());
339:                         final Predicate<Diagnostic> isVowel = d -> vowels.contains(d.getMessage());
340:                         final Predicate<Diagnostic> isProblem = d -> d.getSeverity() > Diagnostic.INFO;
341:
342:                         fixture().addDiagnosticFilter(isVowel);
343:                         fixture().addDiagnosticFilter(isProblem);
344:
345:                         // initial conditions
346:                         assumeThat(fixture().size(), is(0));
347:                         assumeThat(fixture().getDiscardedSeverity(), is(Diagnostic.OK));
348:
349:                         charStream("abcd").map(this::info).forEach(fixture()::add);
350:                         assertThat(fixture().size(), is(0));
351:                         assertThat(fixture().getDiscardedSeverity(), is(Diagnostic.INFO));
352:                         charStream("efghijklmnop").map(this::warning).forEach(fixture()::add);
353:                         assertThat(fixture().size(), is(3));
354:                         assertThat(fixture().getDiscardedSeverity(), is(Diagnostic.WARNING));
355:                         charStream("qrstuvwxyz").map(this::error).forEach(fixture()::add);
356:                         assertThat(fixture().size(), is(4));
357:                         assertThat(fixture().getDiscardedSeverity(), is(Diagnostic.ERROR));
358:
359:                         final List<Diagnostic> collected = iterate(fixture());
360:                         assertThat(collected, hasItems(
361:                                 matches(Diagnostic.WARNING, "e"),
362:                                 matches(Diagnostic.WARNING, "i"),
363:                                 matches(Diagnostic.WARNING, "o"),
364:                                 matches(Diagnostic.ERROR, "u")));
365:                 }
366:
367:         }
368:
369:         //
370:         // Common test framework
371:         //
372:
373:         final DiagnosticFrequencyMap fixture() {
374:                 return fixture;
375:         }
376:
377:         /**
378:          * Iterate a diagnostic {@code freq}uency map to collect its diagnostics.
379:          *
380:          * @param freq the map to {@linkplain Iterable#iterator() iterate}
381:          * @return the diagnostics gathered from the iterator
382:          */
383:         final List<Diagnostic> iterate(DiagnosticFrequencyMap freq) {
384:                 final List<Diagnostic> result = new ArrayList<>(freq.size());
385:                 for (final Diagnostic next : freq) {
386:                         result.add(next);
387:                 }
388:                 return result;
389:         }
390:
391:         final Stream<String> charStream(String chars) {
392:                 return chars.chars().mapToObj(Character::toChars).map(String::new);
393:         }
394:
395:         final Diagnostic diagnostic(int severity, String message) {
396:                 return new BasicDiagnostic(severity, "org.eclipse.emfforms.common.tests", 0, message, null);
397:         }
398:
399:         final Diagnostic error(String message) {
400:                 return diagnostic(Diagnostic.ERROR, message);
401:         }
402:
403:         final Diagnostic warning(String message) {
404:                 return diagnostic(Diagnostic.WARNING, message);
405:         }
406:
407:         final Diagnostic info(String message) {
408:                 return diagnostic(Diagnostic.INFO, message);
409:         }
410:
411:         final Matcher<Diagnostic> matches(int severityMask, Matcher<String> message) {
412:                 return new TypeSafeDiagnosingMatcher<Diagnostic>() {
413:                         @Override
414:                         protected boolean matchesSafely(Diagnostic item, Description mismatchDescription) {
415:                                 if ((severityMask & item.getSeverity()) != item.getSeverity()) {
416:                                         mismatchDescription.appendText(String.format("severity does not match %x mask", severityMask)); //$NON-NLS-1$
417:                                         return false;
418:                                 }
419:
420:                                 if (!message.matches(item.getMessage())) {
421:                                         message.describeMismatch(item.getMessage(), mismatchDescription);
422:                                         return false;
423:                                 }
424:
425:                                 return true;
426:                         }
427:
428:                         @Override
429:                         public void describeTo(Description description) {
430:                                 description.appendText(
431:                                         String.format("diagnostic of severity matching %x mask with message that ", severityMask)); //$NON-NLS-1$
432:                                 description.appendDescriptionOf(message);
433:                         }
434:                 };
435:         }
436:
437:         final Matcher<Diagnostic> matches(int severityMask, String message) {
438:                 return matches(severityMask, is(message));
439:         }
440:
441:         /**
442:          * Obtain a matcher that matches strings that are sub-strings of some target string.
443:          *
444:          * @param s the target string
445:          * @return a matcher of strings that are substrings of {@code s}
446:          */
447:         final Matcher<String> substringOf(String s) {
448:                 return new TypeSafeDiagnosingMatcher<String>() {
449:                         @Override
450:                         protected boolean matchesSafely(String item, Description mismatchDescription) {
451:                                 final boolean result = s.contains(item);
452:
453:                                 if (!result) {
454:                                         mismatchDescription.appendText(String.format("\"%s\" is not contained in \"%s\"", item, s)); //$NON-NLS-1$
455:                                 }
456:
457:                                 return result;
458:                         }
459:
460:                         @Override
461:                         public void describeTo(Description description) {
462:                                 description.appendText(String.format("string contained in \"%s\"", s)); //$NON-NLS-1$
463:                         }
464:                 };
465:         }
466: }