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:
committed by
Brian Clozel
parent
9fb8a2eb2e
commit
3635c9dbfe
@@ -16,12 +16,10 @@
|
||||
|
||||
package org.springframework.http.codec.xml;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.xml.sax.SAXException;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.test.TestSubscriber;
|
||||
|
||||
@@ -33,10 +31,10 @@ import org.springframework.core.io.buffer.support.DataBufferUtils;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.codec.Pojo;
|
||||
|
||||
import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual;
|
||||
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.xmlunit.matchers.CompareMatcher.isSimilarTo;
|
||||
|
||||
/**
|
||||
* @author Sebastien Deleuze
|
||||
@@ -80,10 +78,8 @@ public class Jaxb2EncoderTests extends AbstractDataBufferAllocatingTestCase {
|
||||
try {
|
||||
String s = DataBufferTestUtils
|
||||
.dumpString(dataBuffer, StandardCharsets.UTF_8);
|
||||
assertXMLEqual("<pojo><bar>barbar</bar><foo>foofoo</foo></pojo>", s);
|
||||
}
|
||||
catch (SAXException | IOException e) {
|
||||
fail(e.getMessage());
|
||||
assertThat(s, isSimilarTo("<?xml version='1.0' encoding='UTF-8' standalone='yes'?>" +
|
||||
"<pojo><bar>barbar</bar><foo>foofoo</foo></pojo>"));
|
||||
}
|
||||
finally {
|
||||
DataBufferUtils.release(dataBuffer);
|
||||
|
||||
@@ -16,6 +16,9 @@
|
||||
|
||||
package org.springframework.http.converter.feed;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.xmlunit.matchers.CompareMatcher.*;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.charset.Charset;
|
||||
@@ -25,19 +28,17 @@ import java.util.List;
|
||||
|
||||
import com.rometools.rome.feed.atom.Entry;
|
||||
import com.rometools.rome.feed.atom.Feed;
|
||||
import org.custommonkey.xmlunit.XMLUnit;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.xml.sax.SAXException;
|
||||
import org.xmlunit.diff.DefaultNodeMatcher;
|
||||
import org.xmlunit.diff.ElementSelectors;
|
||||
import org.xmlunit.diff.NodeMatcher;
|
||||
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.MockHttpInputMessage;
|
||||
import org.springframework.http.MockHttpOutputMessage;
|
||||
|
||||
import static org.custommonkey.xmlunit.XMLAssert.*;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
/**
|
||||
* @author Arjen Poutsma
|
||||
*/
|
||||
@@ -49,7 +50,6 @@ public class AtomFeedHttpMessageConverterTests {
|
||||
@Before
|
||||
public void setUp() {
|
||||
converter = new AtomFeedHttpMessageConverter();
|
||||
XMLUnit.setIgnoreWhitespace(true);
|
||||
}
|
||||
|
||||
|
||||
@@ -111,7 +111,9 @@ public class AtomFeedHttpMessageConverterTests {
|
||||
String expected = "<feed xmlns=\"http://www.w3.org/2005/Atom\">" + "<title>title</title>" +
|
||||
"<entry><id>id1</id><title>title1</title></entry>" +
|
||||
"<entry><id>id2</id><title>title2</title></entry></feed>";
|
||||
assertXMLEqual(expected, outputMessage.getBodyAsString(StandardCharsets.UTF_8));
|
||||
NodeMatcher nm = new DefaultNodeMatcher(ElementSelectors.byName);
|
||||
assertThat(outputMessage.getBodyAsString(StandardCharsets.UTF_8),
|
||||
isSimilarTo(expected).ignoreWhitespace().withNodeMatcher(nm));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -16,6 +16,8 @@
|
||||
|
||||
package org.springframework.http.converter.feed;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.charset.Charset;
|
||||
@@ -25,19 +27,15 @@ import java.util.List;
|
||||
|
||||
import com.rometools.rome.feed.rss.Channel;
|
||||
import com.rometools.rome.feed.rss.Item;
|
||||
import org.custommonkey.xmlunit.XMLUnit;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.xml.sax.SAXException;
|
||||
import org.xmlunit.matchers.CompareMatcher;
|
||||
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.MockHttpInputMessage;
|
||||
import org.springframework.http.MockHttpOutputMessage;
|
||||
|
||||
import static org.custommonkey.xmlunit.XMLAssert.*;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
/**
|
||||
* @author Arjen Poutsma
|
||||
*/
|
||||
@@ -49,7 +47,6 @@ public class RssChannelHttpMessageConverterTests {
|
||||
@Before
|
||||
public void setUp() {
|
||||
converter = new RssChannelHttpMessageConverter();
|
||||
XMLUnit.setIgnoreWhitespace(true);
|
||||
}
|
||||
|
||||
|
||||
@@ -113,7 +110,7 @@ public class RssChannelHttpMessageConverterTests {
|
||||
"<item><title>title1</title></item>" +
|
||||
"<item><title>title2</title></item>" +
|
||||
"</channel></rss>";
|
||||
assertXMLEqual(expected, outputMessage.getBodyAsString(StandardCharsets.UTF_8));
|
||||
assertThat(outputMessage.getBodyAsString(StandardCharsets.UTF_8), isSimilarTo(expected));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -136,4 +133,8 @@ public class RssChannelHttpMessageConverterTests {
|
||||
outputMessage.getHeaders().getContentType());
|
||||
}
|
||||
|
||||
private static CompareMatcher isSimilarTo(final String content) {
|
||||
return CompareMatcher.isSimilarTo(content)
|
||||
.ignoreWhitespace();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,7 +16,13 @@
|
||||
|
||||
package org.springframework.http.converter.xml;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.xmlunit.diff.ComparisonType.*;
|
||||
import static org.xmlunit.diff.DifferenceEvaluators.*;
|
||||
import static org.xmlunit.matchers.CompareMatcher.*;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
import javax.xml.bind.Marshaller;
|
||||
import javax.xml.bind.Unmarshaller;
|
||||
import javax.xml.bind.annotation.XmlAttribute;
|
||||
@@ -30,6 +36,7 @@ import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
import org.xmlunit.diff.DifferenceEvaluator;
|
||||
|
||||
import org.springframework.aop.framework.AdvisedSupport;
|
||||
import org.springframework.aop.framework.AopProxy;
|
||||
@@ -41,11 +48,6 @@ import org.springframework.http.MockHttpInputMessage;
|
||||
import org.springframework.http.MockHttpOutputMessage;
|
||||
import org.springframework.http.converter.HttpMessageNotReadableException;
|
||||
|
||||
import static org.custommonkey.xmlunit.XMLAssert.*;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
/**
|
||||
* Tests for {@link Jaxb2RootElementHttpMessageConverter}.
|
||||
*
|
||||
@@ -175,8 +177,9 @@ public class Jaxb2RootElementHttpMessageConverterTests {
|
||||
converter.write(rootElement, null, outputMessage);
|
||||
assertEquals("Invalid content-type", new MediaType("application", "xml"),
|
||||
outputMessage.getHeaders().getContentType());
|
||||
assertXMLEqual("Invalid result", "<rootElement><type s=\"Hello World\"/></rootElement>",
|
||||
outputMessage.getBodyAsString(StandardCharsets.UTF_8));
|
||||
DifferenceEvaluator ev = chain(Default, downgradeDifferencesToEqual(XML_STANDALONE));
|
||||
assertThat("Invalid result", outputMessage.getBodyAsString(StandardCharsets.UTF_8),
|
||||
isSimilarTo("<rootElement><type s=\"Hello World\"/></rootElement>").withDifferenceEvaluator(ev));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -185,8 +188,9 @@ public class Jaxb2RootElementHttpMessageConverterTests {
|
||||
converter.write(rootElementCglib, null, outputMessage);
|
||||
assertEquals("Invalid content-type", new MediaType("application", "xml"),
|
||||
outputMessage.getHeaders().getContentType());
|
||||
assertXMLEqual("Invalid result", "<rootElement><type s=\"Hello World\"/></rootElement>",
|
||||
outputMessage.getBodyAsString(StandardCharsets.UTF_8));
|
||||
DifferenceEvaluator ev = chain(Default, downgradeDifferencesToEqual(XML_STANDALONE));
|
||||
assertThat("Invalid result", outputMessage.getBodyAsString(StandardCharsets.UTF_8),
|
||||
isSimilarTo("<rootElement><type s=\"Hello World\"/></rootElement>").withDifferenceEvaluator(ev));
|
||||
}
|
||||
|
||||
// SPR-11488
|
||||
@@ -196,8 +200,9 @@ public class Jaxb2RootElementHttpMessageConverterTests {
|
||||
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
|
||||
MyJaxb2RootElementHttpMessageConverter myConverter = new MyJaxb2RootElementHttpMessageConverter();
|
||||
myConverter.write(new MyRootElement(new MyCustomElement("a", "b")), null, outputMessage);
|
||||
assertXMLEqual("Invalid result", "<myRootElement><element>a|||b</element></myRootElement>",
|
||||
outputMessage.getBodyAsString(StandardCharsets.UTF_8));
|
||||
DifferenceEvaluator ev = chain(Default, downgradeDifferencesToEqual(XML_STANDALONE));
|
||||
assertThat("Invalid result", outputMessage.getBodyAsString(StandardCharsets.UTF_8),
|
||||
isSimilarTo("<myRootElement><element>a|||b</element></myRootElement>").withDifferenceEvaluator(ev));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -49,10 +49,11 @@ import org.springframework.http.MockHttpOutputMessage;
|
||||
import org.springframework.http.converter.HttpMessageNotReadableException;
|
||||
import org.springframework.util.FileCopyUtils;
|
||||
|
||||
import static org.custommonkey.xmlunit.XMLAssert.*;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotEquals;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.xmlunit.matchers.CompareMatcher.isSimilarTo;
|
||||
|
||||
// Do NOT statically import org.junit.Assert.*, since XMLAssert extends junit.framework.Assert
|
||||
|
||||
@@ -150,7 +151,7 @@ public class SourceHttpMessageConverterTests {
|
||||
SAXSource result = (SAXSource) converter.read(SAXSource.class, inputMessage);
|
||||
InputSource inputSource = result.getInputSource();
|
||||
String s = FileCopyUtils.copyToString(new InputStreamReader(inputSource.getByteStream()));
|
||||
assertXMLEqual("Invalid result", BODY, s);
|
||||
assertThat("Invalid result", s, isSimilarTo(BODY));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -278,7 +279,7 @@ public class SourceHttpMessageConverterTests {
|
||||
inputMessage.getHeaders().setContentType(new MediaType("application", "xml"));
|
||||
StreamSource result = (StreamSource) converter.read(StreamSource.class, inputMessage);
|
||||
String s = FileCopyUtils.copyToString(new InputStreamReader(result.getInputStream()));
|
||||
assertXMLEqual("Invalid result", BODY, s);
|
||||
assertThat("Invalid result", s, isSimilarTo(BODY));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -300,8 +301,8 @@ public class SourceHttpMessageConverterTests {
|
||||
|
||||
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
|
||||
converter.write(domSource, null, outputMessage);
|
||||
assertXMLEqual("Invalid result", "<root>Hello World</root>",
|
||||
outputMessage.getBodyAsString(StandardCharsets.UTF_8));
|
||||
assertThat("Invalid result", outputMessage.getBodyAsString(StandardCharsets.UTF_8),
|
||||
isSimilarTo("<root>Hello World</root>"));
|
||||
assertEquals("Invalid content-type", new MediaType("application", "xml"),
|
||||
outputMessage.getHeaders().getContentType());
|
||||
assertEquals("Invalid content-length", outputMessage.getBodyAsBytes().length,
|
||||
@@ -315,8 +316,8 @@ public class SourceHttpMessageConverterTests {
|
||||
|
||||
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
|
||||
converter.write(saxSource, null, outputMessage);
|
||||
assertXMLEqual("Invalid result", "<root>Hello World</root>",
|
||||
outputMessage.getBodyAsString(StandardCharsets.UTF_8));
|
||||
assertThat("Invalid result", outputMessage.getBodyAsString(StandardCharsets.UTF_8),
|
||||
isSimilarTo("<root>Hello World</root>"));
|
||||
assertEquals("Invalid content-type", new MediaType("application", "xml"),
|
||||
outputMessage.getHeaders().getContentType());
|
||||
}
|
||||
@@ -328,8 +329,8 @@ public class SourceHttpMessageConverterTests {
|
||||
|
||||
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
|
||||
converter.write(streamSource, null, outputMessage);
|
||||
assertXMLEqual("Invalid result", "<root>Hello World</root>",
|
||||
outputMessage.getBodyAsString(StandardCharsets.UTF_8));
|
||||
assertThat("Invalid result", outputMessage.getBodyAsString(StandardCharsets.UTF_8),
|
||||
isSimilarTo("<root>Hello World</root>"));
|
||||
assertEquals("Invalid content-type", new MediaType("application", "xml"),
|
||||
outputMessage.getHeaders().getContentType());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user