|
10 | 10 |
|
11 | 11 | package platform.tooling.support.tests;
|
12 | 12 |
|
| 13 | +import static de.skuzzle.test.snapshots.data.xml.XmlSnapshot.xml; |
13 | 14 | import static org.junit.platform.reporting.testutil.FileUtils.findPath;
|
14 | 15 |
|
| 16 | +import java.io.IOException; |
| 17 | +import java.nio.file.Files; |
15 | 18 | import java.nio.file.Path;
|
| 19 | +import java.util.Map; |
| 20 | +import java.util.function.UnaryOperator; |
16 | 21 | import java.util.regex.Pattern;
|
17 | 22 |
|
18 |
| -import org.xmlunit.assertj3.XmlAssert; |
19 |
| -import org.xmlunit.placeholder.PlaceholderDifferenceEvaluator; |
| 23 | +import de.skuzzle.test.snapshots.Snapshot; |
| 24 | +import de.skuzzle.test.snapshots.SnapshotSerializer; |
| 25 | +import de.skuzzle.test.snapshots.StructuredData; |
| 26 | +import de.skuzzle.test.snapshots.StructuredDataProvider; |
20 | 27 |
|
21 | 28 | class XmlAssertions {
|
22 | 29 |
|
23 |
| - static void verifyContainsExpectedStartedOpenTestReport(Path testResultsDir) { |
| 30 | + static void verifyContainsExpectedStartedOpenTestReport(Path testResultsDir, Snapshot snapshot) throws IOException { |
24 | 31 | var xmlFile = findPath(testResultsDir, "glob:**/open-test-report.xml");
|
25 |
| - verifyContent(xmlFile); |
| 32 | + verifyContent(xmlFile, snapshot); |
26 | 33 | }
|
27 | 34 |
|
28 |
| - private static void verifyContent(Path xmlFile) { |
29 |
| - var expected = """ |
30 |
| - <e:events xmlns="https://schemas.opentest4j.org/reporting/core/0.2.0" |
31 |
| - xmlns:e="https://schemas.opentest4j.org/reporting/events/0.2.0" |
32 |
| - xmlns:git="https://schemas.opentest4j.org/reporting/git/0.2.0" |
33 |
| - xmlns:java="https://schemas.opentest4j.org/reporting/java/0.2.0" |
34 |
| - xmlns:junit="https://schemas.junit.org/open-test-reporting" |
35 |
| - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
36 |
| - xsi:schemaLocation="https://schemas.junit.org/open-test-reporting https://junit.org/junit5/schemas/open-test-reporting/junit-1.9.xsd"> |
37 |
| - <infrastructure> |
38 |
| - <hostName>${xmlunit.ignore}</hostName> |
39 |
| - <userName>${xmlunit.ignore}</userName> |
40 |
| - <operatingSystem>${xmlunit.ignore}</operatingSystem> |
41 |
| - <cpuCores>${xmlunit.ignore}</cpuCores> |
42 |
| - <java:javaVersion>${xmlunit.ignore}</java:javaVersion> |
43 |
| - <java:fileEncoding>${xmlunit.ignore}</java:fileEncoding> |
44 |
| - <java:heapSize max="${xmlunit.isNumber}"/> |
45 |
| - </infrastructure> |
46 |
| - <e:started id="1" name="JUnit Jupiter" time="${xmlunit.isDateTime}"> |
47 |
| - <metadata> |
48 |
| - <junit:uniqueId>[engine:junit-jupiter]</junit:uniqueId> |
49 |
| - <junit:legacyReportingName>JUnit Jupiter</junit:legacyReportingName> |
50 |
| - <junit:type>CONTAINER</junit:type> |
51 |
| - </metadata> |
52 |
| - </e:started> |
53 |
| - <e:started id="2" name="CalculatorTests" parentId="1" time="${xmlunit.isDateTime}"> |
54 |
| - <metadata> |
55 |
| - <junit:uniqueId>[engine:junit-jupiter]/[class:com.example.project.CalculatorTests]</junit:uniqueId> |
56 |
| - <junit:legacyReportingName>com.example.project.CalculatorTests</junit:legacyReportingName> |
57 |
| - <junit:type>CONTAINER</junit:type> |
58 |
| - </metadata> |
59 |
| - <sources> |
60 |
| - <java:classSource className="com.example.project.CalculatorTests"/> |
61 |
| - </sources> |
62 |
| - </e:started> |
63 |
| - <e:started id="3" name="1 + 1 = 2" parentId="2" time="${xmlunit.isDateTime}"> |
64 |
| - <metadata> |
65 |
| - <junit:uniqueId>[engine:junit-jupiter]/[class:com.example.project.CalculatorTests]/[method:addsTwoNumbers()]</junit:uniqueId> |
66 |
| - <junit:legacyReportingName>addsTwoNumbers()</junit:legacyReportingName> |
67 |
| - <junit:type>TEST</junit:type> |
68 |
| - </metadata> |
69 |
| - <sources> |
70 |
| - <java:methodSource className="com.example.project.CalculatorTests" methodName="addsTwoNumbers" methodParameterTypes=""/> |
71 |
| - </sources> |
72 |
| - </e:started> |
73 |
| - <e:finished id="3" time="${xmlunit.isDateTime}"> |
74 |
| - <result status="SUCCESSFUL"/> |
75 |
| - </e:finished> |
76 |
| - <e:started id="4" name="add(int, int, int)" parentId="2" time="${xmlunit.isDateTime}"> |
77 |
| - <metadata> |
78 |
| - <junit:uniqueId>[engine:junit-jupiter]/[class:com.example.project.CalculatorTests]/[test-template:add(int, int, int)]</junit:uniqueId> |
79 |
| - <junit:legacyReportingName>add(int, int, int)</junit:legacyReportingName> |
80 |
| - <junit:type>CONTAINER</junit:type> |
81 |
| - </metadata> |
82 |
| - <sources> |
83 |
| - <java:methodSource className="com.example.project.CalculatorTests" methodName="add" methodParameterTypes="int, int, int"/> |
84 |
| - </sources> |
85 |
| - </e:started> |
86 |
| - <e:started id="5" name="0 + 1 = 1" parentId="4" time="${xmlunit.isDateTime}"> |
87 |
| - <metadata> |
88 |
| - <junit:uniqueId>[engine:junit-jupiter]/[class:com.example.project.CalculatorTests]/[test-template:add(int, int, int)]/[test-template-invocation:#1]</junit:uniqueId> |
89 |
| - <junit:legacyReportingName>add(int, int, int)[1]</junit:legacyReportingName> |
90 |
| - <junit:type>TEST</junit:type> |
91 |
| - </metadata> |
92 |
| - <sources> |
93 |
| - <java:methodSource className="com.example.project.CalculatorTests" methodName="add" methodParameterTypes="int, int, int"/> |
94 |
| - </sources> |
95 |
| - </e:started> |
96 |
| - <e:finished id="5" time="${xmlunit.isDateTime}"> |
97 |
| - <result status="SUCCESSFUL"/> |
98 |
| - </e:finished> |
99 |
| - <e:started id="6" name="1 + 2 = 3" parentId="4" time="${xmlunit.isDateTime}"> |
100 |
| - <metadata> |
101 |
| - <junit:uniqueId>[engine:junit-jupiter]/[class:com.example.project.CalculatorTests]/[test-template:add(int, int, int)]/[test-template-invocation:#2]</junit:uniqueId> |
102 |
| - <junit:legacyReportingName>add(int, int, int)[2]</junit:legacyReportingName> |
103 |
| - <junit:type>TEST</junit:type> |
104 |
| - </metadata> |
105 |
| - <sources> |
106 |
| - <java:methodSource className="com.example.project.CalculatorTests" methodName="add" methodParameterTypes="int, int, int"/> |
107 |
| - </sources> |
108 |
| - </e:started> |
109 |
| - <e:finished id="6" time="${xmlunit.isDateTime}"> |
110 |
| - <result status="SUCCESSFUL"/> |
111 |
| - </e:finished> |
112 |
| - <e:started id="7" name="49 + 51 = 100" parentId="4" time="${xmlunit.isDateTime}"> |
113 |
| - <metadata> |
114 |
| - <junit:uniqueId>[engine:junit-jupiter]/[class:com.example.project.CalculatorTests]/[test-template:add(int, int, int)]/[test-template-invocation:#3]</junit:uniqueId> |
115 |
| - <junit:legacyReportingName>add(int, int, int)[3]</junit:legacyReportingName> |
116 |
| - <junit:type>TEST</junit:type> |
117 |
| - </metadata> |
118 |
| - <sources> |
119 |
| - <java:methodSource className="com.example.project.CalculatorTests" methodName="add" methodParameterTypes="int, int, int"/> |
120 |
| - </sources> |
121 |
| - </e:started> |
122 |
| - <e:finished id="7" time="${xmlunit.isDateTime}"> |
123 |
| - <result status="SUCCESSFUL"/> |
124 |
| - </e:finished> |
125 |
| - <e:started id="8" name="1 + 100 = 101" parentId="4" time="${xmlunit.isDateTime}"> |
126 |
| - <metadata> |
127 |
| - <junit:uniqueId>[engine:junit-jupiter]/[class:com.example.project.CalculatorTests]/[test-template:add(int, int, int)]/[test-template-invocation:#4]</junit:uniqueId> |
128 |
| - <junit:legacyReportingName>add(int, int, int)[4]</junit:legacyReportingName> |
129 |
| - <junit:type>TEST</junit:type> |
130 |
| - </metadata> |
131 |
| - <sources> |
132 |
| - <java:methodSource className="com.example.project.CalculatorTests" methodName="add" methodParameterTypes="int, int, int"/> |
133 |
| - </sources> |
134 |
| - </e:started> |
135 |
| - <e:finished id="8" time="${xmlunit.isDateTime}"> |
136 |
| - <result status="SUCCESSFUL"/> |
137 |
| - </e:finished> |
138 |
| - <e:finished id="4" time="${xmlunit.isDateTime}"> |
139 |
| - <result status="SUCCESSFUL"/> |
140 |
| - </e:finished> |
141 |
| - <e:finished id="2" time="${xmlunit.isDateTime}"> |
142 |
| - <result status="SUCCESSFUL"/> |
143 |
| - </e:finished> |
144 |
| - <e:finished id="1" time="${xmlunit.isDateTime}"> |
145 |
| - <result status="SUCCESSFUL"/> |
146 |
| - </e:finished> |
147 |
| - </e:events> |
148 |
| - """; |
| 35 | + private static void verifyContent(Path xmlFile, Snapshot snapshot) throws IOException { |
| 36 | + snapshot.named("open-test-report.xml") // |
| 37 | + .assertThat(Files.readString(xmlFile)) // |
| 38 | + .as(obfuscated( // |
| 39 | + xml() // |
| 40 | + .withXPathNamespaceContext(Map.of( // |
| 41 | + "c", "https://schemas.opentest4j.org/reporting/core/0.2.0", // |
| 42 | + "e", "https://schemas.opentest4j.org/reporting/events/0.2.0", // |
| 43 | + "java", "https://schemas.opentest4j.org/reporting/java/0.2.0" // |
| 44 | + )) // |
| 45 | + .withComparisonRules(rules -> rules // |
| 46 | + .pathAt("//@time").mustMatch( |
| 47 | + Pattern.compile("\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}(?:\\.\\d+)?Z")) // |
| 48 | + .pathAt("//c:infrastructure/c:hostName/text()").ignore() // |
| 49 | + .pathAt("//c:infrastructure/c:userName/text()").ignore() // |
| 50 | + .pathAt("//c:infrastructure/c:operatingSystem/text()").ignore() // |
| 51 | + .pathAt("//c:infrastructure/c:cpuCores/text()").ignore() // |
| 52 | + .pathAt("//c:infrastructure/java:javaVersion/text()").ignore() // |
| 53 | + .pathAt("//c:infrastructure/java:fileEncoding/text()").ignore() // |
| 54 | + .pathAt("//c:infrastructure/java:heapSize/@max").ignore() // |
| 55 | + ), // |
| 56 | + text -> text // |
| 57 | + .replaceAll("<hostName>.+?</hostName>", "<hostName>obfuscated</hostName>") // |
| 58 | + .replaceAll("<userName>.+?</userName>", "<userName>obfuscated</userName>") // |
| 59 | + )) // |
| 60 | + .matchesSnapshotStructure(); |
| 61 | + } |
| 62 | + |
| 63 | + private static StructuredDataProvider obfuscated(StructuredDataProvider provider, |
| 64 | + UnaryOperator<String> obfuscator) { |
| 65 | + return () -> { |
| 66 | + var structuredData = provider.build(); |
| 67 | + var snapshotSerializer = obfuscatingSnapshotSerializer(structuredData.snapshotSerializer(), obfuscator); |
| 68 | + return StructuredData.with(snapshotSerializer, structuredData.structuralAssertions()); |
| 69 | + }; |
| 70 | + } |
149 | 71 |
|
150 |
| - XmlAssert.assertThat(xmlFile).and(expected) // |
151 |
| - .withDifferenceEvaluator(new PlaceholderDifferenceEvaluator(Pattern.quote("${"), Pattern.quote("}"), |
152 |
| - Pattern.quote("#"), Pattern.quote("#"), ",")) // |
153 |
| - .ignoreWhitespace() // |
154 |
| - .areIdentical(); |
| 72 | + private static SnapshotSerializer obfuscatingSnapshotSerializer(SnapshotSerializer delegate, |
| 73 | + UnaryOperator<String> obfuscator) { |
| 74 | + return testResult -> { |
| 75 | + Object obfuscatedTestResult = testResult; |
| 76 | + if (testResult instanceof String) { |
| 77 | + obfuscatedTestResult = obfuscator.apply((String) testResult); |
| 78 | + } |
| 79 | + return delegate.serialize(obfuscatedTestResult); |
| 80 | + }; |
155 | 81 | }
|
156 | 82 | }
|
0 commit comments