This commit is contained in:
jstaerk
2025-04-18 16:09:26 +02:00
parent 31c5d0461e
commit f1adde1aba
3 changed files with 73 additions and 61 deletions

View File

@@ -13,6 +13,7 @@
- #802 - #802
- #809 - #809
- #812 - #812
- #818
2.16.3 2.16.3

View File

@@ -347,8 +347,11 @@ public class Main {
Option attachmentOpt = new Option("attachments", "attachments", true, "File attachments"); Option attachmentOpt = new Option("attachments", "attachments", true, "File attachments");
attachmentOpt.setValueSeparator(','); attachmentOpt.setValueSeparator(',');
attachmentOpt.setArgs(Option.UNLIMITED_VALUES); attachmentOpt.setArgs(Option.UNLIMITED_VALUES);
options.addOption(attachmentOpt); options.addOption(attachmentOpt);
Option excludeOpt = new Option("exclude", "exclude", true, "Files to exclude from recursive directory traversal");
excludeOpt.setValueSeparator(',');
excludeOpt.setArgs(Option.UNLIMITED_VALUES);
options.addOption(excludeOpt);
options.addOption(new Option("source", "source", true, "which source file to use")); options.addOption(new Option("source", "source", true, "which source file to use"));
options.addOption(new Option("source-xml", "source-xml", true, "which source file to use")); options.addOption(new Option("source-xml", "source-xml", true, "which source file to use"));
options.addOption(new Option("language", "language", true, "output language (en, de or fr)")); options.addOption(new Option("language", "language", true, "output language (en, de or fr)"));
@@ -389,6 +392,7 @@ public class Main {
String zugferdProfile = cmd.getOptionValue("profile"); String zugferdProfile = cmd.getOptionValue("profile");
String[] attachmentFilenames = cmd.hasOption("attachments") ? cmd.getOptionValues("attachments") : null; String[] attachmentFilenames = cmd.hasOption("attachments") ? cmd.getOptionValues("attachments") : null;
String[] excludedFilenames = cmd.hasOption("exclude") ? cmd.getOptionValues("exclude") : null;
ArrayList<FileAttachment> attachments = new ArrayList<>(); ArrayList<FileAttachment> attachments = new ArrayList<>();
@@ -433,9 +437,9 @@ public class Main {
} else if ((action != null) && (action.equals("validate"))) { } else if ((action != null) && (action.equals("validate"))) {
optionsRecognized = performValidate(sourceName, noNotices, cmd.getOptionValue("logAppend"), LogAsPDF); optionsRecognized = performValidate(sourceName, noNotices, cmd.getOptionValue("logAppend"), LogAsPDF);
} else if ((action != null) && (action.equals("validateExpectValid"))) { } else if ((action != null) && (action.equals("validateExpectValid"))) {
optionsRecognized = performValidateExpect(true, directoryName); optionsRecognized = performValidateExpect(true, directoryName, excludedFilenames);
} else if ((action != null) && (action.equals("validateExpectInvalid"))) { } else if ((action != null) && (action.equals("validateExpectInvalid"))) {
optionsRecognized = performValidateExpect(false, directoryName); optionsRecognized = performValidateExpect(false, directoryName, excludedFilenames);
} }
} catch (UnrecognizedOptionException ex) { } catch (UnrecognizedOptionException ex) {
@@ -487,8 +491,8 @@ public class Main {
return optionsRecognized; return optionsRecognized;
} }
private static boolean performValidateExpect(boolean valid, String dirName) { private static boolean performValidateExpect(boolean valid, String dirName, String[] excludedFiles) {
ValidatorFileWalker zfWalk = new ValidatorFileWalker(valid); ValidatorFileWalker zfWalk = new ValidatorFileWalker(valid, excludedFiles);
Path startingDir = Paths.get(dirName); Path startingDir = Paths.get(dirName);
try { try {
Files.walkFileTree(startingDir, zfWalk); Files.walkFileTree(startingDir, zfWalk);

View File

@@ -1,7 +1,6 @@
package org.mustangproject.commandline; package org.mustangproject.commandline;
import java.io.IOException; import java.io.IOException;
import java.nio.file.FileSystems; import java.nio.file.FileSystems;
import java.nio.file.FileVisitResult; import java.nio.file.FileVisitResult;
@@ -11,25 +10,29 @@ import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes; import java.nio.file.attribute.BasicFileAttributes;
import java.text.DateFormat; import java.text.DateFormat;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date; import java.util.Date;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.mustangproject.validator.ZUGFeRDValidator; import org.mustangproject.validator.ZUGFeRDValidator;
import static org.xmlunit.assertj.XmlAssert.assertThat; import static org.xmlunit.assertj.XmlAssert.assertThat;
public class ValidatorFileWalker public class ValidatorFileWalker
extends SimpleFileVisitor<Path> { extends SimpleFileVisitor<Path> {
private static final Logger LOGGER = LoggerFactory.getLogger(ValidatorFileWalker.class.getCanonicalName()); // log private static final Logger LOGGER = LoggerFactory.getLogger(ValidatorFileWalker.class.getCanonicalName()); // log
protected PathMatcher matcher; protected PathMatcher matcher;
protected ZUGFeRDValidator zul; protected ZUGFeRDValidator zul;
protected int fileCount=1; protected int fileCount = 1;
protected boolean expectValid=true; protected boolean expectValid = true;
protected boolean allValid=true; protected boolean allValid = true;
protected String[] excludedFiles = {};
public ValidatorFileWalker(boolean expectValid) { public ValidatorFileWalker(boolean expectValid, String[] excludedFiles) {
this.zul = new ZUGFeRDValidator(); this.zul = new ZUGFeRDValidator();
this.expectValid=expectValid; this.expectValid = expectValid;
this.excludedFiles = excludedFiles;
matcher = FileSystems.getDefault().getPathMatcher("glob:*.{pdf,xml}"); matcher = FileSystems.getDefault().getPathMatcher("glob:*.{pdf,xml}");
} }
@@ -37,54 +40,58 @@ public class ValidatorFileWalker
public boolean getResult() { public boolean getResult() {
return allValid; return allValid;
} }
// Print information about
// each type of file.
@Override
public FileVisitResult visitFile(Path file,
BasicFileAttributes attr) {
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//get current date time with Date()
Date date = new Date();
String expectedString="valid";
if (!expectValid) {
expectedString="invalid";
}
if ((attr!=null)&&(attr.isRegularFile())) {
if (matcher.matches(file.getFileName())) {
String thisResultString=" valid";
try {
assertThat(zul.validate(file.toAbsolutePath().toString())).valueByXPath("/validation/summary/@status")
.asString()
.isEqualTo(expectedString);
} catch (AssertionError ae) { // Print information about
thisResultString="invalid"; // each type of file.
allValid=false; @Override
} public FileVisitResult visitFile(Path file,
LOGGER.info(String.format("\n@%s Testing file %d: %s (%s)", dateFormat.format(date), fileCount++, thisResultString, file)); BasicFileAttributes attr) {
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//get current date time with Date()
Date date = new Date();
String expectedString = "valid";
if (!expectValid) {
expectedString = "invalid";
}
if ((attr != null) && (attr.isRegularFile())) {
if (matcher.matches(file.getFileName())) {
// I could have extended the path matcher but an exclusion list is quite simple
if ((excludedFiles == null) || (!Arrays.asList(excludedFiles).contains(file.getFileName().toString()))) {
} String thisResultString = " valid";
} try {
return FileVisitResult.CONTINUE; assertThat(zul.validate(file.toAbsolutePath().toString())).valueByXPath("/validation/summary/@status")
} .asString()
.isEqualTo(expectedString);
// Print each directory visited. } catch (AssertionError ae) {
@Override thisResultString = "invalid";
public FileVisitResult postVisitDirectory(Path dir, allValid = false;
IOException exc) { }
LOGGER.info("\nDirectory: %s%n", dir); LOGGER.info(String.format("\n@%s Testing file %d: %s (%s) ", dateFormat.format(date), fileCount++, thisResultString, file));
return FileVisitResult.CONTINUE; }
} }
}
return FileVisitResult.CONTINUE;
}
// If there is some error accessing // Print each directory visited.
// the file, let the user know. @Override
// If you don't override this method public FileVisitResult postVisitDirectory(Path dir,
// and an error occurs, an IOException IOException exc) {
// is thrown. LOGGER.info("\nDirectory: %s%n", dir);
@Override return FileVisitResult.CONTINUE;
public FileVisitResult visitFileFailed(Path file, }
IOException exc) {
LOGGER.error(exc.getMessage(),exc); // If there is some error accessing
return FileVisitResult.CONTINUE; // the file, let the user know.
} // If you don't override this method
// and an error occurs, an IOException
// is thrown.
@Override
public FileVisitResult visitFileFailed(Path file,
IOException exc) {
LOGGER.error(exc.getMessage(), exc);
return FileVisitResult.CONTINUE;
}
} }