From 9c1f36ec5e3b4e69e10ddd9894c52647852b7f83 Mon Sep 17 00:00:00 2001 From: Arjen Poutsma Date: Wed, 22 Sep 2010 12:52:18 +0000 Subject: [PATCH] SWS-632 - Create Server-Side testing framework --- test/pom.xml | 4 +- .../mock/client/DefaultXPathExpectations.java | 4 +- .../mock/client/SoapFaultResponseCreator.java | 10 +- .../ws/mock/client/SoapHeaderMatcher.java | 5 +- .../ws/mock/client/UriMatcher.java | 2 +- .../ws/mock/client/WebServiceMock.java | 67 +++++++------ .../ws/mock/server/WebServiceMock.java | 55 ++++++++++- .../ws/mock/server/WebServiceTestContext.java | 54 +++++++++++ .../server/WebServiceTestContextHolder.java | 51 ++++++++++ .../WebServiceTestExecutionListener.java | 97 +++++++++++++++++++ .../ws/mock/{client => support}/Assert.java | 10 +- .../mock/{client => support}/DiffMatcher.java | 15 ++- .../PayloadDiffMatcher.java | 13 ++- .../integration/ClientIntegrationTest.java | 12 ++- .../integration/CustomerCountRequest.java | 4 +- .../integration/CustomerCountResponse.java | 4 +- .../integration/ServerIntegrationTest.java | 12 ++- .../PayloadDiffMatcherTest.java | 6 +- .../server/integration/integration-test.xml | 10 +- 19 files changed, 347 insertions(+), 88 deletions(-) create mode 100644 test/src/main/java/org/springframework/ws/mock/server/WebServiceTestContext.java create mode 100644 test/src/main/java/org/springframework/ws/mock/server/WebServiceTestContextHolder.java create mode 100644 test/src/main/java/org/springframework/ws/mock/server/WebServiceTestExecutionListener.java rename test/src/main/java/org/springframework/ws/mock/{client => support}/Assert.java (86%) rename test/src/main/java/org/springframework/ws/mock/{client => support}/DiffMatcher.java (74%) rename test/src/main/java/org/springframework/ws/mock/{client => support}/PayloadDiffMatcher.java (82%) rename test/src/test/java/org/springframework/ws/mock/{client => support}/PayloadDiffMatcherTest.java (94%) diff --git a/test/pom.xml b/test/pom.xml index a0708bbc..2bfe45b3 100644 --- a/test/pom.xml +++ b/test/pom.xml @@ -1,4 +1,5 @@ - + spring-ws-parent org.springframework.ws @@ -51,6 +52,7 @@ org.springframework spring-test + compile diff --git a/test/src/main/java/org/springframework/ws/mock/client/DefaultXPathExpectations.java b/test/src/main/java/org/springframework/ws/mock/client/DefaultXPathExpectations.java index 2950a785..97340462 100644 --- a/test/src/main/java/org/springframework/ws/mock/client/DefaultXPathExpectations.java +++ b/test/src/main/java/org/springframework/ws/mock/client/DefaultXPathExpectations.java @@ -30,8 +30,8 @@ import org.springframework.xml.xpath.XPathExpressionFactory; import org.w3c.dom.Node; -import static org.springframework.ws.mock.client.Assert.assertEquals; -import static org.springframework.ws.mock.client.Assert.fail; +import static org.springframework.ws.mock.support.Assert.assertEquals; +import static org.springframework.ws.mock.support.Assert.fail; /** * Default implementation of {@link XPathExpectations}. diff --git a/test/src/main/java/org/springframework/ws/mock/client/SoapFaultResponseCreator.java b/test/src/main/java/org/springframework/ws/mock/client/SoapFaultResponseCreator.java index 20b5ef67..c934ada5 100644 --- a/test/src/main/java/org/springframework/ws/mock/client/SoapFaultResponseCreator.java +++ b/test/src/main/java/org/springframework/ws/mock/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.mock.client.Assert.fail; +import static org.springframework.ws.mock.support.Assert.fail; /** * Implementation of {@link ResponseCreator} that responds with a SOAP fault. @@ -45,7 +45,7 @@ abstract class SoapFaultResponseCreator extends AbstractResponseCreatorMain entry point for client-side Web service testing. Typically used to mock a {@link * WebServiceTemplate}, set up expectations on request messages, and create response messages. *

- * The typical usage of this mock is similar to any other mocking library (such as EasyMock), that is: - *

    - *
  1. Statically import {@link org.springframework.ws.mock.client.WebServiceMock - * org.springframework.ws.mock.client.WebServiceMock.*}. + * The typical usage of this mock is similar to any other mocking library (such as EasyMock), that is:
      + *
    1. Statically import {@link org.springframework.ws.mock.client.WebServiceMock org.springframework.ws.mock.client.WebServiceMock.*}. *
    2. Use the {@link #mockWebServiceTemplate(WebServiceTemplate)} method to mock a web service template. Typically, - * this template is configured as a Spring bean, either explicitly or as a property of a class that extends - * {@link org.springframework.ws.client.core.support.WebServiceGatewaySupport WebServiceGatewaySupport}.
    3. - *
    4. Set up expectations on the outgoing request message by calling {@link #expect(RequestMatcher)} and - * {@link #payload(Source)}, {@link #connectionTo(String)}, {@link #xpath(String)}, or any of the other - * {@linkplain RequestMatcher request matcher} methods. - * Multiple expectations can be set up by calling - * {@link ResponseActions#andExpect(RequestMatcher) andExpect(RequestMatcher)}.
    5. - *
    6. Indicate the desired response actions by calling - * {@link ResponseActions#andRespond(ResponseCreator) andRespond(ResponseCreator)}. - * See {@link #withPayload(Source)}, {@link #withError(String)}, - * {@link #withClientOrSenderFault(String, Locale)}, or any of the other {@linkplain ResponseCreator response creator} - * methods.
    7. - *
    8. Use the {@code WebServiceTemplate} as normal, either directly of through client code. - *
    9. Call {@link #verifyConnections()}. - *
    - * Note that because of the 'fluent' API offered by this class, you can typically use the Code Completion features (i.e. - * ctrl-space) in your IDE to set up the mocks. + * this template is configured as a Spring bean, either explicitly or as a property of a class that extends {@link + * org.springframework.ws.client.core.support.WebServiceGatewaySupport WebServiceGatewaySupport}.
  2. Set up + * expectations on the outgoing request message by calling {@link #expect(RequestMatcher)} and {@link #payload(Source)}, + * {@link #connectionTo(String)}, {@link #xpath(String)}, or any of the other {@linkplain RequestMatcher request + * matcher} methods. Multiple expectations can be set up by calling {@link ResponseActions#andExpect(RequestMatcher) + * andExpect(RequestMatcher)}.
  3. Indicate the desired response actions by calling {@link + * ResponseActions#andRespond(ResponseCreator) andRespond(ResponseCreator)}. See {@link #withPayload(Source)}, {@link + * #withError(String)}, {@link #withClientOrSenderFault(String, Locale)}, or any of the other {@linkplain + * ResponseCreator response creator} methods.
  4. Use the {@code WebServiceTemplate} as normal, either directly of + * through client code.
  5. Call {@link #verifyConnections()}.
Note that because of the 'fluent' API offered by + * this class, you can typically use the Code Completion features (i.e. ctrl-space) in your IDE to set up the mocks. *

* For example: *

@@ -68,35 +61,36 @@ import org.springframework.xml.validation.XmlValidatorFactory;
  * import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  * import org.springframework.xml.transform.StringSource;
  * import static org.springframework.ws.mock.client.WebServiceMock.*;
- *
- * 
+ * 

+ *

* @RunWith(SpringJUnit4ClassRunner.class) * @ContextConfiguration("applicationContext.xml") * public class IntegrationTest { - * + *

* // MyWebServiceClient extends WebServiceGatewaySupport, and is configured in applicationContext.xml * @Autowired * private MyWebServiceClient client; - * + *

* @Before * public void setUpMocks() throws Exception { * mockWebServiceTemplate(client.getWebServiceTemplate()); * } - * + *

* @Test * public void getCustomerCount() throws Exception { * Source requestPayload = * new StringSource("<customerCountRequest xmlns='http://springframework.org/spring-ws/test' />"; - * Source responsePayload = new StringSource("<customerCountResponse xmlns='http://springframework.org/spring-ws/test'>" + + * Source responsePayload = new StringSource("<customerCountResponse xmlns='http://springframework.org/spring-ws/test'>" + * + * "<customerCount>10</customerCount>" + * "</customerCountResponse>"); - * + *

* expect(payload(requestPayload)).andRespond(withPayload(responsePayload)); - * + *

* // client.getCustomerCount() uses the WebServiceTemplate * int customerCount = client.getCustomerCount(); * assertEquals(10, response.getCustomerCount()); - * + *

* verifyConnections(); * } * } @@ -161,7 +155,7 @@ public abstract class WebServiceMock { */ public static RequestMatcher payload(Source payload) { Assert.notNull(payload, "'payload' must not be null"); - return new PayloadDiffMatcher(payload); + return createPayloadDiffMatcher(payload); } /** @@ -172,7 +166,16 @@ public abstract class WebServiceMock { */ public static RequestMatcher payload(Resource payload) { Assert.notNull(payload, "'payload' must not be null"); - return new PayloadDiffMatcher(createResourceSource(payload)); + return createPayloadDiffMatcher(createResourceSource(payload)); + } + + private static RequestMatcher createPayloadDiffMatcher(Source payload) { + final PayloadDiffMatcher matcher = new PayloadDiffMatcher(payload); + return new RequestMatcher() { + public void match(URI uri, WebServiceMessage request) throws IOException, AssertionError { + matcher.match(request); + } + }; } /** diff --git a/test/src/main/java/org/springframework/ws/mock/server/WebServiceMock.java b/test/src/main/java/org/springframework/ws/mock/server/WebServiceMock.java index ac4174ef..d05d49bf 100644 --- a/test/src/main/java/org/springframework/ws/mock/server/WebServiceMock.java +++ b/test/src/main/java/org/springframework/ws/mock/server/WebServiceMock.java @@ -22,18 +22,50 @@ 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.context.DefaultMessageContext; +import org.springframework.ws.context.MessageContext; +import org.springframework.ws.mock.support.PayloadDiffMatcher; +import org.springframework.ws.transport.WebServiceMessageReceiver; import org.springframework.xml.transform.ResourceSource; +import static org.springframework.ws.mock.support.Assert.fail; + /** * @author Arjen Poutsma */ public abstract class WebServiceMock { - + @SuppressWarnings("unchecked") public static ResponseActions receiveMessage(RequestCreator requestCreator) { + final WebServiceTestContext testContext = WebServiceTestContextHolder.get(); + Assert.state(testContext != null, "No test context found. Did you annotate your test class with " + + "@TestExecutionListeners(WebServiceTestExecutionListener.class) ?"); + + try { + WebServiceMessageFactory messageFactory = testContext.getMessageFactory(); + WebServiceMessage request = requestCreator.createRequest(messageFactory); + + MessageContext messageContext = new DefaultMessageContext(request, messageFactory); + + WebServiceMessageReceiver messageReceiver = testContext.getMessageReceiver(); + messageReceiver.receive(messageContext); + + return new ResponseActions() { + public ResponseActions andExpect(ResponseMatcher responseMatcher) { + testContext.addResponseMatcher(responseMatcher); + return this; + } + }; + } + catch (Exception ex) { + fail(ex.getMessage()); + } return null; } + // RequestCreators + public static RequestCreator withPayload(Source payload) { Assert.notNull(payload, "'payload' must not be null"); return new PayloadRequestCreator(payload); @@ -44,9 +76,25 @@ public abstract class WebServiceMock { return new PayloadRequestCreator(createResourceSource(payload)); } + // ResponseMatchers public static ResponseMatcher payload(Source payload) { - return null; + Assert.notNull(payload, "'payload' must not be null"); + return createPayloadDiffMatcher(payload); + } + + public static ResponseMatcher payload(Resource payload) { + Assert.notNull(payload, "'payload' must not be null"); + return createPayloadDiffMatcher(createResourceSource(payload)); + } + + private static ResponseMatcher createPayloadDiffMatcher(Source payload) { + final PayloadDiffMatcher matcher = new PayloadDiffMatcher(payload); + return new ResponseMatcher() { + public void match(WebServiceMessage response) throws IOException, AssertionError { + matcher.match(response); + } + }; } /** @@ -61,8 +109,6 @@ public abstract class WebServiceMock { }; } - - private static ResourceSource createResourceSource(Resource resource) { try { return new ResourceSource(resource); @@ -72,4 +118,5 @@ public abstract class WebServiceMock { } } + } diff --git a/test/src/main/java/org/springframework/ws/mock/server/WebServiceTestContext.java b/test/src/main/java/org/springframework/ws/mock/server/WebServiceTestContext.java new file mode 100644 index 00000000..59fec633 --- /dev/null +++ b/test/src/main/java/org/springframework/ws/mock/server/WebServiceTestContext.java @@ -0,0 +1,54 @@ +/* + * 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.mock.server; + +import java.util.ArrayList; +import java.util.List; + +import org.springframework.util.Assert; +import org.springframework.ws.WebServiceMessageFactory; +import org.springframework.ws.transport.WebServiceMessageReceiver; + +/** + * @author Arjen Poutsma + */ +class WebServiceTestContext { + + private final WebServiceMessageReceiver messageReceiver; + + private final WebServiceMessageFactory messageFactory; + + private final List responseMatchers = new ArrayList(); + + public WebServiceTestContext(WebServiceMessageReceiver messageReceiver, WebServiceMessageFactory messageFactory) { + this.messageReceiver = messageReceiver; + this.messageFactory = messageFactory; + } + + WebServiceMessageReceiver getMessageReceiver() { + return messageReceiver; + } + + WebServiceMessageFactory getMessageFactory() { + return messageFactory; + } + + void addResponseMatcher(ResponseMatcher responseMatcher) { + Assert.notNull(responseMatcher, "'responseMatcher' must not be null"); + responseMatchers.add(responseMatcher); + } +} diff --git a/test/src/main/java/org/springframework/ws/mock/server/WebServiceTestContextHolder.java b/test/src/main/java/org/springframework/ws/mock/server/WebServiceTestContextHolder.java new file mode 100644 index 00000000..1d7ed4fe --- /dev/null +++ b/test/src/main/java/org/springframework/ws/mock/server/WebServiceTestContextHolder.java @@ -0,0 +1,51 @@ +/* + * 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.mock.server; + +import org.springframework.core.NamedThreadLocal; + +/** + * @author Arjen Poutsma + * @since 2.0 + */ +class WebServiceTestContextHolder { + + private static final NamedThreadLocal webServiceTestContextHolder = + new NamedThreadLocal("Web Service Test Context"); + + /** + * Associate the given {@link WebServiceTestContext} with the current thread. + */ + public static void set(WebServiceTestContext messageReceiver) { + webServiceTestContextHolder.set(messageReceiver); + } + + /** + * Return the {@link WebServiceTestContext} associated with the current thread, if any. + */ + public static WebServiceTestContext get() { + return webServiceTestContextHolder.get(); + } + + /** + * Clears the holder. + */ + public static void clear() { + set(null); + } + +} diff --git a/test/src/main/java/org/springframework/ws/mock/server/WebServiceTestExecutionListener.java b/test/src/main/java/org/springframework/ws/mock/server/WebServiceTestExecutionListener.java new file mode 100644 index 00000000..c71835f0 --- /dev/null +++ b/test/src/main/java/org/springframework/ws/mock/server/WebServiceTestExecutionListener.java @@ -0,0 +1,97 @@ +/* + * 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.mock.server; + +import java.util.Map; + +import org.springframework.beans.factory.BeanInitializationException; +import org.springframework.context.ApplicationContext; +import org.springframework.test.context.TestContext; +import org.springframework.test.context.support.AbstractTestExecutionListener; +import org.springframework.util.ClassUtils; +import org.springframework.ws.WebServiceMessageFactory; +import org.springframework.ws.soap.saaj.SaajSoapMessageFactory; +import org.springframework.ws.soap.server.SoapMessageDispatcher; +import org.springframework.ws.transport.WebServiceMessageReceiver; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +/** + * @author Arjen Poutsma + */ +public class WebServiceTestExecutionListener extends AbstractTestExecutionListener { + + private static final Log logger = LogFactory.getLog(WebServiceTestExecutionListener.class); + + @Override + public void beforeTestClass(TestContext testContext) throws Exception { + ApplicationContext applicationContext = testContext.getApplicationContext(); + WebServiceMessageReceiver messageReceiver = getMessageReceiver(applicationContext); + WebServiceMessageFactory messageFactory = getMessageFactory(applicationContext); + WebServiceTestContext context = new WebServiceTestContext(messageReceiver, messageFactory); + WebServiceTestContextHolder.set(context); + } + + private WebServiceMessageReceiver getMessageReceiver(ApplicationContext applicationContext) { + WebServiceMessageReceiver messageReceiver = getStrategy(applicationContext, WebServiceMessageReceiver.class); + if (messageReceiver == null) { + if (logger.isDebugEnabled()) { + logger.debug("No WebServiceMessageReceiver found, using default"); + } + SoapMessageDispatcher soapMessageDispatcher = new SoapMessageDispatcher(); + soapMessageDispatcher.setApplicationContext(applicationContext); + messageReceiver = soapMessageDispatcher; + } + return messageReceiver; + } + + private WebServiceMessageFactory getMessageFactory(ApplicationContext applicationContext) throws Exception { + WebServiceMessageFactory messageFactory = getStrategy(applicationContext, WebServiceMessageFactory.class); + if (messageFactory == null) { + if (logger.isDebugEnabled()) { + logger.debug("No WebServiceMessageFactory found, using default"); + } + SaajSoapMessageFactory saajSoapMessageFactory = new SaajSoapMessageFactory(); + saajSoapMessageFactory.afterPropertiesSet(); + messageFactory = saajSoapMessageFactory; + } + return messageFactory; + } + + private T getStrategy(ApplicationContext applicationContext, Class strategyInterface) { + Map map = applicationContext.getBeansOfType(strategyInterface); + if (map.isEmpty()) { + return null; + } + else if (map.size() == 1) { + Map.Entry entry = map.entrySet().iterator().next(); + if (logger.isDebugEnabled()) { + logger.debug("Using " + ClassUtils.getShortName(strategyInterface) + " [" + entry.getKey() + "]"); + } + return entry.getValue(); + } + else { + throw new BeanInitializationException("Could not find exactly 1 Message Dispatcher in application context"); + } + } + + @Override + public void afterTestClass(TestContext testContext) throws Exception { + WebServiceTestContextHolder.clear(); + } +} diff --git a/test/src/main/java/org/springframework/ws/mock/client/Assert.java b/test/src/main/java/org/springframework/ws/mock/support/Assert.java similarity index 86% rename from test/src/main/java/org/springframework/ws/mock/client/Assert.java rename to test/src/main/java/org/springframework/ws/mock/support/Assert.java index c8a5caff..9c9c29fd 100644 --- a/test/src/main/java/org/springframework/ws/mock/client/Assert.java +++ b/test/src/main/java/org/springframework/ws/mock/support/Assert.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.ws.mock.client; +package org.springframework.ws.mock.support; /** * JUnit independent assertion class. @@ -23,14 +23,14 @@ package org.springframework.ws.mock.client; * @author Arjen Poutsma * @since 2.0 */ -class Assert { +public class Assert { /** * Fails a test with the given message. * * @param message the message */ - static void fail(String message) { + public static void fail(String message) { throw new AssertionError(message); } @@ -40,7 +40,7 @@ class Assert { * @param message the message * @param condition the condition to test for */ - static void assertTrue(String message, boolean condition) { + public static void assertTrue(String message, boolean condition) { if (!condition) { fail(message); } @@ -53,7 +53,7 @@ class Assert { * @param expected the expected value * @param actual the actual value */ - static void assertEquals(String message, Object expected, Object actual) { + public static void assertEquals(String message, Object expected, Object actual) { if (expected == null && actual == null) { return; } diff --git a/test/src/main/java/org/springframework/ws/mock/client/DiffMatcher.java b/test/src/main/java/org/springframework/ws/mock/support/DiffMatcher.java similarity index 74% rename from test/src/main/java/org/springframework/ws/mock/client/DiffMatcher.java rename to test/src/main/java/org/springframework/ws/mock/support/DiffMatcher.java index 7871a121..b8254b9a 100644 --- a/test/src/main/java/org/springframework/ws/mock/client/DiffMatcher.java +++ b/test/src/main/java/org/springframework/ws/mock/support/DiffMatcher.java @@ -14,34 +14,31 @@ * limitations under the License. */ -package org.springframework.ws.mock.client; +package org.springframework.ws.mock.support; import java.io.IOException; -import java.net.URI; import org.springframework.ws.WebServiceMessage; -import org.springframework.xml.transform.TransformerObjectSupport; import org.custommonkey.xmlunit.Diff; import org.custommonkey.xmlunit.XMLUnit; -import static org.springframework.ws.mock.client.Assert.assertTrue; -import static org.springframework.ws.mock.client.Assert.fail; +import static org.springframework.ws.mock.support.Assert.assertTrue; +import static org.springframework.ws.mock.support.Assert.fail; /** - * Implementation of {@link RequestMatcher} based on XMLUnit's {@link Diff}. + * Implementation of {@link org.springframework.ws.mock.client.RequestMatcher} based on XMLUnit's {@link Diff}. * * @author Arjen Poutsma * @since 2.0 */ -abstract class DiffMatcher extends TransformerObjectSupport implements RequestMatcher { +public abstract class DiffMatcher { static { XMLUnit.setIgnoreWhitespace(true); } - - public final void match(URI uri, WebServiceMessage request) throws IOException, AssertionError { + public final void match(WebServiceMessage request) throws IOException, AssertionError { try { Diff diff = createDiff(request); assertTrue("Messages are different, " + diff.toString(), diff.similar()); diff --git a/test/src/main/java/org/springframework/ws/mock/client/PayloadDiffMatcher.java b/test/src/main/java/org/springframework/ws/mock/support/PayloadDiffMatcher.java similarity index 82% rename from test/src/main/java/org/springframework/ws/mock/client/PayloadDiffMatcher.java rename to test/src/main/java/org/springframework/ws/mock/support/PayloadDiffMatcher.java index 6ced4252..cf50bc4e 100644 --- a/test/src/main/java/org/springframework/ws/mock/client/PayloadDiffMatcher.java +++ b/test/src/main/java/org/springframework/ws/mock/support/PayloadDiffMatcher.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.ws.mock.client; +package org.springframework.ws.mock.support; import javax.xml.transform.Source; import javax.xml.transform.TransformerException; @@ -22,11 +22,12 @@ import javax.xml.transform.dom.DOMResult; import org.springframework.util.Assert; import org.springframework.ws.WebServiceMessage; +import org.springframework.xml.transform.TransformerHelper; import org.custommonkey.xmlunit.Diff; import org.w3c.dom.Document; -import static org.springframework.ws.mock.client.Assert.fail; +import static org.springframework.ws.mock.support.Assert.fail; /** * Matches {@link Source} payloads. @@ -35,11 +36,13 @@ import static org.springframework.ws.mock.client.Assert.fail; * @author Lukas Krecan * @since 2.0 */ -class PayloadDiffMatcher extends DiffMatcher { +public class PayloadDiffMatcher extends DiffMatcher { private final Source expected; - PayloadDiffMatcher(Source expected) { + private final TransformerHelper transformerHelper = new TransformerHelper(); + + public PayloadDiffMatcher(Source expected) { Assert.notNull(expected, "'expected' must not be null"); this.expected = expected; } @@ -61,7 +64,7 @@ class PayloadDiffMatcher extends DiffMatcher { private Document createDocumentFromSource(Source source) throws TransformerException { DOMResult result = new DOMResult(); - transform(source, result); + transformerHelper.transform(source, result); return (Document) result.getNode(); } } \ No newline at end of file diff --git a/test/src/test/java/org/springframework/ws/mock/client/integration/ClientIntegrationTest.java b/test/src/test/java/org/springframework/ws/mock/client/integration/ClientIntegrationTest.java index 97766394..9b76727d 100644 --- a/test/src/test/java/org/springframework/ws/mock/client/integration/ClientIntegrationTest.java +++ b/test/src/test/java/org/springframework/ws/mock/client/integration/ClientIntegrationTest.java @@ -53,10 +53,12 @@ public class ClientIntegrationTest { @Test public void basic() throws Exception { - Source expectedRequestPayload = new StringSource("" + - "John Doe" + ""); - Source responsePayload = new StringSource("" + - "10" + ""); + Source expectedRequestPayload = new StringSource( + "" + + "John Doe" + ""); + Source responsePayload = new StringSource( + "" + + "10" + ""); expect(payload(expectedRequestPayload)).andRespond(withPayload(responsePayload)); @@ -65,7 +67,7 @@ public class ClientIntegrationTest { CustomerCountResponse response = (CustomerCountResponse) webServiceTemplate.marshalSendAndReceive(request); assertEquals(10, response.getCustomerCount()); - + verifyConnections(); } diff --git a/test/src/test/java/org/springframework/ws/mock/integration/CustomerCountRequest.java b/test/src/test/java/org/springframework/ws/mock/integration/CustomerCountRequest.java index 0bb3cb5f..f748c43f 100644 --- a/test/src/test/java/org/springframework/ws/mock/integration/CustomerCountRequest.java +++ b/test/src/test/java/org/springframework/ws/mock/integration/CustomerCountRequest.java @@ -19,12 +19,12 @@ package org.springframework.ws.mock.integration; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; -@XmlRootElement(namespace = "http://springframework.org/client") +@XmlRootElement(namespace = "http://springframework.org/spring-ws") public class CustomerCountRequest { private String customerName; - @XmlElement(namespace = "http://springframework.org/client") + @XmlElement(namespace = "http://springframework.org/spring-ws") public String getCustomerName() { return customerName; } diff --git a/test/src/test/java/org/springframework/ws/mock/integration/CustomerCountResponse.java b/test/src/test/java/org/springframework/ws/mock/integration/CustomerCountResponse.java index 503a2b05..3a30eb70 100644 --- a/test/src/test/java/org/springframework/ws/mock/integration/CustomerCountResponse.java +++ b/test/src/test/java/org/springframework/ws/mock/integration/CustomerCountResponse.java @@ -19,12 +19,12 @@ package org.springframework.ws.mock.integration; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; -@XmlRootElement(namespace = "http://springframework.org/client") +@XmlRootElement(namespace = "http://springframework.org/spring-ws") public class CustomerCountResponse { private int customerCount; - @XmlElement(namespace = "http://springframework.org/client") + @XmlElement(namespace = "http://springframework.org/spring-ws") public int getCustomerCount() { return customerCount; } diff --git a/test/src/test/java/org/springframework/ws/mock/server/integration/ServerIntegrationTest.java b/test/src/test/java/org/springframework/ws/mock/server/integration/ServerIntegrationTest.java index 546c352f..b0c9f433 100644 --- a/test/src/test/java/org/springframework/ws/mock/server/integration/ServerIntegrationTest.java +++ b/test/src/test/java/org/springframework/ws/mock/server/integration/ServerIntegrationTest.java @@ -19,7 +19,9 @@ package org.springframework.ws.mock.server.integration; import javax.xml.transform.Source; import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.TestExecutionListeners; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.ws.mock.server.WebServiceTestExecutionListener; import org.springframework.xml.transform.StringSource; import org.junit.Test; @@ -32,14 +34,18 @@ import static org.springframework.ws.mock.server.WebServiceMock.*; */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("integration-test.xml") +@TestExecutionListeners(WebServiceTestExecutionListener.class) public class ServerIntegrationTest { @Test public void basic() throws Exception { - Source requestPayload = new StringSource("" + + Source requestPayload = new StringSource("" + "John Doe" + ""); - Source responsePayload = new StringSource("" + - "10" + ""); + Source responsePayload = new StringSource( + "" + + "10" + ""); + +// expect(payload(responsePayload)).andExpect(anything()).whenReceivingRequest(withPayload(requestPayload)); receiveMessage(withPayload(requestPayload)).andExpect(payload(responsePayload)); } diff --git a/test/src/test/java/org/springframework/ws/mock/client/PayloadDiffMatcherTest.java b/test/src/test/java/org/springframework/ws/mock/support/PayloadDiffMatcherTest.java similarity index 94% rename from test/src/test/java/org/springframework/ws/mock/client/PayloadDiffMatcherTest.java rename to test/src/test/java/org/springframework/ws/mock/support/PayloadDiffMatcherTest.java index e7da3822..1669f399 100644 --- a/test/src/test/java/org/springframework/ws/mock/client/PayloadDiffMatcherTest.java +++ b/test/src/test/java/org/springframework/ws/mock/support/PayloadDiffMatcherTest.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.ws.mock.client; +package org.springframework.ws.mock.support; import javax.xml.soap.MessageFactory; @@ -37,7 +37,7 @@ public class PayloadDiffMatcherTest { replay(message); PayloadDiffMatcher matcher = new PayloadDiffMatcher(new StringSource(xml)); - matcher.match(null, message); + matcher.match(message); verify(message); } @@ -51,7 +51,7 @@ public class PayloadDiffMatcherTest { String expected = ""; PayloadDiffMatcher matcher = new PayloadDiffMatcher(new StringSource(expected)); - matcher.match(null, message); + matcher.match(message); } @Test(expected = AssertionError.class) diff --git a/test/src/test/resources/org/springframework/ws/mock/server/integration/integration-test.xml b/test/src/test/resources/org/springframework/ws/mock/server/integration/integration-test.xml index b3fce516..61fb75cd 100644 --- a/test/src/test/resources/org/springframework/ws/mock/server/integration/integration-test.xml +++ b/test/src/test/resources/org/springframework/ws/mock/server/integration/integration-test.xml @@ -3,12 +3,10 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> - - - - - - + + + +