From 3f0e9f7d449a1a117516c4676586d340a2f6204f Mon Sep 17 00:00:00 2001 From: Arjen Poutsma Date: Tue, 2 Nov 2010 12:13:27 +0000 Subject: [PATCH] SWS-632 - Renamed Assert to AssertionErrors, to reduce confusion with JUnit's and Spring's Assert classes. - Added SOAP fault expectations --- .../test/client/DefaultXPathExpectations.java | 4 +- .../test/client/SoapFaultResponseCreator.java | 2 +- .../ws/test/client/SoapHeaderMatcher.java | 4 +- .../ws/test/client/UriMatcher.java | 2 +- .../ws/test/server/MockWebServiceClient.java | 2 +- .../ws/test/server/ResponseMatchers.java | 160 ++++++++++++++++++ .../{Assert.java => AssertionErrors.java} | 5 +- .../ws/test/support/DiffMatcher.java | 4 +- .../ws/test/support/PayloadDiffMatcher.java | 2 +- 9 files changed, 174 insertions(+), 11 deletions(-) rename test/src/main/java/org/springframework/ws/test/support/{Assert.java => AssertionErrors.java} (96%) diff --git a/test/src/main/java/org/springframework/ws/test/client/DefaultXPathExpectations.java b/test/src/main/java/org/springframework/ws/test/client/DefaultXPathExpectations.java index 488fa114..9f4da9a9 100644 --- a/test/src/main/java/org/springframework/ws/test/client/DefaultXPathExpectations.java +++ b/test/src/main/java/org/springframework/ws/test/client/DefaultXPathExpectations.java @@ -30,8 +30,8 @@ import org.springframework.xml.xpath.XPathExpressionFactory; import org.w3c.dom.Node; -import static org.springframework.ws.test.support.Assert.assertEquals; -import static org.springframework.ws.test.support.Assert.fail; +import static org.springframework.ws.test.support.AssertionErrors.assertEquals; +import static org.springframework.ws.test.support.AssertionErrors.fail; /** * Default implementation of {@link XPathExpectations}. diff --git a/test/src/main/java/org/springframework/ws/test/client/SoapFaultResponseCreator.java b/test/src/main/java/org/springframework/ws/test/client/SoapFaultResponseCreator.java index b9080037..d8f192f8 100644 --- a/test/src/main/java/org/springframework/ws/test/client/SoapFaultResponseCreator.java +++ b/test/src/main/java/org/springframework/ws/test/client/SoapFaultResponseCreator.java @@ -23,7 +23,7 @@ import java.util.Locale; import org.springframework.ws.soap.SoapBody; import org.springframework.ws.soap.SoapMessage; -import static org.springframework.ws.test.support.Assert.fail; +import static org.springframework.ws.test.support.AssertionErrors.fail; /** * Implementation of {@link ResponseCreator} that responds with a SOAP fault. diff --git a/test/src/main/java/org/springframework/ws/test/client/SoapHeaderMatcher.java b/test/src/main/java/org/springframework/ws/test/client/SoapHeaderMatcher.java index 9dbb6180..4dd31de3 100644 --- a/test/src/main/java/org/springframework/ws/test/client/SoapHeaderMatcher.java +++ b/test/src/main/java/org/springframework/ws/test/client/SoapHeaderMatcher.java @@ -26,8 +26,8 @@ import org.springframework.ws.soap.SoapHeader; import org.springframework.ws.soap.SoapHeaderElement; import org.springframework.ws.soap.SoapMessage; -import static org.springframework.ws.test.support.Assert.assertTrue; -import static org.springframework.ws.test.support.Assert.fail; +import static org.springframework.ws.test.support.AssertionErrors.assertTrue; +import static org.springframework.ws.test.support.AssertionErrors.fail; /** * Matches SOAP headers. diff --git a/test/src/main/java/org/springframework/ws/test/client/UriMatcher.java b/test/src/main/java/org/springframework/ws/test/client/UriMatcher.java index 147f42f5..c3574717 100644 --- a/test/src/main/java/org/springframework/ws/test/client/UriMatcher.java +++ b/test/src/main/java/org/springframework/ws/test/client/UriMatcher.java @@ -20,7 +20,7 @@ import java.net.URI; import org.springframework.ws.WebServiceMessage; -import static org.springframework.ws.test.support.Assert.assertEquals; +import static org.springframework.ws.test.support.AssertionErrors.assertEquals; /** * Matches {@link URI}s. diff --git a/test/src/main/java/org/springframework/ws/test/server/MockWebServiceClient.java b/test/src/main/java/org/springframework/ws/test/server/MockWebServiceClient.java index 56ef6615..99160750 100644 --- a/test/src/main/java/org/springframework/ws/test/server/MockWebServiceClient.java +++ b/test/src/main/java/org/springframework/ws/test/server/MockWebServiceClient.java @@ -34,7 +34,7 @@ import org.springframework.ws.transport.WebServiceMessageReceiver; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import static org.springframework.ws.test.support.Assert.fail; +import static org.springframework.ws.test.support.AssertionErrors.fail; /** * Main entry point for server-side Web service testing. Typically used to test a {@link diff --git a/test/src/main/java/org/springframework/ws/test/server/ResponseMatchers.java b/test/src/main/java/org/springframework/ws/test/server/ResponseMatchers.java index 4a905624..4785515a 100644 --- a/test/src/main/java/org/springframework/ws/test/server/ResponseMatchers.java +++ b/test/src/main/java/org/springframework/ws/test/server/ResponseMatchers.java @@ -17,13 +17,22 @@ package org.springframework.ws.test.server; import java.io.IOException; +import java.util.Locale; +import javax.xml.namespace.QName; import javax.xml.transform.Source; import org.springframework.core.io.Resource; import org.springframework.ws.WebServiceMessage; +import org.springframework.ws.soap.SoapBody; +import org.springframework.ws.soap.SoapFault; +import org.springframework.ws.soap.SoapMessage; +import org.springframework.ws.soap.SoapVersion; import org.springframework.ws.test.support.PayloadDiffMatcher; import org.springframework.xml.transform.ResourceSource; +import static org.springframework.ws.test.support.AssertionErrors.assertEquals; +import static org.springframework.ws.test.support.AssertionErrors.fail; + /** * Factory methods for {@link ResponseMatcher} classes. Typically used to provide input for {@link * ResponseActions#andExpect(ResponseMatcher)}. @@ -48,6 +57,8 @@ public abstract class ResponseMatchers { }; } + // Payload + /** * Expects the given {@link Source} XML payload. * @@ -73,4 +84,153 @@ public abstract class ResponseMatchers { return payload(new ResourceSource(payload)); } + // SOAP Fault + + /** + * Expects a {@code MustUnderstand} fault. + * + * @see org.springframework.ws.soap.SoapBody#addMustUnderstandFault(String, Locale) + */ + public static ResponseMatcher mustUnderstandFault() { + return mustUnderstandFault(null); + } + + /** + * Expects a {@code MustUnderstand} fault with a particular fault string or reason. + * + * @param faultStringOrReason the SOAP 1.1 fault string or SOAP 1.2 reason text. If {@code null} the fault string or + * reason text will not be verified + * @see org.springframework.ws.soap.SoapBody#addMustUnderstandFault(String, Locale) + */ + public static ResponseMatcher mustUnderstandFault(String faultStringOrReason) { + return new SoapFaultResponseMatcher(faultStringOrReason) { + @Override + protected QName getExpectedFaultCode(SoapVersion version) { + return version.getMustUnderstandFaultName(); + } + }; + } + + /** + * Expects a {@code Client} (SOAP 1.1) or {@code Sender} (SOAP 1.2) fault. + * + * @see org.springframework.ws.soap.SoapBody#addClientOrSenderFault(String, Locale) + */ + public static ResponseMatcher clientOrSenderFault() { + return clientOrSenderFault(null); + } + + /** + * Expects a {@code Client} (SOAP 1.1) or {@code Sender} (SOAP 1.2) fault with a particular fault string or reason. + * + * @param faultStringOrReason the SOAP 1.1 fault string or SOAP 1.2 reason text. If {@code null} the fault string or + * reason text will not be verified + * @see org.springframework.ws.soap.SoapBody#addClientOrSenderFault(String, Locale) + */ + public static ResponseMatcher clientOrSenderFault(String faultStringOrReason) { + return new SoapFaultResponseMatcher(faultStringOrReason) { + @Override + protected QName getExpectedFaultCode(SoapVersion version) { + return version.getClientOrSenderFaultName(); + } + }; + } + + /** + * Expects a {@code Server} (SOAP 1.1) or {@code Receiver} (SOAP 1.2) fault. + * + * @see org.springframework.ws.soap.SoapBody#addServerOrReceiverFault(String, java.util.Locale) + */ + public static ResponseMatcher serverOrReceiverFault() { + return serverOrReceiverFault(null); + } + + /** + * Expects a {@code Server} (SOAP 1.1) or {@code Receiver} (SOAP 1.2) fault with a particular fault string or reason. + * + * @param faultStringOrReason the SOAP 1.1 fault string or SOAP 1.2 reason text. If {@code null} the fault string or + * reason text will not be verified + * @see org.springframework.ws.soap.SoapBody#addClientOrSenderFault(String, Locale) + */ + public static ResponseMatcher serverOrReceiverFault(String faultStringOrReason) { + return new SoapFaultResponseMatcher(faultStringOrReason) { + @Override + protected QName getExpectedFaultCode(SoapVersion version) { + return version.getServerOrReceiverFaultName(); + } + }; + } + + /** + * Expects a {@code VersionMismatch} fault. + * + * @see org.springframework.ws.soap.SoapBody#addVersionMismatchFault(String, java.util.Locale) + */ + public static ResponseMatcher versionMismatchFault() { + return versionMismatchFault(null); + } + + /** + * Expects a {@code VersionMismatch} fault with a particular fault string or reason. + * + * @param faultStringOrReason the SOAP 1.1 fault string or SOAP 1.2 reason text. If {@code null} the fault string or + * reason text will not be verified + * @see org.springframework.ws.soap.SoapBody#addClientOrSenderFault(String, Locale) + */ + public static ResponseMatcher versionMismatchFault(String faultStringOrReason) { + return new SoapFaultResponseMatcher(faultStringOrReason) { + @Override + protected QName getExpectedFaultCode(SoapVersion version) { + return version.getVersionMismatchFaultName(); + } + }; + } + + + private static abstract class SoapResponseMatcher implements ResponseMatcher { + + public final void match(WebServiceMessage response) throws IOException, AssertionError { + if (!(response instanceof SoapMessage)) { + fail("Response is not a SOAP message"); + return; + } + SoapMessage soapResponse = (SoapMessage) response; + match(soapResponse); + } + + protected abstract void match(SoapMessage response) throws IOException, AssertionError; + + } + private static abstract class SoapFaultResponseMatcher extends SoapResponseMatcher { + + private final String expectedFaultStringOrReason; + + protected SoapFaultResponseMatcher(String expectedFaultStringOrReason) { + this.expectedFaultStringOrReason = expectedFaultStringOrReason; + } + + @Override + protected void match(SoapMessage response) throws IOException, AssertionError { + SoapBody responseBody = response.getSoapBody(); + if (responseBody == null) { + fail("Response has no SOAP Body"); + return; + } + if (!responseBody.hasFault()) { + fail("Response has no SOAP Fault"); + return; + } + SoapFault soapFault = responseBody.getFault(); + QName expectedFaultCode = getExpectedFaultCode(response.getVersion()); + assertEquals("Invalid SOAP Fault code", expectedFaultCode, soapFault.getFaultCode()); + if (expectedFaultStringOrReason != null) { + assertEquals("Invalid SOAP Fault string/reason", expectedFaultStringOrReason, + soapFault.getFaultStringOrReason()); + } + } + + protected abstract QName getExpectedFaultCode(SoapVersion version); + + } + } diff --git a/test/src/main/java/org/springframework/ws/test/support/Assert.java b/test/src/main/java/org/springframework/ws/test/support/AssertionErrors.java similarity index 96% rename from test/src/main/java/org/springframework/ws/test/support/Assert.java rename to test/src/main/java/org/springframework/ws/test/support/AssertionErrors.java index 9e6f7985..4035304d 100644 --- a/test/src/main/java/org/springframework/ws/test/support/Assert.java +++ b/test/src/main/java/org/springframework/ws/test/support/AssertionErrors.java @@ -23,7 +23,10 @@ package org.springframework.ws.test.support; * @author Arjen Poutsma * @since 2.0 */ -public class Assert { +public abstract class AssertionErrors { + + private AssertionErrors() { + } /** * Fails a test with the given message. diff --git a/test/src/main/java/org/springframework/ws/test/support/DiffMatcher.java b/test/src/main/java/org/springframework/ws/test/support/DiffMatcher.java index fff9d035..ff0251c8 100644 --- a/test/src/main/java/org/springframework/ws/test/support/DiffMatcher.java +++ b/test/src/main/java/org/springframework/ws/test/support/DiffMatcher.java @@ -23,8 +23,8 @@ import org.springframework.ws.WebServiceMessage; import org.custommonkey.xmlunit.Diff; import org.custommonkey.xmlunit.XMLUnit; -import static org.springframework.ws.test.support.Assert.assertTrue; -import static org.springframework.ws.test.support.Assert.fail; +import static org.springframework.ws.test.support.AssertionErrors.assertTrue; +import static org.springframework.ws.test.support.AssertionErrors.fail; /** * Implementation of {@link org.springframework.ws.test.client.RequestMatcher} based on XMLUnit's {@link Diff}. diff --git a/test/src/main/java/org/springframework/ws/test/support/PayloadDiffMatcher.java b/test/src/main/java/org/springframework/ws/test/support/PayloadDiffMatcher.java index d2f73f7b..e953fb87 100644 --- a/test/src/main/java/org/springframework/ws/test/support/PayloadDiffMatcher.java +++ b/test/src/main/java/org/springframework/ws/test/support/PayloadDiffMatcher.java @@ -27,7 +27,7 @@ import org.springframework.xml.transform.TransformerHelper; import org.custommonkey.xmlunit.Diff; import org.w3c.dom.Document; -import static org.springframework.ws.test.support.Assert.fail; +import static org.springframework.ws.test.support.AssertionErrors.fail; /** * Matches {@link Source} payloads.