From 5500ae79b85ada29bb31ea64ef968e32dd032c65 Mon Sep 17 00:00:00 2001 From: Arjen Poutsma Date: Thu, 4 Nov 2010 11:00:40 +0000 Subject: [PATCH] Created support.creator package, with support for generic WebService creation (irrespective of whether it's a request or response). --- .../test/client/PayloadResponseCreator.java | 52 ------------------- .../ws/test/client/ResponseCreators.java | 26 +++++++++- .../ws/test/server/RequestCreators.java | 22 +++++++- .../creator/AbstractMessageCreator.java | 50 ++++++++++++++++++ .../creator/PayloadMessageCreator.java} | 24 ++++++--- .../creator/WebServiceMessageCreator.java} | 22 ++++---- .../ws/test/support/creator/package-info.java | 21 ++++++++ .../test/client/MockSenderConnectionTest.java | 8 +-- 8 files changed, 148 insertions(+), 77 deletions(-) delete mode 100644 test/src/main/java/org/springframework/ws/test/client/PayloadResponseCreator.java create mode 100644 test/src/main/java/org/springframework/ws/test/support/creator/AbstractMessageCreator.java rename test/src/main/java/org/springframework/ws/test/{server/PayloadRequestCreator.java => support/creator/PayloadMessageCreator.java} (58%) rename test/src/main/java/org/springframework/ws/test/{server/AbstractRequestCreator.java => support/creator/WebServiceMessageCreator.java} (52%) create mode 100644 test/src/main/java/org/springframework/ws/test/support/creator/package-info.java diff --git a/test/src/main/java/org/springframework/ws/test/client/PayloadResponseCreator.java b/test/src/main/java/org/springframework/ws/test/client/PayloadResponseCreator.java deleted file mode 100644 index 944ef1d3..00000000 --- a/test/src/main/java/org/springframework/ws/test/client/PayloadResponseCreator.java +++ /dev/null @@ -1,52 +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.net.URI; -import javax.xml.transform.Source; -import javax.xml.transform.TransformerException; - -import org.springframework.ws.WebServiceMessage; -import org.springframework.xml.transform.TransformerHelper; - -/** - * Implementation of {@link ResponseCreator} that writes a {@link Source} response. - * - * @author Arjen Poutsma - * @since 2.0 - */ -class PayloadResponseCreator extends AbstractResponseCreator { - - private final Source payload; - - private TransformerHelper transformerHelper = new TransformerHelper(); - - PayloadResponseCreator(Source payload) { - this.payload = payload; - } - - @Override - protected void doWithResponse(URI uri, WebServiceMessage request, WebServiceMessage response) throws IOException { - try { - transformerHelper.transform(payload, response.getPayloadResult()); - } - catch (TransformerException ex) { - throw new AssertionError("Could not transform response payload to message: " + ex.getMessage()); - } - } -} 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 9cf2f12f..8da2ef2e 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 @@ -17,11 +17,16 @@ package org.springframework.ws.test.client; import java.io.IOException; +import java.net.URI; import java.util.Locale; import javax.xml.transform.Source; 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.test.support.creator.PayloadMessageCreator; +import org.springframework.ws.test.support.creator.WebServiceMessageCreator; import org.springframework.xml.transform.ResourceSource; /** @@ -40,7 +45,7 @@ public abstract class ResponseCreators { */ public static ResponseCreator withPayload(Source payload) { Assert.notNull(payload, "'payload' must not be null"); - return new PayloadResponseCreator(payload); + return new WebServiceMessageCreatorAdapter(new PayloadMessageCreator(payload)); } /** @@ -137,4 +142,23 @@ public abstract class ResponseCreators { return SoapFaultResponseCreator.createVersionMismatchFault(faultStringOrReason, locale); } + /** + * Adapts a {@link WebServiceMessageCreator} to the {@link ResponseCreator} contract. + */ + private static class WebServiceMessageCreatorAdapter implements ResponseCreator { + + private final WebServiceMessageCreator adaptee; + + private WebServiceMessageCreatorAdapter(WebServiceMessageCreator adaptee) { + this.adaptee = adaptee; + } + + public WebServiceMessage createResponse(URI uri, + WebServiceMessage request, + WebServiceMessageFactory messageFactory) throws IOException { + return adaptee.createMessage(messageFactory); + } + } + + } diff --git a/test/src/main/java/org/springframework/ws/test/server/RequestCreators.java b/test/src/main/java/org/springframework/ws/test/server/RequestCreators.java index 5dc26887..7a07356d 100644 --- a/test/src/main/java/org/springframework/ws/test/server/RequestCreators.java +++ b/test/src/main/java/org/springframework/ws/test/server/RequestCreators.java @@ -21,6 +21,10 @@ import javax.xml.transform.Source; 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.test.support.creator.PayloadMessageCreator; +import org.springframework.ws.test.support.creator.WebServiceMessageCreator; import org.springframework.xml.transform.ResourceSource; /** @@ -43,7 +47,7 @@ public abstract class RequestCreators { */ public static RequestCreator withPayload(Source payload) { Assert.notNull(payload, "'payload' must not be null"); - return new PayloadRequestCreator(payload); + return new WebServiceMessageCreatorAdapter(new PayloadMessageCreator(payload)); } /** @@ -57,5 +61,21 @@ public abstract class RequestCreators { return withPayload(new ResourceSource(payload)); } + /** + * Adapts a {@link WebServiceMessageCreator} to the {@link RequestCreator} contract. + */ + private static class WebServiceMessageCreatorAdapter implements RequestCreator { + + private final WebServiceMessageCreator adaptee; + + private WebServiceMessageCreatorAdapter(WebServiceMessageCreator adaptee) { + this.adaptee = adaptee; + } + + public WebServiceMessage createRequest(WebServiceMessageFactory messageFactory) throws IOException { + return adaptee.createMessage(messageFactory); + } + } + } diff --git a/test/src/main/java/org/springframework/ws/test/support/creator/AbstractMessageCreator.java b/test/src/main/java/org/springframework/ws/test/support/creator/AbstractMessageCreator.java new file mode 100644 index 00000000..b7598da1 --- /dev/null +++ b/test/src/main/java/org/springframework/ws/test/support/creator/AbstractMessageCreator.java @@ -0,0 +1,50 @@ +/* + * 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.support.creator; + +import java.io.IOException; + +import org.springframework.ws.WebServiceMessage; +import org.springframework.ws.WebServiceMessageFactory; + +/** + * Abstract base class for the {@link WebServiceMessageCreator} interface. + *

+ * Creates a message using the given {@link WebServiceMessageFactory}, and passes it on to {@link + * #doWithMessage(WebServiceMessage)}. + * + * @author Arjen Poutsma + * @since 2.0 + */ +public abstract class AbstractMessageCreator implements WebServiceMessageCreator { + + public final WebServiceMessage createMessage(WebServiceMessageFactory messageFactory) throws IOException { + WebServiceMessage message = messageFactory.createWebServiceMessage(); + doWithMessage(message); + return message; + } + + /** + * Abstract template method, invoked by {@link #createMessage(WebServiceMessageFactory)} after a message has been + * created. + * + * @param message the message + * @throws IOException in case of I/O errors + */ + protected abstract void doWithMessage(WebServiceMessage message) throws IOException; + +} diff --git a/test/src/main/java/org/springframework/ws/test/server/PayloadRequestCreator.java b/test/src/main/java/org/springframework/ws/test/support/creator/PayloadMessageCreator.java similarity index 58% rename from test/src/main/java/org/springframework/ws/test/server/PayloadRequestCreator.java rename to test/src/main/java/org/springframework/ws/test/support/creator/PayloadMessageCreator.java index e3a6365c..70638772 100644 --- a/test/src/main/java/org/springframework/ws/test/server/PayloadRequestCreator.java +++ b/test/src/main/java/org/springframework/ws/test/support/creator/PayloadMessageCreator.java @@ -14,39 +14,47 @@ * limitations under the License. */ -package org.springframework.ws.test.server; +package org.springframework.ws.test.support.creator; import java.io.IOException; import javax.xml.transform.Source; import javax.xml.transform.TransformerException; +import org.springframework.util.Assert; import org.springframework.ws.WebServiceMessage; import org.springframework.xml.transform.TransformerHelper; +import static org.springframework.ws.test.support.AssertionErrors.fail; + /** - * Implementation of {@link org.springframework.ws.test.server.RequestCreator} that creates a request based on a {@link - * javax.xml.transform.Source}. + * Implementation of {@link WebServiceMessageCreator} that creates a request based on a {@link Source}. * * @author Arjen Poutsma * @since 2.0 */ -class PayloadRequestCreator extends AbstractRequestCreator { +public class PayloadMessageCreator extends AbstractMessageCreator { private final Source payload; private TransformerHelper transformerHelper = new TransformerHelper(); - PayloadRequestCreator(Source payload) { + /** + * Creates a new instance of the {@code PayloadMessageCreator} with the given payload source. + * + * @param payload the payload source + */ + public PayloadMessageCreator(Source payload) { + Assert.notNull(payload, "'payload' must not be null"); this.payload = payload; } @Override - protected void doWithRequest(WebServiceMessage request) throws IOException { + protected void doWithMessage(WebServiceMessage message) throws IOException { try { - transformerHelper.transform(payload, request.getPayloadResult()); + transformerHelper.transform(payload, message.getPayloadResult()); } catch (TransformerException ex) { - throw new AssertionError("Could not transform request payload to message: " + ex.getMessage()); + fail("Could not transform request payload to message: " + ex.getMessage()); } } } diff --git a/test/src/main/java/org/springframework/ws/test/server/AbstractRequestCreator.java b/test/src/main/java/org/springframework/ws/test/support/creator/WebServiceMessageCreator.java similarity index 52% rename from test/src/main/java/org/springframework/ws/test/server/AbstractRequestCreator.java rename to test/src/main/java/org/springframework/ws/test/support/creator/WebServiceMessageCreator.java index 96d7fc10..13245ef5 100644 --- a/test/src/main/java/org/springframework/ws/test/server/AbstractRequestCreator.java +++ b/test/src/main/java/org/springframework/ws/test/support/creator/WebServiceMessageCreator.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.ws.test.server; +package org.springframework.ws.test.support.creator; import java.io.IOException; @@ -22,22 +22,20 @@ import org.springframework.ws.WebServiceMessage; import org.springframework.ws.WebServiceMessageFactory; /** - * Abstract base class for the {@link org.springframework.ws.test.server.RequestCreator} interface. - *

- * Creates a response using the given {@link org.springframework.ws.WebServiceMessageFactory}, and passes it on to - * {@link #doWithRequest(org.springframework.ws.WebServiceMessage)}. + * Defines the general contract for creating messages used in test scenarios. * * @author Arjen Poutsma * @since 2.0 */ -abstract class AbstractRequestCreator implements RequestCreator { +public interface WebServiceMessageCreator { - public final WebServiceMessage createRequest(WebServiceMessageFactory messageFactory) throws IOException { - WebServiceMessage request = messageFactory.createWebServiceMessage(); - doWithRequest(request); - return request; - } + /** + * Create a message. + * + * @param messageFactory the message that can be used to create the message + * @throws IOException in case of I/O errors + */ + WebServiceMessage createMessage(WebServiceMessageFactory messageFactory) throws IOException; - protected abstract void doWithRequest(WebServiceMessage request) throws IOException; } diff --git a/test/src/main/java/org/springframework/ws/test/support/creator/package-info.java b/test/src/main/java/org/springframework/ws/test/support/creator/package-info.java new file mode 100644 index 00000000..7e00dd65 --- /dev/null +++ b/test/src/main/java/org/springframework/ws/test/support/creator/package-info.java @@ -0,0 +1,21 @@ +/* + * 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. + */ + +/** + * Provides the generic {@link org.springframework.ws.test.support.creator.WebServiceMessageCreator WebServiceMessageCreator} + * interface, and implementations. + */ +package org.springframework.ws.test.support.creator; diff --git a/test/src/test/java/org/springframework/ws/test/client/MockSenderConnectionTest.java b/test/src/test/java/org/springframework/ws/test/client/MockSenderConnectionTest.java index 688c15e6..374c41df 100644 --- a/test/src/test/java/org/springframework/ws/test/client/MockSenderConnectionTest.java +++ b/test/src/test/java/org/springframework/ws/test/client/MockSenderConnectionTest.java @@ -23,6 +23,8 @@ import org.springframework.xml.transform.StringSource; import org.junit.Test; import static org.junit.Assert.*; +import static org.springframework.ws.test.client.ResponseCreators.withError; +import static org.springframework.ws.test.client.ResponseCreators.withPayload; public class MockSenderConnectionTest { @@ -30,7 +32,7 @@ public class MockSenderConnectionTest { public void error() throws IOException { String testErrorMessage = "Test Error Message"; MockSenderConnection connection = new MockSenderConnection(); - connection.andRespond(new ErrorResponseCreator(testErrorMessage)); + connection.andRespond(withError(testErrorMessage)); assertTrue(connection.hasError()); assertEquals(testErrorMessage, connection.getErrorMessage()); } @@ -38,7 +40,7 @@ public class MockSenderConnectionTest { @Test public void normal() throws IOException { MockSenderConnection connection = new MockSenderConnection(); - connection.andRespond(new PayloadResponseCreator(new StringSource(""))); + connection.andRespond(withPayload(new StringSource(""))); assertFalse(connection.hasError()); assertNull(connection.getErrorMessage()); } @@ -46,7 +48,7 @@ public class MockSenderConnectionTest { @Test(expected = AssertionError.class) public void noRequestMatchers() throws IOException { MockSenderConnection connection = new MockSenderConnection(); - connection.andRespond(new PayloadResponseCreator(new StringSource(""))); + connection.andRespond(withPayload(new StringSource(""))); connection.send(null); } }