diff --git a/README.md b/README.md index 09f53e6..36a7165 100644 --- a/README.md +++ b/README.md @@ -91,3 +91,18 @@ void example() { xtfMerger.merge(Path.of("path", "to", "base.xtf"), Path.of("path", "to", "failcase.xtf"), Path.of("path", "to", "output.xtf")); } ``` + +### Prüfen von Constraint-Fehlern +Um zu prüfen, ob mindestens ein Fehler zu einem spezifischen Constraint im Log vorkommt, kann die Klasse `IliValidatorLogParser` verwendet werden. +Die statische Methode `containsConstraintError` erwartet dazu den Pfad zur Log-Datei sowie den voll-qualifizierten Namen des Constraints. + +Beispiel: +```java +import ch.geowerkstatt.interlis.testbed.runner.validation.IliValidatorLogParser; + +//... + +void example() { + boolean hasError = IliValidatorLogParser.containsConstraintError(Path.of("path", "to", "validator-log.log"), "Model.Topic.Class.Constraint"); +} +``` diff --git a/src/main/java/ch/geowerkstatt/interlis/testbed/runner/validation/IliValidatorLogParser.java b/src/main/java/ch/geowerkstatt/interlis/testbed/runner/validation/IliValidatorLogParser.java new file mode 100644 index 0000000..39f3303 --- /dev/null +++ b/src/main/java/ch/geowerkstatt/interlis/testbed/runner/validation/IliValidatorLogParser.java @@ -0,0 +1,43 @@ +package ch.geowerkstatt.interlis.testbed.runner.validation; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.regex.Pattern; + +/** + * Provides static methods that can be used to get information from ilivalidator log files. + */ +public final class IliValidatorLogParser { + private static final Logger LOGGER = LogManager.getLogger(); + + private IliValidatorLogParser() { + } + + /** + * Checks if the log file contains at least one error for the provided constraint. + * + * @param logFile the path to the log file. + * @param constraintName the fully qualified name of the constraint to check. + * @return {@code true} if the log file contains an error for the constraint, {@code false} otherwise. + * @throws ValidatorException if an unexpected error such as an {@link IOException} occurred. + */ + public static boolean containsConstraintError(Path logFile, String constraintName) throws ValidatorException { + var constraintPattern = Pattern.compile("^Error: .*\\b" + Pattern.quote(constraintName) + "\\b"); + + try (var lines = Files.lines(logFile)) { + return lines.anyMatch(line -> { + if (constraintPattern.matcher(line).find()) { + LOGGER.info("Found expected error for constraint {} in log file: {}", constraintName, line); + return true; + } + return false; + }); + } catch (IOException e) { + throw new ValidatorException(e); + } + } +} 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 fe21368..f8e4985 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 @@ -7,7 +7,6 @@ import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; -import java.util.regex.Pattern; public final class InterlisValidator implements Validator { private static final Logger LOGGER = LogManager.getLogger(); @@ -48,21 +47,4 @@ public boolean validate(Path filePath, Path logFile) throws ValidatorException { throw new ValidatorException(e); } } - - @Override - public boolean containsConstraintError(Path logFile, String constraintName) throws ValidatorException { - var constraintPattern = Pattern.compile("^Error: .*\\b" + Pattern.quote(constraintName) + "\\b"); - - try (var lines = Files.lines(logFile)) { - return lines.anyMatch(line -> { - if (constraintPattern.matcher(line).find()) { - LOGGER.info("Found expected error for constraint {} in log file: {}", constraintName, line); - return true; - } - return false; - }); - } catch (IOException e) { - throw new ValidatorException(e); - } - } } diff --git a/src/main/java/ch/geowerkstatt/interlis/testbed/runner/validation/Validator.java b/src/main/java/ch/geowerkstatt/interlis/testbed/runner/validation/Validator.java index d14507e..3a158e8 100644 --- a/src/main/java/ch/geowerkstatt/interlis/testbed/runner/validation/Validator.java +++ b/src/main/java/ch/geowerkstatt/interlis/testbed/runner/validation/Validator.java @@ -14,11 +14,14 @@ public interface Validator { boolean validate(Path filePath, Path logFile) throws ValidatorException; /** - * Checks if the log file contains an error for the provided constraint. + * Checks if the log file contains at least one error for the provided constraint. * * @param logFile the path to the log file. - * @param constraintName the name of the constraint to check. + * @param constraintName the fully qualified name of the constraint to check. * @return {@code true} if the log file contains an error for the constraint, {@code false} otherwise. + * @throws ValidatorException if an unexpected error occurred. */ - boolean containsConstraintError(Path logFile, String constraintName) throws ValidatorException; + default boolean containsConstraintError(Path logFile, String constraintName) throws ValidatorException { + return IliValidatorLogParser.containsConstraintError(logFile, constraintName); + } }