|
8 | 8 | import org.junit.jupiter.api.AfterAll; |
9 | 9 | import org.junit.jupiter.api.Assumptions; |
10 | 10 |
|
11 | | -import java.io.File; |
12 | 11 | import java.io.FileInputStream; |
13 | 12 | import java.io.IOException; |
14 | 13 | import java.nio.file.Files; |
|
17 | 16 | import java.util.zip.ZipEntry; |
18 | 17 | import java.util.zip.ZipInputStream; |
19 | 18 | import java.util.concurrent.ConcurrentHashMap; |
20 | | -import java.util.concurrent.atomic.LongAdder; |
21 | 19 | import java.util.stream.Stream; |
22 | 20 | import java.util.stream.StreamSupport; |
23 | 21 |
|
@@ -108,93 +106,97 @@ Stream<DynamicTest> testsFromFile(Path file) { |
108 | 106 | } |
109 | 107 | METRICS.testsDiscovered.add(testCount); |
110 | 108 | perFile(file).tests.add(testCount); |
111 | | - |
112 | | - return StreamSupport.stream(root.spliterator(), false) |
113 | | - .flatMap(group -> { |
114 | | - final var groupDesc = group.get("description").asText(); |
115 | | - try { |
116 | | - /// Attempt to compile the schema for this group; if unsupported features |
117 | | - /// (e.g., unresolved anchors) are present, skip this group gracefully. |
118 | | - final var schema = JsonSchema.compile( |
119 | | - Json.parse(group.get("schema").toString())); |
120 | | - |
121 | | - return StreamSupport.stream(group.get("tests").spliterator(), false) |
122 | | - .map(test -> DynamicTest.dynamicTest( |
123 | | - groupDesc + " – " + test.get("description").asText(), |
124 | | - () -> { |
125 | | - final var expected = test.get("valid").asBoolean(); |
126 | | - final boolean actual; |
127 | | - try { |
128 | | - actual = schema.validate( |
129 | | - Json.parse(test.get("data").toString())).valid(); |
130 | | - |
131 | | - /// Count validation attempt |
132 | | - METRICS.run.increment(); |
133 | | - perFile(file).run.increment(); |
134 | | - } catch (Exception e) { |
135 | | - final var reason = e.getMessage() == null ? e.getClass().getSimpleName() : e.getMessage(); |
136 | | - System.err.println("[JsonSchemaCheckIT] Skipping test due to exception: " |
137 | | - + groupDesc + " — " + reason + " (" + file.getFileName() + ")"); |
138 | | - |
139 | | - /// Count exception as skipped mismatch in strict metrics |
140 | | - METRICS.skippedMismatch.increment(); |
141 | | - perFile(file).skipMismatch.increment(); |
142 | | - |
143 | | - if (isStrict()) throw e; |
144 | | - Assumptions.assumeTrue(false, "Skipped: " + reason); |
145 | | - return; /// not reached when strict |
146 | | - } |
147 | 109 |
|
148 | | - if (isStrict()) { |
149 | | - try { |
150 | | - assertEquals(expected, actual); |
151 | | - /// Count pass in strict mode |
152 | | - METRICS.passed.increment(); |
153 | | - perFile(file).pass.increment(); |
154 | | - } catch (AssertionError e) { |
155 | | - /// Count failure in strict mode |
156 | | - METRICS.failed.increment(); |
157 | | - perFile(file).fail.increment(); |
158 | | - throw e; |
159 | | - } |
160 | | - } else if (expected != actual) { |
161 | | - System.err.println("[JsonSchemaCheckIT] Mismatch (ignored): " |
162 | | - + groupDesc + " — expected=" + expected + ", actual=" + actual |
163 | | - + " (" + file.getFileName() + ")"); |
164 | | - |
165 | | - /// Count lenient mismatch skip |
166 | | - METRICS.skippedMismatch.increment(); |
167 | | - perFile(file).skipMismatch.increment(); |
168 | | - |
169 | | - Assumptions.assumeTrue(false, "Mismatch ignored"); |
170 | | - } else { |
171 | | - /// Count pass in lenient mode |
172 | | - METRICS.passed.increment(); |
173 | | - perFile(file).pass.increment(); |
174 | | - } |
175 | | - })); |
176 | | - } catch (Exception ex) { |
177 | | - /// Unsupported schema for this group; emit a single skipped test for visibility |
178 | | - final var reason = ex.getMessage() == null ? ex.getClass().getSimpleName() : ex.getMessage(); |
179 | | - System.err.println("[JsonSchemaCheckIT] Skipping group due to unsupported schema: " |
180 | | - + groupDesc + " — " + reason + " (" + file.getFileName() + ")"); |
181 | | - |
182 | | - /// Count unsupported group skip |
183 | | - METRICS.skippedUnsupported.increment(); |
184 | | - perFile(file).skipUnsupported.increment(); |
185 | | - |
186 | | - return Stream.of(DynamicTest.dynamicTest( |
187 | | - groupDesc + " – SKIPPED: " + reason, |
188 | | - () -> { if (isStrict()) throw ex; Assumptions.assumeTrue(false, "Unsupported schema: " + reason); } |
189 | | - )); |
190 | | - } |
191 | | - }); |
| 110 | + return dynamicTestStream(file, root); |
192 | 111 | } catch (Exception ex) { |
193 | 112 | throw new RuntimeException("Failed to process " + file, ex); |
194 | 113 | } |
195 | 114 | } |
196 | 115 |
|
197 | | - static StrictMetrics.FileCounters perFile(Path file) { |
| 116 | + static Stream<DynamicTest> dynamicTestStream(Path file, JsonNode root) { |
| 117 | + return StreamSupport.stream(root.spliterator(), false) |
| 118 | + .flatMap(group -> { |
| 119 | + final var groupDesc = group.get("description").asText(); |
| 120 | + try { |
| 121 | + /// Attempt to compile the schema for this group; if unsupported features |
| 122 | + /// (e.g., unresolved anchors) are present, skip this group gracefully. |
| 123 | + final var schema = JsonSchema.compile( |
| 124 | + Json.parse(group.get("schema").toString())); |
| 125 | + |
| 126 | + return StreamSupport.stream(group.get("tests").spliterator(), false) |
| 127 | + .map(test -> DynamicTest.dynamicTest( |
| 128 | + groupDesc + " – " + test.get("description").asText(), |
| 129 | + () -> { |
| 130 | + final var expected = test.get("valid").asBoolean(); |
| 131 | + final boolean actual; |
| 132 | + try { |
| 133 | + actual = schema.validate( |
| 134 | + Json.parse(test.get("data").toString())).valid(); |
| 135 | + |
| 136 | + /// Count validation attempt |
| 137 | + METRICS.run.increment(); |
| 138 | + perFile(file).run.increment(); |
| 139 | + } catch (Exception e) { |
| 140 | + final var reason = e.getMessage() == null ? e.getClass().getSimpleName() : e.getMessage(); |
| 141 | + System.err.println("[JsonSchemaCheckIT] Skipping test due to exception: " |
| 142 | + + groupDesc + " — " + reason + " (" + file.getFileName() + ")"); |
| 143 | + |
| 144 | + /// Count exception as skipped mismatch in strict metrics |
| 145 | + METRICS.skippedMismatch.increment(); |
| 146 | + perFile(file).skipMismatch.increment(); |
| 147 | + |
| 148 | + if (isStrict()) throw e; |
| 149 | + Assumptions.assumeTrue(false, "Skipped: " + reason); |
| 150 | + return; /// not reached when strict |
| 151 | + } |
| 152 | + |
| 153 | + if (isStrict()) { |
| 154 | + try { |
| 155 | + assertEquals(expected, actual); |
| 156 | + /// Count pass in strict mode |
| 157 | + METRICS.passed.increment(); |
| 158 | + perFile(file).pass.increment(); |
| 159 | + } catch (AssertionError e) { |
| 160 | + /// Count failure in strict mode |
| 161 | + METRICS.failed.increment(); |
| 162 | + perFile(file).fail.increment(); |
| 163 | + throw e; |
| 164 | + } |
| 165 | + } else if (expected != actual) { |
| 166 | + System.err.println("[JsonSchemaCheckIT] Mismatch (ignored): " |
| 167 | + + groupDesc + " — expected=" + expected + ", actual=" + actual |
| 168 | + + " (" + file.getFileName() + ")"); |
| 169 | + |
| 170 | + /// Count lenient mismatch skip |
| 171 | + METRICS.skippedMismatch.increment(); |
| 172 | + perFile(file).skipMismatch.increment(); |
| 173 | + |
| 174 | + Assumptions.assumeTrue(false, "Mismatch ignored"); |
| 175 | + } else { |
| 176 | + /// Count pass in lenient mode |
| 177 | + METRICS.passed.increment(); |
| 178 | + perFile(file).pass.increment(); |
| 179 | + } |
| 180 | + })); |
| 181 | + } catch (Exception ex) { |
| 182 | + /// Unsupported schema for this group; emit a single skipped test for visibility |
| 183 | + final var reason = ex.getMessage() == null ? ex.getClass().getSimpleName() : ex.getMessage(); |
| 184 | + System.err.println("[JsonSchemaCheckIT] Skipping group due to unsupported schema: " |
| 185 | + + groupDesc + " — " + reason + " (" + file.getFileName() + ")"); |
| 186 | + |
| 187 | + /// Count unsupported group skip |
| 188 | + METRICS.skippedUnsupported.increment(); |
| 189 | + perFile(file).skipUnsupported.increment(); |
| 190 | + |
| 191 | + return Stream.of(DynamicTest.dynamicTest( |
| 192 | + groupDesc + " – SKIPPED: " + reason, |
| 193 | + () -> { if (isStrict()) throw ex; Assumptions.assumeTrue(false, "Unsupported schema: " + reason); } |
| 194 | + )); |
| 195 | + } |
| 196 | + }); |
| 197 | + } |
| 198 | + |
| 199 | + static StrictMetrics.FileCounters perFile(Path file) { |
198 | 200 | return METRICS.perFile.computeIfAbsent(file.getFileName().toString(), k -> new StrictMetrics.FileCounters()); |
199 | 201 | } |
200 | 202 |
|
|
0 commit comments