Skip to content

Commit 407129c

Browse files
authored
Extract JTD test suite from ZIP instead of committing large JSON files (#140)
Previously, two copies of the 78KB jtd-spec-validation.json file were committed to the repository (156KB total), bloating the PR and git history. Changes: - Created JtdTestDataExtractor utility class to extract test data from existing jtd-test-suite.zip at test runtime - Updated JtdSpecConformanceTest and CodegenSpecConformanceTest to use extraction instead of classpath resources - Updated JtdSpecIT and CompilerSpecIT to use shared extractor - Deleted committed JSON files from both modules - Codegen module references parent module's ZIP file Testing: - Run: ./mvnw -pl json-java21-jtd test - All 452 tests pass (136 unit + 316 spec conformance) - Test data is automatically extracted from ZIP on first run - Reduces PR size by ~156KB (9,390 lines)
1 parent c1805a9 commit 407129c

File tree

8 files changed

+161
-9482
lines changed

8 files changed

+161
-9482
lines changed

json-java21-jtd-codegen/src/test/java/json/java21/jtd/codegen/CodegenSpecConformanceTest.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,18 @@
2525
class CodegenSpecConformanceTest extends CodegenTestBase {
2626

2727
static Stream<Arguments> cases() throws IOException {
28-
final var raw = CodegenSpecConformanceTest.class.getClassLoader()
29-
.getResourceAsStream("jtd-spec-validation.json");
30-
assert raw != null : "jtd-spec-validation.json not found on classpath";
31-
final var jsonText = new String(raw.readAllBytes(), StandardCharsets.UTF_8);
32-
final var root = Json.parse(jsonText);
33-
assert root instanceof JsonObject : "expected top-level object";
34-
final var obj = (JsonObject) root;
28+
// Extract test suite from ZIP (same data as IT tests, avoids committing large JSON)
29+
try (final var raw = JtdTestDataExtractor.getValidationTestDataStream()) {
30+
final var jsonText = new String(raw.readAllBytes(), StandardCharsets.UTF_8);
31+
final var root = Json.parse(jsonText);
32+
assert root instanceof JsonObject : "expected top-level object";
33+
final var obj = (JsonObject) root;
3534

36-
return obj.members().entrySet().stream()
37-
.map(entry -> Arguments.of(
38-
entry.getKey(),
39-
entry.getValue()));
35+
return obj.members().entrySet().stream()
36+
.map(entry -> Arguments.of(
37+
entry.getKey(),
38+
entry.getValue()));
39+
}
4040
}
4141

4242
@ParameterizedTest(name = "{0}")
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package json.java21.jtd.codegen;
2+
3+
import java.io.IOException;
4+
import java.io.InputStream;
5+
import java.nio.file.Files;
6+
import java.nio.file.Path;
7+
import java.nio.file.Paths;
8+
import java.nio.file.StandardCopyOption;
9+
import java.util.logging.Logger;
10+
import java.util.zip.ZipEntry;
11+
import java.util.zip.ZipInputStream;
12+
13+
/// Shared utility for extracting the JTD test suite from the embedded ZIP file.
14+
/// Used by both unit tests and integration tests to avoid committing large JSON files.
15+
final class JtdTestDataExtractor {
16+
17+
private static final Logger LOG = Logger.getLogger("json.java21.jtd.codegen");
18+
// Reference the ZIP file from the sibling json-java21-jtd module
19+
private static final Path ZIP_FILE = Paths.get("../json-java21-jtd/src/test/resources/jtd-test-suite.zip");
20+
private static final Path TARGET_DIR = Paths.get("target/test-data");
21+
private static final Path VALIDATION_FILE = TARGET_DIR.resolve("json-typedef-spec-2025-09-27/tests/validation.json");
22+
23+
private JtdTestDataExtractor() {
24+
// Utility class
25+
}
26+
27+
/// Ensures the test suite is extracted and returns the path to validation.json.
28+
/// Extraction happens at most once per build (target/ is cleaned between builds).
29+
static synchronized Path ensureValidationTestData() throws IOException {
30+
if (Files.exists(VALIDATION_FILE)) {
31+
LOG.fine(() -> "JTD test suite already extracted at: " + VALIDATION_FILE);
32+
return VALIDATION_FILE;
33+
}
34+
35+
if (!Files.exists(ZIP_FILE)) {
36+
throw new RuntimeException("JTD test suite ZIP not found: " + ZIP_FILE.toAbsolutePath());
37+
}
38+
39+
LOG.info(() -> "Extracting JTD test suite from: " + ZIP_FILE);
40+
Files.createDirectories(TARGET_DIR);
41+
42+
try (ZipInputStream zis = new ZipInputStream(Files.newInputStream(ZIP_FILE))) {
43+
ZipEntry entry;
44+
while ((entry = zis.getNextEntry()) != null) {
45+
if (!entry.isDirectory() && entry.getName().startsWith("json-typedef-spec-")) {
46+
Path outputPath = TARGET_DIR.resolve(entry.getName());
47+
Files.createDirectories(outputPath.getParent());
48+
Files.copy(zis, outputPath, StandardCopyOption.REPLACE_EXISTING);
49+
}
50+
zis.closeEntry();
51+
}
52+
}
53+
54+
if (!Files.exists(VALIDATION_FILE)) {
55+
throw new RuntimeException("Extraction completed but validation.json not found: " + VALIDATION_FILE);
56+
}
57+
58+
LOG.info(() -> "JTD test suite extracted successfully");
59+
return VALIDATION_FILE;
60+
}
61+
62+
/// Returns an InputStream for the validation test data, extracting if necessary.
63+
/// Suitable for use with classpath-style resource loading patterns.
64+
static InputStream getValidationTestDataStream() throws IOException {
65+
Path dataFile = ensureValidationTestData();
66+
return Files.newInputStream(dataFile);
67+
}
68+
}

0 commit comments

Comments
 (0)