closes #818
This commit is contained in:
@@ -347,8 +347,11 @@ public class Main {
|
||||
Option attachmentOpt = new Option("attachments", "attachments", true, "File attachments");
|
||||
attachmentOpt.setValueSeparator(',');
|
||||
attachmentOpt.setArgs(Option.UNLIMITED_VALUES);
|
||||
|
||||
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-xml", "source-xml", true, "which source file to use"));
|
||||
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[] attachmentFilenames = cmd.hasOption("attachments") ? cmd.getOptionValues("attachments") : null;
|
||||
String[] excludedFilenames = cmd.hasOption("exclude") ? cmd.getOptionValues("exclude") : null;
|
||||
|
||||
|
||||
ArrayList<FileAttachment> attachments = new ArrayList<>();
|
||||
@@ -433,9 +437,9 @@ public class Main {
|
||||
} else if ((action != null) && (action.equals("validate"))) {
|
||||
optionsRecognized = performValidate(sourceName, noNotices, cmd.getOptionValue("logAppend"), LogAsPDF);
|
||||
} else if ((action != null) && (action.equals("validateExpectValid"))) {
|
||||
optionsRecognized = performValidateExpect(true, directoryName);
|
||||
optionsRecognized = performValidateExpect(true, directoryName, excludedFilenames);
|
||||
} else if ((action != null) && (action.equals("validateExpectInvalid"))) {
|
||||
optionsRecognized = performValidateExpect(false, directoryName);
|
||||
optionsRecognized = performValidateExpect(false, directoryName, excludedFilenames);
|
||||
}
|
||||
|
||||
} catch (UnrecognizedOptionException ex) {
|
||||
@@ -487,8 +491,8 @@ public class Main {
|
||||
return optionsRecognized;
|
||||
}
|
||||
|
||||
private static boolean performValidateExpect(boolean valid, String dirName) {
|
||||
ValidatorFileWalker zfWalk = new ValidatorFileWalker(valid);
|
||||
private static boolean performValidateExpect(boolean valid, String dirName, String[] excludedFiles) {
|
||||
ValidatorFileWalker zfWalk = new ValidatorFileWalker(valid, excludedFiles);
|
||||
Path startingDir = Paths.get(dirName);
|
||||
try {
|
||||
Files.walkFileTree(startingDir, zfWalk);
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package org.mustangproject.commandline;
|
||||
|
||||
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.FileSystems;
|
||||
import java.nio.file.FileVisitResult;
|
||||
@@ -11,25 +10,29 @@ import java.nio.file.SimpleFileVisitor;
|
||||
import java.nio.file.attribute.BasicFileAttributes;
|
||||
import java.text.DateFormat;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Arrays;
|
||||
import java.util.Date;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.mustangproject.validator.ZUGFeRDValidator;
|
||||
|
||||
import static org.xmlunit.assertj.XmlAssert.assertThat;
|
||||
|
||||
public class ValidatorFileWalker
|
||||
extends SimpleFileVisitor<Path> {
|
||||
public class ValidatorFileWalker
|
||||
extends SimpleFileVisitor<Path> {
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(ValidatorFileWalker.class.getCanonicalName()); // log
|
||||
protected PathMatcher matcher;
|
||||
protected ZUGFeRDValidator zul;
|
||||
protected int fileCount=1;
|
||||
protected boolean expectValid=true;
|
||||
protected boolean allValid=true;
|
||||
protected int fileCount = 1;
|
||||
protected boolean expectValid = true;
|
||||
protected boolean allValid = true;
|
||||
protected String[] excludedFiles = {};
|
||||
|
||||
public ValidatorFileWalker(boolean expectValid) {
|
||||
public ValidatorFileWalker(boolean expectValid, String[] excludedFiles) {
|
||||
this.zul = new ZUGFeRDValidator();
|
||||
this.expectValid=expectValid;
|
||||
this.expectValid = expectValid;
|
||||
this.excludedFiles = excludedFiles;
|
||||
matcher = FileSystems.getDefault().getPathMatcher("glob:*.{pdf,xml}");
|
||||
|
||||
}
|
||||
@@ -37,54 +40,58 @@ public class ValidatorFileWalker
|
||||
public boolean getResult() {
|
||||
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) {
|
||||
thisResultString="invalid";
|
||||
allValid=false;
|
||||
}
|
||||
LOGGER.info(String.format("\n@%s Testing file %d: %s (%s)", dateFormat.format(date), fileCount++, thisResultString, file));
|
||||
|
||||
}
|
||||
}
|
||||
return FileVisitResult.CONTINUE;
|
||||
}
|
||||
|
||||
// Print each directory visited.
|
||||
@Override
|
||||
public FileVisitResult postVisitDirectory(Path dir,
|
||||
IOException exc) {
|
||||
LOGGER.info("\nDirectory: %s%n", dir);
|
||||
return FileVisitResult.CONTINUE;
|
||||
}
|
||||
// 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())) {
|
||||
// I could have extended the path matcher but an exclusion list is quite simple
|
||||
if ((excludedFiles == null) || (!Arrays.asList(excludedFiles).contains(file.getFileName().toString()))) {
|
||||
|
||||
// If there is some error accessing
|
||||
// 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;
|
||||
}
|
||||
String thisResultString = " valid";
|
||||
try {
|
||||
assertThat(zul.validate(file.toAbsolutePath().toString())).valueByXPath("/validation/summary/@status")
|
||||
.asString()
|
||||
.isEqualTo(expectedString);
|
||||
|
||||
} catch (AssertionError ae) {
|
||||
thisResultString = "invalid";
|
||||
allValid = false;
|
||||
}
|
||||
LOGGER.info(String.format("\n@%s Testing file %d: %s (%s) ", dateFormat.format(date), fileCount++, thisResultString, file));
|
||||
}
|
||||
}
|
||||
}
|
||||
return FileVisitResult.CONTINUE;
|
||||
}
|
||||
|
||||
// Print each directory visited.
|
||||
@Override
|
||||
public FileVisitResult postVisitDirectory(Path dir,
|
||||
IOException exc) {
|
||||
LOGGER.info("\nDirectory: %s%n", dir);
|
||||
return FileVisitResult.CONTINUE;
|
||||
}
|
||||
|
||||
// If there is some error accessing
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user