MultipleAssertionError.java

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

Mutations

35

1.1
Location : getFailures
Killed by :
replaced return value with Collections.emptyList for fr/sii/ogham/testing/assertion/util/MultipleAssertionError::getFailures → NO_COVERAGE

46

1.1
Location : toAssertionFailedError
Killed by :
replaced return value with null for fr/sii/ogham/testing/assertion/util/MultipleAssertionError::toAssertionFailedError → NO_COVERAGE

58

1.1
Location : generateMessage
Killed by :
Changed increment from 1 to -1 → NO_COVERAGE

60

1.1
Location : generateMessage
Killed by :
replaced return value with "" for fr/sii/ogham/testing/assertion/util/MultipleAssertionError::generateMessage → NO_COVERAGE

64

1.1
Location : generateExpected
Killed by :
replaced return value with "" for fr/sii/ogham/testing/assertion/util/MultipleAssertionError::generateExpected → NO_COVERAGE

68

1.1
Location : generateActual
Killed by :
replaced return value with "" for fr/sii/ogham/testing/assertion/util/MultipleAssertionError::generateActual → NO_COVERAGE

76

1.1
Location : generateComparison
Killed by :
negated conditional → NO_COVERAGE

81

1.1
Location : generateComparison
Killed by :
Changed increment from 1 to -1 → NO_COVERAGE

83

1.1
Location : generateComparison
Killed by :
replaced return value with "" for fr/sii/ogham/testing/assertion/util/MultipleAssertionError::generateComparison → NO_COVERAGE

87

1.1
Location : indent
Killed by :
replaced return value with "" for fr/sii/ogham/testing/assertion/util/MultipleAssertionError::indent → NO_COVERAGE

92

1.1
Location : getMessage
Killed by :
negated conditional → NO_COVERAGE

95

1.1
Location : getMessage
Killed by :
replaced return value with "" for fr/sii/ogham/testing/assertion/util/MultipleAssertionError::getMessage → NO_COVERAGE

Active mutators

Tests examined


Report generated by PIT 1.13.1