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 <robert.varga@pantheon.tech>
(cherry picked from commit
fea3a6991aa892b676ab550393e944c3e6215ef3)
final NormalizedNodeWriter normalizedNodeWriter = NormalizedNodeWriter.forStreamWriter(
xmlNormalizedNodeStreamWriter);
normalizedNodeWriter.write(transformedInput);
final NormalizedNodeWriter normalizedNodeWriter = NormalizedNodeWriter.forStreamWriter(
xmlNormalizedNodeStreamWriter);
normalizedNodeWriter.write(transformedInput);
+ normalizedNodeWriter.flush();
final String serializedXml = writer.toString();
assertFalse(serializedXml.isEmpty());
final String serializedXml = writer.toString();
assertFalse(serializedXml.isEmpty());
import static org.junit.Assert.assertTrue;
import java.io.ByteArrayOutputStream;
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;
import java.net.URI;
import java.util.AbstractMap;
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.xml.stream.XMLOutputFactory;
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;
import javax.xml.stream.XMLStreamWriter;
import org.custommonkey.xmlunit.Diff;
import org.custommonkey.xmlunit.XMLUnit;
import org.w3c.dom.Document;
public class XmlStreamUtilsTest {
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();
public static final XMLOutputFactory XML_OUTPUT_FACTORY = XMLOutputFactory.newFactory();
@Test
public void testWriteIdentityRef() throws Exception {
@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"));
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"));
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());
}
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.
*/
/**
* One leafref reference to other leafref via relative references.
*/
package org.opendaylight.yangtools.yang.data.codec.xml;
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.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;
import static org.junit.Assert.fail;
import com.google.common.collect.ImmutableList;
xmlParser.parse(reader);
fail("IllegalStateException should have been thrown because of duplicate leaf.");
} catch (IllegalStateException ex) {
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 "));
xmlParser.parse(reader);
fail("IllegalStateException should have been thrown because of duplicate anyxml");
} catch (IllegalStateException ex) {
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 "));
xmlParser.parse(reader);
fail("IllegalStateException should have been thrown because of duplicate container");
} catch (IllegalStateException ex) {
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 "));
xmlParser.parse(reader);
fail("XMLStreamException should have been thrown because of unterminated leaf element.");
} catch (XMLStreamException ex) {
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 "));
xmlParser.parse(reader);
fail("XMLStreamException should have been thrown because of unterminated leaf element.");
} catch (XMLStreamException ex) {
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 \"</my-leaf-1>\"."));
+ assertThat(ex.getMessage(), containsString("</my-leaf-1>"));
xmlParser.parse(reader);
fail("XMLStreamException should have been thrown because of unterminated container element.");
} catch (XMLStreamException ex) {
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 \"</my-container-1>\"."));
+ assertThat(ex.getMessage(), containsString("</my-container-1>"));