From 5b8ed483e4fb0e89523386a272c48160dfc783ff Mon Sep 17 00:00:00 2001 From: Andre Kemper Date: Sun, 7 Apr 2019 15:26:03 +0200 Subject: [PATCH] use XMLUnit --- pom.xml | 6 ++ .../ZUGFeRD/VersionMigrationTest.java | 42 +++---------- .../migration/reference/ZUGFeRD2-invoice.xml | 60 +++++++++---------- 3 files changed, 43 insertions(+), 65 deletions(-) diff --git a/pom.xml b/pom.xml index f921e8d4..e3773f75 100644 --- a/pom.xml +++ b/pom.xml @@ -88,6 +88,12 @@ 5.0.4 test + + org.xmlunit + xmlunit-core + 2.6.2 + test + com.sanityinc diff --git a/src/test/java/org/mustangproject/ZUGFeRD/VersionMigrationTest.java b/src/test/java/org/mustangproject/ZUGFeRD/VersionMigrationTest.java index 1f2a0fde..ab628dcd 100644 --- a/src/test/java/org/mustangproject/ZUGFeRD/VersionMigrationTest.java +++ b/src/test/java/org/mustangproject/ZUGFeRD/VersionMigrationTest.java @@ -21,6 +21,8 @@ import org.junit.BeforeClass; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; +import org.xmlunit.builder.DiffBuilder; +import org.xmlunit.diff.Diff; import javax.xml.transform.TransformerException; import java.io.File; @@ -28,10 +30,11 @@ import java.io.IOException; import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Collection; -import java.util.Scanner; import java.util.logging.Level; import java.util.logging.Logger; +import static org.junit.Assert.assertFalse; + /** * Every ZUGFeRD 1.0 XML file from src/test/resources/migration/input @@ -69,9 +72,8 @@ public class VersionMigrationTest { } private static void addFilesFromFolder(final File folder, Collection testSuiteData) { - String filePath = null; for (final File fileEntry : folder.listFiles()) { - filePath = fileEntry.getAbsolutePath(); + String filePath = fileEntry.getAbsolutePath(); if (fileEntry.isDirectory()) { LOG.log(Level.INFO, "*** testDirectory:{0}", filePath); addFilesFromFolder(fileEntry, testSuiteData); @@ -100,20 +102,8 @@ public class VersionMigrationTest { // we need to save the string and reload it otherwise two bytes are missing (likely EOF related) String outXML = ResourceUtilities.readFile(StandardCharsets.UTF_8, OUTPUT_DIR + newName); String refXML = ResourceUtilities.readFile(StandardCharsets.UTF_8, REFERENCE_DIR + newName); - int t = outXML.length(); - int r = refXML.length(); - if (t != r || !(outXML.equals(refXML))) { - LOG.info("There are differences between:" - + "\nZUGFeRD 2.0 Test Output @ " + OUTPUT_DIR + newName - + "\nZUGFeRD 2.0 Test Reference @ " + REFERENCE_DIR + newName); - LOG.info("\n\nFile sizes:\n " - + "\nZUGFeRD 2.0 Test Output character count: " + t - + "\nZUGFeRD 2.0 Test Refer. character count: " + r); - findLineDiffs(outXML, refXML); - Assert.fail("Version update failed, as test result and reference are different!"); - } else { - LOG.log(Level.INFO, "\n*** Tested successfull migration from ZUGFeRD 1.0 to 2.0: '" + newName + "'' ***\n", OUTPUT_DIR); - } + Diff myDiff = DiffBuilder.compare(refXML).withTest(outXML).checkForSimilar().ignoreComments().ignoreWhitespace().build(); + assertFalse(myDiff.toString(), myDiff.hasDifferences()); } else { LOG.log(Level.INFO, "\n*** Migrated from ZUGFeRD 1.0 to 2.0 invoice: '" + newName + "'' ***\n", OUTPUT_DIR); } @@ -123,22 +113,4 @@ public class VersionMigrationTest { } } - - private static void findLineDiffs(String outXML, String refXML) { - Scanner outScanner = new Scanner(outXML); - Scanner refScanner = new Scanner(refXML); - String outLine = null; - String refLine = null; - while (outScanner.hasNextLine() && refScanner.hasNextLine()) { - outLine = outScanner.nextLine(); - refLine = refScanner.nextLine(); - if (!outLine.equals(refLine)) { - LOG.info("First line difference between reference and output file:" + - "\nRefLine: " + refLine + - "\nOutLine: " + outLine + "\n"); - } - } - refScanner.close(); - outScanner.close(); - } } diff --git a/src/test/resources/migration/reference/ZUGFeRD2-invoice.xml b/src/test/resources/migration/reference/ZUGFeRD2-invoice.xml index 12043217..b0a390e8 100644 --- a/src/test/resources/migration/reference/ZUGFeRD2-invoice.xml +++ b/src/test/resources/migration/reference/ZUGFeRD2-invoice.xml @@ -18,9 +18,9 @@ + xmlns:udt="urn:un:unece:uncefact:data:standard:UnqualifiedDataType:100"> + + false @@ -28,7 +28,7 @@ Migrated by Mustangproject XSLT urn:ferd:CrossIndustryDocument:invoice:1p0:extended - + RE-20170509/505 RECHNUNG 380 @@ -36,16 +36,16 @@ Migrated by Mustangproject XSLT 20170509 - - - + + + 1 - + Künstlerische Gestaltung (Stunde): Einer Beispielrechnung - + 160.0000 1.0000 @@ -55,10 +55,10 @@ Migrated by Mustangproject XSLT 1.0000 - + 1.0000 - + VAT S @@ -68,16 +68,16 @@ Migrated by Mustangproject XSLT 160.00 - - - + + + 2 - + Luftballon: Bunt, ca. 500ml - + 0.7900 1.0000 @@ -87,10 +87,10 @@ Migrated by Mustangproject XSLT 1.0000 - + 400.0000 - + VAT S @@ -100,16 +100,16 @@ Migrated by Mustangproject XSLT 316.00 - - - + + + 3 - + Heiße Luft pro Liter - + 0.1000 1.0000 @@ -119,10 +119,10 @@ Migrated by Mustangproject XSLT 1.0000 - + 200.0000 - + VAT S @@ -132,8 +132,8 @@ Migrated by Mustangproject XSLT 20.00 - - + + Bei Spiel GmbH @@ -162,14 +162,14 @@ Migrated by Mustangproject XSLT - + 20170507 - + RE-20170509/505 EUR @@ -215,6 +215,6 @@ Migrated by Mustangproject XSLT 571.04 - +