From 42e13d85b0517138fa79e5d53f96afde8eecbb4e Mon Sep 17 00:00:00 2001 From: Kevin Balthaser Date: Mon, 11 Jun 2018 08:55:52 -0400 Subject: [PATCH] Add test case which illustrates issue with `XmlParser` being unable to parse a `Document` that it creates. --- .../ca/uhn/hl7v2/parser/XMLParserTest.java | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/hapi-test/src/test/java/ca/uhn/hl7v2/parser/XMLParserTest.java b/hapi-test/src/test/java/ca/uhn/hl7v2/parser/XMLParserTest.java index f5c56dcef..23ac54b46 100644 --- a/hapi-test/src/test/java/ca/uhn/hl7v2/parser/XMLParserTest.java +++ b/hapi-test/src/test/java/ca/uhn/hl7v2/parser/XMLParserTest.java @@ -20,7 +20,9 @@ import org.junit.Test; import org.w3c.dom.Document; +import ca.uhn.hl7v2.DefaultHapiContext; import ca.uhn.hl7v2.HL7Exception; +import ca.uhn.hl7v2.HapiContext; import ca.uhn.hl7v2.model.Message; import ca.uhn.hl7v2.model.Segment; import ca.uhn.hl7v2.model.Type; @@ -32,6 +34,7 @@ import ca.uhn.hl7v2.model.v25.message.ORU_R01; import ca.uhn.hl7v2.model.v25.segment.OBX; import ca.uhn.hl7v2.model.v251.message.ERP_R09; +import ca.uhn.hl7v2.util.Hl7InputStreamMessageIterator; import ca.uhn.hl7v2.util.Terser; /** @@ -431,6 +434,36 @@ public void testMessageParseMethodAndEncodeMethodWithEscapes() throws HL7Excepti } + + /** + *

+ * Test to ensure that we can parse an XML document that we ourselves have encoded. Starting with a known Pipe message, + * parse it, encode it to XML, and then attempt to parse that resultant XML back to a {@link Message}. The expectation is that we + * can parse our own XML. + *

+ */ + @Test + public void test_ParseEncodedXml() throws Exception { + + // Create a context + HapiContext context = new DefaultHapiContext(); + context.getParserConfiguration().setValidating(false); + + XMLParser xp = context.getXMLParser(); + + InputStream str = getClass().getClassLoader().getResourceAsStream("ca/uhn/hl7v2/parser/adt_a03.txt"); + Hl7InputStreamMessageIterator iter = new Hl7InputStreamMessageIterator(str, context); + while (iter.hasNext()) { + + Message msg = iter.next(); + Document dom = xp.encodeDocument(msg); + + // We would expect to be able to parse this document back to a Message, as we encoded it. + Message outmsg = xp.parseDocument(dom, msg.getVersion()); + outmsg.printStructure(); + } + } + private String loadFile(String name) throws IOException { return loadFile(name, '\n'); }