AssertTemplate.java

1
package fr.sii.ogham.testing.assertion.template;
2
3
import fr.sii.ogham.testing.assertion.util.AssertionRegistry;
4
import fr.sii.ogham.testing.assertion.util.Executable;
5
import fr.sii.ogham.testing.assertion.util.FailAtEndRegistry;
6
import org.junit.jupiter.api.Assertions;
7
8
import java.io.IOException;
9
import java.nio.charset.Charset;
10
11
import static fr.sii.ogham.testing.util.ResourceUtils.resourceAsString;
12
13
/**
14
 * Assertion class that simplifies checking template content.
15
 * 
16
 * @author Aurélien Baudet
17
 *
18
 */
19
public final class AssertTemplate {
20
	/**
21
	 * Assert that the received content is same as the expected content. The
22
	 * check is strict (totally identical even new line characters). The expected content is loaded
23
	 * from the classpath.
24
	 * 
25
	 * @param expectedContentPath
26
	 *            the path to the expected content available in the classpath
27
	 * @param content
28
	 *            the received content to check
29
	 * @throws IOException
30
	 *             when the expected content couldn't be read
31
	 */
32
	public static void assertEquals(String expectedContentPath, Object content) throws IOException {
33
		AssertionRegistry registry = new FailAtEndRegistry();
34 1 1. assertEquals : removed call to fr/sii/ogham/testing/assertion/template/AssertTemplate::assertEquals → NO_COVERAGE
		assertEquals(loadOrNull(expectedContentPath, registry), content, true, registry);
35 1 1. assertEquals : removed call to fr/sii/ogham/testing/assertion/util/AssertionRegistry::execute → NO_COVERAGE
		registry.execute();
36
	}
37
38
	/**
39
	 * Assert that the received content is same as the expected content. The
40
	 * check is permissive (new line characters are ignored). The expected
41
	 * content is loaded from the classpath.
42
	 * 
43
	 * @param expectedContentPath
44
	 *            the path to the expected content available in the classpath
45
	 * @param content
46
	 *            the received content to check
47
	 * @throws IOException
48
	 *             when the expected content couldn't be read
49
	 */
50
	public static void assertSimilar(String expectedContentPath, Object content) throws IOException {
51
		AssertionRegistry registry = new FailAtEndRegistry();
52 1 1. assertSimilar : removed call to fr/sii/ogham/testing/assertion/template/AssertTemplate::assertEquals → NO_COVERAGE
		assertEquals(loadOrNull(expectedContentPath, registry), content, false, registry);
53 1 1. assertSimilar : removed call to fr/sii/ogham/testing/assertion/util/AssertionRegistry::execute → NO_COVERAGE
		registry.execute();
54
	}
55
56
	/**
57
	 * Assert that the received content is same as the expected content. The
58
	 * check is strict (totally identical even new line characters).
59
	 * 
60
	 * @param expectedContent
61
	 *            the expected content
62
	 * @param content
63
	 *            the received content to check
64
	 */
65
	public static void assertEquals(String expectedContent, String content) {
66
		AssertionRegistry registry = new FailAtEndRegistry();
67 1 1. assertEquals : removed call to fr/sii/ogham/testing/assertion/template/AssertTemplate::assertEquals → NO_COVERAGE
		assertEquals(expectedContent, content, true, registry);
68 1 1. assertEquals : removed call to fr/sii/ogham/testing/assertion/util/AssertionRegistry::execute → NO_COVERAGE
		registry.execute();
69
	}
70
71
	/**
72
	 * Assert that the received content is same as the expected content. The
73
	 * check is permissive (new line characters are ignored).
74
	 * 
75
	 * @param expectedContent
76
	 *            the expected content
77
	 * @param content
78
	 *            the received content to check
79
	 */
80
	public static void assertSimilar(String expectedContent, String content) {
81
		AssertionRegistry registry = new FailAtEndRegistry();
82 1 1. assertSimilar : removed call to fr/sii/ogham/testing/assertion/template/AssertTemplate::assertEquals → NO_COVERAGE
		assertEquals(expectedContent, content, false, registry);
83 1 1. assertSimilar : removed call to fr/sii/ogham/testing/assertion/util/AssertionRegistry::execute → NO_COVERAGE
		registry.execute();
84
	}
85
	
86
	private static void assertEquals(String expectedContent, Object content, boolean strict, AssertionRegistry registry) {
87 1 1. assertEquals : negated conditional → NO_COVERAGE
		String expected = strict ? expectedContent : sanitize(expectedContent);
88 1 1. assertEquals : negated conditional → NO_COVERAGE
		String contentAsString = content==null ? null : content.toString();
89 1 1. assertEquals : negated conditional → NO_COVERAGE
		String actual = strict ? contentAsString : sanitize(contentAsString);
90 4 1. assertEquals : removed call to fr/sii/ogham/testing/assertion/util/AssertionRegistry::register → NO_COVERAGE
2. lambda$assertEquals$0 : removed call to org/junit/jupiter/api/Assertions::assertEquals → NO_COVERAGE
3. lambda$assertEquals$0 : negated conditional → NO_COVERAGE
4. lambda$assertEquals$0 : negated conditional → NO_COVERAGE
		registry.register(() -> Assertions.assertEquals(expected == null ? null : expected.replace("\r", ""), actual == null ? null : actual.replace("\r", ""), "parsed template is different to expected content"));
91
	}
92
	
93
	private static String loadOrNull(String path, AssertionRegistry registry) throws IOException {
94 1 1. loadOrNull : negated conditional → NO_COVERAGE
		if (path == null) {
95 1 1. loadOrNull : replaced return value with "" for fr/sii/ogham/testing/assertion/template/AssertTemplate::loadOrNull → NO_COVERAGE
			return null;
96
		}
97
		try {
98 1 1. loadOrNull : replaced return value with "" for fr/sii/ogham/testing/assertion/template/AssertTemplate::loadOrNull → NO_COVERAGE
			return resourceAsString(path, Charset.defaultCharset());
99
		} catch (IOException e) {
100 1 1. loadOrNull : removed call to fr/sii/ogham/testing/assertion/util/AssertionRegistry::register → NO_COVERAGE
			registry.register(failure(e));
101 1 1. loadOrNull : replaced return value with "" for fr/sii/ogham/testing/assertion/template/AssertTemplate::loadOrNull → NO_COVERAGE
			return null;
102
		}
103
	}
104
	
105
	private static <E extends Exception> Executable<E> failure(E exception) {
106 1 1. failure : replaced return value with null for fr/sii/ogham/testing/assertion/template/AssertTemplate::failure → NO_COVERAGE
		return new Executable<E>() {
107
			@Override
108
			public void run() throws E {
109
				throw exception;
110
			}
111
		};
112
	}
113
114
	private static String sanitize(String str) {
115 1 1. sanitize : negated conditional → NO_COVERAGE
		if (str == null) {
116 1 1. sanitize : replaced return value with "" for fr/sii/ogham/testing/assertion/template/AssertTemplate::sanitize → NO_COVERAGE
			return null;
117
		}
118 1 1. sanitize : replaced return value with "" for fr/sii/ogham/testing/assertion/template/AssertTemplate::sanitize → NO_COVERAGE
		return str.replaceAll("\r|\n", "");
119
	}
120
	
121
	private AssertTemplate() {
122
		super();
123
	}
124
}

