From b9f5a75a30ec78eb87e3e0c1871712d44563a61b Mon Sep 17 00:00:00 2001 From: Arjen Poutsma Date: Thu, 4 Nov 2010 11:43:31 +0000 Subject: [PATCH] Polishing --- .../ws/test/server/ResponseMatchers.java | 72 ++++++------------- .../test/server/SoapFaultResponseMatcher.java | 65 +++++++++++++++++ ...est.java => ErrorResponseCreatorTest.java} | 2 +- ...java => ExceptionResponseCreatorTest.java} | 2 +- ...java => SoapFaultResponseCreatorTest.java} | 2 +- 5 files changed, 91 insertions(+), 52 deletions(-) create mode 100644 test/src/main/java/org/springframework/ws/test/server/SoapFaultResponseMatcher.java rename test/src/test/java/org/springframework/ws/test/client/{ErrorResponseCallbackTest.java => ErrorResponseCreatorTest.java} (96%) rename test/src/test/java/org/springframework/ws/test/client/{ExceptionResponseCallbackTest.java => ExceptionResponseCreatorTest.java} (96%) rename test/src/test/java/org/springframework/ws/test/client/{SoapFaultResponseCallbackTest.java => SoapFaultResponseCreatorTest.java} (99%) 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 efc38a5d..4fd2c1ce 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 @@ -22,17 +22,16 @@ import javax.xml.namespace.QName; import javax.xml.transform.Source; import org.springframework.core.io.Resource; +import org.springframework.util.Assert; +import org.springframework.ws.FaultAwareWebServiceMessage; 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.matcher.PayloadDiffMatcher; import org.springframework.ws.test.support.matcher.SchemaValidatingMatcher; +import org.springframework.ws.test.support.matcher.SoapHeaderMatcher; import org.springframework.ws.test.support.matcher.WebServiceMessageMatcher; import org.springframework.xml.transform.ResourceSource; -import static org.springframework.ws.test.support.AssertionErrors.assertEquals; import static org.springframework.ws.test.support.AssertionErrors.assertTrue; /** @@ -94,7 +93,18 @@ public abstract class ResponseMatchers { } - // SOAP Fault + // SOAP + + /** + * Expects the given SOAP header in the outgoing message. + * + * @param soapHeaderName the qualified name of the SOAP header to expect + * @return the request matcher + */ + public static ResponseMatcher soapHeader(QName soapHeaderName) { + Assert.notNull(soapHeaderName, "'soapHeaderName' must not be null"); + return new WebServiceMessageMatcherAdapter(new SoapHeaderMatcher(soapHeaderName)); + } /** * Expects the response not to contain a SOAP fault. @@ -102,13 +112,15 @@ public abstract class ResponseMatchers { * @return the response matcher */ public static ResponseMatcher noFault() { - return new SoapResponseMatcher() { - @Override - protected void matchSoap(SoapMessage request, SoapMessage response) throws IOException, AssertionError { - SoapBody responseBody = response.getSoapBody(); - assertTrue("Response has no SOAP Body", responseBody != null); - assertTrue("Response has a SOAP Fault", !responseBody.hasFault()); + return new ResponseMatcher() { + public void match(WebServiceMessage request, WebServiceMessage response) + throws IOException, AssertionError { + if (response instanceof FaultAwareWebServiceMessage) { + FaultAwareWebServiceMessage faultMessage = (FaultAwareWebServiceMessage) response; + assertTrue("Response has a SOAP Fault", !faultMessage.hasFault()); + } } + }; } @@ -212,44 +224,6 @@ public abstract class ResponseMatchers { }; } - - private static abstract class SoapResponseMatcher implements ResponseMatcher { - - public final void match(WebServiceMessage request, WebServiceMessage response) throws IOException, AssertionError { - assertTrue("Request is not a SOAP message", request instanceof SoapMessage); - assertTrue("Response is not a SOAP message", response instanceof SoapMessage); - matchSoap((SoapMessage)request, (SoapMessage) response); - } - - protected abstract void matchSoap(SoapMessage request, 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 matchSoap(SoapMessage request, SoapMessage response) throws IOException, AssertionError { - SoapBody responseBody = response.getSoapBody(); - assertTrue("Response has no SOAP Body", responseBody != null); - assertTrue("Response has no SOAP Fault", responseBody.hasFault()); - 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); - - } - /** * Adapts a {@link WebServiceMessageMatcher} to the {@link ResponseMatcher} contract. */ diff --git a/test/src/main/java/org/springframework/ws/test/server/SoapFaultResponseMatcher.java b/test/src/main/java/org/springframework/ws/test/server/SoapFaultResponseMatcher.java new file mode 100644 index 00000000..75bf53fa --- /dev/null +++ b/test/src/main/java/org/springframework/ws/test/server/SoapFaultResponseMatcher.java @@ -0,0 +1,65 @@ +/* + * Copyright 2005-2010 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.ws.test.server; + +import java.io.IOException; +import javax.xml.namespace.QName; + +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 static org.springframework.ws.test.support.AssertionErrors.assertEquals; +import static org.springframework.ws.test.support.AssertionErrors.assertTrue; + +/** + * Abstract Implementation of {@link ResponseMatcher} that checks for a SOAP fault. + * + * @author Arjen Poutsma + * @since 2.0 + */ +abstract class SoapFaultResponseMatcher implements ResponseMatcher { + + private final String expectedFaultStringOrReason; + + SoapFaultResponseMatcher(String expectedFaultStringOrReason) { + this.expectedFaultStringOrReason = expectedFaultStringOrReason; + } + + public void match(WebServiceMessage request, WebServiceMessage response) throws IOException, AssertionError { + assertTrue("Response is not a SOAP message", response instanceof SoapMessage); + SoapMessage soapResponse = (SoapMessage) response; + SoapBody responseBody = soapResponse.getSoapBody(); + assertTrue("Response has no SOAP Body", responseBody != null); + assertTrue("Response has no SOAP Fault", responseBody.hasFault()); + SoapFault soapFault = responseBody.getFault(); + QName expectedFaultCode = getExpectedFaultCode(soapResponse.getVersion()); + assertEquals("Invalid SOAP Fault code", expectedFaultCode, soapFault.getFaultCode()); + if (expectedFaultStringOrReason != null) { + assertEquals("Invalid SOAP Fault string/reason", expectedFaultStringOrReason, + soapFault.getFaultStringOrReason()); + } + } + + /** + * Returns the SOAP fault code to check for, given the SOAP version. + */ + protected abstract QName getExpectedFaultCode(SoapVersion version); + +} diff --git a/test/src/test/java/org/springframework/ws/test/client/ErrorResponseCallbackTest.java b/test/src/test/java/org/springframework/ws/test/client/ErrorResponseCreatorTest.java similarity index 96% rename from test/src/test/java/org/springframework/ws/test/client/ErrorResponseCallbackTest.java rename to test/src/test/java/org/springframework/ws/test/client/ErrorResponseCreatorTest.java index 9be779a4..a00f6a61 100644 --- a/test/src/test/java/org/springframework/ws/test/client/ErrorResponseCallbackTest.java +++ b/test/src/test/java/org/springframework/ws/test/client/ErrorResponseCreatorTest.java @@ -22,7 +22,7 @@ import org.junit.Test; import static org.junit.Assert.assertEquals; -public class ErrorResponseCallbackTest { +public class ErrorResponseCreatorTest { @Test public void callback() throws IOException { diff --git a/test/src/test/java/org/springframework/ws/test/client/ExceptionResponseCallbackTest.java b/test/src/test/java/org/springframework/ws/test/client/ExceptionResponseCreatorTest.java similarity index 96% rename from test/src/test/java/org/springframework/ws/test/client/ExceptionResponseCallbackTest.java rename to test/src/test/java/org/springframework/ws/test/client/ExceptionResponseCreatorTest.java index 78ed3b86..9b9ec11c 100644 --- a/test/src/test/java/org/springframework/ws/test/client/ExceptionResponseCallbackTest.java +++ b/test/src/test/java/org/springframework/ws/test/client/ExceptionResponseCreatorTest.java @@ -20,7 +20,7 @@ import java.io.IOException; import org.junit.Test; -public class ExceptionResponseCallbackTest { +public class ExceptionResponseCreatorTest { @Test(expected = IOException.class) public void ioException() throws Exception { diff --git a/test/src/test/java/org/springframework/ws/test/client/SoapFaultResponseCallbackTest.java b/test/src/test/java/org/springframework/ws/test/client/SoapFaultResponseCreatorTest.java similarity index 99% rename from test/src/test/java/org/springframework/ws/test/client/SoapFaultResponseCallbackTest.java rename to test/src/test/java/org/springframework/ws/test/client/SoapFaultResponseCreatorTest.java index 8fb55da7..1edcdd77 100644 --- a/test/src/test/java/org/springframework/ws/test/client/SoapFaultResponseCallbackTest.java +++ b/test/src/test/java/org/springframework/ws/test/client/SoapFaultResponseCreatorTest.java @@ -33,7 +33,7 @@ import org.junit.Test; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; -public class SoapFaultResponseCallbackTest { +public class SoapFaultResponseCreatorTest { private SoapMessage response;