1 | package fr.sii.ogham.testing.assertion.util; | |
2 | ||
3 | import static java.util.regex.Pattern.MULTILINE; | |
4 | ||
5 | import java.util.List; | |
6 | import java.util.StringJoiner; | |
7 | import java.util.function.Function; | |
8 | import java.util.regex.Pattern; | |
9 | ||
10 | import org.junit.ComparisonFailure; | |
11 | ||
12 | public class MultipleAssertionError extends AssertionError { | |
13 | /** | |
14 | * | |
15 | */ | |
16 | private static final long serialVersionUID = 1L; | |
17 | private static final String FAILURE_SEPARATOR = "\n\t______________________________\n"; | |
18 | private static final Pattern INDENT = Pattern.compile("^", MULTILINE); | |
19 | ||
20 | private final List<Throwable> failures; | |
21 | ||
22 | public MultipleAssertionError(List<Throwable> failures) { | |
23 | super(generateMessage(failures)); | |
24 | this.failures = failures; | |
25 | } | |
26 | ||
27 | /** | |
28 | * Get the whole list of failures/failed assertions. | |
29 | * | |
30 | * @return list of failures/failed assertions | |
31 | */ | |
32 | public List<Throwable> getFailures() { | |
33 |
2
1. getFailures : replaced return value with Collections.emptyList for fr/sii/ogham/testing/assertion/util/MultipleAssertionError::getFailures → NO_COVERAGE 2. getFailures : replaced return value with Collections.emptyList for fr/sii/ogham/testing/assertion/util/MultipleAssertionError::getFailures → KILLED |
return failures; |
34 | } | |
35 | ||
36 | /** | |
37 | * Eclipse can only handle {@link ComparisonFailure} instance (not | |
38 | * sub-classes...) | |
39 | * | |
40 | * @return the same exception but converted to {@link ComparisonFailure} | |
41 | * (list of failures is lost) | |
42 | */ | |
43 | public ComparisonFailure toComparisonFailure() { | |
44 |
1
1. toComparisonFailure : replaced return value with null for fr/sii/ogham/testing/assertion/util/MultipleAssertionError::toComparisonFailure → NO_COVERAGE |
return new ComparisonFailure(generateMessage(failures), generateExpected(failures), generateActual(failures)); |
45 | } | |
46 | ||
47 | private static String generateMessage(List<Throwable> failures) { | |
48 | StringJoiner joiner = new StringJoiner(FAILURE_SEPARATOR, "Multiple assertions/failures (" + failures.size() + "):\n", FAILURE_SEPARATOR); | |
49 | int idx = 1; | |
50 | for (Throwable f : failures) { | |
51 | joiner.add(indent("Failure " + idx + ":\n" + getMessage(f))); | |
52 |
2
1. generateMessage : Changed increment from 1 to -1 → SURVIVED 2. generateMessage : Changed increment from 1 to -1 → NO_COVERAGE |
idx++; |
53 | } | |
54 |
2
1. generateMessage : replaced return value with "" for fr/sii/ogham/testing/assertion/util/MultipleAssertionError::generateMessage → NO_COVERAGE 2. generateMessage : replaced return value with "" for fr/sii/ogham/testing/assertion/util/MultipleAssertionError::generateMessage → SURVIVED |
return joiner.toString(); |
55 | } | |
56 | ||
57 | private static String generateExpected(List<Throwable> failures) { | |
58 |
1
1. generateExpected : replaced return value with "" for fr/sii/ogham/testing/assertion/util/MultipleAssertionError::generateExpected → NO_COVERAGE |
return generateComparison(failures, "Expected", ComparisonFailure::getExpected); |
59 | } | |
60 | ||
61 | private static String generateActual(List<Throwable> failures) { | |
62 |
1
1. generateActual : replaced return value with "" for fr/sii/ogham/testing/assertion/util/MultipleAssertionError::generateActual → NO_COVERAGE |
return generateComparison(failures, "Actual", ComparisonFailure::getActual); |
63 | } | |
64 | ||
65 | private static String generateComparison(List<Throwable> failures, String name, Function<ComparisonFailure, String> getter) { | |
66 | StringJoiner joiner = new StringJoiner(FAILURE_SEPARATOR, name + " (" + failures.size() + "):\n", FAILURE_SEPARATOR); | |
67 | int idx = 1; | |
68 | for (Throwable f : failures) { | |
69 | String prefix = "Failure " + idx + ": " + getMessage(f) + "\n\n"; | |
70 |
1
1. generateComparison : negated conditional → NO_COVERAGE |
if (f instanceof ComparisonFailure) { |
71 | joiner.add(indent(prefix + getter.apply((ComparisonFailure) f))); | |
72 | } else { | |
73 | joiner.add(indent(prefix + "</!\\ no comparison available>")); | |
74 | } | |
75 |
1
1. generateComparison : Changed increment from 1 to -1 → NO_COVERAGE |
idx++; |
76 | } | |
77 |
1
1. generateComparison : replaced return value with "" for fr/sii/ogham/testing/assertion/util/MultipleAssertionError::generateComparison → NO_COVERAGE |
return joiner.toString(); |
78 | } | |
79 | ||
80 | private static String indent(String message) { | |
81 |
2
1. indent : replaced return value with "" for fr/sii/ogham/testing/assertion/util/MultipleAssertionError::indent → NO_COVERAGE 2. indent : replaced return value with "" for fr/sii/ogham/testing/assertion/util/MultipleAssertionError::indent → SURVIVED |
return INDENT.matcher(message).replaceAll(" "); |
82 | } | |
83 | ||
84 | private static String getMessage(Throwable failure) { | |
85 | String message = failure.getMessage(); | |
86 |
2
1. getMessage : negated conditional → SURVIVED 2. getMessage : negated conditional → NO_COVERAGE |
if (message == null) { |
87 | message = ""; | |
88 | } | |
89 |
2
1. getMessage : replaced return value with "" for fr/sii/ogham/testing/assertion/util/MultipleAssertionError::getMessage → NO_COVERAGE 2. getMessage : replaced return value with "" for fr/sii/ogham/testing/assertion/util/MultipleAssertionError::getMessage → SURVIVED |
return failure.getClass().getName() + ": " + message; |
90 | } | |
91 | ||
92 | } | |
Mutations | ||
33 |
1.1 2.2 |
|
44 |
1.1 |
|
52 |
1.1 2.2 |
|
54 |
1.1 2.2 |
|
58 |
1.1 |
|
62 |
1.1 |
|
70 |
1.1 |
|
75 |
1.1 |
|
77 |
1.1 |
|
81 |
1.1 2.2 |
|
86 |
1.1 2.2 |
|
89 |
1.1 2.2 |