From dce37f5e17c01472a78ef439cc922e24cb56fc69 Mon Sep 17 00:00:00 2001 From: Jochen Staerk Date: Mon, 1 Jun 2020 10:14:04 +0200 Subject: [PATCH] cli method for validatoion walk --- Mustang-CLI/pom.xml | 15 +++ .../commandline/FileChecker.java | 0 .../commandline/FileTraverser.java | 0 .../org/mustangproject/commandline/Main.java | 18 +++ .../mustangproject/commandline/StatRun.java | 0 .../commandline/ValidatorFileWalker.java | 23 +++- .../mustangproject/validator/PathTest.java | 126 ------------------ .../validator/ZUGFeRDValidatorTest.java | 1 - 8 files changed, 49 insertions(+), 134 deletions(-) mode change 100755 => 100644 Mustang-CLI/src/main/java/org/mustangproject/commandline/FileChecker.java mode change 100755 => 100644 Mustang-CLI/src/main/java/org/mustangproject/commandline/FileTraverser.java mode change 100755 => 100644 Mustang-CLI/src/main/java/org/mustangproject/commandline/StatRun.java rename validator/src/test/java/org/mustangproject/validator/TestFileWalker.java => Mustang-CLI/src/main/java/org/mustangproject/commandline/ValidatorFileWalker.java (74%) delete mode 100644 validator/src/test/java/org/mustangproject/validator/PathTest.java diff --git a/Mustang-CLI/pom.xml b/Mustang-CLI/pom.xml index d21580bb..8b7fcaef 100644 --- a/Mustang-CLI/pom.xml +++ b/Mustang-CLI/pom.xml @@ -53,6 +53,21 @@ 4.12 test + + + + org.xmlunit + xmlunit-core + 2.6.3 + + + org.xmlunit + xmlunit-assertj + 2.6.3 + + + + diff --git a/Mustang-CLI/src/main/java/org/mustangproject/commandline/FileChecker.java b/Mustang-CLI/src/main/java/org/mustangproject/commandline/FileChecker.java old mode 100755 new mode 100644 diff --git a/Mustang-CLI/src/main/java/org/mustangproject/commandline/FileTraverser.java b/Mustang-CLI/src/main/java/org/mustangproject/commandline/FileTraverser.java old mode 100755 new mode 100644 diff --git a/Mustang-CLI/src/main/java/org/mustangproject/commandline/Main.java b/Mustang-CLI/src/main/java/org/mustangproject/commandline/Main.java index 91daf37b..6cfcb9e0 100755 --- a/Mustang-CLI/src/main/java/org/mustangproject/commandline/Main.java +++ b/Mustang-CLI/src/main/java/org/mustangproject/commandline/Main.java @@ -324,6 +324,10 @@ public class Main { optionsRecognized=performValidate(sourceName); + } else if ((action!=null)&&(action.equals("validateExpectValid"))) { + + optionsRecognized=performValidateExpect(true, directoryName); + } else { // no argument or argument unknown printUsage(); @@ -354,6 +358,20 @@ public class Main { return optionsRecognized; } + private static boolean performValidateExpect(boolean valid, String dirName) { + ValidatorFileWalker zfWalk = new ValidatorFileWalker(); + Path startingDir = Paths.get(dirName); + try { + Files.walkFileTree(startingDir, zfWalk); + + } catch (IOException e1) { + // TODO Auto-generated catch block + e1.printStackTrace(); + } + + return true; + } + private static void performUpgrade(String xmlName, String outName) throws IOException, TransformerException { // Get params from user if not already defined diff --git a/Mustang-CLI/src/main/java/org/mustangproject/commandline/StatRun.java b/Mustang-CLI/src/main/java/org/mustangproject/commandline/StatRun.java old mode 100755 new mode 100644 diff --git a/validator/src/test/java/org/mustangproject/validator/TestFileWalker.java b/Mustang-CLI/src/main/java/org/mustangproject/commandline/ValidatorFileWalker.java similarity index 74% rename from validator/src/test/java/org/mustangproject/validator/TestFileWalker.java rename to Mustang-CLI/src/main/java/org/mustangproject/commandline/ValidatorFileWalker.java index bc5bb4f7..5edae80b 100644 --- a/validator/src/test/java/org/mustangproject/validator/TestFileWalker.java +++ b/Mustang-CLI/src/main/java/org/mustangproject/commandline/ValidatorFileWalker.java @@ -1,6 +1,6 @@ -package org.mustangproject.validator; +package org.mustangproject.commandline; + -import static org.xmlunit.assertj.XmlAssert.assertThat; import java.io.IOException; import java.nio.file.FileSystems; @@ -12,14 +12,23 @@ import java.nio.file.attribute.BasicFileAttributes; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.xmlunit.builder.Input; +import org.xmlunit.xpath.JAXPXPathEngine; +import org.xmlunit.xpath.XPathEngine; +import org.mustangproject.validator.ZUGFeRDValidator; -public class TestFileWalker +import static org.xmlunit.assertj.XmlAssert.assertThat; + +public class ValidatorFileWalker extends SimpleFileVisitor { + private static final Logger LOGGER = LoggerFactory.getLogger(ValidatorFileWalker.class.getCanonicalName()); // log protected PathMatcher matcher; protected ZUGFeRDValidator zul; protected int fileCount=1; - public TestFileWalker() { + public ValidatorFileWalker() { this.zul = new ZUGFeRDValidator(); ; matcher = FileSystems.getDefault().getPathMatcher("glob:*.{pdf,xml}"); @@ -36,7 +45,7 @@ public class TestFileWalker if ((attr!=null)&&(attr.isRegularFile())) { if (matcher.matches(file.getFileName())) { - System.out.format("\n@%s Testing file %d: %s ", dateFormat.format(date), fileCount++, file); + LOGGER.info(String.format("\n@%s Testing file %d: %s ", dateFormat.format(date), fileCount++, file)); assertThat(zul.validate(file.toAbsolutePath().toString())).valueByXPath("/validation/summary/@status") .asString() .isEqualTo( @@ -51,7 +60,7 @@ public class TestFileWalker @Override public FileVisitResult postVisitDirectory(Path dir, IOException exc) { - System.out.format("Directory: %s%n", dir); + LOGGER.info("\nDirectory: %s%n", dir); return FileVisitResult.CONTINUE; } @@ -63,7 +72,7 @@ public class TestFileWalker @Override public FileVisitResult visitFileFailed(Path file, IOException exc) { - System.err.println(exc); + LOGGER.error(exc.getMessage(),exc); return FileVisitResult.CONTINUE; } } diff --git a/validator/src/test/java/org/mustangproject/validator/PathTest.java b/validator/src/test/java/org/mustangproject/validator/PathTest.java deleted file mode 100644 index 21506a9c..00000000 --- a/validator/src/test/java/org/mustangproject/validator/PathTest.java +++ /dev/null @@ -1,126 +0,0 @@ -package org.mustangproject.validator; - -import static org.xmlunit.assertj.XmlAssert.assertThat; - -import java.io.File; -import java.io.IOException; -import java.net.MalformedURLException; -import java.net.URL; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; - -import org.junit.Before; -import org.junit.Test; -import org.xmlunit.xpath.JAXPXPathEngine; -import org.xmlunit.xpath.XPathEngine; - -import junit.framework.TestCase; - -public class PathTest extends TestCase { - - TestFileWalker zfWalk; - - @Before - public void beforeEachTestMethod() { - System.out.println("Invoked before each test method"); - TestFileWalker zfWalk = new TestFileWalker(); - - } - - @Test - public void testOfficialSamples() { - // ignored for the - // time being - Path startingDir = Paths.get(System.getProperty("user.dir") + "/../../Release/Beispiele/"); - TestFileWalker zfWalk = new TestFileWalker(); - try { - Files.walkFileTree(startingDir, zfWalk); - - } catch (IOException e1) { - // TODO Auto-generated catch block - e1.printStackTrace(); - } - - // XPathEngine xpath = new JAXPXPathEngine(); - // File tempFile = getResourceAsFile("invalidV2.xml"); -//https://stackoverflow.com/questions/16245914/execute-an-external-jar -> http://docs.oracle.com/javase/tutorial/deployment/jar/jarclassloader.html - - /** - * assertThat(content).valueByXPath("count(//error)") .asInt() - * .isGreaterThan(1); //2 errors are OK because there is a known bug - * - * - * assertThat(content).valueByXPath("//error[@type=\"4\"]") .asString() - * .contains( "In Deutschland sind die Profile MINIMUM und BASIC WL nur als - * Buchungshilfe (TypeCode: 751) zugelassen."); - * - */ - - } - - @Test - public void testFacturX() { - TestFileWalker zfWalk = new TestFileWalker(); - Path startingDir = Paths.get(System.getProperty("user.dir") + "/../../foreign_samples/fx/"); - try { - Files.walkFileTree(startingDir, zfWalk); - - } catch (IOException e1) { - // TODO Auto-generated catch block - e1.printStackTrace(); - } - - // XPathEngine xpath = new JAXPXPathEngine(); - // File tempFile = getResourceAsFile("invalidV2.xml"); -//https://stackoverflow.com/questions/16245914/execute-an-external-jar -> http://docs.oracle.com/javase/tutorial/deployment/jar/jarclassloader.html - - /** - * assertThat(content).valueByXPath("count(//error)") .asInt() - * .isGreaterThan(1); //2 errors are OK because there is a known bug - * - * - * assertThat(content).valueByXPath("//error[@type=\"4\"]") .asString() - * .contains( "In Deutschland sind die Profile MINIMUM und BASIC WL nur als - * Buchungshilfe (TypeCode: 751) zugelassen."); - * - */ - - } - - @Test - public void testTestFiles() { - // ignored for the - // time being - /*https://www.xoev.de/sixcms/media.php/13/XRechnung_Kompakt.pdf "Problem - – - Nur positive Beispiele als Testgrundlage - • - Negative Beispiele sind sehr wichtig!"*/ - /* Path startingDir = Paths.get(System.getProperty("user.dir") + "/testfiles/toPass/"); - try { - Files.walkFileTree(startingDir, zfWalk); - - } catch (IOException e1) { - // TODO Auto-generated catch block - e1.printStackTrace(); - } -*/ - // XPathEngine xpath = new JAXPXPathEngine(); - // File tempFile = getResourceAsFile("invalidV2.xml"); -//https://stackoverflow.com/questions/16245914/execute-an-external-jar -> http://docs.oracle.com/javase/tutorial/deployment/jar/jarclassloader.html - - /** - * assertThat(content).valueByXPath("count(//error)") .asInt() - * .isGreaterThan(1); //2 errors are OK because there is a known bug - * - * - * assertThat(content).valueByXPath("//error[@type=\"4\"]") .asString() - * .contains( "In Deutschland sind die Profile MINIMUM und BASIC WL nur als - * Buchungshilfe (TypeCode: 751) zugelassen."); - * - */ - - } - -} diff --git a/validator/src/test/java/org/mustangproject/validator/ZUGFeRDValidatorTest.java b/validator/src/test/java/org/mustangproject/validator/ZUGFeRDValidatorTest.java index 202a2d75..c6b57b13 100644 --- a/validator/src/test/java/org/mustangproject/validator/ZUGFeRDValidatorTest.java +++ b/validator/src/test/java/org/mustangproject/validator/ZUGFeRDValidatorTest.java @@ -1,6 +1,5 @@ package org.mustangproject.validator; -import static org.xmlunit.assertj.XmlAssert.assertThat; import java.io.File; import java.util.regex.Matcher; import java.util.regex.Pattern;