1 | package fr.sii.ogham.testing.assertion.util; | |
2 | ||
3 | import java.util.ArrayList; | |
4 | import java.util.List; | |
5 | ||
6 | import org.junit.ComparisonFailure; | |
7 | ||
8 | /** | |
9 | * The aim of this registry is to report all errors at once. | |
10 | * | |
11 | * It registers all functions but doesn't execute them. | |
12 | * | |
13 | * When {@link #execute()} method is called, then all functions are executed. | |
14 | * All failures/failed assertions are collected and reported using a | |
15 | * {@link MultipleAssertionError}. | |
16 | * | |
17 | * <p> | |
18 | * A {@link ComparisonFailure} exception is thrown. Therefore Eclipse can handle | |
19 | * this exception and provide a comparison view with all differences. | |
20 | * | |
21 | * @author Aurélien Baudet | |
22 | * | |
23 | */ | |
24 | public class FailAtEndRegistry implements AssertionRegistry { | |
25 | private final List<Executable<?>> assertions; | |
26 | private final boolean convertToComparisonFailure; | |
27 | ||
28 | /** | |
29 | * Initializes an empty registry. | |
30 | * | |
31 | * If the system property | |
32 | * {@code "ogham.testing.assertions.fail-at-end.throw-comparison-failure"} | |
33 | * is set to true (or not set at all), a {@link ComparisonFailure} exception | |
34 | * is thrown. Using a {@link ComparisonFailure} lets Eclipse handle it | |
35 | * specifically and provides a comparison view (Eclipse doesn't handle | |
36 | * sub-classes of {@link ComparisonFailure}). | |
37 | * | |
38 | * To throw {@link MultipleAssertionError} instead of | |
39 | * {@link ComparisonFailure}, you can set the | |
40 | * {@code "ogham.testing.assertions.fail-at-end.throw-comparison-failure"} | |
41 | * system property to {@code "false"}. This can be useful if you need to | |
42 | * manually handle every failure/failed assertions. | |
43 | */ | |
44 | public FailAtEndRegistry() { | |
45 | this(Boolean.valueOf(System.getProperty("ogham.testing.assertions.fail-at-end.throw-comparison-failure", "true"))); | |
46 | } | |
47 | ||
48 | /** | |
49 | * Initializes an empty registry. | |
50 | * | |
51 | * If {@code convertToComparisonFailure} parameter is set to true, a | |
52 | * {@link ComparisonFailure} exception is thrown. Using a | |
53 | * {@link ComparisonFailure} lets Eclipse handle it specifically and | |
54 | * provides a comparison view (Eclipse doesn't handle sub-classes of | |
55 | * {@link ComparisonFailure}). | |
56 | * | |
57 | * @param convertToComparisonFailure | |
58 | * true to generate a {@link ComparisonFailure} to let Eclipse | |
59 | * display a comparison view. | |
60 | */ | |
61 | public FailAtEndRegistry(boolean convertToComparisonFailure) { | |
62 | super(); | |
63 | assertions = new ArrayList<>(); | |
64 | this.convertToComparisonFailure = convertToComparisonFailure; | |
65 | } | |
66 | ||
67 | public <E extends Exception> void register(Executable<E> executable) throws E { | |
68 | assertions.add(executable); | |
69 | } | |
70 | ||
71 | @SuppressWarnings("squid:S2221") // we can't know which exception may be | |
72 | // thrown so we have to catch all | |
73 | // exceptions | |
74 | public void execute() { | |
75 | List<Throwable> failures = new ArrayList<>(); | |
76 | for (Executable<?> executable : assertions) { | |
77 | try { | |
78 |
3
1. execute : removed call to fr/sii/ogham/testing/assertion/util/Executable::run → NO_COVERAGE 2. execute : removed call to fr/sii/ogham/testing/assertion/util/Executable::run → SURVIVED 3. execute : removed call to fr/sii/ogham/testing/assertion/util/Executable::run → KILLED |
executable.run(); |
79 | } catch (Exception | AssertionError e) { | |
80 | failures.add(e); | |
81 | } | |
82 | } | |
83 |
6
1. execute : negated conditional → NO_COVERAGE 2. execute : negated conditional → KILLED 3. execute : negated conditional → KILLED 4. execute : negated conditional → KILLED 5. execute : negated conditional → KILLED 6. execute : negated conditional → KILLED |
if (!failures.isEmpty()) { |
84 |
2
1. execute : removed call to fr/sii/ogham/testing/assertion/util/FailAtEndRegistry::throwFailures → NO_COVERAGE 2. execute : removed call to fr/sii/ogham/testing/assertion/util/FailAtEndRegistry::throwFailures → KILLED |
throwFailures(failures); |
85 | } | |
86 | } | |
87 | ||
88 | private void throwFailures(List<Throwable> failures) { | |
89 | MultipleAssertionError e = new MultipleAssertionError(failures); | |
90 |
2
1. throwFailures : negated conditional → NO_COVERAGE 2. throwFailures : negated conditional → KILLED |
if (convertToComparisonFailure) { |
91 | throw e.toComparisonFailure(); | |
92 | } | |
93 | throw e; | |
94 | } | |
95 | } | |
Mutations | ||
78 |
1.1 2.2 3.3 |
|
83 |
1.1 2.2 3.3 4.4 5.5 6.6 |
|
84 |
1.1 2.2 |
|
90 |
1.1 2.2 |