Update xmlunit library to version 2.1.0

xmlunit 2.1.0 is the latest release for xmlunit.
Most of the xmlunit functionality used within spring-framework
was done through the xmlunit 1.x helper class
`org.custommonkey.xmlunit.XMLAssert`.

As of xmlunit 2.0.0 most of the XML comparison methods are done
through hamcrest matchers exposed by the xmlunit-matchers
library. In some cases during the migration, the matchers
had to be customized with custom `NodeMatcher` or
`DifferenceEvaluator` instances in order to keep the assertions
correct (they were performed with xmlunit 1.x previously).

Issue: SPR-14043
This commit is contained in:
Marius Grama
2016-03-13 15:33:15 +01:00
committed by Brian Clozel
parent 9fb8a2eb2e
commit 3635c9dbfe
22 changed files with 345 additions and 284 deletions

View File

@@ -16,8 +16,14 @@
package org.springframework.oxm;
import java.io.ByteArrayOutputStream;
import java.io.StringWriter;
import org.junit.Before;
import org.junit.Test;
import org.springframework.util.xml.StaxUtils;
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Text;
import org.xmlunit.matchers.CompareMatcher;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
@@ -28,20 +34,10 @@ import javax.xml.transform.Result;
import javax.xml.transform.dom.DOMResult;
import javax.xml.transform.stax.StAXResult;
import javax.xml.transform.stream.StreamResult;
import java.io.ByteArrayOutputStream;
import java.io.StringWriter;
import org.custommonkey.xmlunit.XMLUnit;
import org.junit.Before;
import org.junit.Test;
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Text;
import org.springframework.util.xml.StaxUtils;
import static org.custommonkey.xmlunit.XMLAssert.*;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
/**
@@ -62,7 +58,6 @@ public abstract class AbstractMarshallerTests<M extends Marshaller> {
public final void setUp() throws Exception {
marshaller = createMarshaller();
flights = createFlights();
XMLUnit.setIgnoreWhitespace(true);
}
protected abstract M createMarshaller() throws Exception;
@@ -89,7 +84,7 @@ public abstract class AbstractMarshallerTests<M extends Marshaller> {
flightElement.appendChild(numberElement);
Text text = expected.createTextNode("42");
numberElement.appendChild(text);
assertXMLEqual("Marshaller writes invalid DOMResult", expected, result);
assertThat("Marshaller writes invalid DOMResult", result, isSimilarTo(expected));
}
@Test
@@ -113,7 +108,7 @@ public abstract class AbstractMarshallerTests<M extends Marshaller> {
flightElement.appendChild(numberElement);
Text text = expected.createTextNode("42");
numberElement.appendChild(text);
assertXMLEqual("Marshaller writes invalid DOMResult", expected, result);
assertThat("Marshaller writes invalid DOMResult", result, isSimilarTo(expected));
}
@Test
@@ -121,7 +116,7 @@ public abstract class AbstractMarshallerTests<M extends Marshaller> {
StringWriter writer = new StringWriter();
StreamResult result = new StreamResult(writer);
marshaller.marshal(flights, result);
assertXMLEqual("Marshaller writes invalid StreamResult", EXPECTED_STRING, writer.toString());
assertThat("Marshaller writes invalid StreamResult", writer.toString(), isSimilarTo(EXPECTED_STRING));
}
@Test
@@ -129,8 +124,8 @@ public abstract class AbstractMarshallerTests<M extends Marshaller> {
ByteArrayOutputStream os = new ByteArrayOutputStream();
StreamResult result = new StreamResult(os);
marshaller.marshal(flights, result);
assertXMLEqual("Marshaller writes invalid StreamResult", EXPECTED_STRING,
new String(os.toByteArray(), "UTF-8"));
assertThat("Marshaller writes invalid StreamResult", new String(os.toByteArray(), "UTF-8"),
isSimilarTo(EXPECTED_STRING));
}
@Test
@@ -140,7 +135,7 @@ public abstract class AbstractMarshallerTests<M extends Marshaller> {
XMLStreamWriter streamWriter = outputFactory.createXMLStreamWriter(writer);
Result result = StaxUtils.createStaxResult(streamWriter);
marshaller.marshal(flights, result);
assertXMLEqual("Marshaller writes invalid StreamResult", EXPECTED_STRING, writer.toString());
assertThat("Marshaller writes invalid StreamResult", writer.toString(), isSimilarTo(EXPECTED_STRING));
}
@Test
@@ -150,7 +145,7 @@ public abstract class AbstractMarshallerTests<M extends Marshaller> {
XMLEventWriter eventWriter = outputFactory.createXMLEventWriter(writer);
Result result = StaxUtils.createStaxResult(eventWriter);
marshaller.marshal(flights, result);
assertXMLEqual("Marshaller writes invalid StreamResult", EXPECTED_STRING, writer.toString());
assertThat("Marshaller writes invalid StreamResult", writer.toString(), isSimilarTo(EXPECTED_STRING));
}
@Test
@@ -160,7 +155,7 @@ public abstract class AbstractMarshallerTests<M extends Marshaller> {
XMLStreamWriter streamWriter = outputFactory.createXMLStreamWriter(writer);
StAXResult result = new StAXResult(streamWriter);
marshaller.marshal(flights, result);
assertXMLEqual("Marshaller writes invalid StreamResult", EXPECTED_STRING, writer.toString());
assertThat("Marshaller writes invalid StreamResult", writer.toString(), isSimilarTo(EXPECTED_STRING));
}
@Test
@@ -170,7 +165,11 @@ public abstract class AbstractMarshallerTests<M extends Marshaller> {
XMLEventWriter eventWriter = outputFactory.createXMLEventWriter(writer);
StAXResult result = new StAXResult(eventWriter);
marshaller.marshal(flights, result);
assertXMLEqual("Marshaller writes invalid StreamResult", EXPECTED_STRING, writer.toString());
assertThat("Marshaller writes invalid StreamResult", writer.toString(), isSimilarTo(EXPECTED_STRING));
}
private static CompareMatcher isSimilarTo(final Object content) {
return CompareMatcher.isSimilarTo(content)
.ignoreWhitespace();
}
}

View File

@@ -16,35 +16,32 @@
package org.springframework.oxm.castor;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
import static org.xmlunit.matchers.CompareMatcher.*;
import java.io.StringWriter;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import javax.xml.transform.Source;
import javax.xml.transform.sax.SAXResult;
import javax.xml.transform.stream.StreamResult;
import org.castor.xml.XMLProperties;
import org.custommonkey.xmlunit.NamespaceContext;
import org.custommonkey.xmlunit.SimpleNamespaceContext;
import org.custommonkey.xmlunit.XMLUnit;
import org.custommonkey.xmlunit.XpathEngine;
import org.exolab.castor.xml.XercesXMLSerializerFactory;
import org.junit.Test;
import org.mockito.InOrder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xmlunit.builder.Input;
import org.xmlunit.xpath.JAXPXPathEngine;
import org.springframework.core.io.ClassPathResource;
import org.springframework.oxm.AbstractMarshallerTests;
import static org.custommonkey.xmlunit.XMLAssert.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.*;
/**
* Tests the {@link CastorMarshaller} class.
*
@@ -163,14 +160,14 @@ public class CastorMarshallerTests extends AbstractMarshallerTests<CastorMarshal
public void suppressNamespacesTrue() throws Exception {
marshaller.setSuppressNamespaces(true);
String result = marshalFlights();
assertXMLEqual("Marshaller wrote invalid result", SUPPRESSED_NAMESPACE_EXPECTED_STRING, result);
assertThat("Marshaller wrote invalid result", result, isSimilarTo(SUPPRESSED_NAMESPACE_EXPECTED_STRING));
}
@Test
public void suppressNamespacesFalse() throws Exception {
marshaller.setSuppressNamespaces(false);
String result = marshalFlights();
assertXMLEqual("Marshaller wrote invalid result", EXPECTED_STRING, result);
assertThat("Marshaller wrote invalid result", result, isSimilarTo(EXPECTED_STRING));
}
@Test
@@ -179,7 +176,7 @@ public class CastorMarshallerTests extends AbstractMarshallerTests<CastorMarshal
marshaller.setSuppressXsiType(true);
marshaller.setRootElement("objects");
String result = marshal(Arrays.asList(castorObject));
assertXMLEqual("Marshaller wrote invalid result", SUPPRESSED_XSI_EXPECTED_STRING, result);
assertThat("Marshaller wrote invalid result", result, isSimilarTo(SUPPRESSED_XSI_EXPECTED_STRING));
}
@Test
@@ -188,14 +185,14 @@ public class CastorMarshallerTests extends AbstractMarshallerTests<CastorMarshal
marshaller.setSuppressXsiType(false);
marshaller.setRootElement("objects");
String result = marshal(Arrays.asList(castorObject));
assertXMLEqual("Marshaller wrote invalid result", XSI_EXPECTED_STRING, result);
assertThat("Marshaller wrote invalid result", result, isSimilarTo(XSI_EXPECTED_STRING));
}
@Test
public void marshalAsDocumentTrue() throws Exception {
marshaller.setMarshalAsDocument(true);
String result = marshalFlights();
assertXMLEqual("Marshaller wrote invalid result", DOCUMENT_EXPECTED_STRING, result);
assertThat("Marshaller wrote invalid result", result, isSimilarTo(DOCUMENT_EXPECTED_STRING));
assertTrue("Result doesn't contain xml declaration.",
result.contains("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"));
}
@@ -204,7 +201,7 @@ public class CastorMarshallerTests extends AbstractMarshallerTests<CastorMarshal
public void marshalAsDocumentFalse() throws Exception {
marshaller.setMarshalAsDocument(true);
String result = marshalFlights();
assertXMLEqual("Marshaller wrote invalid result", EXPECTED_STRING, result);
assertThat("Marshaller wrote invalid result", result, isSimilarTo(EXPECTED_STRING));
assertFalse("Result contains xml declaration.", result.matches("<\\?\\s*xml"));
}
@@ -212,7 +209,7 @@ public class CastorMarshallerTests extends AbstractMarshallerTests<CastorMarshal
public void rootElement() throws Exception {
marshaller.setRootElement("canceledFlights");
String result = marshalFlights();
assertXMLEqual("Marshaller wrote invalid result", ROOT_ELEMENT_EXPECTED_STRING, result);
assertThat("Marshaller wrote invalid result", result, isSimilarTo(ROOT_ELEMENT_EXPECTED_STRING));
}
@Test
@@ -222,7 +219,7 @@ public class CastorMarshallerTests extends AbstractMarshallerTests<CastorMarshal
String result = marshalFlights();
assertXpathEvaluatesTo("The xsi:noNamespaceSchemaLocation hasn't been written or has invalid value.",
noNamespaceSchemaLocation, "/tns:flights/@xsi:noNamespaceSchemaLocation", result);
assertXMLEqual("Marshaller wrote invalid result", EXPECTED_STRING, result);
assertThat("Marshaller wrote invalid result", result, isSimilarTo(EXPECTED_STRING));
}
@Test
@@ -232,7 +229,7 @@ public class CastorMarshallerTests extends AbstractMarshallerTests<CastorMarshal
String result = marshalFlights();
assertXpathEvaluatesTo("The xsi:noNamespaceSchemaLocation hasn't been written or has invalid value.",
schemaLocation, "/tns:flights/@xsi:schemaLocation", result);
assertXMLEqual("Marshaller wrote invalid result", EXPECTED_STRING, result);
assertThat("Marshaller wrote invalid result", result, isSimilarTo(EXPECTED_STRING));
}
@Test
@@ -242,7 +239,7 @@ public class CastorMarshallerTests extends AbstractMarshallerTests<CastorMarshal
marshaller.setUseXSITypeAtRoot(true);
marshaller.setRootElement("objects");
String result = marshal(Arrays.asList(castorObject));
assertXMLEqual("Marshaller wrote invalid result", ROOT_WITH_XSI_EXPECTED_STRING, result);
assertThat("Marshaller wrote invalid result", result, isSimilarTo(ROOT_WITH_XSI_EXPECTED_STRING));
}
@Test
@@ -252,7 +249,7 @@ public class CastorMarshallerTests extends AbstractMarshallerTests<CastorMarshal
marshaller.setUseXSITypeAtRoot(false);
marshaller.setRootElement("objects");
String result = marshal(Arrays.asList(castorObject));
assertXMLEqual("Marshaller wrote invalid result", ROOT_WITHOUT_XSI_EXPECTED_STRING, result);
assertThat("Marshaller wrote invalid result", result, isSimilarTo(ROOT_WITHOUT_XSI_EXPECTED_STRING));
}
@@ -282,13 +279,12 @@ public class CastorMarshallerTests extends AbstractMarshallerTests<CastorMarshal
namespaces.put("tns", "http://samples.springframework.org/flight");
namespaces.put("xsi", "http://www.w3.org/2001/XMLSchema-instance");
NamespaceContext ctx = new SimpleNamespaceContext(namespaces);
XpathEngine engine = XMLUnit.newXpathEngine();
engine.setNamespaceContext(ctx);
JAXPXPathEngine engine = new JAXPXPathEngine();
engine.setNamespaceContext(namespaces);
Document doc = XMLUnit.buildControlDocument(xmlDoc);
NodeList node = engine.getMatchingNodes(xpath, doc);
assertEquals(msg, expected, node.item(0).getNodeValue());
Source source = Input.fromString(xmlDoc).build();
Iterable<Node> nodeList = engine.selectNodes(xpath, source);
assertEquals(msg, expected, nodeList.iterator().next().getNodeValue());
}
/**

View File

@@ -16,34 +16,9 @@
package org.springframework.oxm.jaxb;
import java.io.ByteArrayOutputStream;
import java.io.StringWriter;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.Collections;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
import javax.xml.namespace.QName;
import javax.xml.transform.Result;
import javax.xml.transform.sax.SAXResult;
import javax.xml.transform.sax.SAXSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.InOrder;
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.InputSource;
import org.xml.sax.Locator;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.oxm.AbstractMarshallerTests;
@@ -55,13 +30,53 @@ import org.springframework.oxm.jaxb.test.ObjectFactory;
import org.springframework.oxm.mime.MimeContainer;
import org.springframework.util.FileCopyUtils;
import org.springframework.util.ReflectionUtils;
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.InputSource;
import org.xml.sax.Locator;
import org.xmlunit.diff.Comparison;
import org.xmlunit.diff.ComparisonResult;
import org.xmlunit.diff.ComparisonType;
import org.xmlunit.diff.DifferenceEvaluator;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
import javax.xml.namespace.QName;
import javax.xml.transform.Result;
import javax.xml.transform.sax.SAXResult;
import javax.xml.transform.sax.SAXSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import java.io.ByteArrayOutputStream;
import java.io.StringWriter;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.Arrays;
import java.util.Collections;
import java.util.EnumSet;
import static org.custommonkey.xmlunit.XMLAssert.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.BDDMockito.*;
import static org.mockito.BDDMockito.eq;
import static org.mockito.BDDMockito.given;
import static org.mockito.BDDMockito.inOrder;
import static org.mockito.BDDMockito.isA;
import static org.mockito.BDDMockito.mock;
import static org.mockito.BDDMockito.reset;
import static org.mockito.BDDMockito.times;
import static org.mockito.BDDMockito.verify;
import static org.xmlunit.diff.ComparisonType.XML_STANDALONE;
import static org.xmlunit.diff.DifferenceEvaluators.Default;
import static org.xmlunit.diff.DifferenceEvaluators.chain;
import static org.xmlunit.diff.DifferenceEvaluators.downgradeDifferencesToEqual;
import static org.xmlunit.matchers.CompareMatcher.isSimilarTo;
/**
* @author Arjen Poutsma
@@ -122,7 +137,9 @@ public class Jaxb2MarshallerTests extends AbstractMarshallerTests<Jaxb2Marshalle
StringWriter writer = new StringWriter();
StreamResult result = new StreamResult(writer);
marshaller.marshal(flights, result);
assertXMLEqual("Marshaller writes invalid StreamResult", EXPECTED_STRING, writer.toString());
DifferenceEvaluator ev = chain(Default, downgradeDifferencesToEqual(XML_STANDALONE));
assertThat("Marshaller writes invalid StreamResult", writer.toString(),
isSimilarTo(EXPECTED_STRING).withDifferenceEvaluator(ev));
}
@Test
@@ -305,8 +322,10 @@ public class Jaxb2MarshallerTests extends AbstractMarshallerTests<Jaxb2Marshalle
StringWriter writer = new StringWriter();
Result result = new StreamResult(writer);
marshaller.marshal(airplane, result);
assertXMLEqual("Marshalling should use root Element",
writer.toString(), "<airplane><name>test</name></airplane>");
DifferenceEvaluator ev = chain(Default, downgradeDifferencesToEqual(XML_STANDALONE));
assertThat("Marshalling should use root Element",
writer.toString(),
isSimilarTo("<airplane><name>test</name></airplane>").withDifferenceEvaluator(ev));
}
// SPR-10806
@@ -407,5 +426,4 @@ public class Jaxb2MarshallerTests extends AbstractMarshallerTests<Jaxb2Marshalle
private JAXBElement<DummyType> createDummyType() {
return null;
}
}

View File

@@ -16,21 +16,19 @@
package org.springframework.oxm.jibx;
import java.io.StringWriter;
import javax.xml.transform.stream.StreamResult;
import org.custommonkey.xmlunit.XMLUnit;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.oxm.AbstractMarshallerTests;
import org.springframework.tests.Assume;
import org.springframework.tests.TestGroup;
import static org.custommonkey.xmlunit.XMLAssert.*;
import javax.xml.transform.stream.StreamResult;
import java.io.StringWriter;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.xmlunit.matchers.CompareMatcher.isSimilarTo;
/**
* NOTE: These tests fail under Eclipse/IDEA because JiBX binding does not occur by
@@ -74,11 +72,10 @@ public class JibxMarshallerTests extends AbstractMarshallerTests<JibxMarshaller>
marshaller.setIndent(4);
StringWriter writer = new StringWriter();
marshaller.marshal(flights, new StreamResult(writer));
XMLUnit.setIgnoreWhitespace(false);
String expected =
"<?xml version=\"1.0\"?>\n" + "<flights xmlns=\"http://samples.springframework.org/flight\">\n" +
" <flight>\n" + " <number>42</number>\n" + " </flight>\n" + "</flights>";
assertXMLEqual(expected, writer.toString());
assertThat(writer.toString(), isSimilarTo(expected).ignoreWhitespace());
}
@Test

View File

@@ -16,26 +16,6 @@
package org.springframework.oxm.xstream;
import java.io.ByteArrayOutputStream;
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.stream.XMLEventWriter;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamWriter;
import javax.xml.transform.Result;
import javax.xml.transform.dom.DOMResult;
import javax.xml.transform.sax.SAXResult;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.converters.extended.EncodedByteArrayConverter;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
@@ -46,19 +26,46 @@ import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.mockito.InOrder;
import org.springframework.util.xml.StaxUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.Text;
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xmlunit.builder.Input;
import org.xmlunit.xpath.JAXPXPathEngine;
import org.springframework.util.xml.StaxUtils;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.stream.XMLEventWriter;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamWriter;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.dom.DOMResult;
import javax.xml.transform.sax.SAXResult;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import java.io.ByteArrayOutputStream;
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import static org.custommonkey.xmlunit.XMLAssert.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.mockito.BDDMockito.*;
import static org.mockito.BDDMockito.eq;
import static org.mockito.BDDMockito.inOrder;
import static org.mockito.BDDMockito.isA;
import static org.mockito.BDDMockito.mock;
import static org.xmlunit.matchers.CompareMatcher.isSimilarTo;
/**
* @author Arjen Poutsma
@@ -95,7 +102,7 @@ public class XStreamMarshallerTests {
flightElement.appendChild(numberElement);
Text text = expected.createTextNode("42");
numberElement.appendChild(text);
assertXMLEqual("Marshaller writes invalid DOMResult", expected, document);
assertThat("Marshaller writes invalid DOMResult", document, isSimilarTo(expected));
}
// see SWS-392
@@ -124,7 +131,7 @@ public class XStreamMarshallerTests {
eFlightElement.appendChild(eNumberElement);
Text text = expected.createTextNode("42");
eNumberElement.appendChild(text);
assertXMLEqual("Marshaller writes invalid DOMResult", expected, existent);
assertThat("Marshaller writes invalid DOMResult", existent, isSimilarTo(expected));
}
@Test
@@ -132,7 +139,7 @@ public class XStreamMarshallerTests {
StringWriter writer = new StringWriter();
StreamResult result = new StreamResult(writer);
marshaller.marshal(flight, result);
assertXMLEqual("Marshaller writes invalid StreamResult", EXPECTED_STRING, writer.toString());
assertThat("Marshaller writes invalid StreamResult", writer.toString(), isSimilarTo(EXPECTED_STRING));
}
@Test
@@ -141,7 +148,7 @@ public class XStreamMarshallerTests {
StreamResult result = new StreamResult(os);
marshaller.marshal(flight, result);
String s = new String(os.toByteArray(), "UTF-8");
assertXMLEqual("Marshaller writes invalid StreamResult", EXPECTED_STRING, s);
assertThat("Marshaller writes invalid StreamResult", s, isSimilarTo(EXPECTED_STRING));
}
@Test
@@ -166,7 +173,7 @@ public class XStreamMarshallerTests {
XMLStreamWriter streamWriter = outputFactory.createXMLStreamWriter(writer);
Result result = StaxUtils.createStaxResult(streamWriter);
marshaller.marshal(flight, result);
assertXMLEqual("Marshaller writes invalid StreamResult", EXPECTED_STRING, writer.toString());
assertThat("Marshaller writes invalid StreamResult", writer.toString(), isSimilarTo(EXPECTED_STRING));
}
@Test
@@ -176,7 +183,7 @@ public class XStreamMarshallerTests {
XMLEventWriter eventWriter = outputFactory.createXMLEventWriter(writer);
Result result = StaxUtils.createStaxResult(eventWriter);
marshaller.marshal(flight, result);
assertXMLEqual("Marshaller writes invalid StreamResult", EXPECTED_STRING, writer.toString());
assertThat("Marshaller writes invalid StreamResult", writer.toString(), isSimilarTo(EXPECTED_STRING));
}
@Test
@@ -185,7 +192,7 @@ public class XStreamMarshallerTests {
byte[] buf = new byte[]{0x1, 0x2};
Writer writer = new StringWriter();
marshaller.marshal(buf, new StreamResult(writer));
assertXMLEqual("<byte-array>AQI=</byte-array>", writer.toString());
assertThat(writer.toString(), isSimilarTo("<byte-array>AQI=</byte-array>"));
Reader reader = new StringReader(writer.toString());
byte[] bufResult = (byte[]) marshaller.unmarshal(new StreamSource(reader));
assertTrue("Invalid result", Arrays.equals(buf, bufResult));
@@ -197,7 +204,7 @@ public class XStreamMarshallerTests {
Writer writer = new StringWriter();
marshaller.marshal(flight, new StreamResult(writer));
String expected = "<flight flightNumber=\"42\" />";
assertXMLEqual("Marshaller does not use attributes", expected, writer.toString());
assertThat("Marshaller does not use attributes", writer.toString(), isSimilarTo(expected));
}
@Test
@@ -206,7 +213,7 @@ public class XStreamMarshallerTests {
Writer writer = new StringWriter();
marshaller.marshal(flight, new StreamResult(writer));
String expected = "<flight flightNumber=\"42\" />";
assertXMLEqual("Marshaller does not use attributes", expected, writer.toString());
assertThat("Marshaller does not use attributes", writer.toString(), isSimilarTo(expected));
}
@Test
@@ -215,7 +222,7 @@ public class XStreamMarshallerTests {
Writer writer = new StringWriter();
marshaller.marshal(flight, new StreamResult(writer));
String expected = "<flight flightNumber=\"42\" />";
assertXMLEqual("Marshaller does not use attributes", expected, writer.toString());
assertThat("Marshaller does not use attributes", writer.toString(), isSimilarTo(expected));
}
@Test
@@ -225,7 +232,7 @@ public class XStreamMarshallerTests {
Writer writer = new StringWriter();
marshaller.marshal(flight, new StreamResult(writer));
String expected = "<flight flightNumber=\"42\" />";
assertXMLEqual("Marshaller does not use attributes", expected, writer.toString());
assertThat("Marshaller does not use attributes", writer.toString(), isSimilarTo(expected));
}
@Test
@@ -239,7 +246,7 @@ public class XStreamMarshallerTests {
Writer writer = new StringWriter();
marshaller.marshal(flight, new StreamResult(writer));
assertXMLEqual("Marshaller does not use attributes", EXPECTED_STRING, writer.toString());
assertThat("Marshaller does not use attributes", writer.toString(), isSimilarTo(EXPECTED_STRING));
}
@Test
@@ -253,7 +260,7 @@ public class XStreamMarshallerTests {
Writer writer = new StringWriter();
marshaller.marshal(flight, new StreamResult(writer));
assertXMLEqual("Marshaller does not use attributes", EXPECTED_STRING, writer.toString());
assertThat("Marshaller does not use attributes", writer.toString(), isSimilarTo(EXPECTED_STRING));
}
@Test
@@ -262,7 +269,7 @@ public class XStreamMarshallerTests {
Writer writer = new StringWriter();
marshaller.marshal(flight, new StreamResult(writer));
String expected = "<flight><flightNo>42</flightNo></flight>";
assertXMLEqual("Marshaller does not use aliases", expected, writer.toString());
assertThat("Marshaller does not use aliases", writer.toString(), isSimilarTo(expected));
}
@Test
@@ -337,8 +344,31 @@ public class XStreamMarshallerTests {
flight.setFlightNumber(42);
marshaller.marshal(flight, result);
String expected = "<flight><number>42</number></flight>";
assertXMLEqual("Marshaller writes invalid StreamResult", expected, writer.toString());
assertThat("Marshaller writes invalid StreamResult", writer.toString(), isSimilarTo(expected));
}
private static void assertXpathExists(String xPathExpression, String inXMLString){
Source source = Input.fromString(inXMLString).build();
Iterable<Node> nodes = new JAXPXPathEngine().selectNodes(xPathExpression, source);
assertNotNull(nodes);
int count = 0;
for (Node node : nodes){
count++;
}
assertTrue("Expecting to find matches for Xpath " + xPathExpression, count > 0);
}
private static void assertXpathNotExists(String xPathExpression, String inXMLString){
Source source = Input.fromString(inXMLString).build();
Iterable<Node> nodes = new JAXPXPathEngine().selectNodes(xPathExpression, source);
assertNotNull(nodes);
int count = 0;
for (Node node : nodes){
count++;
}
assertEquals("Should be zero matches for Xpath " + xPathExpression, 0, count);
}
}