Applied Lukas' patch

This commit is contained in:
Arjen Poutsma
2010-06-30 08:01:36 +00:00
parent 93bf7ad65f
commit 4bc5bfd452
4 changed files with 57 additions and 95 deletions

View File

@@ -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) {

View File

@@ -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();
}
}

View File

@@ -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 = "<request xmlns='http://example.com'/>";
String response = "<response xmlns='http://example.com'/>";
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 = "<request1 xmlns='http://example.com'/>";

View File

@@ -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 = "<element xmlns='http://example.com'/>";
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 = "<element2 xmlns='http://example.com'/>";
PayloadMatcher matcher = PayloadMatcher.createStringPayloadMatcher(expected);
PayloadMatcher matcher = new PayloadMatcher(new StringSource(expected));
matcher.match(message);
}
@Test
public void resourcePayload() throws Exception {
String xml = "<element xmlns='http://example.com'/>";
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 = "<element xmlns='http://example.com'/>";
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);
}
}