From 51d445102dce25fe7ecd054f801a4b18ab9aab9f Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Tue, 15 Jan 2019 17:50:03 +0100 Subject: [PATCH] Improve JAXP implementation compatibility Testing with woodstox-core shows a couple of UT failures that are really assumptions on the implementation. Address them so we do not get failures when implementation changes (too much). While we are at it, improve startsWith/contains string checks by using hamcrest. Change-Id: I4af0c9cc370e5699255725f049ee7bb9b5c171ac Signed-off-by: Robert Varga (cherry picked from commit fea3a6991aa892b676ab550393e944c3e6215ef3) --- .../yang/data/codec/xml/Bug8803Test.java | 1 + .../data/codec/xml/XmlStreamUtilsTest.java | 43 +++++++++++++------ .../codec/xml/XmlToNormalizedNodesTest.java | 25 ++++++----- 3 files changed, 43 insertions(+), 26 deletions(-) diff --git a/yang/yang-data-codec-xml/src/test/java/org/opendaylight/yangtools/yang/data/codec/xml/Bug8803Test.java b/yang/yang-data-codec-xml/src/test/java/org/opendaylight/yangtools/yang/data/codec/xml/Bug8803Test.java index b4a3fd970f..97e9d80c5e 100644 --- a/yang/yang-data-codec-xml/src/test/java/org/opendaylight/yangtools/yang/data/codec/xml/Bug8803Test.java +++ b/yang/yang-data-codec-xml/src/test/java/org/opendaylight/yangtools/yang/data/codec/xml/Bug8803Test.java @@ -66,6 +66,7 @@ public class Bug8803Test { final NormalizedNodeWriter normalizedNodeWriter = NormalizedNodeWriter.forStreamWriter( xmlNormalizedNodeStreamWriter); normalizedNodeWriter.write(transformedInput); + normalizedNodeWriter.flush(); final String serializedXml = writer.toString(); assertFalse(serializedXml.isEmpty()); diff --git a/yang/yang-data-codec-xml/src/test/java/org/opendaylight/yangtools/yang/data/codec/xml/XmlStreamUtilsTest.java b/yang/yang-data-codec-xml/src/test/java/org/opendaylight/yangtools/yang/data/codec/xml/XmlStreamUtilsTest.java index 5447f05eac..c8365dd132 100644 --- a/yang/yang-data-codec-xml/src/test/java/org/opendaylight/yangtools/yang/data/codec/xml/XmlStreamUtilsTest.java +++ b/yang/yang-data-codec-xml/src/test/java/org/opendaylight/yangtools/yang/data/codec/xml/XmlStreamUtilsTest.java @@ -15,6 +15,7 @@ import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; import java.io.ByteArrayOutputStream; +import java.io.IOException; import java.net.URI; import java.util.AbstractMap; import java.util.HashMap; @@ -23,6 +24,7 @@ import java.util.Optional; import java.util.regex.Matcher; import java.util.regex.Pattern; import javax.xml.stream.XMLOutputFactory; +import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamWriter; import org.custommonkey.xmlunit.Diff; import org.custommonkey.xmlunit.XMLUnit; @@ -47,6 +49,10 @@ import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils; import org.w3c.dom.Document; public class XmlStreamUtilsTest { + @FunctionalInterface + interface XMLStreamWriterConsumer { + void accept(XMLStreamWriter writer) throws XMLStreamException; + } public static final XMLOutputFactory XML_OUTPUT_FACTORY = XMLOutputFactory.newFactory(); @@ -108,29 +114,40 @@ public class XmlStreamUtilsTest { @Test public void testWriteIdentityRef() throws Exception { - final ByteArrayOutputStream out = new ByteArrayOutputStream(); - final XMLStreamWriter writer = XML_OUTPUT_FACTORY.createXMLStreamWriter(out); - - writer.writeStartElement("element"); final QNameModule parent = QNameModule.create(URI.create("parent:uri"), Revision.of("2000-01-01")); - XMLStreamWriterUtils.write(writer, null, QName.create(parent, "identity"), parent); - writer.writeEndElement(); - writer.writeStartElement("elementDifferent"); - XMLStreamWriterUtils.write(writer, null, QName.create("different:namespace", "identity"), parent); - writer.writeEndElement(); + String xmlAsString = createXml(writer -> { + writer.writeStartElement("element"); + XMLStreamWriterUtils.write(writer, null, QName.create(parent, "identity"), parent); + writer.writeEndElement(); + }); - writer.close(); - out.close(); - - final String xmlAsString = new String(out.toByteArray()).replaceAll("\\s*", ""); assertThat(xmlAsString, containsString("element>identity")); + xmlAsString = createXml(writer -> { + writer.writeStartElement("elementDifferent"); + XMLStreamWriterUtils.write(writer, null, QName.create("different:namespace", "identity"), parent); + writer.writeEndElement(); + + }); + final Pattern prefixedIdentityPattern = Pattern.compile(".*\"different:namespace\">(.*):identity.*"); final Matcher matcher = prefixedIdentityPattern.matcher(xmlAsString); assertTrue("Xml: " + xmlAsString + " should match: " + prefixedIdentityPattern, matcher.matches()); } + private static String createXml(XMLStreamWriterConsumer consumer) throws XMLStreamException, IOException { + final ByteArrayOutputStream out = new ByteArrayOutputStream(); + final XMLStreamWriter writer = XML_OUTPUT_FACTORY.createXMLStreamWriter(out); + + consumer.accept(writer); + + writer.close(); + out.close(); + + return new String(out.toByteArray()).replaceAll("\\s*", ""); + } + /** * One leafref reference to other leafref via relative references. */ diff --git a/yang/yang-data-codec-xml/src/test/java/org/opendaylight/yangtools/yang/data/codec/xml/XmlToNormalizedNodesTest.java b/yang/yang-data-codec-xml/src/test/java/org/opendaylight/yangtools/yang/data/codec/xml/XmlToNormalizedNodesTest.java index 28d1cab4b8..9947379fa9 100644 --- a/yang/yang-data-codec-xml/src/test/java/org/opendaylight/yangtools/yang/data/codec/xml/XmlToNormalizedNodesTest.java +++ b/yang/yang-data-codec-xml/src/test/java/org/opendaylight/yangtools/yang/data/codec/xml/XmlToNormalizedNodesTest.java @@ -8,9 +8,11 @@ package org.opendaylight.yangtools.yang.data.codec.xml; +import static org.hamcrest.CoreMatchers.containsString; +import static org.hamcrest.CoreMatchers.startsWith; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; +import static org.junit.Assert.assertThat; import static org.junit.Assert.fail; import com.google.common.collect.ImmutableList; @@ -157,8 +159,8 @@ public class XmlToNormalizedNodesTest { xmlParser.parse(reader); fail("IllegalStateException should have been thrown because of duplicate leaf."); } catch (IllegalStateException ex) { - assertEquals("Duplicate namespace \"foo-namespace\" element \"decimal64-leaf\" in XML input at: line 7 " - + "column 25", ex.getMessage()); + assertThat(ex.getMessage(), startsWith("Duplicate namespace \"foo-namespace\" element \"decimal64-leaf\" " + + "in XML input at: line 7 column ")); } } @@ -177,8 +179,8 @@ public class XmlToNormalizedNodesTest { xmlParser.parse(reader); fail("IllegalStateException should have been thrown because of duplicate anyxml"); } catch (IllegalStateException ex) { - assertEquals("Duplicate namespace \"foo-namespace\" element \"my-anyxml\" in XML input at: line 19 " - + "column 20", ex.getMessage()); + assertThat(ex.getMessage(), startsWith("Duplicate namespace \"foo-namespace\" element \"my-anyxml\" in XML " + + "input at: line 19 column ")); } } @@ -197,8 +199,8 @@ public class XmlToNormalizedNodesTest { xmlParser.parse(reader); fail("IllegalStateException should have been thrown because of duplicate container"); } catch (IllegalStateException ex) { - assertEquals("Duplicate namespace \"foo-namespace\" element \"leaf-container\" in XML input at: line 13 " - + "column 21", ex.getMessage()); + assertThat(ex.getMessage(), startsWith("Duplicate namespace \"foo-namespace\" element \"leaf-container\" " + + "in XML input at: line 13 column ")); } } @@ -217,8 +219,7 @@ public class XmlToNormalizedNodesTest { xmlParser.parse(reader); fail("XMLStreamException should have been thrown because of unterminated leaf element."); } catch (XMLStreamException ex) { - assertTrue(ex.getMessage().contains("elementGetText() function expects text only elment but " - + "START_ELEMENT was encountered.")); + assertThat(ex.getMessage(), containsString(" START_ELEMENT ")); } } @@ -237,8 +238,7 @@ public class XmlToNormalizedNodesTest { xmlParser.parse(reader); fail("XMLStreamException should have been thrown because of unterminated leaf element."); } catch (XMLStreamException ex) { - assertTrue(ex.getMessage().contains("The element type \"my-leaf-1\" must be terminated by the matching " - + "end-tag \"\".")); + assertThat(ex.getMessage(), containsString("")); } } @@ -257,8 +257,7 @@ public class XmlToNormalizedNodesTest { xmlParser.parse(reader); fail("XMLStreamException should have been thrown because of unterminated container element."); } catch (XMLStreamException ex) { - assertTrue(ex.getMessage().contains("The element type \"my-container-1\" must be terminated by the " - + "matching end-tag \"\".")); + assertThat(ex.getMessage(), containsString("")); } } -- 2.36.6