-
Notifications
You must be signed in to change notification settings - Fork 0
Jtd test suite inclusion #140
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| package json.java21.jtd.codegen; | ||
|
|
||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import java.nio.file.Paths; | ||
| import java.nio.file.StandardCopyOption; | ||
| import java.util.logging.Logger; | ||
| import java.util.zip.ZipEntry; | ||
| import java.util.zip.ZipInputStream; | ||
|
|
||
| /// Shared utility for extracting the JTD test suite from the embedded ZIP file. | ||
| /// Used by both unit tests and integration tests to avoid committing large JSON files. | ||
| final class JtdTestDataExtractor { | ||
|
|
||
| private static final Logger LOG = Logger.getLogger("json.java21.jtd.codegen"); | ||
| // Reference the ZIP file from the sibling json-java21-jtd module | ||
| private static final Path ZIP_FILE = Paths.get("../json-java21-jtd/src/test/resources/jtd-test-suite.zip"); | ||
| private static final Path TARGET_DIR = Paths.get("target/test-data"); | ||
|
Comment on lines
+18
to
+20
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The codegen test suite now hard-codes the ZIP path to Useful? React with 👍 / 👎. |
||
| private static final Path VALIDATION_FILE = TARGET_DIR.resolve("json-typedef-spec-2025-09-27/tests/validation.json"); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The path to |
||
|
|
||
| private JtdTestDataExtractor() { | ||
| // Utility class | ||
| } | ||
|
|
||
| /// Ensures the test suite is extracted and returns the path to validation.json. | ||
| /// Extraction happens at most once per build (target/ is cleaned between builds). | ||
| static synchronized Path ensureValidationTestData() throws IOException { | ||
| if (Files.exists(VALIDATION_FILE)) { | ||
| LOG.fine(() -> "JTD test suite already extracted at: " + VALIDATION_FILE); | ||
| return VALIDATION_FILE; | ||
| } | ||
|
|
||
| if (!Files.exists(ZIP_FILE)) { | ||
| throw new RuntimeException("JTD test suite ZIP not found: " + ZIP_FILE.toAbsolutePath()); | ||
| } | ||
|
|
||
| LOG.info(() -> "Extracting JTD test suite from: " + ZIP_FILE); | ||
| Files.createDirectories(TARGET_DIR); | ||
|
|
||
| try (ZipInputStream zis = new ZipInputStream(Files.newInputStream(ZIP_FILE))) { | ||
| ZipEntry entry; | ||
| while ((entry = zis.getNextEntry()) != null) { | ||
| if (!entry.isDirectory() && entry.getName().startsWith("json-typedef-spec-")) { | ||
| Path outputPath = TARGET_DIR.resolve(entry.getName()); | ||
| Files.createDirectories(outputPath.getParent()); | ||
| Files.copy(zis, outputPath, StandardCopyOption.REPLACE_EXISTING); | ||
| } | ||
| zis.closeEntry(); | ||
| } | ||
| } | ||
|
|
||
| if (!Files.exists(VALIDATION_FILE)) { | ||
| throw new RuntimeException("Extraction completed but validation.json not found: " + VALIDATION_FILE); | ||
| } | ||
|
|
||
| LOG.info(() -> "JTD test suite extracted successfully"); | ||
| return VALIDATION_FILE; | ||
| } | ||
|
|
||
| /// Returns an InputStream for the validation test data, extracting if necessary. | ||
| /// Suitable for use with classpath-style resource loading patterns. | ||
| static InputStream getValidationTestDataStream() throws IOException { | ||
| Path dataFile = ensureValidationTestData(); | ||
| return Files.newInputStream(dataFile); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is significant code duplication. This class is nearly identical to
json-java21-jtd/src/test/java/json/java21/jtd/JtdTestDataExtractor.java. Duplicating utility code across modules makes maintenance harder.Consider creating a shared test utility module or using Maven's
test-jarfeature to share this code from one module to the other. This would ensure that any future changes to the extraction logic only need to be made in one place.This would also solve the problem of the fragile relative path used to locate the ZIP file (line 19), as the test resource could be loaded from the classpath via
getResourceAsStream.