Skip to content

Commit

Permalink
Java API for constraint error check (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
domi-b authored Apr 15, 2024
2 parents 04ed8cf + fccce9d commit 55706e6
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 21 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
```
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit 55706e6

Please sign in to comment.