From 4bc5bfd452af0f577d548224ad1682c916640786 Mon Sep 17 00:00:00 2001 From: Arjen Poutsma Date: Wed, 30 Jun 2010 08:01:36 +0000 Subject: [PATCH] Applied Lukas' patch --- .../ws/mock/client/MockSenderConnection.java | 17 +++-- .../ws/mock/client/PayloadMatcher.java | 75 +++++-------------- .../MockWebServiceMessageSenderTest.java | 24 ++++++ .../ws/mock/client/PayloadMatcherTest.java | 36 +-------- 4 files changed, 57 insertions(+), 95 deletions(-) diff --git a/test/src/main/java/org/springframework/ws/mock/client/MockSenderConnection.java b/test/src/main/java/org/springframework/ws/mock/client/MockSenderConnection.java index 5d3f8e93..fd5dd877 100644 --- a/test/src/main/java/org/springframework/ws/mock/client/MockSenderConnection.java +++ b/test/src/main/java/org/springframework/ws/mock/client/MockSenderConnection.java @@ -30,7 +30,8 @@ import org.springframework.ws.WebServiceMessage; import org.springframework.ws.WebServiceMessageFactory; import org.springframework.ws.transport.FaultAwareWebServiceConnection; import org.springframework.ws.transport.WebServiceConnection; -import org.springframework.xml.transform.TransformerObjectSupport; +import org.springframework.xml.transform.ResourceSource; +import org.springframework.xml.transform.StringSource; import static org.junit.Assert.fail; @@ -42,8 +43,7 @@ import static org.junit.Assert.fail; * @author Lukas Krecan * @since 2.0 */ -class MockSenderConnection extends TransformerObjectSupport - implements FaultAwareWebServiceConnection, RequestExpectations, ResponseActions { +class MockSenderConnection implements FaultAwareWebServiceConnection, RequestExpectations, ResponseActions { private static final URI ANY_URI = URI.create("ANY"); @@ -69,17 +69,22 @@ class MockSenderConnection extends TransformerObjectSupport public ResponseActions expectPayload(String payload) { Assert.notNull(payload, "'payload' must not be null"); - return addRequestMatcher(PayloadMatcher.createStringPayloadMatcher(payload)); + return addRequestMatcher(new PayloadMatcher(new StringSource(payload))); } public ResponseActions expectPayload(Source payload) { Assert.notNull(payload, "'payload' must not be null"); - return addRequestMatcher(PayloadMatcher.createSourcePayloadMatcher(payload)); + return addRequestMatcher(new PayloadMatcher(payload)); } public ResponseActions expectPayload(Resource payload) { Assert.notNull(payload, "'payload' must not be null"); - return addRequestMatcher(PayloadMatcher.createResourcePayloadMatcher(payload)); + try { + return addRequestMatcher(new PayloadMatcher(new ResourceSource(payload))); + } + catch (IOException ex) { + throw new IllegalArgumentException(payload + " could not be opened", ex); + } } public ResponseActions expectSoapHeader(QName soapHeaderName) { diff --git a/test/src/main/java/org/springframework/ws/mock/client/PayloadMatcher.java b/test/src/main/java/org/springframework/ws/mock/client/PayloadMatcher.java index 7ad3fbad..86bc7865 100644 --- a/test/src/main/java/org/springframework/ws/mock/client/PayloadMatcher.java +++ b/test/src/main/java/org/springframework/ws/mock/client/PayloadMatcher.java @@ -16,32 +16,33 @@ package org.springframework.ws.mock.client; -import java.io.IOException; import javax.xml.transform.Source; import javax.xml.transform.TransformerException; import javax.xml.transform.dom.DOMResult; -import javax.xml.transform.dom.DOMSource; -import org.springframework.core.io.Resource; +import org.springframework.util.Assert; import org.springframework.ws.WebServiceMessage; -import org.springframework.xml.sax.SaxUtils; -import org.springframework.xml.transform.StringResult; import org.custommonkey.xmlunit.Diff; -import org.custommonkey.xmlunit.XMLUnit; import org.w3c.dom.Document; -import org.xml.sax.InputSource; -import org.xml.sax.SAXException; -import static org.custommonkey.xmlunit.XMLAssert.fail; +import static junit.framework.Assert.fail; /** * Abstract base class that matches payloads. * * @author Arjen Poutsma + * @author Lukas Krecan * @since 2.0 */ -abstract class PayloadMatcher extends DiffMatcher { +class PayloadMatcher extends DiffMatcher { + + private final Source expected; + + PayloadMatcher(Source expected) { + Assert.notNull(expected, "'expected' must not be null"); + this.expected = expected; + } @Override protected final Diff createDiff(WebServiceMessage request) throws Exception { @@ -52,53 +53,15 @@ abstract class PayloadMatcher extends DiffMatcher { return createDiff(payload); } - protected abstract Diff createDiff(Source payload) throws Exception; - - public static PayloadMatcher createStringPayloadMatcher(final String control) { - return new PayloadMatcher() { - @Override - protected Diff createDiff(Source payload) throws TransformerException, IOException, SAXException { - StringResult result = new StringResult(); - transform(payload, result); - return new Diff(control, result.toString()); - } - }; + protected Diff createDiff(Source payload) throws TransformerException { + Document expectedDocument = createDocumentFromSource(expected); + Document actualDocument = createDocumentFromSource(payload); + return new Diff(expectedDocument, actualDocument); } - public static PayloadMatcher createResourcePayloadMatcher(final Resource control) { - return new PayloadMatcher() { - @Override - protected Diff createDiff(Source payload) throws IOException, SAXException, TransformerException { - InputSource controlInputSource = SaxUtils.createInputSource(control); - Document controlDocument = XMLUnit.buildDocument(XMLUnit.newControlParser(), controlInputSource); - DOMSource controlSource = new DOMSource(controlDocument); - - DOMResult result = new DOMResult(); - transform(payload, result); - DOMSource resultSource = new DOMSource(result.getNode()); - - return new Diff(controlSource, resultSource); - } - }; - } - - public static PayloadMatcher createSourcePayloadMatcher(final Source control) { - return new PayloadMatcher() { - @Override - protected Diff createDiff(Source payload) throws Exception { - if (control instanceof DOMSource && payload instanceof DOMSource) { - return new Diff((DOMSource) control, (DOMSource) payload); - } - DOMResult controlResult = new DOMResult(); - transform(control, controlResult); - DOMSource controlSource = new DOMSource(controlResult.getNode()); - - DOMResult payloadResult = new DOMResult(); - transform(payload, payloadResult); - DOMSource payloadSource = new DOMSource(payloadResult.getNode()); - - return new Diff(controlSource, payloadSource); - } - }; + private Document createDocumentFromSource(Source source) throws TransformerException { + DOMResult result = new DOMResult(); + transform(source, result); + return (Document) result.getNode(); } } diff --git a/test/src/test/java/org/springframework/ws/mock/client/MockWebServiceMessageSenderTest.java b/test/src/test/java/org/springframework/ws/mock/client/MockWebServiceMessageSenderTest.java index cb96e2a5..f551913a 100644 --- a/test/src/test/java/org/springframework/ws/mock/client/MockWebServiceMessageSenderTest.java +++ b/test/src/test/java/org/springframework/ws/mock/client/MockWebServiceMessageSenderTest.java @@ -16,7 +16,10 @@ package org.springframework.ws.mock.client; +import java.io.ByteArrayInputStream; import java.io.IOException; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.transform.dom.DOMSource; import org.springframework.ws.client.WebServiceIOException; import org.springframework.ws.client.WebServiceTransportException; @@ -25,8 +28,10 @@ import org.springframework.ws.soap.client.SoapFaultClientException; import org.springframework.xml.transform.StringResult; import org.springframework.xml.transform.StringSource; +import org.custommonkey.xmlunit.XMLUnit; import org.junit.Before; import org.junit.Test; +import org.w3c.dom.Document; import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual; @@ -42,6 +47,7 @@ public class MockWebServiceMessageSenderTest { template = new WebServiceTemplate(); template.setMessageSender(messageSender); template.setDefaultUri("http://example.com/airline"); + XMLUnit.setIgnoreWhitespace(true); } @Test @@ -58,6 +64,24 @@ public class MockWebServiceMessageSenderTest { messageSender.verify(); } + @Test + public void normalDomSource() throws Exception { + String request = ""; + String response = ""; + + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + Document document = factory.newDocumentBuilder().parse(new ByteArrayInputStream(request.getBytes())); + + messageSender.whenConnecting().expectPayload(new DOMSource(document)).andRespondWithPayload(response); + messageSender.replay(); + + + StringResult result = new StringResult(); + template.sendSourceAndReceiveToResult(new StringSource(request), result); + assertXMLEqual(result.toString(), response); + messageSender.verify(); + } + @Test(expected = AssertionError.class) public void invalidRequest() throws Exception { String expectedRequest = ""; diff --git a/test/src/test/java/org/springframework/ws/mock/client/PayloadMatcherTest.java b/test/src/test/java/org/springframework/ws/mock/client/PayloadMatcherTest.java index 7b6c82ee..3bf69d74 100644 --- a/test/src/test/java/org/springframework/ws/mock/client/PayloadMatcherTest.java +++ b/test/src/test/java/org/springframework/ws/mock/client/PayloadMatcherTest.java @@ -16,8 +16,6 @@ package org.springframework.ws.mock.client; -import org.springframework.core.io.ByteArrayResource; -import org.springframework.core.io.Resource; import org.springframework.ws.WebServiceMessage; import org.springframework.xml.transform.StringSource; @@ -28,13 +26,13 @@ import static org.easymock.EasyMock.*; public class PayloadMatcherTest { @Test - public void stringPayloadMatch() throws Exception { + public void match() throws Exception { String xml = ""; WebServiceMessage message = createMock(WebServiceMessage.class); expect(message.getPayloadSource()).andReturn(new StringSource(xml)); replay(message); - PayloadMatcher matcher = PayloadMatcher.createStringPayloadMatcher(xml); + PayloadMatcher matcher = new PayloadMatcher(new StringSource(xml)); matcher.match(message); verify(message); @@ -48,36 +46,8 @@ public class PayloadMatcherTest { replay(message); String expected = ""; - PayloadMatcher matcher = PayloadMatcher.createStringPayloadMatcher(expected); + PayloadMatcher matcher = new PayloadMatcher(new StringSource(expected)); matcher.match(message); } - @Test - public void resourcePayload() throws Exception { - String xml = ""; - WebServiceMessage message = createMock(WebServiceMessage.class); - expect(message.getPayloadSource()).andReturn(new StringSource(xml)); - replay(message); - - Resource resource = new ByteArrayResource(xml.getBytes()); - PayloadMatcher matcher = PayloadMatcher.createResourcePayloadMatcher(resource); - matcher.match(message); - - verify(message); - } - - - @Test - public void sourcePayloadMatcher() throws Exception { - String xml = ""; - WebServiceMessage message = createMock(WebServiceMessage.class); - expect(message.getPayloadSource()).andReturn(new StringSource(xml)); - replay(message); - - Resource resource = new ByteArrayResource(xml.getBytes()); - PayloadMatcher matcher = PayloadMatcher.createSourcePayloadMatcher(new StringSource(xml)); - matcher.match(message); - - verify(message); - } }