SWS-632 - Create Server-Side testing framework
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||
<parent>
|
||||
<artifactId>spring-ws-parent</artifactId>
|
||||
<groupId>org.springframework.ws</groupId>
|
||||
@@ -51,6 +52,7 @@
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-test</artifactId>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
|
||||
@@ -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}.
|
||||
|
||||
@@ -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 AbstractResponseCreator<SoapMess
|
||||
public abstract void addSoapFault(SoapBody soapBody);
|
||||
|
||||
public static SoapFaultResponseCreator createMustUnderstandFault(final String faultStringOrReason,
|
||||
final Locale locale) {
|
||||
final Locale locale) {
|
||||
return new SoapFaultResponseCreator() {
|
||||
@Override
|
||||
public void addSoapFault(SoapBody soapBody) {
|
||||
@@ -56,7 +56,7 @@ abstract class SoapFaultResponseCreator extends AbstractResponseCreator<SoapMess
|
||||
}
|
||||
|
||||
public static SoapFaultResponseCreator createClientOrSenderFault(final String faultStringOrReason,
|
||||
final Locale locale) {
|
||||
final Locale locale) {
|
||||
return new SoapFaultResponseCreator() {
|
||||
@Override
|
||||
public void addSoapFault(SoapBody soapBody) {
|
||||
@@ -66,7 +66,7 @@ abstract class SoapFaultResponseCreator extends AbstractResponseCreator<SoapMess
|
||||
}
|
||||
|
||||
public static SoapFaultResponseCreator createServerOrReceiverFault(final String faultStringOrReason,
|
||||
final Locale locale) {
|
||||
final Locale locale) {
|
||||
return new SoapFaultResponseCreator() {
|
||||
@Override
|
||||
public void addSoapFault(SoapBody soapBody) {
|
||||
@@ -77,7 +77,7 @@ abstract class SoapFaultResponseCreator extends AbstractResponseCreator<SoapMess
|
||||
}
|
||||
|
||||
public static SoapFaultResponseCreator createVersionMismatchFault(final String faultStringOrReason,
|
||||
final Locale locale) {
|
||||
final Locale locale) {
|
||||
return new SoapFaultResponseCreator() {
|
||||
@Override
|
||||
public void addSoapFault(SoapBody soapBody) {
|
||||
|
||||
@@ -26,9 +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.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;
|
||||
|
||||
/**
|
||||
* Matches SOAP headers.
|
||||
|
||||
@@ -20,7 +20,7 @@ import java.net.URI;
|
||||
|
||||
import org.springframework.ws.WebServiceMessage;
|
||||
|
||||
import static org.springframework.ws.mock.client.Assert.assertEquals;
|
||||
import static org.springframework.ws.mock.support.Assert.assertEquals;
|
||||
|
||||
/**
|
||||
* Matches {@link URI}s.
|
||||
|
||||
@@ -27,6 +27,7 @@ import org.springframework.core.io.Resource;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.ws.WebServiceMessage;
|
||||
import org.springframework.ws.client.core.WebServiceTemplate;
|
||||
import org.springframework.ws.mock.support.PayloadDiffMatcher;
|
||||
import org.springframework.xml.transform.ResourceSource;
|
||||
import org.springframework.xml.validation.XmlValidator;
|
||||
import org.springframework.xml.validation.XmlValidatorFactory;
|
||||
@@ -35,28 +36,20 @@ import org.springframework.xml.validation.XmlValidatorFactory;
|
||||
* <strong>Main entry point for client-side Web service testing</strong>. Typically used to mock a {@link
|
||||
* WebServiceTemplate}, set up expectations on request messages, and create response messages.
|
||||
* <p/>
|
||||
* The typical usage of this mock is similar to any other mocking library (such as EasyMock), that is:
|
||||
* <ol>
|
||||
* <li>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: <ol>
|
||||
* <li>Statically import {@link org.springframework.ws.mock.client.WebServiceMock org.springframework.ws.mock.client.WebServiceMock.*}.
|
||||
* <li>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}.</li>
|
||||
* <li>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)}.</li>
|
||||
* <li>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.</li>
|
||||
* <li>Use the {@code WebServiceTemplate} as normal, either directly of through client code.
|
||||
* <li>Call {@link #verifyConnections()}.
|
||||
* </ol>
|
||||
* 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}.</li> <li>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)}.</li> <li>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.</li> <li>Use the {@code WebServiceTemplate} as normal, either directly of
|
||||
* through client code. <li>Call {@link #verifyConnections()}. </ol> 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.
|
||||
* <p/>
|
||||
* For example:
|
||||
* <blockquote><pre>
|
||||
@@ -68,35 +61,36 @@ import org.springframework.xml.validation.XmlValidatorFactory;
|
||||
* import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
* import org.springframework.xml.transform.StringSource;
|
||||
* <strong>import static org.springframework.ws.mock.client.WebServiceMock.*</strong>;
|
||||
*
|
||||
*
|
||||
* <p/>
|
||||
* <p/>
|
||||
* @RunWith(SpringJUnit4ClassRunner.class)
|
||||
* @ContextConfiguration("applicationContext.xml")
|
||||
* public class IntegrationTest {
|
||||
*
|
||||
* <p/>
|
||||
* // MyWebServiceClient extends WebServiceGatewaySupport, and is configured in applicationContext.xml
|
||||
* @Autowired
|
||||
* private MyWebServiceClient client;
|
||||
*
|
||||
* <p/>
|
||||
* @Before
|
||||
* public void setUpMocks() throws Exception {
|
||||
* <strong>mockWebServiceTemplate(client.getWebServiceTemplate())</strong>;
|
||||
* }
|
||||
*
|
||||
* <p/>
|
||||
* @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>");
|
||||
*
|
||||
* <p/>
|
||||
* <strong>expect(payload(requestPayload)).andRespond(withPayload(responsePayload));</strong>
|
||||
*
|
||||
* <p/>
|
||||
* // client.getCustomerCount() uses the WebServiceTemplate
|
||||
* int customerCount = client.getCustomerCount();
|
||||
* assertEquals(10, response.getCustomerCount());
|
||||
*
|
||||
* <p/>
|
||||
* <strong>verifyConnections();</strong>
|
||||
* }
|
||||
* }
|
||||
@@ -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);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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 {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -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<ResponseMatcher> responseMatchers = new ArrayList<ResponseMatcher>();
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -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<WebServiceTestContext> webServiceTestContextHolder =
|
||||
new NamedThreadLocal<WebServiceTestContext>("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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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> T getStrategy(ApplicationContext applicationContext, Class<T> strategyInterface) {
|
||||
Map<String, T> map = applicationContext.getBeansOfType(strategyInterface);
|
||||
if (map.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
else if (map.size() == 1) {
|
||||
Map.Entry<String, T> 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();
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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());
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -53,10 +53,12 @@ public class ClientIntegrationTest {
|
||||
|
||||
@Test
|
||||
public void basic() throws Exception {
|
||||
Source expectedRequestPayload = new StringSource("<customerCountRequest xmlns='http://springframework.org/client'>" +
|
||||
"<customerName>John Doe</customerName>" + "</customerCountRequest>");
|
||||
Source responsePayload = new StringSource("<customerCountResponse xmlns='http://springframework.org/client'>" +
|
||||
"<customerCount>10</customerCount>" + "</customerCountResponse>");
|
||||
Source expectedRequestPayload = new StringSource(
|
||||
"<customerCountRequest xmlns='http://springframework.org/spring-ws'>" +
|
||||
"<customerName>John Doe</customerName>" + "</customerCountRequest>");
|
||||
Source responsePayload = new StringSource(
|
||||
"<customerCountResponse xmlns='http://springframework.org/spring-ws'>" +
|
||||
"<customerCount>10</customerCount>" + "</customerCountResponse>");
|
||||
|
||||
expect(payload(expectedRequestPayload)).andRespond(withPayload(responsePayload));
|
||||
|
||||
@@ -65,7 +67,7 @@ public class ClientIntegrationTest {
|
||||
|
||||
CustomerCountResponse response = (CustomerCountResponse) webServiceTemplate.marshalSendAndReceive(request);
|
||||
assertEquals(10, response.getCustomerCount());
|
||||
|
||||
|
||||
verifyConnections();
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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("<customerCountRequest xmlns='http://springframework.org/client'>" +
|
||||
Source requestPayload = new StringSource("<customerCountRequest xmlns='http://springframework.org/spring-ws'>" +
|
||||
"<customerName>John Doe</customerName>" + "</customerCountRequest>");
|
||||
Source responsePayload = new StringSource("<customerCountResponse xmlns='http://springframework.org/client'>" +
|
||||
"<customerCount>10</customerCount>" + "</customerCountResponse>");
|
||||
Source responsePayload = new StringSource(
|
||||
"<customerCountResponse xmlns='http://springframework.org/spring-ws'>" +
|
||||
"<customerCount>10</customerCount>" + "</customerCountResponse>");
|
||||
|
||||
// expect(payload(responsePayload)).andExpect(anything()).whenReceivingRequest(withPayload(requestPayload));
|
||||
|
||||
receiveMessage(withPayload(requestPayload)).andExpect(payload(responsePayload));
|
||||
}
|
||||
|
||||
@@ -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 = "<element2 xmlns='http://example.com'/>";
|
||||
PayloadDiffMatcher matcher = new PayloadDiffMatcher(new StringSource(expected));
|
||||
matcher.match(null, message);
|
||||
matcher.match(message);
|
||||
}
|
||||
|
||||
@Test(expected = AssertionError.class)
|
||||
@@ -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">
|
||||
|
||||
<bean id="webServiceTemplate" class="org.springframework.ws.client.core.WebServiceTemplate">
|
||||
<property name="marshaller" ref="marshaller"/>
|
||||
<property name="unmarshaller" ref="marshaller"/>
|
||||
<property name="defaultUri" value="http://example.com"/>
|
||||
</bean>
|
||||
|
||||
<bean class="org.springframework.ws.server.endpoint.mapping.XmlRootElementEndpointMapping"/>
|
||||
|
||||
<bean class="org.springframework.ws.mock.server.integration.CustomerEndpoint"/>
|
||||
|
||||
<bean id="marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
|
||||
<property name="classesToBeBound">
|
||||
<list>
|
||||
|
||||
Reference in New Issue
Block a user