Skip to content

Commit 2fc07d1

Browse files
committed
wip
1 parent 8e8384d commit 2fc07d1

File tree

4 files changed

+462
-165
lines changed

4 files changed

+462
-165
lines changed

json-java21-schema/src/test/java/io/github/simbo1905/json/schema/JsonSchemaCheckIT.java

Lines changed: 85 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import org.junit.jupiter.api.AfterAll;
99
import org.junit.jupiter.api.Assumptions;
1010

11-
import java.io.File;
1211
import java.io.FileInputStream;
1312
import java.io.IOException;
1413
import java.nio.file.Files;
@@ -17,7 +16,6 @@
1716
import java.util.zip.ZipEntry;
1817
import java.util.zip.ZipInputStream;
1918
import java.util.concurrent.ConcurrentHashMap;
20-
import java.util.concurrent.atomic.LongAdder;
2119
import java.util.stream.Stream;
2220
import java.util.stream.StreamSupport;
2321

@@ -108,93 +106,97 @@ Stream<DynamicTest> testsFromFile(Path file) {
108106
}
109107
METRICS.testsDiscovered.add(testCount);
110108
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-
}
147109

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);
192111
} catch (Exception ex) {
193112
throw new RuntimeException("Failed to process " + file, ex);
194113
}
195114
}
196115

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) {
198200
return METRICS.perFile.computeIfAbsent(file.getFileName().toString(), k -> new StrictMetrics.FileCounters());
199201
}
200202

0 commit comments

Comments
 (0)