From 73fa4fb2423b3346e6206290409d1c6665f6c738 Mon Sep 17 00:00:00 2001 From: Grahame Grieve Date: Sat, 20 Nov 2021 06:33:07 +1100 Subject: [PATCH] fix bug doing non-namespaced XML comparison --- .../hl7/fhir/r5/test/utils/TestingUtilities.java | 6 +++++- .../fhir/r5/test/NarrativeGenerationTests.java | 16 ++++++++++------ .../org/hl7/fhir/utilities/UtilitiesTest.java | 2 +- 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/test/utils/TestingUtilities.java b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/test/utils/TestingUtilities.java index 7c0d6cf53..28fdf6d84 100644 --- a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/test/utils/TestingUtilities.java +++ b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/test/utils/TestingUtilities.java @@ -171,7 +171,7 @@ public class TestingUtilities extends BaseTestingUtilities { } private static String compareElements(String path, Element e1, Element e2) { - if (!e1.getNamespaceURI().equals(e2.getNamespaceURI())) + if (!namespacesMatch(e1.getNamespaceURI(), e2.getNamespaceURI())) return "Namespaces differ at " + path + ": " + e1.getNamespaceURI() + "/" + e2.getNamespaceURI(); if (!e1.getLocalName().equals(e2.getLocalName())) return "Names differ at " + path + ": " + e1.getLocalName() + "/" + e2.getLocalName(); @@ -209,6 +209,10 @@ public class TestingUtilities extends BaseTestingUtilities { return null; } + private static boolean namespacesMatch(String ns1, String ns2) { + return ns1 == null ? ns2 == null : ns1.equals(ns2); + } + private static Object normalise(String text) { String result = text.trim().replace('\r', ' ').replace('\n', ' ').replace('\t', ' '); while (result.contains(" ")) diff --git a/org.hl7.fhir.r5/src/test/java/org/hl7/fhir/r5/test/NarrativeGenerationTests.java b/org.hl7.fhir.r5/src/test/java/org/hl7/fhir/r5/test/NarrativeGenerationTests.java index 3547785e7..d37a0218b 100644 --- a/org.hl7.fhir.r5/src/test/java/org/hl7/fhir/r5/test/NarrativeGenerationTests.java +++ b/org.hl7.fhir.r5/src/test/java/org/hl7/fhir/r5/test/NarrativeGenerationTests.java @@ -56,7 +56,6 @@ public class NarrativeGenerationTests { public Base parseType(String xml, String type) throws FHIRFormatError, IOException, FHIRException { return new org.hl7.fhir.r5.formats.XmlParser().parseType(xml, type); } - } public static final String WINDOWS = "WINDOWS"; @@ -143,9 +142,12 @@ public class NarrativeGenerationTests { XhtmlNode x = RendererFactory.factory(source, rc).build(source); String target = TextFile.streamToString(TestingUtilities.loadTestResourceStream("r5", "narrative", test.getId() + ".html")); String output = HEADER+new XhtmlComposer(true, true).compose(x)+FOOTER; - TextFile.stringToFile(target, TestingUtilities.tempFile("narrative", test.getId() + ".target.html")); - TextFile.stringToFile(output, TestingUtilities.tempFile("narrative", test.getId() + ".output.html")); - Assertions.assertTrue(output.equals(target), "Output does not match expected"); + String tfn = TestingUtilities.tempFile("narrative", test.getId() + ".target.html"); + String ofn = TestingUtilities.tempFile("narrative", test.getId() + ".output.html"); + TextFile.stringToFile(target, tfn); + TextFile.stringToFile(output, ofn); + String msg = TestingUtilities.checkXMLIsSame(ofn, tfn); + Assertions.assertTrue(msg == null, "Output does not match expected: "+msg); if (test.isMeta()) { org.hl7.fhir.r5.elementmodel.Element e = Manager.parseSingle(context, TestingUtilities.loadTestResourceStream("r5", "narrative", test.getId() + ".xml"), FhirFormat.XML); @@ -153,8 +155,10 @@ public class NarrativeGenerationTests { target = TextFile.streamToString(TestingUtilities.loadTestResourceStream("r5", "narrative", test.getId() + "-meta.html")); output = HEADER+new XhtmlComposer(true, true).compose(x)+FOOTER; - TextFile.stringToFile(output, TestingUtilities.tempFile("narrative", test.getId() + "-meta.output.html")); - Assertions.assertTrue(output.equals(target), "Output does not match expected (meta)"); + ofn = TestingUtilities.tempFile("narrative", test.getId() + "-meta.output.html"); + TextFile.stringToFile(output, ofn); + msg = TestingUtilities.checkXMLIsSame(ofn, tfn); + Assertions.assertTrue(msg == null, "Meta output does not match expected: "+msg); } } diff --git a/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/UtilitiesTest.java b/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/UtilitiesTest.java index 82ca542bb..252a0d033 100644 --- a/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/UtilitiesTest.java +++ b/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/UtilitiesTest.java @@ -30,7 +30,7 @@ class UtilitiesTest { public static final String WIN_JAVA_HOME = System.getenv("JAVA_HOME") + "\\"; public static final String OSX_USER_DIR = System.getProperty("user.home") + "/"; - public static final String OSX_JAVA_HOME = Paths.get(System.getenv("JAVA_HOME")).normalize().toString() + "/"; + public static final String OSX_JAVA_HOME = System.getenv("JAVA_HOME") == null ? null : Paths.get(System.getenv("JAVA_HOME")).normalize().toString() + "/"; @Test @DisplayName("Test Utilities.path maps temp directory correctly")