From 90bdf17f13e2a25a0bdd9fbff1bfd8a271cc01b2 Mon Sep 17 00:00:00 2001 From: Arjen Poutsma Date: Fri, 5 Nov 2010 11:32:32 +0000 Subject: [PATCH] Refactored some tests --- .../ws/test/client/ResponseCreators.java | 39 +++-- .../test/client/SoapFaultResponseCreator.java | 53 +------ .../test/client/MockWebServiceServerTest.java | 2 +- .../ws/test/client/ResponseCreatorsTest.java | 148 ++++++++++++++++++ .../client/SoapFaultResponseCreatorTest.java | 111 ------------- 5 files changed, 185 insertions(+), 168 deletions(-) create mode 100644 test/src/test/java/org/springframework/ws/test/client/ResponseCreatorsTest.java delete mode 100644 test/src/test/java/org/springframework/ws/test/client/SoapFaultResponseCreatorTest.java diff --git a/test/src/main/java/org/springframework/ws/test/client/ResponseCreators.java b/test/src/main/java/org/springframework/ws/test/client/ResponseCreators.java index f922426c..80852931 100644 --- a/test/src/main/java/org/springframework/ws/test/client/ResponseCreators.java +++ b/test/src/main/java/org/springframework/ws/test/client/ResponseCreators.java @@ -25,6 +25,7 @@ import org.springframework.core.io.Resource; import org.springframework.util.Assert; import org.springframework.ws.WebServiceMessage; import org.springframework.ws.WebServiceMessageFactory; +import org.springframework.ws.soap.SoapBody; import org.springframework.ws.test.support.creator.PayloadMessageCreator; import org.springframework.ws.test.support.creator.WebServiceMessageCreator; import org.springframework.xml.transform.ResourceSource; @@ -103,11 +104,16 @@ public abstract class ResponseCreators { * * @param faultStringOrReason the SOAP 1.1 fault string or SOAP 1.2 reason text * @param locale the language of faultStringOrReason. Optional for SOAP 1.1 - * @see org.springframework.ws.soap.SoapBody#addMustUnderstandFault(String, java.util.Locale) + * @see SoapBody#addMustUnderstandFault(String, java.util.Locale) */ - public static ResponseCreator withMustUnderstandFault(String faultStringOrReason, Locale locale) { + public static ResponseCreator withMustUnderstandFault(final String faultStringOrReason, final Locale locale) { Assert.hasLength(faultStringOrReason, "'faultStringOrReason' must not be empty"); - return SoapFaultResponseCreator.createMustUnderstandFault(faultStringOrReason, locale); + return new SoapFaultResponseCreator() { + @Override + public void addSoapFault(SoapBody soapBody) { + soapBody.addMustUnderstandFault(faultStringOrReason, locale); + } + }; } /** @@ -117,9 +123,14 @@ public abstract class ResponseCreators { * @param locale the language of faultStringOrReason. Optional for SOAP 1.1 * @see org.springframework.ws.soap.SoapBody#addClientOrSenderFault(String, Locale) */ - public static ResponseCreator withClientOrSenderFault(String faultStringOrReason, Locale locale) { + public static ResponseCreator withClientOrSenderFault(final String faultStringOrReason, final Locale locale) { Assert.hasLength(faultStringOrReason, "'faultStringOrReason' must not be empty"); - return SoapFaultResponseCreator.createClientOrSenderFault(faultStringOrReason, locale); + return new SoapFaultResponseCreator() { + @Override + public void addSoapFault(SoapBody soapBody) { + soapBody.addClientOrSenderFault(faultStringOrReason, locale); + } + }; } /** @@ -129,9 +140,14 @@ public abstract class ResponseCreators { * @param locale the language of faultStringOrReason. Optional for SOAP 1.1 * @see org.springframework.ws.soap.SoapBody#addServerOrReceiverFault(String, Locale) */ - public static ResponseCreator withServerOrReceiverFault(String faultStringOrReason, Locale locale) { + public static ResponseCreator withServerOrReceiverFault(final String faultStringOrReason, final Locale locale) { Assert.hasLength(faultStringOrReason, "'faultStringOrReason' must not be empty"); - return SoapFaultResponseCreator.createServerOrReceiverFault(faultStringOrReason, locale); + return new SoapFaultResponseCreator() { + @Override + public void addSoapFault(SoapBody soapBody) { + soapBody.addServerOrReceiverFault(faultStringOrReason, locale); + } + }; } /** @@ -141,9 +157,14 @@ public abstract class ResponseCreators { * @param locale the language of faultStringOrReason. Optional for SOAP 1.1 * @see org.springframework.ws.soap.SoapBody#addVersionMismatchFault(String, Locale) */ - public static ResponseCreator withVersionMismatchFault(String faultStringOrReason, Locale locale) { + public static ResponseCreator withVersionMismatchFault(final String faultStringOrReason, final Locale locale) { Assert.hasLength(faultStringOrReason, "'faultStringOrReason' must not be empty"); - return SoapFaultResponseCreator.createVersionMismatchFault(faultStringOrReason, locale); + return new SoapFaultResponseCreator() { + @Override + public void addSoapFault(SoapBody soapBody) { + soapBody.addVersionMismatchFault(faultStringOrReason, locale); + } + }; } /** 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 31fe2219..edaf6632 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 @@ -18,7 +18,6 @@ package org.springframework.ws.test.client; import java.io.IOException; import java.net.URI; -import java.util.Locale; import org.springframework.ws.WebServiceMessage; import org.springframework.ws.soap.SoapBody; @@ -48,50 +47,10 @@ abstract class SoapFaultResponseCreator extends AbstractResponseCreator { addSoapFault(responseBody); } - public abstract void addSoapFault(SoapBody soapBody); - - public static SoapFaultResponseCreator createMustUnderstandFault(final String faultStringOrReason, - final Locale locale) { - return new SoapFaultResponseCreator() { - @Override - public void addSoapFault(SoapBody soapBody) { - soapBody.addMustUnderstandFault(faultStringOrReason, locale); - } - }; - - } - - public static SoapFaultResponseCreator createClientOrSenderFault(final String faultStringOrReason, - final Locale locale) { - return new SoapFaultResponseCreator() { - @Override - public void addSoapFault(SoapBody soapBody) { - soapBody.addClientOrSenderFault(faultStringOrReason, locale); - } - }; - } - - public static SoapFaultResponseCreator createServerOrReceiverFault(final String faultStringOrReason, - final Locale locale) { - return new SoapFaultResponseCreator() { - @Override - public void addSoapFault(SoapBody soapBody) { - soapBody.addServerOrReceiverFault(faultStringOrReason, locale); - } - }; - - } - - public static SoapFaultResponseCreator createVersionMismatchFault(final String faultStringOrReason, - final Locale locale) { - return new SoapFaultResponseCreator() { - @Override - public void addSoapFault(SoapBody soapBody) { - soapBody.addVersionMismatchFault(faultStringOrReason, locale); - } - }; - - } - - + /** + * Abstract template method that allows subclasses to add a SOAP Fault to the given Body. + * + * @param soapBody the body to attach a fault to + */ + protected abstract void addSoapFault(SoapBody soapBody); } diff --git a/test/src/test/java/org/springframework/ws/test/client/MockWebServiceServerTest.java b/test/src/test/java/org/springframework/ws/test/client/MockWebServiceServerTest.java index 2598ae2f..88ba7d64 100644 --- a/test/src/test/java/org/springframework/ws/test/client/MockWebServiceServerTest.java +++ b/test/src/test/java/org/springframework/ws/test/client/MockWebServiceServerTest.java @@ -304,7 +304,7 @@ public class MockWebServiceServerTest { StringResult result = new StringResult(); template.sendSourceAndReceiveToResult(request, result); } - + public static class MyClient extends WebServiceGatewaySupport { } diff --git a/test/src/test/java/org/springframework/ws/test/client/ResponseCreatorsTest.java b/test/src/test/java/org/springframework/ws/test/client/ResponseCreatorsTest.java new file mode 100644 index 00000000..d634b708 --- /dev/null +++ b/test/src/test/java/org/springframework/ws/test/client/ResponseCreatorsTest.java @@ -0,0 +1,148 @@ +/* + * 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.client; + +import java.io.IOException; +import java.util.Locale; +import javax.xml.namespace.QName; +import javax.xml.transform.Result; +import javax.xml.transform.TransformerException; + +import org.springframework.core.io.ByteArrayResource; +import org.springframework.ws.WebServiceMessage; +import org.springframework.ws.soap.SoapMessage; +import org.springframework.ws.soap.SoapVersion; +import org.springframework.ws.soap.saaj.SaajSoapMessageFactory; +import org.springframework.ws.soap.soap11.Soap11Fault; +import org.springframework.xml.transform.StringResult; +import org.springframework.xml.transform.StringSource; +import org.springframework.xml.transform.TransformerHelper; + +import org.junit.Before; +import org.junit.Test; + +import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual; +import static org.junit.Assert.*; + +public class ResponseCreatorsTest { + + private final TransformerHelper transformerHelper = new TransformerHelper(); + + private SaajSoapMessageFactory messageFactory; + + @Before + public void createMessageFactory() { + messageFactory = new SaajSoapMessageFactory(); + messageFactory.afterPropertiesSet(); + } + + @Test + public void withPayloadSource() throws Exception { + String payload = ""; + ResponseCreator responseCreator = ResponseCreators.withPayload(new StringSource(payload)); + + WebServiceMessage response = responseCreator.createResponse(null, null, messageFactory); + + assertXMLEqual(payload, getPayloadAsString(response)); + + } + + @Test + public void withPayloadResource() throws Exception { + String payload = ""; + ResponseCreator responseCreator = + ResponseCreators.withPayload(new ByteArrayResource(payload.getBytes("UTF-8"))); + + WebServiceMessage response = responseCreator.createResponse(null, null, messageFactory); + + assertXMLEqual(payload, getPayloadAsString(response)); + } + + @Test + public void withIOException() throws Exception { + IOException expected = new IOException("Foo"); + ResponseCreator responseCreator = ResponseCreators.withException(expected); + + try { + responseCreator.createResponse(null, null, null); + } + catch (IOException actual) { + assertSame(expected, actual); + } + } + + @Test + public void withRuntimeException() throws Exception { + RuntimeException expected = new RuntimeException("Foo"); + ResponseCreator responseCreator = ResponseCreators.withException(expected); + + try { + responseCreator.createResponse(null, null, null); + } + catch (RuntimeException actual) { + assertSame(expected, actual); + } + } + + @Test + public void withMustUnderstandFault() throws Exception { + String faultString = "Foo"; + ResponseCreator responseCreator = ResponseCreators.withMustUnderstandFault(faultString, Locale.ENGLISH); + + testFault(responseCreator, faultString, SoapVersion.SOAP_11.getMustUnderstandFaultName()); + } + + @Test + public void withClientOrSenderFault() throws Exception { + String faultString = "Foo"; + ResponseCreator responseCreator = ResponseCreators.withClientOrSenderFault(faultString, Locale.ENGLISH); + + testFault(responseCreator, faultString, SoapVersion.SOAP_11.getClientOrSenderFaultName()); + } + + @Test + public void withServerOrReceiverFault() throws Exception { + String faultString = "Foo"; + ResponseCreator responseCreator = ResponseCreators.withServerOrReceiverFault(faultString, Locale.ENGLISH); + + testFault(responseCreator, faultString, SoapVersion.SOAP_11.getServerOrReceiverFaultName()); + } + + @Test + public void withVersionMismatchFault() throws Exception { + String faultString = "Foo"; + ResponseCreator responseCreator = ResponseCreators.withVersionMismatchFault(faultString, Locale.ENGLISH); + + testFault(responseCreator, faultString, SoapVersion.SOAP_11.getVersionMismatchFaultName()); + } + + private void testFault(ResponseCreator responseCreator, String faultString, QName faultCode) throws IOException { + SoapMessage response = (SoapMessage) responseCreator.createResponse(null, null, messageFactory); + + assertTrue("Response has no fault", response.hasFault()); + Soap11Fault soapFault = (Soap11Fault) response.getSoapBody().getFault(); + assertEquals("Response has invalid fault code", faultCode, soapFault.getFaultCode()); + assertEquals("Response has invalid fault string", faultString, soapFault.getFaultStringOrReason()); + assertEquals("Response has invalid fault locale", Locale.ENGLISH, soapFault.getFaultStringLocale()); + } + + private String getPayloadAsString(WebServiceMessage message) throws TransformerException { + Result result = new StringResult(); + transformerHelper.transform(message.getPayloadSource(), result); + return result.toString(); + } +} diff --git a/test/src/test/java/org/springframework/ws/test/client/SoapFaultResponseCreatorTest.java b/test/src/test/java/org/springframework/ws/test/client/SoapFaultResponseCreatorTest.java deleted file mode 100644 index 1edcdd77..00000000 --- a/test/src/test/java/org/springframework/ws/test/client/SoapFaultResponseCreatorTest.java +++ /dev/null @@ -1,111 +0,0 @@ -/* - * 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.client; - -import java.io.IOException; -import java.util.Locale; -import javax.xml.soap.MessageFactory; -import javax.xml.soap.SOAPException; -import javax.xml.soap.SOAPMessage; - -import org.springframework.ws.soap.SoapMessage; -import org.springframework.ws.soap.SoapVersion; -import org.springframework.ws.soap.saaj.SaajSoapMessage; -import org.springframework.ws.soap.soap11.Soap11Fault; - -import org.junit.Before; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; - -public class SoapFaultResponseCreatorTest { - - private SoapMessage response; - - @Before - public void createResponse() throws SOAPException { - MessageFactory messageFactory = MessageFactory.newInstance(); - SOAPMessage saajResponse = messageFactory.createMessage(); - this.response = new SaajSoapMessage(saajResponse); - } - - @Test - public void clientOrSenderFault() throws IOException { - String faultString = "Foo"; - SoapFaultResponseCreator callback = - SoapFaultResponseCreator.createClientOrSenderFault(faultString, Locale.ENGLISH); - - callback.doWithResponse(null, null, response); - - assertTrue("Response has no fault", response.hasFault()); - Soap11Fault soapFault = (Soap11Fault) response.getSoapBody().getFault(); - assertEquals("Response has invalid fault code", SoapVersion.SOAP_11.getClientOrSenderFaultName(), - soapFault.getFaultCode()); - assertEquals("Response has invalid fault string", faultString, soapFault.getFaultStringOrReason()); - assertEquals("Response has invalid fault locale", Locale.ENGLISH, soapFault.getFaultStringLocale()); - } - - @Test - public void mustUnderstandFault() throws IOException { - String faultString = "Foo"; - SoapFaultResponseCreator callback = - SoapFaultResponseCreator.createMustUnderstandFault(faultString, Locale.ENGLISH); - - callback.doWithResponse(null, null, response); - - assertTrue("Response has no fault", response.hasFault()); - Soap11Fault soapFault = (Soap11Fault) response.getSoapBody().getFault(); - assertEquals("Response has invalid fault code", SoapVersion.SOAP_11.getMustUnderstandFaultName(), - soapFault.getFaultCode()); - assertEquals("Response has invalid fault string", faultString, soapFault.getFaultStringOrReason()); - assertEquals("Response has invalid fault locale", Locale.ENGLISH, soapFault.getFaultStringLocale()); - } - - @Test - public void serverOrReceiverFault() throws IOException { - String faultString = "Foo"; - SoapFaultResponseCreator callback = - SoapFaultResponseCreator.createServerOrReceiverFault(faultString, Locale.ENGLISH); - - callback.doWithResponse(null, null, response); - - assertTrue("Response has no fault", response.hasFault()); - Soap11Fault soapFault = (Soap11Fault) response.getSoapBody().getFault(); - assertEquals("Response has invalid fault code", SoapVersion.SOAP_11.getServerOrReceiverFaultName(), - soapFault.getFaultCode()); - assertEquals("Response has invalid fault string", faultString, soapFault.getFaultStringOrReason()); - assertEquals("Response has invalid fault locale", Locale.ENGLISH, soapFault.getFaultStringLocale()); - } - - @Test - public void versionMismatchFault() throws IOException { - String faultString = "Foo"; - SoapFaultResponseCreator callback = - SoapFaultResponseCreator.createVersionMismatchFault(faultString, Locale.ENGLISH); - - callback.doWithResponse(null, null, response); - - assertTrue("Response has no fault", response.hasFault()); - Soap11Fault soapFault = (Soap11Fault) response.getSoapBody().getFault(); - assertEquals("Response has invalid fault code", SoapVersion.SOAP_11.getVersionMismatchFaultName(), - soapFault.getFaultCode()); - assertEquals("Response has invalid fault string", faultString, soapFault.getFaultStringOrReason()); - assertEquals("Response has invalid fault locale", Locale.ENGLISH, soapFault.getFaultStringLocale()); - } - -}