Skip to content
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

Java API for constraint error check #13

Merged
merged 2 commits into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
Loading