From 5b2ce766838656f4f8a37f1ba90c9548dc7f2bc3 Mon Sep 17 00:00:00 2001 From: Dominic Burger Date: Wed, 10 Apr 2024 09:48:26 +0200 Subject: [PATCH 1/3] Move validator to validation package --- .../java/ch/geowerkstatt/interlis/testbed/runner/Main.java | 1 + .../java/ch/geowerkstatt/interlis/testbed/runner/Runner.java | 2 ++ .../testbed/runner/{ => validation}/InterlisValidator.java | 3 ++- .../interlis/testbed/runner/{ => validation}/Validator.java | 2 +- .../testbed/runner/{ => validation}/ValidatorException.java | 2 +- .../interlis/testbed/runner/RunnerBaseDataTest.java | 2 ++ .../interlis/testbed/runner/RunnerWithConstraintsTest.java | 2 ++ .../ch/geowerkstatt/interlis/testbed/runner/ValidatorTest.java | 2 ++ 8 files changed, 13 insertions(+), 3 deletions(-) rename src/main/java/ch/geowerkstatt/interlis/testbed/runner/{ => validation}/InterlisValidator.java (95%) rename src/main/java/ch/geowerkstatt/interlis/testbed/runner/{ => validation}/Validator.java (93%) rename src/main/java/ch/geowerkstatt/interlis/testbed/runner/{ => validation}/ValidatorException.java (93%) diff --git a/src/main/java/ch/geowerkstatt/interlis/testbed/runner/Main.java b/src/main/java/ch/geowerkstatt/interlis/testbed/runner/Main.java index 2271481..c5d71f4 100644 --- a/src/main/java/ch/geowerkstatt/interlis/testbed/runner/Main.java +++ b/src/main/java/ch/geowerkstatt/interlis/testbed/runner/Main.java @@ -1,5 +1,6 @@ package ch.geowerkstatt.interlis.testbed.runner; +import ch.geowerkstatt.interlis.testbed.runner.validation.InterlisValidator; import ch.geowerkstatt.interlis.testbed.runner.xtf.XtfFileMerger; import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.DefaultParser; diff --git a/src/main/java/ch/geowerkstatt/interlis/testbed/runner/Runner.java b/src/main/java/ch/geowerkstatt/interlis/testbed/runner/Runner.java index c509273..473b9b7 100644 --- a/src/main/java/ch/geowerkstatt/interlis/testbed/runner/Runner.java +++ b/src/main/java/ch/geowerkstatt/interlis/testbed/runner/Runner.java @@ -1,5 +1,7 @@ package ch.geowerkstatt.interlis.testbed.runner; +import ch.geowerkstatt.interlis.testbed.runner.validation.Validator; +import ch.geowerkstatt.interlis.testbed.runner.validation.ValidatorException; import ch.geowerkstatt.interlis.testbed.runner.xtf.XtfMerger; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; diff --git a/src/main/java/ch/geowerkstatt/interlis/testbed/runner/InterlisValidator.java b/src/main/java/ch/geowerkstatt/interlis/testbed/runner/validation/InterlisValidator.java similarity index 95% rename from src/main/java/ch/geowerkstatt/interlis/testbed/runner/InterlisValidator.java rename to src/main/java/ch/geowerkstatt/interlis/testbed/runner/validation/InterlisValidator.java index 0a6e082..4ba07ef 100644 --- a/src/main/java/ch/geowerkstatt/interlis/testbed/runner/InterlisValidator.java +++ b/src/main/java/ch/geowerkstatt/interlis/testbed/runner/validation/InterlisValidator.java @@ -1,5 +1,6 @@ -package ch.geowerkstatt.interlis.testbed.runner; +package ch.geowerkstatt.interlis.testbed.runner.validation; +import ch.geowerkstatt.interlis.testbed.runner.TestOptions; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; diff --git a/src/main/java/ch/geowerkstatt/interlis/testbed/runner/Validator.java b/src/main/java/ch/geowerkstatt/interlis/testbed/runner/validation/Validator.java similarity index 93% rename from src/main/java/ch/geowerkstatt/interlis/testbed/runner/Validator.java rename to src/main/java/ch/geowerkstatt/interlis/testbed/runner/validation/Validator.java index 71e33f2..d14507e 100644 --- a/src/main/java/ch/geowerkstatt/interlis/testbed/runner/Validator.java +++ b/src/main/java/ch/geowerkstatt/interlis/testbed/runner/validation/Validator.java @@ -1,4 +1,4 @@ -package ch.geowerkstatt.interlis.testbed.runner; +package ch.geowerkstatt.interlis.testbed.runner.validation; import java.nio.file.Path; diff --git a/src/main/java/ch/geowerkstatt/interlis/testbed/runner/ValidatorException.java b/src/main/java/ch/geowerkstatt/interlis/testbed/runner/validation/ValidatorException.java similarity index 93% rename from src/main/java/ch/geowerkstatt/interlis/testbed/runner/ValidatorException.java rename to src/main/java/ch/geowerkstatt/interlis/testbed/runner/validation/ValidatorException.java index 9cddb72..d9098f0 100644 --- a/src/main/java/ch/geowerkstatt/interlis/testbed/runner/ValidatorException.java +++ b/src/main/java/ch/geowerkstatt/interlis/testbed/runner/validation/ValidatorException.java @@ -1,4 +1,4 @@ -package ch.geowerkstatt.interlis.testbed.runner; +package ch.geowerkstatt.interlis.testbed.runner.validation; /** * Exception that is thrown when the validation failed to run. diff --git a/src/test/java/ch/geowerkstatt/interlis/testbed/runner/RunnerBaseDataTest.java b/src/test/java/ch/geowerkstatt/interlis/testbed/runner/RunnerBaseDataTest.java index dcdcf1c..2370c47 100644 --- a/src/test/java/ch/geowerkstatt/interlis/testbed/runner/RunnerBaseDataTest.java +++ b/src/test/java/ch/geowerkstatt/interlis/testbed/runner/RunnerBaseDataTest.java @@ -1,5 +1,7 @@ package ch.geowerkstatt.interlis.testbed.runner; +import ch.geowerkstatt.interlis.testbed.runner.validation.Validator; +import ch.geowerkstatt.interlis.testbed.runner.validation.ValidatorException; import ch.geowerkstatt.interlis.testbed.runner.xtf.XtfMerger; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; diff --git a/src/test/java/ch/geowerkstatt/interlis/testbed/runner/RunnerWithConstraintsTest.java b/src/test/java/ch/geowerkstatt/interlis/testbed/runner/RunnerWithConstraintsTest.java index 1d695c9..6819fd2 100644 --- a/src/test/java/ch/geowerkstatt/interlis/testbed/runner/RunnerWithConstraintsTest.java +++ b/src/test/java/ch/geowerkstatt/interlis/testbed/runner/RunnerWithConstraintsTest.java @@ -1,5 +1,7 @@ package ch.geowerkstatt.interlis.testbed.runner; +import ch.geowerkstatt.interlis.testbed.runner.validation.Validator; +import ch.geowerkstatt.interlis.testbed.runner.validation.ValidatorException; import ch.geowerkstatt.interlis.testbed.runner.xtf.XtfMerger; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; diff --git a/src/test/java/ch/geowerkstatt/interlis/testbed/runner/ValidatorTest.java b/src/test/java/ch/geowerkstatt/interlis/testbed/runner/ValidatorTest.java index 4643d15..8ffb4be 100644 --- a/src/test/java/ch/geowerkstatt/interlis/testbed/runner/ValidatorTest.java +++ b/src/test/java/ch/geowerkstatt/interlis/testbed/runner/ValidatorTest.java @@ -1,5 +1,7 @@ package ch.geowerkstatt.interlis.testbed.runner; +import ch.geowerkstatt.interlis.testbed.runner.validation.InterlisValidator; +import ch.geowerkstatt.interlis.testbed.runner.validation.ValidatorException; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; From 08c86d578aaf8a16eb55a9d6437eaeed72db698e Mon Sep 17 00:00:00 2001 From: Dominic Burger Date: Wed, 10 Apr 2024 11:25:09 +0200 Subject: [PATCH 2/3] Pass log arguments as parameters --- .../interlis/testbed/runner/Runner.java | 14 +++++++------- .../runner/validation/InterlisValidator.java | 6 +++--- .../testbed/runner/xtf/XtfFileMerger.java | 16 ++++++++-------- 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/src/main/java/ch/geowerkstatt/interlis/testbed/runner/Runner.java b/src/main/java/ch/geowerkstatt/interlis/testbed/runner/Runner.java index 473b9b7..e565fd0 100644 --- a/src/main/java/ch/geowerkstatt/interlis/testbed/runner/Runner.java +++ b/src/main/java/ch/geowerkstatt/interlis/testbed/runner/Runner.java @@ -37,7 +37,7 @@ public Runner(TestOptions options, Validator validator, XtfMerger xtfMerger) { * @return {@code true} if the validation was successful, {@code false} otherwise. */ public boolean run() { - LOGGER.info("Starting validation of testbed at " + options.basePath()); + LOGGER.info("Starting validation of testbed at {}", options.basePath()); try { if (!validateBaseData()) { @@ -68,15 +68,15 @@ private boolean validateBaseData() throws ValidatorException { throw new ValidatorException(e); } - LOGGER.info("Validating base data file " + baseFilePath); + LOGGER.info("Validating base data file {}", baseFilePath); var filenameWithoutExtension = StringUtils.getFilenameWithoutExtension(baseFilePath.getFileName().toString()); var logFile = options.resolveOutputFilePath(baseFilePath, filenameWithoutExtension + ".log"); var valid = validator.validate(baseFilePath, logFile); if (valid) { - LOGGER.info("Validation of " + baseFilePath + " completed successfully."); + LOGGER.info("Validation of {} completed successfully.", baseFilePath); } else { - LOGGER.error("Validation of " + baseFilePath + " failed. See " + logFile + " for details."); + LOGGER.error("Validation of {} failed. See {} for details.", baseFilePath, logFile); } return valid; } @@ -115,16 +115,16 @@ private boolean mergeAndValidateTransferFiles() throws ValidatorException { private boolean validateMergedFile(Path mergedFile, Path logFile, String constraintName) throws ValidatorException { if (validator.validate(mergedFile, logFile)) { - LOGGER.error("Validation of " + mergedFile + " was expected to fail but completed successfully."); + LOGGER.error("Validation of {} was expected to fail but completed successfully.", mergedFile); return false; } if (!validator.containsConstraintError(logFile, constraintName)) { - LOGGER.error("Could not verify constraint " + constraintName + " for merged file " + mergedFile + ". Check the log file at " + logFile + " for details."); + LOGGER.error("Could not verify constraint {} for merged file {}. Check the log file at {} for details.", constraintName, mergedFile, logFile); return false; } - LOGGER.info("Validation of " + mergedFile + " failed as expected."); + LOGGER.info("Validation of {} failed as expected.", mergedFile); return true; } } diff --git a/src/main/java/ch/geowerkstatt/interlis/testbed/runner/validation/InterlisValidator.java b/src/main/java/ch/geowerkstatt/interlis/testbed/runner/validation/InterlisValidator.java index 4ba07ef..fe21368 100644 --- a/src/main/java/ch/geowerkstatt/interlis/testbed/runner/validation/InterlisValidator.java +++ b/src/main/java/ch/geowerkstatt/interlis/testbed/runner/validation/InterlisValidator.java @@ -22,12 +22,12 @@ public final class InterlisValidator implements Validator { public InterlisValidator(TestOptions options) { this.options = options; - LOGGER.info("Using ilivalidator at " + options.ilivalidatorPath()); + LOGGER.info("Using ilivalidator at {}", options.ilivalidatorPath()); } @Override public boolean validate(Path filePath, Path logFile) throws ValidatorException { - LOGGER.info("Validating " + filePath + " with log file " + logFile); + LOGGER.info("Validating {} with log file {}", filePath, logFile); try { Files.createDirectories(logFile.getParent()); @@ -56,7 +56,7 @@ public boolean containsConstraintError(Path logFile, String constraintName) thro try (var lines = Files.lines(logFile)) { return lines.anyMatch(line -> { if (constraintPattern.matcher(line).find()) { - LOGGER.info("Found expected error for constraint " + constraintName + " in log file: " + line); + LOGGER.info("Found expected error for constraint {} in log file: {}", constraintName, line); return true; } return false; diff --git a/src/main/java/ch/geowerkstatt/interlis/testbed/runner/xtf/XtfFileMerger.java b/src/main/java/ch/geowerkstatt/interlis/testbed/runner/xtf/XtfFileMerger.java index 95018a8..66fabf6 100644 --- a/src/main/java/ch/geowerkstatt/interlis/testbed/runner/xtf/XtfFileMerger.java +++ b/src/main/java/ch/geowerkstatt/interlis/testbed/runner/xtf/XtfFileMerger.java @@ -43,7 +43,7 @@ public XtfFileMerger() { @Override public boolean merge(Path baseFile, Path patchFile, Path outputFile) { try { - LOGGER.info("Merging " + baseFile + " with " + patchFile + " into " + outputFile); + LOGGER.info("Merging {} with {} into {}", baseFile, patchFile, outputFile); var documentBuilder = createDocumentBuilder(); var baseDocument = documentBuilder.parse(baseFile.toFile()); @@ -51,13 +51,13 @@ public boolean merge(Path baseFile, Path patchFile, Path outputFile) { var baseBaskets = findBaskets(baseDocument); if (baseBaskets.isEmpty()) { - LOGGER.error("No baskets found in base file " + baseFile + "."); + LOGGER.error("No baskets found in base file {}.", baseFile); return false; } var patchBaskets = findBaskets(patchDocument); if (patchBaskets.isEmpty()) { - LOGGER.error("No baskets found in patch file " + patchFile + "."); + LOGGER.error("No baskets found in patch file {}.", patchFile); return false; } @@ -66,7 +66,7 @@ public boolean merge(Path baseFile, Path patchFile, Path outputFile) { } writeMergedFile(baseDocument, outputFile); - LOGGER.info("Successfully merged files into " + outputFile); + LOGGER.info("Successfully merged files into {}", outputFile); return true; } catch (Exception e) { LOGGER.error("Failed to merge files.", e); @@ -87,7 +87,7 @@ private static boolean mergeBaskets(Document document, Map baseB var originalBasket = baseBaskets.get(basketId); if (originalBasket == null) { - LOGGER.error("Basket " + basketId + " not found in base file."); + LOGGER.error("Basket {} not found in base file.", basketId); isValid = false; continue; } @@ -103,7 +103,7 @@ private static boolean mergeBaskets(Document document, Map baseB if (hasDeleteAttribute(element)) { if (!originalBasket.removeChildNode(entryId)) { - LOGGER.error("Could not remove entry " + entryId + " from basket " + basketId + " as it does not exist."); + LOGGER.error("Could not remove entry {} from basket {} as it does not exist.", entryId, basketId); isValid = false; } } else { @@ -136,7 +136,7 @@ static Optional> findBaskets(Document document) { .filter(e -> { var hasId = hasInterlisAttribute(e, BASKET_ID); if (!hasId) { - LOGGER.warn("Basket without " + BASKET_ID + " found."); + LOGGER.warn("Basket without {} found.", BASKET_ID); } return hasId; }) @@ -149,7 +149,7 @@ private static Basket collectBasket(Element basket) { .filter(e -> { var hasId = hasInterlisAttribute(e, OBJECT_ID); if (!hasId) { - LOGGER.warn("Entry without " + OBJECT_ID + " found in basket " + basket.getAttribute(BASKET_ID) + "."); + LOGGER.warn("Entry without {} found in basket {}.", OBJECT_ID, basket.getAttribute(BASKET_ID)); } return hasId; }) From 8b9fcab74e63f1eb2413c6aadb459955acab1d12 Mon Sep 17 00:00:00 2001 From: Dominic Burger Date: Wed, 10 Apr 2024 11:46:35 +0200 Subject: [PATCH 3/3] Cleanup Main and XtfFileMerger --- .../ch/geowerkstatt/interlis/testbed/runner/Main.java | 4 ++-- .../interlis/testbed/runner/xtf/XtfFileMerger.java | 8 +------- 2 files changed, 3 insertions(+), 9 deletions(-) diff --git a/src/main/java/ch/geowerkstatt/interlis/testbed/runner/Main.java b/src/main/java/ch/geowerkstatt/interlis/testbed/runner/Main.java index c5d71f4..492b913 100644 --- a/src/main/java/ch/geowerkstatt/interlis/testbed/runner/Main.java +++ b/src/main/java/ch/geowerkstatt/interlis/testbed/runner/Main.java @@ -56,9 +56,9 @@ private static void printUsage(Options options) { formatter.printHelp("java -jar interlis-testbed-runner.jar [options] [testbed directory (default: current directory)]", options); } - private static TestOptions getTestOptions(CommandLine commandLine) throws ParseException { + private static TestOptions getTestOptions(CommandLine commandLine) { var remainingArgs = commandLine.getArgList(); - var basePath = remainingArgs.isEmpty() ? Path.of(".") : Path.of(remainingArgs.get(0)); + var basePath = remainingArgs.isEmpty() ? Path.of(".") : Path.of(remainingArgs.getFirst()); var validatorPath = Path.of(commandLine.getOptionValue(VALIDATOR_PATH_OPTION)); return new TestOptions(basePath, validatorPath); } diff --git a/src/main/java/ch/geowerkstatt/interlis/testbed/runner/xtf/XtfFileMerger.java b/src/main/java/ch/geowerkstatt/interlis/testbed/runner/xtf/XtfFileMerger.java index 66fabf6..b2ed357 100644 --- a/src/main/java/ch/geowerkstatt/interlis/testbed/runner/xtf/XtfFileMerger.java +++ b/src/main/java/ch/geowerkstatt/interlis/testbed/runner/xtf/XtfFileMerger.java @@ -6,9 +6,7 @@ import org.w3c.dom.Element; import org.w3c.dom.Node; -import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; -import javax.xml.parsers.ParserConfigurationException; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; @@ -44,7 +42,7 @@ public XtfFileMerger() { public boolean merge(Path baseFile, Path patchFile, Path outputFile) { try { LOGGER.info("Merging {} with {} into {}", baseFile, patchFile, outputFile); - var documentBuilder = createDocumentBuilder(); + var documentBuilder = factory.newDocumentBuilder(); var baseDocument = documentBuilder.parse(baseFile.toFile()); var patchDocument = documentBuilder.parse(patchFile.toFile()); @@ -74,10 +72,6 @@ public boolean merge(Path baseFile, Path patchFile, Path outputFile) { } } - DocumentBuilder createDocumentBuilder() throws ParserConfigurationException { - return factory.newDocumentBuilder(); - } - private static boolean mergeBaskets(Document document, Map baseBaskets, Map patchBaskets) { var isValid = true;