Replace xmlunit dependecy with AssertJ methods
This commit is contained in:
committed by
Gary Russell
parent
93d7c58b64
commit
0d09bdccd4
@@ -141,7 +141,6 @@ subprojects { subproject ->
|
||||
springVersion = project.hasProperty('springVersion') ? project.springVersion : '5.1.3.RELEASE'
|
||||
springWsVersion = '3.0.4.RELEASE'
|
||||
tomcatVersion = "9.0.12"
|
||||
xmlUnitVersion = '1.6'
|
||||
xstreamVersion = '1.4.11.1'
|
||||
}
|
||||
|
||||
@@ -703,8 +702,6 @@ project('spring-integration-xml') {
|
||||
compile "org.springframework:spring-oxm:$springVersion"
|
||||
compile ("org.springframework.ws:spring-xml:$springWsVersion")
|
||||
compile ("org.springframework.ws:spring-ws-core:$springWsVersion")
|
||||
|
||||
testCompile "xmlunit:xmlunit:$xmlUnitVersion"
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2017 the original author or authors.
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -16,8 +16,8 @@
|
||||
|
||||
package org.springframework.integration.xml;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
@@ -30,9 +30,7 @@ import javax.xml.transform.dom.DOMSource;
|
||||
import javax.xml.transform.sax.SAXSource;
|
||||
import javax.xml.transform.stream.StreamSource;
|
||||
|
||||
import org.custommonkey.xmlunit.XMLAssert;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Node;
|
||||
@@ -40,6 +38,7 @@ import org.w3c.dom.NodeList;
|
||||
import org.xml.sax.InputSource;
|
||||
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.integration.xml.util.XmlTestUtil;
|
||||
import org.springframework.messaging.MessagingException;
|
||||
|
||||
/**
|
||||
@@ -50,80 +49,83 @@ import org.springframework.messaging.MessagingException;
|
||||
*/
|
||||
public class DefaultXmlPayloadConverterTests {
|
||||
|
||||
private static final String TEST_DOCUMENT_AS_STRING = "<test>hello</test>";
|
||||
private static final String TEST_DOCUMENT_AS_STRING =
|
||||
"<?xml version=\"1.0\" encoding=\"UTF-8\"?><test>hello</test>";
|
||||
|
||||
private DefaultXmlPayloadConverter converter;
|
||||
private static final DefaultXmlPayloadConverter converter = new DefaultXmlPayloadConverter();
|
||||
|
||||
private Document testDocument;
|
||||
private static Document testDocument;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
converter = new DefaultXmlPayloadConverter();
|
||||
testDocument = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(
|
||||
new InputSource(new StringReader(TEST_DOCUMENT_AS_STRING)));
|
||||
@BeforeClass
|
||||
public static void setUp() throws Exception {
|
||||
testDocument =
|
||||
DocumentBuilderFactory.newInstance()
|
||||
.newDocumentBuilder()
|
||||
.parse(new InputSource(new StringReader(TEST_DOCUMENT_AS_STRING)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetDocumentWithString() {
|
||||
Document doc = converter.convertToDocument("<test>hello</test>");
|
||||
XMLAssert.assertXMLEqual(testDocument, doc);
|
||||
public void testGetDocumentWithString() throws Exception {
|
||||
Document doc = converter.convertToDocument(TEST_DOCUMENT_AS_STRING);
|
||||
assertThat(XmlTestUtil.docToString(doc)).isXmlEqualTo(TEST_DOCUMENT_AS_STRING);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetDocumentWithDocument() {
|
||||
Document doc = converter.convertToDocument(testDocument);
|
||||
Assert.assertTrue(doc == testDocument);
|
||||
assertThat(doc).isSameAs(testDocument);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetNodePassingNode() {
|
||||
Node element = testDocument.getElementsByTagName("test").item(0);
|
||||
Node n = converter.convertToNode(element);
|
||||
assertTrue("Wrong node returned", element == n);
|
||||
assertThat(n).isSameAs(element);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetNodePassingString() {
|
||||
Node n = converter.convertToNode("<test>hello</test>");
|
||||
XMLAssert.assertXMLEqual(testDocument, (Document) n);
|
||||
public void testGetNodePassingString() throws Exception {
|
||||
Node n = converter.convertToNode(TEST_DOCUMENT_AS_STRING);
|
||||
assertThat(XmlTestUtil.docToString((Document) n)).isXmlEqualTo(TEST_DOCUMENT_AS_STRING);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetNodePassingDocument() {
|
||||
public void testGetNodePassingDocument() throws Exception {
|
||||
Node n = converter.convertToNode(testDocument);
|
||||
XMLAssert.assertXMLEqual(testDocument, (Document) n);
|
||||
assertThat(XmlTestUtil.docToString((Document) n)).isXmlEqualTo(TEST_DOCUMENT_AS_STRING);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testGetSourcePassingDocument() throws Exception {
|
||||
public void testGetSourcePassingDocument() {
|
||||
Source source = converter.convertToSource(testDocument);
|
||||
assertEquals(DOMSource.class, source.getClass());
|
||||
assertThat(source).isInstanceOf(DOMSource.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetSourcePassingString() throws Exception {
|
||||
public void testGetSourcePassingString() {
|
||||
Source source = converter.convertToSource(TEST_DOCUMENT_AS_STRING);
|
||||
assertEquals(DOMSource.class, source.getClass());
|
||||
assertThat(source).isInstanceOf(DOMSource.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetSourcePassingSource() throws Exception {
|
||||
public void testGetSourcePassingSource() {
|
||||
SAXSource passedInSource = new SAXSource();
|
||||
Source source = converter.convertToSource(passedInSource);
|
||||
assertEquals(source, passedInSource);
|
||||
assertThat(source).isEqualTo(passedInSource);
|
||||
}
|
||||
|
||||
@Test(expected = MessagingException.class)
|
||||
@Test
|
||||
public void testInvalidPayload() {
|
||||
converter.convertToSource(12);
|
||||
assertThatThrownBy(() -> converter.convertToSource(12))
|
||||
.isExactlyInstanceOf(MessagingException.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetNodePassingDOMSource() {
|
||||
Node element = testDocument.getElementsByTagName("test").item(0);
|
||||
Node n = converter.convertToNode(new DOMSource(element));
|
||||
assertTrue("Wrong node returned", element == n);
|
||||
assertThat(n).isSameAs(element);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -131,33 +133,33 @@ public class DefaultXmlPayloadConverterTests {
|
||||
Node element = testDocument.getElementsByTagName("test").item(0);
|
||||
Document doc = converter.convertToDocument(element);
|
||||
NodeList childNodes = doc.getChildNodes();
|
||||
assertEquals(1, childNodes.getLength());
|
||||
assertEquals("test", childNodes.item(0).getNodeName());
|
||||
assertEquals("hello", childNodes.item(0).getTextContent());
|
||||
assertThat(childNodes.getLength()).isEqualTo(1);
|
||||
assertThat(childNodes.item(0).getNodeName()).isEqualTo("test");
|
||||
assertThat(childNodes.item(0).getTextContent()).isEqualTo("hello");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConvertSourceToDocument() throws Exception {
|
||||
public void testConvertSourceToDocument() {
|
||||
Node element = testDocument.getElementsByTagName("test").item(0);
|
||||
DOMSource domSource = new DOMSource(element);
|
||||
Document doc = converter.convertToDocument(domSource);
|
||||
NodeList childNodes = doc.getChildNodes();
|
||||
assertEquals(1, childNodes.getLength());
|
||||
assertEquals("test", childNodes.item(0).getNodeName());
|
||||
assertEquals("hello", childNodes.item(0).getTextContent());
|
||||
assertThat(childNodes.getLength()).isEqualTo(1);
|
||||
assertThat(childNodes.item(0).getNodeName()).isEqualTo("test");
|
||||
assertThat(childNodes.item(0).getTextContent()).isEqualTo("hello");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConvertBytesToDocument() throws Exception {
|
||||
Document doc = converter.convertToDocument("<test>hello</test>".getBytes());
|
||||
XMLAssert.assertXMLEqual(testDocument, doc);
|
||||
Document doc = converter.convertToDocument(TEST_DOCUMENT_AS_STRING.getBytes());
|
||||
assertThat(XmlTestUtil.docToString(doc)).isXmlEqualTo(TEST_DOCUMENT_AS_STRING);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConvertFileToDocument() throws Exception {
|
||||
File file = new ClassPathResource("org/springframework/integration/xml/customSource.data").getFile();
|
||||
Document doc = converter.convertToDocument(file);
|
||||
XMLAssert.assertXMLEqual(testDocument, doc);
|
||||
assertThat(XmlTestUtil.docToString(doc)).isXmlEqualTo(TEST_DOCUMENT_AS_STRING);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -165,7 +167,7 @@ public class DefaultXmlPayloadConverterTests {
|
||||
InputStream inputStream = new ClassPathResource("org/springframework/integration/xml/customSource.data")
|
||||
.getInputStream();
|
||||
Document doc = converter.convertToDocument(inputStream);
|
||||
XMLAssert.assertXMLEqual(testDocument, doc);
|
||||
assertThat(XmlTestUtil.docToString(doc)).isXmlEqualTo(TEST_DOCUMENT_AS_STRING);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -173,13 +175,13 @@ public class DefaultXmlPayloadConverterTests {
|
||||
ClassPathResource resource = new ClassPathResource("org/springframework/integration/xml/customSource.data");
|
||||
StreamSource source = new StreamSource(resource.getInputStream());
|
||||
Document doc = converter.convertToDocument(source);
|
||||
XMLAssert.assertXMLEqual(testDocument, doc);
|
||||
assertThat(XmlTestUtil.docToString(doc)).isXmlEqualTo(TEST_DOCUMENT_AS_STRING);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConvertCustomSourceToDocument() throws Exception {
|
||||
Document doc = converter.convertToDocument(new MySource());
|
||||
XMLAssert.assertXMLEqual(testDocument, doc);
|
||||
assertThat(XmlTestUtil.docToString(doc)).isXmlEqualTo(TEST_DOCUMENT_AS_STRING);
|
||||
}
|
||||
|
||||
private static class MySource implements Source {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2017 the original author or authors.
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -16,9 +16,8 @@
|
||||
|
||||
package org.springframework.integration.xml.source;
|
||||
|
||||
import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
|
||||
import java.io.StringReader;
|
||||
|
||||
@@ -28,59 +27,55 @@ import javax.xml.transform.Transformer;
|
||||
import javax.xml.transform.TransformerFactory;
|
||||
import javax.xml.transform.dom.DOMSource;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import org.w3c.dom.Document;
|
||||
import org.xml.sax.InputSource;
|
||||
|
||||
import org.springframework.integration.xml.util.XmlTestUtil;
|
||||
import org.springframework.messaging.MessagingException;
|
||||
import org.springframework.xml.transform.StringResult;
|
||||
|
||||
/**
|
||||
* @author Jonas Partner
|
||||
* @author Artem Bilan
|
||||
*/
|
||||
public class DomSourceFactoryTests {
|
||||
|
||||
Document doc;
|
||||
private static final String docContent = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><root>testValue</root>";
|
||||
|
||||
DomSourceFactory sourceFactory;
|
||||
private static final DomSourceFactory sourceFactory = new DomSourceFactory();
|
||||
|
||||
String docContent = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><root>testValue</root>";
|
||||
private static Document doc;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
private static Transformer transformer;
|
||||
|
||||
@BeforeClass
|
||||
public static void setUp() throws Exception {
|
||||
StringReader reader = new StringReader(docContent);
|
||||
doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new InputSource(reader));
|
||||
sourceFactory = new DomSourceFactory();
|
||||
transformer = TransformerFactory.newInstance().newTransformer();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWithDocumentPayload() throws Exception {
|
||||
Source source = sourceFactory.createSource(doc);
|
||||
assertNotNull("Returned source was null", source);
|
||||
assertEquals("Expected DOMSource", DOMSource.class, source.getClass());
|
||||
assertXMLEqual("Wrong content in source ", docContent, getAsString(source));
|
||||
assertThat(source).isNotNull();
|
||||
assertThat(source).isInstanceOf(DOMSource.class);
|
||||
assertThat(XmlTestUtil.sourceToString(source)).isXmlEqualTo(docContent);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWithStringPayload() throws Exception {
|
||||
Source source = sourceFactory.createSource(docContent);
|
||||
assertNotNull("Returned source was null", source);
|
||||
assertEquals("Expected DOMSource", DOMSource.class, source.getClass());
|
||||
assertXMLEqual("Wrong content in source ", docContent, getAsString(source));
|
||||
assertThat(source).isNotNull();
|
||||
assertThat(source).isInstanceOf(DOMSource.class);
|
||||
assertThat(XmlTestUtil.sourceToString(source)).isXmlEqualTo(docContent);
|
||||
}
|
||||
|
||||
@Test(expected = MessagingException.class)
|
||||
public void testWithUnsupportedPayload() throws Exception {
|
||||
sourceFactory.createSource(new Integer(12));
|
||||
}
|
||||
|
||||
|
||||
private String getAsString(Source source) throws Exception {
|
||||
Transformer transformer = TransformerFactory.newInstance().newTransformer();
|
||||
StringResult res = new StringResult();
|
||||
transformer.transform(source, res);
|
||||
return res.toString();
|
||||
@Test
|
||||
public void testWithUnsupportedPayload() {
|
||||
assertThatThrownBy(() -> sourceFactory.createSource(12))
|
||||
.isExactlyInstanceOf(MessagingException.class);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2016 the original author or authors.
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -16,11 +16,11 @@
|
||||
|
||||
package org.springframework.integration.xml.source;
|
||||
|
||||
import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.w3c.dom.Document;
|
||||
|
||||
@@ -28,14 +28,15 @@ import org.springframework.integration.xml.util.XmlTestUtil;
|
||||
import org.springframework.messaging.MessagingException;
|
||||
import org.springframework.xml.transform.StringSource;
|
||||
|
||||
/**
|
||||
* @author Jonas Partner
|
||||
* @author Mark Fisher
|
||||
* @author Gary Russell
|
||||
* @author Artem Bilan
|
||||
*/
|
||||
public class StringSourceTests {
|
||||
|
||||
StringSourceFactory sourceFactory;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
sourceFactory = new StringSourceFactory();
|
||||
}
|
||||
private static final StringSourceFactory sourceFactory = new StringSourceFactory();
|
||||
|
||||
@Test
|
||||
public void testWithDocument() throws Exception {
|
||||
@@ -44,7 +45,8 @@ public class StringSourceTests {
|
||||
StringSource source = (StringSource) sourceFactory.createSource(doc);
|
||||
BufferedReader reader = new BufferedReader(source.getReader());
|
||||
String docAsString = reader.readLine();
|
||||
assertXMLEqual("Wrong content in StringSource", "<?xml version=\"1.0\" encoding=\"UTF-8\"?><item>one</item>", docAsString);
|
||||
|
||||
assertThat(docAsString).isXmlEqualTo(docString);
|
||||
}
|
||||
|
||||
|
||||
@@ -54,18 +56,17 @@ public class StringSourceTests {
|
||||
StringSource source = (StringSource) sourceFactory.createSource(docString);
|
||||
BufferedReader reader = new BufferedReader(source.getReader());
|
||||
String docAsString = reader.readLine();
|
||||
assertXMLEqual("Wrong content in StringSource", "<?xml version=\"1.0\" encoding=\"UTF-8\"?><item>one</item>", docAsString);
|
||||
|
||||
assertThat(docAsString).isXmlEqualTo(docString);
|
||||
}
|
||||
|
||||
|
||||
@Test(expected = MessagingException.class)
|
||||
public void testWithUnsupportedPayload() throws Exception {
|
||||
@Test
|
||||
public void testWithUnsupportedPayload() {
|
||||
String docString = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><item>one</item>";
|
||||
StringBuffer buffer = new StringBuffer(docString);
|
||||
StringSource source = (StringSource) sourceFactory.createSource(buffer);
|
||||
BufferedReader reader = new BufferedReader(source.getReader());
|
||||
String docAsString = reader.readLine();
|
||||
assertXMLEqual("Wrong content in StringSource", "<?xml version=\"1.0\" encoding=\"UTF-8\"?><item>one</item>", docAsString);
|
||||
assertThatThrownBy(() -> sourceFactory.createSource(buffer))
|
||||
.isExactlyInstanceOf(MessagingException.class);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2017 the original author or authors.
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -16,9 +16,8 @@
|
||||
|
||||
package org.springframework.integration.xml.transformer;
|
||||
|
||||
import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
@@ -37,12 +36,13 @@ import org.springframework.xml.transform.StringResult;
|
||||
* @author Jonas Partner
|
||||
* @author Dave Turanski
|
||||
* @author Gunnar Hillert
|
||||
* @author Artem Bilan
|
||||
*/
|
||||
public class ResultToStringTransformerTests {
|
||||
|
||||
private ResultToStringTransformer transformer;
|
||||
|
||||
private String doc = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><order><orderItem>test</orderItem></order>";
|
||||
private String doc = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><order><orderItem>test</orderItem></order>";
|
||||
|
||||
|
||||
@Before
|
||||
@@ -52,11 +52,11 @@ public class ResultToStringTransformerTests {
|
||||
|
||||
@Test
|
||||
public void testWithDomResult() throws Exception {
|
||||
DOMResult result = XmlTestUtil.getDomResultForString(doc);
|
||||
DOMResult result = XmlTestUtil.getDomResultForString(this.doc);
|
||||
Object transformed = transformer.transformResult(result);
|
||||
assertTrue("Wrong transformed type expected String", transformed instanceof String);
|
||||
assertThat(transformed).isInstanceOf(String.class);
|
||||
String transformedString = (String) transformed;
|
||||
assertXMLEqual("Wrong content", doc, transformedString);
|
||||
assertThat(transformedString).isXmlEqualTo(this.doc);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -67,24 +67,24 @@ public class ResultToStringTransformerTests {
|
||||
outputProperties.setProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
|
||||
transformer.setOutputProperties(outputProperties);
|
||||
Object transformed = transformer.transformResult(domResult);
|
||||
assertTrue("Wrong transformed type expected String", transformed instanceof String);
|
||||
assertThat(transformed).isInstanceOf(String.class);
|
||||
String transformedString = (String) transformed;
|
||||
assertEquals("Wrong content", formattedDoc, transformedString);
|
||||
assertThat(transformedString).isEqualTo(formattedDoc);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWithStringResult() throws Exception {
|
||||
StringResult result = XmlTestUtil.getStringResultForString(doc);
|
||||
Object transformed = transformer.transformResult(result);
|
||||
assertTrue("Wrong transformed type expected String", transformed instanceof String);
|
||||
assertThat(transformed).isInstanceOf(String.class);
|
||||
String transformedString = (String) transformed;
|
||||
assertXMLEqual("Wrong content", doc, transformedString);
|
||||
assertThat(transformedString).isXmlEqualTo(this.doc);
|
||||
}
|
||||
|
||||
@Test(expected = MessagingException.class)
|
||||
public void testWithUnsupportedSaxResult() throws Exception {
|
||||
SAXResult result = new SAXResult();
|
||||
transformer.transformResult(result);
|
||||
@Test
|
||||
public void testWithUnsupportedSaxResult() {
|
||||
assertThatThrownBy(() -> this.transformer.transformResult(new SAXResult()))
|
||||
.isExactlyInstanceOf(MessagingException.class);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2017 the original author or authors.
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -16,9 +16,10 @@
|
||||
|
||||
package org.springframework.integration.xml.transformer;
|
||||
|
||||
import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
import javax.xml.transform.Result;
|
||||
import javax.xml.transform.TransformerException;
|
||||
@@ -34,11 +35,12 @@ import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.core.io.ByteArrayResource;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.integration.support.MessageBuilder;
|
||||
import org.springframework.integration.xml.result.DomResultFactory;
|
||||
import org.springframework.integration.xml.result.StringResultFactory;
|
||||
import org.springframework.integration.xml.util.XmlTestUtil;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.messaging.MessagingException;
|
||||
import org.springframework.messaging.support.GenericMessage;
|
||||
import org.springframework.xml.transform.StringResult;
|
||||
import org.springframework.xml.transform.StringSource;
|
||||
|
||||
@@ -48,183 +50,200 @@ import org.springframework.xml.transform.StringSource;
|
||||
* @author Oleg Zhurakousky
|
||||
* @author Gunnar Hillert
|
||||
* @author Mike Bazos
|
||||
* @author Artem Bilan
|
||||
*/
|
||||
public class XsltPayloadTransformerTests {
|
||||
|
||||
private XsltPayloadTransformer transformer;
|
||||
|
||||
private final String docAsString = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><order><orderItem>test</orderItem></order>";
|
||||
private final String docAsString =
|
||||
"<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><order><orderItem>test</orderItem></order>";
|
||||
|
||||
private final String outputAsString = "<bob>test</bob>";
|
||||
private final String outputAsString = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><bob>test</bob>";
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
transformer = new XsltPayloadTransformer(getXslResource());
|
||||
transformer.setBeanFactory(Mockito.mock(BeanFactory.class));
|
||||
transformer.afterPropertiesSet();
|
||||
public void setUp() {
|
||||
this.transformer = new XsltPayloadTransformer(getXslResource());
|
||||
this.transformer.setBeanFactory(Mockito.mock(BeanFactory.class));
|
||||
this.transformer.setAlwaysUseResultFactory(false);
|
||||
this.transformer.afterPropertiesSet();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDocumentAsPayload() throws Exception {
|
||||
Object transformed = transformer.doTransform(buildMessage(XmlTestUtil
|
||||
.getDocumentForString(docAsString)));
|
||||
assertTrue("Wrong return type for document payload", Document.class
|
||||
.isAssignableFrom(transformed.getClass()));
|
||||
Message<?> message = new GenericMessage<>(XmlTestUtil.getDocumentForString(this.docAsString));
|
||||
Object transformed = this.transformer.doTransform(message);
|
||||
assertThat(transformed)
|
||||
.as("Wrong return type for document payload")
|
||||
.isInstanceOf(Document.class);
|
||||
Document transformedDocument = (Document) transformed;
|
||||
assertXMLEqual(outputAsString, XmlTestUtil
|
||||
.docToString(transformedDocument));
|
||||
assertThat(XmlTestUtil.docToString(transformedDocument)).isXmlEqualTo(this.outputAsString);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSourceAsPayload() throws Exception {
|
||||
Object transformed = transformer
|
||||
.doTransform(buildMessage(new StringSource(docAsString)));
|
||||
assertEquals("Wrong return type for source payload", DOMResult.class,
|
||||
transformed.getClass());
|
||||
GenericMessage<?> message = new GenericMessage<>(new StringSource(this.docAsString));
|
||||
Object transformed = transformer.doTransform(message);
|
||||
|
||||
assertThat(transformed)
|
||||
.as("Wrong return type for document payload")
|
||||
.isInstanceOf(DOMResult.class);
|
||||
|
||||
DOMResult result = (DOMResult) transformed;
|
||||
assertXMLEqual("Document incorrect after transformation", XmlTestUtil
|
||||
.getDocumentForString(outputAsString), (Document) result
|
||||
.getNode());
|
||||
assertThat(XmlTestUtil.docToString((Document) result.getNode()))
|
||||
.as("Document incorrect after transformation")
|
||||
.isXmlEqualTo(this.outputAsString);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStringAsPayload() throws Exception {
|
||||
Object transformed = transformer.doTransform(buildMessage(docAsString));
|
||||
assertEquals("Wrong return type for string payload", String.class,
|
||||
transformed.getClass());
|
||||
Object transformed = this.transformer.doTransform(new GenericMessage<>(this.docAsString));
|
||||
|
||||
assertThat(transformed)
|
||||
.as("Wrong return type for document payload")
|
||||
.isInstanceOf(String.class);
|
||||
|
||||
String transformedString = (String) transformed;
|
||||
assertXMLEqual("String incorrect after transform", outputAsString,
|
||||
transformedString);
|
||||
assertThat(transformedString)
|
||||
.as("String incorrect after transform")
|
||||
.isXmlEqualTo(this.outputAsString);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStringAsPayloadUseResultFactoryTrue() throws Exception {
|
||||
transformer.setAlwaysUseResultFactory(true);
|
||||
Object transformed = transformer.doTransform(buildMessage(docAsString));
|
||||
assertEquals("Wrong return type for useFactories true",
|
||||
DOMResult.class, transformed.getClass());
|
||||
this.transformer.setAlwaysUseResultFactory(true);
|
||||
Object transformed = transformer.doTransform(new GenericMessage<>(this.docAsString));
|
||||
|
||||
assertThat(transformed)
|
||||
.as("Wrong return type for useFactories true")
|
||||
.isInstanceOf(DOMResult.class);
|
||||
|
||||
DOMResult result = (DOMResult) transformed;
|
||||
assertXMLEqual("Document incorrect after transformation", XmlTestUtil
|
||||
.getDocumentForString(outputAsString), (Document) result
|
||||
.getNode());
|
||||
assertThat(XmlTestUtil.docToString((Document) result.getNode()))
|
||||
.as("Document incorrect after transformation")
|
||||
.isXmlEqualTo(this.outputAsString);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSourceWithResultTransformer() throws Exception {
|
||||
Integer returnValue = new Integer(13);
|
||||
transformer = new XsltPayloadTransformer(getXslResource(),
|
||||
Integer returnValue = 13;
|
||||
XsltPayloadTransformer transformer = new XsltPayloadTransformer(getXslResource(),
|
||||
new StubResultTransformer(returnValue));
|
||||
transformer.setBeanFactory(Mockito.mock(BeanFactory.class));
|
||||
transformer.afterPropertiesSet();
|
||||
Object transformed = transformer
|
||||
.doTransform(buildMessage(new StringSource(docAsString)));
|
||||
assertEquals("Wrong value from result conversion", returnValue,
|
||||
transformed);
|
||||
.doTransform(new GenericMessage<>(new StringSource(docAsString)));
|
||||
assertThat(transformed).isEqualTo(returnValue);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testXsltPayloadWithTransformerFactoryClassname() throws Exception {
|
||||
Integer returnValue = new Integer(13);
|
||||
transformer = new XsltPayloadTransformer(getXslResource(), new StubResultTransformer(returnValue),
|
||||
"com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl");
|
||||
Integer returnValue = 13;
|
||||
XsltPayloadTransformer transformer =
|
||||
new XsltPayloadTransformer(getXslResource(), new StubResultTransformer(returnValue),
|
||||
"com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl");
|
||||
transformer.setBeanFactory(Mockito.mock(BeanFactory.class));
|
||||
transformer.afterPropertiesSet();
|
||||
Object transformed = transformer
|
||||
.doTransform(buildMessage(new StringSource(docAsString)));
|
||||
assertEquals("Wrong value from result conversion", returnValue,
|
||||
transformed);
|
||||
Object transformed = transformer.doTransform(new GenericMessage<>(new StringSource(this.docAsString)));
|
||||
assertThat(transformed)
|
||||
.as("Wrong value from result conversion")
|
||||
.isEqualTo(returnValue);
|
||||
}
|
||||
|
||||
@Test(expected = TransformerFactoryConfigurationError.class)
|
||||
public void testXsltPayloadWithBadTransformerFactoryClassname() throws Exception {
|
||||
transformer = new XsltPayloadTransformer(getXslResource(), "foo.bar.Baz");
|
||||
@Test
|
||||
public void testXsltPayloadWithBadTransformerFactoryClassname() {
|
||||
XsltPayloadTransformer transformer = new XsltPayloadTransformer(getXslResource(), "foo.bar.Baz");
|
||||
transformer.setBeanFactory(Mockito.mock(BeanFactory.class));
|
||||
transformer.afterPropertiesSet();
|
||||
transformer.doTransform(buildMessage(new StringSource(docAsString)));
|
||||
assertThatThrownBy(transformer::afterPropertiesSet)
|
||||
.isExactlyInstanceOf(TransformerFactoryConfigurationError.class);
|
||||
}
|
||||
|
||||
@Test(expected = TransformerException.class)
|
||||
public void testNonXmlString() throws Exception {
|
||||
transformer.doTransform(buildMessage("test"));
|
||||
@Test
|
||||
public void testNonXmlString() {
|
||||
assertThatThrownBy(() -> this.transformer.doTransform(new GenericMessage<>("test")))
|
||||
.isExactlyInstanceOf(TransformerException.class);
|
||||
}
|
||||
|
||||
@Test(expected = MessagingException.class)
|
||||
public void testUnsupportedPayloadType() throws Exception {
|
||||
transformer.doTransform(buildMessage(new Long(12)));
|
||||
@Test
|
||||
public void testUnsupportedPayloadType() {
|
||||
assertThatThrownBy(() -> this.transformer.doTransform(new GenericMessage<>(12)))
|
||||
.isExactlyInstanceOf(MessagingException.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testXsltWithImports() throws Exception {
|
||||
Resource resource = new ClassPathResource("transform-with-import.xsl",
|
||||
this.getClass());
|
||||
transformer = new XsltPayloadTransformer(resource);
|
||||
Resource resource = new ClassPathResource("transform-with-import.xsl", getClass());
|
||||
XsltPayloadTransformer transformer = new XsltPayloadTransformer(resource);
|
||||
transformer.setBeanFactory(Mockito.mock(BeanFactory.class));
|
||||
transformer.afterPropertiesSet();
|
||||
assertEquals(transformer.doTransform(buildMessage(docAsString)),
|
||||
outputAsString);
|
||||
Object transformed = transformer.doTransform(new GenericMessage<>(this.docAsString));
|
||||
assertThat(transformed).isEqualTo(this.outputAsString);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void documentInStringResultOut() throws Exception {
|
||||
Resource resource = new ClassPathResource("transform-with-import.xsl",
|
||||
this.getClass());
|
||||
transformer = new XsltPayloadTransformer(resource);
|
||||
Resource resource = new ClassPathResource("transform-with-import.xsl", getClass());
|
||||
XsltPayloadTransformer transformer = new XsltPayloadTransformer(resource);
|
||||
transformer.setResultFactory(new StringResultFactory());
|
||||
transformer.setAlwaysUseResultFactory(true);
|
||||
transformer.setBeanFactory(Mockito.mock(BeanFactory.class));
|
||||
transformer.afterPropertiesSet();
|
||||
Object returned = transformer.doTransform(buildMessage(XmlTestUtil.getDocumentForString(docAsString)));
|
||||
assertEquals("Wrong type of return ", StringResult.class, returned.getClass());
|
||||
GenericMessage<Document> message = new GenericMessage<>(XmlTestUtil.getDocumentForString(this.docAsString));
|
||||
Object transformed = transformer.doTransform(message);
|
||||
assertThat(transformed)
|
||||
.as("Wrong type of return")
|
||||
.isInstanceOf(StringResult.class);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void stringInDomResultOut() throws Exception {
|
||||
Resource resource = new ClassPathResource("transform-with-import.xsl",
|
||||
this.getClass());
|
||||
transformer = new XsltPayloadTransformer(resource);
|
||||
transformer.setResultFactory(new StringResultFactory());
|
||||
Resource resource = new ClassPathResource("transform-with-import.xsl", getClass());
|
||||
XsltPayloadTransformer transformer = new XsltPayloadTransformer(resource);
|
||||
transformer.setResultFactory(new DomResultFactory());
|
||||
transformer.setAlwaysUseResultFactory(true);
|
||||
transformer.setBeanFactory(Mockito.mock(BeanFactory.class));
|
||||
transformer.afterPropertiesSet();
|
||||
Object returned = transformer.doTransform(buildMessage(XmlTestUtil.getDocumentForString(docAsString)));
|
||||
assertEquals("Wrong type of return ", StringResult.class, returned.getClass());
|
||||
GenericMessage<Document> message = new GenericMessage<>(XmlTestUtil.getDocumentForString(this.docAsString));
|
||||
Object transformed = transformer.doTransform(message);
|
||||
assertThat(transformed)
|
||||
.as("Wrong type of return")
|
||||
.isInstanceOf(DOMResult.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void docInStringOut() throws Exception {
|
||||
transformer = new XsltPayloadTransformer(getXslResourceThatOutputsText());
|
||||
XsltPayloadTransformer transformer = new XsltPayloadTransformer(getXslResourceThatOutputsText());
|
||||
transformer.setResultFactory(new StringResultFactory());
|
||||
transformer.setAlwaysUseResultFactory(true);
|
||||
transformer.setBeanFactory(Mockito.mock(BeanFactory.class));
|
||||
transformer.afterPropertiesSet();
|
||||
Object returned = transformer.doTransform(buildMessage(XmlTestUtil.getDocumentForString(docAsString)));
|
||||
assertEquals("Wrong type of return ", StringResult.class, returned.getClass());
|
||||
assertEquals("Wrong content in string", "hello world", returned.toString());
|
||||
GenericMessage<Document> message = new GenericMessage<>(XmlTestUtil.getDocumentForString(this.docAsString));
|
||||
Object transformed = transformer.doTransform(message);
|
||||
assertThat(transformed)
|
||||
.as("Wrong type of return")
|
||||
.isInstanceOf(StringResult.class);
|
||||
assertThat(transformed.toString()).isEqualTo("hello world");
|
||||
}
|
||||
|
||||
protected Message<?> buildMessage(Object payload) {
|
||||
return MessageBuilder.withPayload(payload).build();
|
||||
}
|
||||
|
||||
private Resource getXslResource() throws Exception {
|
||||
private Resource getXslResource() {
|
||||
String xsl = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>" +
|
||||
"<xsl:stylesheet version=\"1.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\">" +
|
||||
" <xsl:template match=\"order\">" +
|
||||
" <bob>test</bob>" +
|
||||
" </xsl:template>" +
|
||||
"</xsl:stylesheet>";
|
||||
return new ByteArrayResource(xsl.getBytes("UTF-8"));
|
||||
return new ByteArrayResource(xsl.getBytes(StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
private Resource getXslResourceThatOutputsText() throws Exception {
|
||||
private Resource getXslResourceThatOutputsText() {
|
||||
String xsl = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>" +
|
||||
"<xsl:stylesheet version=\"1.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\">" +
|
||||
" <xsl:output method=\"text\" encoding=\"UTF-8\" />" +
|
||||
" <xsl:template match=\"order\">hello world</xsl:template>" +
|
||||
"</xsl:stylesheet>";
|
||||
return new ByteArrayResource(xsl.getBytes("UTF-8"));
|
||||
return new ByteArrayResource(xsl.getBytes(StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
public static class StubResultTransformer implements ResultTransformer {
|
||||
@@ -238,6 +257,7 @@ public class XsltPayloadTransformerTests {
|
||||
public Object transformResult(Result result) {
|
||||
return objectToReturn;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
|
||||
<xsl:output omit-xml-declaration="yes"/>
|
||||
<xsl:output/>
|
||||
<xsl:template match="order">
|
||||
<bob>test</bob>
|
||||
</xsl:template>
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2016 the original author or authors.
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -34,9 +34,12 @@ import org.springframework.xml.transform.StringResult;
|
||||
* Utility class for XML related testing
|
||||
*
|
||||
* @author Jonas Partner
|
||||
* @author Artem Bilan
|
||||
*/
|
||||
public class XmlTestUtil {
|
||||
|
||||
private static final TransformerFactory TRANSFORMER_FACTORY = TransformerFactory.newInstance();
|
||||
|
||||
private XmlTestUtil() {
|
||||
super();
|
||||
}
|
||||
@@ -74,9 +77,13 @@ public class XmlTestUtil {
|
||||
return stringResult.toString();
|
||||
}
|
||||
|
||||
|
||||
public static void transform(Source source, Result res) throws Exception {
|
||||
TransformerFactory.newInstance().newTransformer().transform(source, res);
|
||||
TRANSFORMER_FACTORY.newTransformer().transform(source, res);
|
||||
}
|
||||
|
||||
public static String sourceToString(Source source) throws Exception {
|
||||
StringResult res = new StringResult();
|
||||
transform(source, res);
|
||||
return res.toString();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user