Mutations

34

1.1
Location : assertEquals
Killed by :
removed call to fr/sii/ogham/testing/assertion/template/AssertTemplate::assertEquals → NO_COVERAGE

35

1.1
Location : assertEquals
Killed by :
removed call to fr/sii/ogham/testing/assertion/util/AssertionRegistry::execute → NO_COVERAGE

52

1.1
Location : assertSimilar
Killed by :
removed call to fr/sii/ogham/testing/assertion/template/AssertTemplate::assertEquals → NO_COVERAGE

53

1.1
Location : assertSimilar
Killed by :
removed call to fr/sii/ogham/testing/assertion/util/AssertionRegistry::execute → NO_COVERAGE

67

1.1
Location : assertEquals
Killed by :
removed call to fr/sii/ogham/testing/assertion/template/AssertTemplate::assertEquals → NO_COVERAGE

68

1.1
Location : assertEquals
Killed by :
removed call to fr/sii/ogham/testing/assertion/util/AssertionRegistry::execute → NO_COVERAGE

82

1.1
Location : assertSimilar
Killed by :
removed call to fr/sii/ogham/testing/assertion/template/AssertTemplate::assertEquals → NO_COVERAGE

83

1.1
Location : assertSimilar
Killed by :
removed call to fr/sii/ogham/testing/assertion/util/AssertionRegistry::execute → NO_COVERAGE

87

1.1
Location : assertEquals
Killed by :
negated conditional → NO_COVERAGE

88

1.1
Location : assertEquals
Killed by :
negated conditional → NO_COVERAGE

89

1.1
Location : assertEquals
Killed by :
negated conditional → NO_COVERAGE

90

1.1
Location : assertEquals
Killed by :
removed call to fr/sii/ogham/testing/assertion/util/AssertionRegistry::register → NO_COVERAGE

2.2
Location : lambda$assertEquals$0
Killed by :
removed call to org/junit/jupiter/api/Assertions::assertEquals → NO_COVERAGE

3.3
Location : lambda$assertEquals$0
Killed by :
negated conditional → NO_COVERAGE

4.4
Location : lambda$assertEquals$0
Killed by :
negated conditional → NO_COVERAGE

94

1.1
Location : loadOrNull
Killed by :
negated conditional → NO_COVERAGE

95

1.1
Location : loadOrNull
Killed by :
replaced return value with "" for fr/sii/ogham/testing/assertion/template/AssertTemplate::loadOrNull → NO_COVERAGE

98

1.1
Location : loadOrNull
Killed by :
replaced return value with "" for fr/sii/ogham/testing/assertion/template/AssertTemplate::loadOrNull → NO_COVERAGE

100

1.1
Location : loadOrNull
Killed by :
removed call to fr/sii/ogham/testing/assertion/util/AssertionRegistry::register → NO_COVERAGE

101

1.1
Location : loadOrNull
Killed by :
replaced return value with "" for fr/sii/ogham/testing/assertion/template/AssertTemplate::loadOrNull → NO_COVERAGE

106

1.1
Location : failure
Killed by :
replaced return value with null for fr/sii/ogham/testing/assertion/template/AssertTemplate::failure → NO_COVERAGE

115

1.1
Location : sanitize
Killed by :
negated conditional → NO_COVERAGE

116

1.1
Location : sanitize
Killed by :
replaced return value with "" for fr/sii/ogham/testing/assertion/template/AssertTemplate::sanitize → NO_COVERAGE

118

1.1
Location : sanitize
Killed by :
replaced return value with "" for fr/sii/ogham/testing/assertion/template/AssertTemplate::sanitize → NO_COVERAGE

Active mutators

Tests examined


Report generated by PIT 1.13.1