SWS-544 - Add test framework for Spring WS client
This commit is contained in:
@@ -677,7 +677,7 @@
|
||||
<dependency>
|
||||
<groupId>xmlunit</groupId>
|
||||
<artifactId>xmlunit</artifactId>
|
||||
<version>1.1</version>
|
||||
<version>1.3</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
|
||||
19
pom.xml
19
pom.xml
@@ -23,10 +23,10 @@
|
||||
<inceptionYear>2005</inceptionYear>
|
||||
<developers>
|
||||
<developer>
|
||||
<id>poutsma</id>
|
||||
<id>apoutsma</id>
|
||||
<name>Arjen Poutsma</name>
|
||||
<email>arjen.poutsma@springsource.com</email>
|
||||
<organization>SpringSource</organization>
|
||||
<email>apoutsma@vmware.com</email>
|
||||
<organization>SpringSource - a division of VMware</organization>
|
||||
<organizationUrl>http://www.springsource.com</organizationUrl>
|
||||
<roles>
|
||||
<role>Project Admin</role>
|
||||
@@ -34,6 +34,15 @@
|
||||
</roles>
|
||||
<timezone>+1</timezone>
|
||||
</developer>
|
||||
<developer>
|
||||
<id>tareqa</id>
|
||||
<name>Tareq Abed Rabbo</name>
|
||||
<email>tareq.abedrabbo@gmail.com</email>
|
||||
<roles>
|
||||
<role>Developer</role>
|
||||
</roles>
|
||||
<timezone>+1</timezone>
|
||||
</developer>
|
||||
</developers>
|
||||
<contributors>
|
||||
<contributor>
|
||||
@@ -52,7 +61,8 @@
|
||||
<name>Giovanni Cuccu</name>
|
||||
</contributor>
|
||||
<contributor>
|
||||
<name>Tareq Abed Rabbo</name>
|
||||
<name>Lukas Krecan</name>
|
||||
<url>http://blog.krecan.net</url>
|
||||
</contributor>
|
||||
</contributors>
|
||||
<organization>
|
||||
@@ -96,6 +106,7 @@
|
||||
<module>core</module>
|
||||
<module>support</module>
|
||||
<module>security</module>
|
||||
<module>test</module>
|
||||
<module>archetype</module>
|
||||
</modules>
|
||||
<profiles>
|
||||
|
||||
11
test/pom.xml
11
test/pom.xml
@@ -38,5 +38,16 @@
|
||||
<groupId>org.springframework.ws</groupId>
|
||||
<artifactId>spring-ws-core</artifactId>
|
||||
</dependency>
|
||||
<!-- Other dependencies -->
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>xmlunit</groupId>
|
||||
<artifactId>xmlunit</artifactId>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
/*
|
||||
* 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.client;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.springframework.ws.WebServiceMessage;
|
||||
import org.springframework.xml.transform.TransformerObjectSupport;
|
||||
|
||||
import org.custommonkey.xmlunit.Diff;
|
||||
|
||||
import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual;
|
||||
import static org.custommonkey.xmlunit.XMLAssert.fail;
|
||||
|
||||
/**
|
||||
* Implementation of {@link RequestMatcher} based on XMLUnit's {@link Diff}.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @since 2.0
|
||||
*/
|
||||
abstract class DiffMatcher extends TransformerObjectSupport implements RequestMatcher {
|
||||
|
||||
public final void match(WebServiceMessage request) throws IOException, AssertionError {
|
||||
try {
|
||||
Diff diff = createDiff(request);
|
||||
assertXMLEqual(diff, true);
|
||||
}
|
||||
catch (IOException ex) {
|
||||
throw ex;
|
||||
}
|
||||
catch (Exception ex) {
|
||||
fail("Could not create Diff: " + ex.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a {@link Diff} for the given request message.
|
||||
*
|
||||
* @param request the request message
|
||||
* @return the diff
|
||||
* @throws Exception in case of errors
|
||||
*/
|
||||
protected abstract Diff createDiff(WebServiceMessage request) throws Exception;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
* 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.client;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.springframework.ws.WebServiceMessage;
|
||||
|
||||
/**
|
||||
* @author Arjen Poutsma
|
||||
* @since 2.0
|
||||
*/
|
||||
class ExceptionResponseCallback implements ResponseCallback {
|
||||
|
||||
private final Exception exception;
|
||||
|
||||
ExceptionResponseCallback(IOException exception) {
|
||||
this.exception = exception;
|
||||
}
|
||||
|
||||
ExceptionResponseCallback(RuntimeException exception) {
|
||||
this.exception = exception;
|
||||
}
|
||||
|
||||
public void doWithResponse(WebServiceMessage message) throws IOException {
|
||||
if (exception instanceof IOException) {
|
||||
throw (IOException) exception;
|
||||
}
|
||||
else {
|
||||
throw (RuntimeException) exception;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,213 @@
|
||||
/*
|
||||
* 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.client;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import javax.xml.namespace.QName;
|
||||
import javax.xml.transform.Source;
|
||||
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.ws.WebServiceMessage;
|
||||
import org.springframework.ws.WebServiceMessageFactory;
|
||||
import org.springframework.ws.transport.FaultAwareWebServiceConnection;
|
||||
import org.springframework.ws.transport.WebServiceConnection;
|
||||
import org.springframework.xml.transform.TransformerObjectSupport;
|
||||
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
/**
|
||||
* Mock implementation of {@link WebServiceConnection}. Implements {@link RequestExpectations} and {@link
|
||||
* ResponseActions} to form a fluent API.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @author Lukas Krecan
|
||||
* @since 2.0
|
||||
*/
|
||||
class MockSenderConnection extends TransformerObjectSupport
|
||||
implements FaultAwareWebServiceConnection, RequestExpectations, ResponseActions {
|
||||
|
||||
private static final URI ANY_URI = URI.create("ANY");
|
||||
|
||||
private final List<RequestMatcher> requestMatchers = new LinkedList<RequestMatcher>();
|
||||
|
||||
private URI uri;
|
||||
|
||||
private ResponseCallback responseCallback;
|
||||
|
||||
private String errorMessage;
|
||||
|
||||
/** Creates a new {@code MockSenderConnection} for use with any URI. */
|
||||
MockSenderConnection() {
|
||||
this.uri = ANY_URI;
|
||||
}
|
||||
|
||||
/** Creates a new {@code MockSenderConnection} for use with the specified URI. */
|
||||
MockSenderConnection(URI uri) {
|
||||
this.uri = uri;
|
||||
}
|
||||
|
||||
// RequestExpectations implementation
|
||||
|
||||
public ResponseActions expectPayload(String payload) {
|
||||
Assert.notNull(payload, "'payload' must not be null");
|
||||
return addRequestMatcher(PayloadMatcher.createStringPayloadMatcher(payload));
|
||||
}
|
||||
|
||||
public ResponseActions expectPayload(Source payload) {
|
||||
Assert.notNull(payload, "'payload' must not be null");
|
||||
return addRequestMatcher(PayloadMatcher.createSourcePayloadMatcher(payload));
|
||||
}
|
||||
|
||||
public ResponseActions expectPayload(Resource payload) {
|
||||
Assert.notNull(payload, "'payload' must not be null");
|
||||
return addRequestMatcher(PayloadMatcher.createResourcePayloadMatcher(payload));
|
||||
}
|
||||
|
||||
public ResponseActions expectSoapHeader(QName soapHeaderName) {
|
||||
Assert.notNull(soapHeaderName, "'soapHeaderName' must not be null");
|
||||
return addRequestMatcher(new SoapHeaderMatcher(soapHeaderName));
|
||||
}
|
||||
|
||||
public ResponseActions addRequestMatcher(RequestMatcher requestMatcher) {
|
||||
requestMatchers.add(requestMatcher);
|
||||
return this;
|
||||
}
|
||||
|
||||
// ResponseActions implementation
|
||||
|
||||
public RequestExpectations and() {
|
||||
return this;
|
||||
}
|
||||
|
||||
public void andRespondWithPayload(Resource resource) {
|
||||
Assert.notNull(resource, "'resource' must not be null");
|
||||
try {
|
||||
this.responseCallback = new PayloadResponseCallback(resource);
|
||||
}
|
||||
catch (IOException ex) {
|
||||
fail("Could not open [" + resource + "]: " + ex.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public void andRespondWithPayload(String payload) {
|
||||
Assert.notNull(payload, "'payload' must not be null");
|
||||
this.responseCallback = new PayloadResponseCallback(payload);
|
||||
}
|
||||
|
||||
public void andRespondWithError(String errorMessage) {
|
||||
Assert.hasLength(errorMessage, "'errorMessage' must not be empty");
|
||||
this.errorMessage = errorMessage;
|
||||
}
|
||||
|
||||
public void andThrowException(IOException ioException) {
|
||||
Assert.notNull(ioException, "'ex' must not be null");
|
||||
this.responseCallback = new ExceptionResponseCallback(ioException);
|
||||
}
|
||||
|
||||
public void andThrowException(RuntimeException ex) {
|
||||
Assert.notNull(ex, "'ex' must not be null");
|
||||
this.responseCallback = new ExceptionResponseCallback(ex);
|
||||
}
|
||||
|
||||
public void andRespondWithMustUnderstandFault(String faultStringOrReason, Locale locale) {
|
||||
Assert.hasLength(faultStringOrReason, "'faultStringOrReason' must not be empty");
|
||||
this.responseCallback = SoapFaultResponseCallback.createMustUnderstandFault(faultStringOrReason, locale);
|
||||
}
|
||||
|
||||
public void andRespondWithClientOrSenderFault(String faultStringOrReason, Locale locale) {
|
||||
Assert.hasLength(faultStringOrReason, "'faultStringOrReason' must not be empty");
|
||||
this.responseCallback = SoapFaultResponseCallback.createClientOrSenderFault(faultStringOrReason, locale);
|
||||
}
|
||||
|
||||
public void andRespondWithServerOrReceiverFault(String faultStringOrReason, Locale locale) {
|
||||
Assert.hasLength(faultStringOrReason, "'faultStringOrReason' must not be empty");
|
||||
this.responseCallback = SoapFaultResponseCallback.createServerOrReceiverFault(faultStringOrReason, locale);
|
||||
}
|
||||
|
||||
public void andRespondWithVersionMismatchFault(String faultStringOrReason, Locale locale) {
|
||||
Assert.hasLength(faultStringOrReason, "'faultStringOrReason' must not be empty");
|
||||
this.responseCallback = SoapFaultResponseCallback.createVersionMismatchFault(faultStringOrReason, locale);
|
||||
}
|
||||
|
||||
public void setResponseCallback(ResponseCallback responseCallback) {
|
||||
Assert.notNull(responseCallback, "'responseCallback' must not be null");
|
||||
this.responseCallback = responseCallback;
|
||||
}
|
||||
|
||||
// WebServiceConnection implementation
|
||||
|
||||
public void send(WebServiceMessage message) throws IOException {
|
||||
if (!requestMatchers.isEmpty()) {
|
||||
for (RequestMatcher requestMatcher : requestMatchers) {
|
||||
requestMatcher.match(message);
|
||||
}
|
||||
}
|
||||
else {
|
||||
throw new AssertionError("Unexpected send() for [" + message + "]");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public WebServiceMessage receive(WebServiceMessageFactory messageFactory) throws IOException {
|
||||
if (responseCallback != null) {
|
||||
WebServiceMessage response = messageFactory.createWebServiceMessage();
|
||||
responseCallback.doWithResponse(response);
|
||||
return response;
|
||||
}
|
||||
else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public URI getUri() {
|
||||
return uri;
|
||||
}
|
||||
|
||||
public boolean hasAnyUri() {
|
||||
return ANY_URI.equals(uri);
|
||||
}
|
||||
|
||||
public boolean hasError() throws IOException {
|
||||
return errorMessage != null;
|
||||
}
|
||||
|
||||
public String getErrorMessage() throws IOException {
|
||||
return errorMessage;
|
||||
}
|
||||
|
||||
public boolean hasFault() throws IOException {
|
||||
return responseCallback instanceof SoapFaultResponseCallback;
|
||||
}
|
||||
|
||||
public void setFault(boolean fault) throws IOException {
|
||||
// Do nothing
|
||||
}
|
||||
|
||||
public void close() throws IOException {
|
||||
requestMatchers.clear();
|
||||
responseCallback = null;
|
||||
errorMessage = null;
|
||||
uri = null;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,192 @@
|
||||
/*
|
||||
* 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.client;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.ws.transport.WebServiceMessageSender;
|
||||
|
||||
/**
|
||||
* Main entry point for client-side Web Service testing. Typically used in combination with a {@link
|
||||
* org.springframework.ws.client.core.WebServiceTemplate WebServiceTemplate}.
|
||||
* <p/>
|
||||
* The typical usage of this mock is similar to any other mocking library (such as EasyMock), that is:
|
||||
* <ol>
|
||||
* <li>Inject this mock into the {@link org.springframework.ws.client.core.WebServiceTemplate WebServiceTemplate}.
|
||||
* See {@link org.springframework.ws.client.core.WebServiceTemplate#setMessageSender(WebServiceMessageSender) WebServiceTemplate.setMessageSender()}.</li>
|
||||
* <li>Set up expectations about the URI to connect to, and about the outgoing request message.
|
||||
* See {@link #whenConnecting()}, {@link #whenConnectingTo(String)}, and {@link RequestExpectations}.</li>
|
||||
* <li>Indicate the desired response actions. See {@link ResponseActions}.</li>
|
||||
* <li>Call {@link #replay()}.
|
||||
* <li>Use the {@code WebServiceTemplate} as normal.
|
||||
* <li>Call {@link #verify()}.
|
||||
* </ol>
|
||||
* Note that because of the 'fluent' API used by this class, you can typically use the Code Completion features
|
||||
* offered by your IDE to set up the mocks.
|
||||
* <p/>
|
||||
* For example:
|
||||
* <blockquote><pre>
|
||||
* // set up
|
||||
* MockWebServiceMessageSender mockMessageSender = new MockWebServiceMessageSender();
|
||||
* AirlineClient client = new AirlineClient(); // AirlineClient extends WebServiceGatewaySupport
|
||||
* <strong>client.getWebServiceTemplate().setMessageSender(mockMessageSender);</strong>
|
||||
* // expectations
|
||||
* String uri = "http://example.com/airline";
|
||||
* String expectedRequest = "<getFlightsRequest xmlns=\"http://example.com/\" >";
|
||||
* String response = "<getFlightsResponse xmlns=\"http://example.com/\">";
|
||||
* <strong>mockMessageSender.whenConnectingTo(uri).expectPayload(request).andRespondWithPayload(response);</strong>
|
||||
* <strong>mockMessageSender.replay();</strong>
|
||||
* // execution
|
||||
* StringResult result = new StringResult();
|
||||
* <strong>template.sendSourceAndReceiveToResult(uri, new StringSource(expectedRequest), stringResult)</strong>;
|
||||
* assertXMLEqual(response, result.toString()); // from XMLUnit
|
||||
* <strong>mockMessageSender.verify();</strong>
|
||||
* </pre></blockquote>
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @author Lukas Krecan
|
||||
* @see org.springframework.ws.client.core.WebServiceTemplate#setMessageSender
|
||||
* @see #whenConnecting()
|
||||
* @see #whenConnectingTo
|
||||
* @see RequestExpectations
|
||||
* @see ResponseActions
|
||||
* @see #replay()
|
||||
* @see #verify()
|
||||
* @since 2.0
|
||||
*/
|
||||
public class MockWebServiceMessageSender implements WebServiceMessageSender {
|
||||
|
||||
private final List<MockSenderConnection> expectedConnections = new LinkedList<MockSenderConnection>();
|
||||
|
||||
private Iterator<MockSenderConnection> connectionIterator;
|
||||
|
||||
/** Creates a new {@code MockWebServiceMessageSender}. */
|
||||
public MockWebServiceMessageSender() {
|
||||
reset();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
* <p/>
|
||||
* This implementation checks whether the given URI has been recorded as expected, and throws an {@code AssertionError} if not.
|
||||
*
|
||||
* @throws IllegalStateException if this mock is not in {@linkplain #replay() replay} state
|
||||
* @throws AssertionError if the given URI is not expected
|
||||
*/
|
||||
public MockSenderConnection createConnection(URI uri) throws IOException {
|
||||
Assert.notNull(uri, "'uri' must not be null");
|
||||
if (connectionIterator == null) {
|
||||
throw new IllegalStateException("Please call replay() after recording expected connections");
|
||||
}
|
||||
if (!connectionIterator.hasNext()) {
|
||||
throw new AssertionError("No further connections expected");
|
||||
}
|
||||
MockSenderConnection currentConnection = connectionIterator.next();
|
||||
if (!currentConnection.getUri().equals(uri) && !currentConnection.hasAnyUri()) {
|
||||
throw new AssertionError("Unexpected connection to \"" + uri + "\"");
|
||||
}
|
||||
return currentConnection;
|
||||
}
|
||||
|
||||
/** Always returns {@code true}. */
|
||||
public boolean supports(URI uri) {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets up an expected connection to the specific URI. Returns a {@link RequestExpectations} object that allows for
|
||||
* further expectations on the request.
|
||||
*
|
||||
* @param uri the URI expected to connect to
|
||||
* @return the request expectations
|
||||
*/
|
||||
public RequestExpectations whenConnectingTo(String uri) {
|
||||
Assert.hasLength(uri, "'uri' must not be empty");
|
||||
return whenConnectingTo(URI.create(uri));
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets up an expected connection to the specific URI. Returns a {@link RequestExpectations} object that allows for
|
||||
* further expectations on the request.
|
||||
*
|
||||
* @param uri the URI expected to connect to
|
||||
* @return the request expectations
|
||||
*/
|
||||
public RequestExpectations whenConnectingTo(URI uri) {
|
||||
Assert.notNull(uri, "'uri' must not be null");
|
||||
MockSenderConnection connection = new MockSenderConnection(uri);
|
||||
expectedConnections.add(connection);
|
||||
return connection;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets up an expected connection to <em>any</em> URI. Returns a {@link RequestExpectations} object that allows for
|
||||
* further expectations on the request.
|
||||
*
|
||||
* @return the request expectations
|
||||
*/
|
||||
public RequestExpectations whenConnecting() {
|
||||
MockSenderConnection connection = new MockSenderConnection();
|
||||
expectedConnections.add(connection);
|
||||
return connection;
|
||||
}
|
||||
|
||||
/** Resets the mock to the state directly after creation. */
|
||||
public void reset() {
|
||||
connectionIterator = null;
|
||||
expectedConnections.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* Switches the mock from record state to replay state.
|
||||
*
|
||||
* @throws IllegalStateException if this mock already is in replay state.
|
||||
*/
|
||||
public void replay() {
|
||||
Assert.state(connectionIterator == null, "Already in replay state");
|
||||
connectionIterator = expectedConnections.iterator();
|
||||
}
|
||||
|
||||
/**
|
||||
* Verifies that all expectations have been met.
|
||||
*
|
||||
* @throws IllegalStateException if this mock is in record state
|
||||
* @throws AssertionError if any expectation have not been met
|
||||
*/
|
||||
public void verify() {
|
||||
Assert.state(connectionIterator != null, "Calling verify() is only allowed after replay()");
|
||||
if (connectionIterator.hasNext()) {
|
||||
StringBuilder builder = new StringBuilder("Expected connection(s) to [");
|
||||
while (connectionIterator.hasNext()) {
|
||||
MockSenderConnection connection = connectionIterator.next();
|
||||
builder.append(connection.getUri());
|
||||
if (connectionIterator.hasNext()) {
|
||||
builder.append(", ");
|
||||
}
|
||||
}
|
||||
builder.append(']');
|
||||
throw new AssertionError(builder.toString());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
/*
|
||||
* 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.client;
|
||||
|
||||
import java.io.IOException;
|
||||
import javax.xml.transform.Source;
|
||||
import javax.xml.transform.TransformerException;
|
||||
import javax.xml.transform.dom.DOMResult;
|
||||
import javax.xml.transform.dom.DOMSource;
|
||||
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.ws.WebServiceMessage;
|
||||
import org.springframework.xml.sax.SaxUtils;
|
||||
import org.springframework.xml.transform.StringResult;
|
||||
|
||||
import org.custommonkey.xmlunit.Diff;
|
||||
import org.custommonkey.xmlunit.XMLUnit;
|
||||
import org.w3c.dom.Document;
|
||||
import org.xml.sax.InputSource;
|
||||
import org.xml.sax.SAXException;
|
||||
|
||||
import static org.custommonkey.xmlunit.XMLAssert.fail;
|
||||
|
||||
/**
|
||||
* Abstract base class that matches payloads.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @since 2.0
|
||||
*/
|
||||
abstract class PayloadMatcher extends DiffMatcher {
|
||||
|
||||
@Override
|
||||
protected final Diff createDiff(WebServiceMessage request) throws Exception {
|
||||
Source payload = request.getPayloadSource();
|
||||
if (payload == null) {
|
||||
fail("Request message does not contain payload");
|
||||
}
|
||||
return createDiff(payload);
|
||||
}
|
||||
|
||||
protected abstract Diff createDiff(Source payload) throws Exception;
|
||||
|
||||
public static PayloadMatcher createStringPayloadMatcher(final String control) {
|
||||
return new PayloadMatcher() {
|
||||
@Override
|
||||
protected Diff createDiff(Source payload) throws TransformerException, IOException, SAXException {
|
||||
StringResult result = new StringResult();
|
||||
transform(payload, result);
|
||||
return new Diff(control, result.toString());
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public static PayloadMatcher createResourcePayloadMatcher(final Resource control) {
|
||||
return new PayloadMatcher() {
|
||||
@Override
|
||||
protected Diff createDiff(Source payload) throws IOException, SAXException, TransformerException {
|
||||
InputSource controlInputSource = SaxUtils.createInputSource(control);
|
||||
Document controlDocument = XMLUnit.buildDocument(XMLUnit.newControlParser(), controlInputSource);
|
||||
DOMSource controlSource = new DOMSource(controlDocument);
|
||||
|
||||
DOMResult result = new DOMResult();
|
||||
transform(payload, result);
|
||||
DOMSource resultSource = new DOMSource(result.getNode());
|
||||
|
||||
return new Diff(controlSource, resultSource);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public static PayloadMatcher createSourcePayloadMatcher(final Source control) {
|
||||
return new PayloadMatcher() {
|
||||
@Override
|
||||
protected Diff createDiff(Source payload) throws Exception {
|
||||
if (control instanceof DOMSource && payload instanceof DOMSource) {
|
||||
return new Diff((DOMSource) control, (DOMSource) payload);
|
||||
}
|
||||
DOMResult controlResult = new DOMResult();
|
||||
transform(control, controlResult);
|
||||
DOMSource controlSource = new DOMSource(controlResult.getNode());
|
||||
|
||||
DOMResult payloadResult = new DOMResult();
|
||||
transform(payload, payloadResult);
|
||||
DOMSource payloadSource = new DOMSource(payloadResult.getNode());
|
||||
|
||||
return new Diff(controlSource, payloadSource);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -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.client;
|
||||
|
||||
import java.io.IOException;
|
||||
import javax.xml.transform.Source;
|
||||
import javax.xml.transform.TransformerException;
|
||||
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.ws.WebServiceMessage;
|
||||
import org.springframework.xml.transform.ResourceSource;
|
||||
import org.springframework.xml.transform.StringSource;
|
||||
import org.springframework.xml.transform.TransformerObjectSupport;
|
||||
|
||||
/** @author Arjen Poutsma */
|
||||
class PayloadResponseCallback extends TransformerObjectSupport implements ResponseCallback {
|
||||
|
||||
private final Source payload;
|
||||
|
||||
PayloadResponseCallback(Source payload) {
|
||||
this.payload = payload;
|
||||
}
|
||||
|
||||
PayloadResponseCallback(String payload) {
|
||||
this.payload = new StringSource(payload);
|
||||
}
|
||||
|
||||
PayloadResponseCallback(Resource payload) throws IOException {
|
||||
this.payload = new ResourceSource(payload);
|
||||
}
|
||||
|
||||
public void doWithResponse(WebServiceMessage message) throws IOException {
|
||||
try {
|
||||
transform(payload, message.getPayloadResult());
|
||||
}
|
||||
catch (TransformerException ex) {
|
||||
throw new AssertionError("Could not transform response payload to message: " + ex.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
/*
|
||||
* 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.client;
|
||||
|
||||
import javax.xml.namespace.QName;
|
||||
import javax.xml.transform.Source;
|
||||
|
||||
import org.springframework.core.io.Resource;
|
||||
|
||||
/**
|
||||
* Allows for setting expectations on the request. Implementations of this interface are returned by {@link
|
||||
* MockWebServiceMessageSender#whenConnectingTo(java.net.URI)}, and by {@link MockWebServiceMessageSender#whenConnecting()}.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @author Lukas Krecan
|
||||
* @since 2.0
|
||||
*/
|
||||
public interface RequestExpectations {
|
||||
|
||||
/**
|
||||
* Records that the mock will expect the given String XML payload. Returns a {@link ResponseActions} object that
|
||||
* allows for setting up the response.
|
||||
*
|
||||
* @param payload the String XML payload
|
||||
* @return the response actions
|
||||
*/
|
||||
ResponseActions expectPayload(String payload);
|
||||
|
||||
/**
|
||||
* Records that the mock will expect the given {@link Source} payload. Returns a {@link ResponseActions} object that
|
||||
* allows for setting up the response.
|
||||
*
|
||||
* @param payload the payload
|
||||
* @return the response actions
|
||||
*/
|
||||
ResponseActions expectPayload(Source payload);
|
||||
|
||||
/**
|
||||
* Records that the mock will expect the given {@link Resource} payload. Returns a {@link ResponseActions} object
|
||||
* that allows for setting up the response.
|
||||
*
|
||||
* @param payload the String XML payload
|
||||
* @return the response actions
|
||||
*/
|
||||
ResponseActions expectPayload(Resource payload);
|
||||
|
||||
/**
|
||||
* Records that the mock will expect the given SOAP header to exist on the outgoing message. Returns a {@link
|
||||
* ResponseActions} object that allows for setting up the response.
|
||||
*
|
||||
* @param soapHeaderName the qualified name of the SOAP header to expect
|
||||
* @return the response actions
|
||||
*/
|
||||
ResponseActions expectSoapHeader(QName soapHeaderName);
|
||||
|
||||
/**
|
||||
* Adds the {@link RequestMatcher} to the list of expectations. Returns a {@link ResponseActions} object that allows
|
||||
* for setting up the response.
|
||||
*
|
||||
* @param requestMatcher the request matcher
|
||||
* @return the response actions
|
||||
*/
|
||||
ResponseActions addRequestMatcher(RequestMatcher requestMatcher);
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
* 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.client;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.springframework.ws.WebServiceMessage;
|
||||
|
||||
/**
|
||||
* Defines the contract for matching request messages to expectations.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @author Lukas Krecan
|
||||
* @since 2.0
|
||||
*/
|
||||
public interface RequestMatcher {
|
||||
|
||||
/**
|
||||
* Matches the given request message against the expectations. Implementations typically make use of JUnit-based
|
||||
* assertions.
|
||||
*
|
||||
* @param request the request message to make assertions on
|
||||
* @throws IOException in case of I/O errors
|
||||
* @throws AssertionError if expectations are not met
|
||||
*/
|
||||
void match(WebServiceMessage request) throws IOException, AssertionError;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,120 @@
|
||||
/*
|
||||
* 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.client;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Locale;
|
||||
|
||||
import org.springframework.core.io.Resource;
|
||||
|
||||
/**
|
||||
* Allows for setting up responses. Implementations of this interface are returned by {@link RequestExpectations}.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @author Lukas Krecan
|
||||
* @since 2.0
|
||||
*/
|
||||
public interface ResponseActions {
|
||||
|
||||
/**
|
||||
* Allows for further expectations to be set on the request.
|
||||
*
|
||||
* @return the request expectations
|
||||
*/
|
||||
RequestExpectations and();
|
||||
|
||||
/**
|
||||
* Records that the mock will receive the given String XML as payload response.
|
||||
*
|
||||
* @param payload the response payload
|
||||
*/
|
||||
void andRespondWithPayload(String payload);
|
||||
|
||||
/**
|
||||
* Records that the mock will receive the given {@link Resource} as payload response.
|
||||
*
|
||||
* @param resource the response payload
|
||||
*/
|
||||
void andRespondWithPayload(Resource resource);
|
||||
|
||||
/**
|
||||
* Records that the mock will respond with the given error message.
|
||||
*
|
||||
* @param errorMessage the error message
|
||||
* @see org.springframework.ws.transport.WebServiceConnection#hasError()
|
||||
* @see org.springframework.ws.transport.WebServiceConnection#getErrorMessage()
|
||||
*/
|
||||
void andRespondWithError(String errorMessage);
|
||||
|
||||
/**
|
||||
* Records that the mock will respond by throwing the given {@link IOException}.
|
||||
*
|
||||
* @param ex the I/O exception
|
||||
*/
|
||||
void andThrowException(IOException ex);
|
||||
|
||||
/**
|
||||
* Records that the mock will respond by throwing the given runtime exception.
|
||||
*
|
||||
* @param ex the runtime exception
|
||||
*/
|
||||
void andThrowException(RuntimeException ex);
|
||||
|
||||
/**
|
||||
* Records that the mock will respond with a {@code MustUnderstand} fault.
|
||||
*
|
||||
* @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)
|
||||
*/
|
||||
void andRespondWithMustUnderstandFault(String faultStringOrReason, Locale locale);
|
||||
|
||||
/**
|
||||
* Records that the mock will respond with a {@code Client} (SOAP 1.1) or {@code Sender} (SOAP 1.2) fault.
|
||||
*
|
||||
* @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#addClientOrSenderFault(String, java.util.Locale)
|
||||
*/
|
||||
void andRespondWithClientOrSenderFault(String faultStringOrReason, Locale locale);
|
||||
|
||||
/**
|
||||
* Records that the mock will respond with a {@code Server} (SOAP 1.1) or {@code Receiver} (SOAP 1.2) fault.
|
||||
*
|
||||
* @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#addServerOrReceiverFault(String, java.util.Locale)
|
||||
*/
|
||||
void andRespondWithServerOrReceiverFault(String faultStringOrReason, Locale locale);
|
||||
|
||||
/**
|
||||
* Records that the mock will respond with a {@code VersionMismatch} fault.
|
||||
*
|
||||
* @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#addVersionMismatchFault(String, java.util.Locale)
|
||||
*/
|
||||
void andRespondWithVersionMismatchFault(String faultStringOrReason, Locale locale);
|
||||
|
||||
/**
|
||||
* Sets the {@link ResponseCallback} for this mock.
|
||||
*
|
||||
* @param responseCallback the response callback
|
||||
*/
|
||||
void setResponseCallback(ResponseCallback responseCallback);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
* 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.client;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.springframework.ws.WebServiceMessage;
|
||||
|
||||
/**
|
||||
* Callback interface for code that operates on a {@link WebServiceMessage}. Defines the contract for matching request
|
||||
* messages to expectations.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @author Lukas Krecan
|
||||
* @since 2.0
|
||||
*/
|
||||
public interface ResponseCallback {
|
||||
|
||||
void doWithResponse(WebServiceMessage message) throws IOException;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
/*
|
||||
* 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.client;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Locale;
|
||||
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.ws.WebServiceMessage;
|
||||
import org.springframework.ws.soap.SoapBody;
|
||||
import org.springframework.ws.soap.SoapMessage;
|
||||
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
/**
|
||||
* @author Arjen Poutsma
|
||||
* @since 2.0
|
||||
*/
|
||||
abstract class SoapFaultResponseCallback implements ResponseCallback {
|
||||
|
||||
public final void doWithResponse(WebServiceMessage message) throws IOException {
|
||||
Assert.isInstanceOf(SoapMessage.class, message);
|
||||
SoapMessage soapMessage = (SoapMessage) message;
|
||||
SoapBody soapBody = soapMessage.getSoapBody();
|
||||
if (soapBody == null) {
|
||||
fail("SOAP message [" + soapMessage + "] does not contain SOAP body");
|
||||
}
|
||||
addSoapFault(soapBody);
|
||||
}
|
||||
|
||||
public abstract void addSoapFault(SoapBody soapBody);
|
||||
|
||||
public static SoapFaultResponseCallback createMustUnderstandFault(final String faultStringOrReason, final Locale locale) {
|
||||
return new SoapFaultResponseCallback() {
|
||||
@Override
|
||||
public void addSoapFault(SoapBody soapBody) {
|
||||
soapBody.addMustUnderstandFault(faultStringOrReason, locale);
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
public static SoapFaultResponseCallback createClientOrSenderFault(final String faultStringOrReason, final Locale locale) {
|
||||
return new SoapFaultResponseCallback() {
|
||||
@Override
|
||||
public void addSoapFault(SoapBody soapBody) {
|
||||
soapBody.addClientOrSenderFault(faultStringOrReason, locale);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public static SoapFaultResponseCallback createServerOrReceiverFault(final String faultStringOrReason, final Locale locale) {
|
||||
return new SoapFaultResponseCallback() {
|
||||
@Override
|
||||
public void addSoapFault(SoapBody soapBody) {
|
||||
soapBody.addServerOrReceiverFault(faultStringOrReason, locale);
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
public static SoapFaultResponseCallback createVersionMismatchFault(final String faultStringOrReason, final Locale locale) {
|
||||
return new SoapFaultResponseCallback() {
|
||||
@Override
|
||||
public void addSoapFault(SoapBody soapBody) {
|
||||
soapBody.addVersionMismatchFault(faultStringOrReason, locale);
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
/*
|
||||
* 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.client;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Iterator;
|
||||
import javax.xml.namespace.QName;
|
||||
|
||||
import org.springframework.ws.WebServiceMessage;
|
||||
import org.springframework.ws.soap.SoapHeader;
|
||||
import org.springframework.ws.soap.SoapHeaderElement;
|
||||
import org.springframework.ws.soap.SoapMessage;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
/**
|
||||
* Matches SOAP headers.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @since 2.0
|
||||
*/
|
||||
class SoapHeaderMatcher implements RequestMatcher {
|
||||
|
||||
private final QName soapHeaderName;
|
||||
|
||||
SoapHeaderMatcher(QName soapHeaderName) {
|
||||
this.soapHeaderName = soapHeaderName;
|
||||
}
|
||||
|
||||
public void match(WebServiceMessage request) throws IOException, AssertionError {
|
||||
if (!(request instanceof SoapMessage)) {
|
||||
fail("Request message is not a SOAP message");
|
||||
return;
|
||||
}
|
||||
SoapMessage soapMessage = (SoapMessage) request;
|
||||
SoapHeader soapHeader = soapMessage.getSoapHeader();
|
||||
if (soapHeader == null) {
|
||||
fail("SOAP message [" + soapMessage + "] does not contain SOAP header");
|
||||
}
|
||||
Iterator<SoapHeaderElement> soapHeaderElementIterator = soapHeader.examineAllHeaderElements();
|
||||
boolean found = false;
|
||||
while (soapHeaderElementIterator.hasNext()) {
|
||||
SoapHeaderElement soapHeaderElement = soapHeaderElementIterator.next();
|
||||
if (soapHeaderName.equals(soapHeaderElement.getName())) {
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
assertTrue("SOAP header [" + soapHeaderName + "] not found", found);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* 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.client;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class ExceptionResponseCallbackTest {
|
||||
|
||||
@Test(expected = IOException.class)
|
||||
public void ioException() throws Exception {
|
||||
ExceptionResponseCallback callback = new ExceptionResponseCallback(new IOException());
|
||||
|
||||
callback.doWithResponse(null);
|
||||
}
|
||||
|
||||
@Test(expected = RuntimeException.class)
|
||||
public void runtimeException() throws Exception {
|
||||
ExceptionResponseCallback callback = new ExceptionResponseCallback(new RuntimeException());
|
||||
|
||||
callback.doWithResponse(null);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
/*
|
||||
* 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.client;
|
||||
|
||||
import java.net.URI;
|
||||
|
||||
import org.springframework.ws.WebServiceMessage;
|
||||
import org.springframework.ws.WebServiceMessageFactory;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.easymock.EasyMock.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/** @author Arjen Poutsma */
|
||||
public class MockSenderConnectionTest {
|
||||
|
||||
private MockSenderConnection connection;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
connection = new MockSenderConnection();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void uri() throws Exception {
|
||||
assertTrue("connection does not have any URI", connection.hasAnyUri());
|
||||
URI uri = new URI("http://example.com");
|
||||
connection = new MockSenderConnection(uri);
|
||||
assertEquals("Invalid uri", uri, connection.getUri());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sendMatch() throws Exception {
|
||||
RequestMatcher requestMatcher1 = createMock("requestMatcher1", RequestMatcher.class);
|
||||
RequestMatcher requestMatcher2 = createMock("requestMatcher2", RequestMatcher.class);
|
||||
connection.addRequestMatcher(requestMatcher1);
|
||||
connection.addRequestMatcher(requestMatcher2);
|
||||
WebServiceMessage request = createMock("request", WebServiceMessage.class);
|
||||
|
||||
requestMatcher1.match(request);
|
||||
requestMatcher2.match(request);
|
||||
|
||||
replay(requestMatcher1, requestMatcher2, request);
|
||||
|
||||
connection.send(request);
|
||||
|
||||
|
||||
verify(requestMatcher1, requestMatcher2, request);
|
||||
}
|
||||
|
||||
@Test(expected = AssertionError.class)
|
||||
public void sendNonMatch() throws Exception {
|
||||
RequestMatcher requestMatcher1 = createMock("requestMatcher1", RequestMatcher.class);
|
||||
RequestMatcher requestMatcher2 = createMock("requestMatcher2", RequestMatcher.class);
|
||||
connection.addRequestMatcher(requestMatcher1);
|
||||
connection.addRequestMatcher(requestMatcher2);
|
||||
WebServiceMessage request = createMock("request", WebServiceMessage.class);
|
||||
|
||||
requestMatcher1.match(request);
|
||||
expectLastCall().andThrow(new AssertionError());
|
||||
|
||||
replay(requestMatcher1, requestMatcher2, request);
|
||||
|
||||
connection.send(request);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void receive() throws Exception {
|
||||
ResponseCallback responseCallback = createMock(ResponseCallback.class);
|
||||
WebServiceMessageFactory messageFactory = createMock(WebServiceMessageFactory.class);
|
||||
WebServiceMessage response = createMock("response", WebServiceMessage.class);
|
||||
connection.setResponseCallback(responseCallback);
|
||||
|
||||
expect(messageFactory.createWebServiceMessage()).andReturn(response);
|
||||
responseCallback.doWithResponse(response);
|
||||
|
||||
replay(responseCallback, messageFactory, response);
|
||||
|
||||
WebServiceMessage result = connection.receive(messageFactory);
|
||||
assertSame(response, result);
|
||||
|
||||
verify(responseCallback, messageFactory, response);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,136 @@
|
||||
/*
|
||||
* 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.client;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.springframework.ws.client.WebServiceIOException;
|
||||
import org.springframework.ws.client.WebServiceTransportException;
|
||||
import org.springframework.ws.client.core.WebServiceTemplate;
|
||||
import org.springframework.ws.soap.client.SoapFaultClientException;
|
||||
import org.springframework.xml.transform.StringResult;
|
||||
import org.springframework.xml.transform.StringSource;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual;
|
||||
|
||||
public class MockWebServiceMessageSenderTest {
|
||||
|
||||
private MockWebServiceMessageSender messageSender;
|
||||
|
||||
private WebServiceTemplate template;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
messageSender = new MockWebServiceMessageSender();
|
||||
template = new WebServiceTemplate();
|
||||
template.setMessageSender(messageSender);
|
||||
template.setDefaultUri("http://example.com/airline");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void normal() throws Exception {
|
||||
String request = "<request xmlns='http://example.com'/>";
|
||||
String response = "<response xmlns='http://example.com'/>";
|
||||
|
||||
messageSender.whenConnecting().expectPayload(request).andRespondWithPayload(response);
|
||||
messageSender.replay();
|
||||
|
||||
StringResult result = new StringResult();
|
||||
template.sendSourceAndReceiveToResult(new StringSource(request), result);
|
||||
assertXMLEqual(result.toString(), response);
|
||||
messageSender.verify();
|
||||
}
|
||||
|
||||
@Test(expected = AssertionError.class)
|
||||
public void invalidRequest() throws Exception {
|
||||
String expectedRequest = "<request1 xmlns='http://example.com'/>";
|
||||
|
||||
messageSender.whenConnecting().expectPayload(expectedRequest);
|
||||
messageSender.replay();
|
||||
|
||||
String realRequest = "<request2 xmlns='http://example.com'/>";
|
||||
template.sendSourceAndReceiveToResult(new StringSource(realRequest), new StringResult());
|
||||
messageSender.verify();
|
||||
}
|
||||
|
||||
@Test(expected = AssertionError.class)
|
||||
public void wrongUri() throws Exception {
|
||||
String expectedUri = "http://example.com/1";
|
||||
|
||||
messageSender.whenConnectingTo(expectedUri);
|
||||
messageSender.replay();
|
||||
|
||||
String realUri = "http://example.com/2";
|
||||
template.sendSourceAndReceiveToResult(realUri, null, null);
|
||||
messageSender.verify();
|
||||
}
|
||||
|
||||
@Test(expected = AssertionError.class)
|
||||
public void notAllUris() throws Exception {
|
||||
String expectedUri1 = "http://example.com/1";
|
||||
String expectedUri2 = "http://example.com/2";
|
||||
String request = "<request xmlns='http://example.com'/>";
|
||||
String response = "<response xmlns='http://example.com'/>";
|
||||
|
||||
|
||||
messageSender.whenConnectingTo(expectedUri1).expectPayload(request).andRespondWithPayload(response);
|
||||
messageSender.whenConnectingTo(expectedUri2).expectPayload(request).andRespondWithPayload(response);
|
||||
messageSender.replay();
|
||||
|
||||
template.sendSourceAndReceiveToResult(expectedUri1, new StringSource(request), new StringResult());
|
||||
messageSender.verify();
|
||||
}
|
||||
|
||||
@Test(expected = WebServiceTransportException.class)
|
||||
public void error() throws Exception {
|
||||
String request = "<root xmlns='http://example.com'/>";
|
||||
|
||||
messageSender.whenConnecting().expectPayload(request).andRespondWithError("Something went wrong");
|
||||
messageSender.replay();
|
||||
|
||||
template.sendSourceAndReceiveToResult(new StringSource(request), new StringResult());
|
||||
messageSender.verify();
|
||||
}
|
||||
|
||||
@Test(expected = WebServiceIOException.class)
|
||||
public void ioException() throws Exception {
|
||||
String request = "<root xmlns='http://example.com'/>";
|
||||
|
||||
IOException ex = new IOException("Something went wrong");
|
||||
messageSender.whenConnecting().expectPayload(request).andThrowException(ex);
|
||||
messageSender.replay();
|
||||
|
||||
template.sendSourceAndReceiveToResult(new StringSource(request), new StringResult());
|
||||
messageSender.verify();
|
||||
}
|
||||
|
||||
@Test(expected = SoapFaultClientException.class)
|
||||
public void soapFault() throws Exception {
|
||||
String request = "<root xmlns='http://example.com'/>";
|
||||
|
||||
String faultString = "Foo";
|
||||
messageSender.whenConnecting().expectPayload(request).andRespondWithMustUnderstandFault(faultString, null);
|
||||
messageSender.replay();
|
||||
|
||||
template.sendSourceAndReceiveToResult(new StringSource(request), new StringResult());
|
||||
messageSender.verify();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
/*
|
||||
* 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.client;
|
||||
|
||||
import org.springframework.core.io.ByteArrayResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.ws.WebServiceMessage;
|
||||
import org.springframework.xml.transform.StringSource;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.easymock.EasyMock.*;
|
||||
|
||||
public class PayloadMatcherTest {
|
||||
|
||||
@Test
|
||||
public void stringPayloadMatch() throws Exception {
|
||||
String xml = "<element xmlns='http://example.com'/>";
|
||||
WebServiceMessage message = createMock(WebServiceMessage.class);
|
||||
expect(message.getPayloadSource()).andReturn(new StringSource(xml));
|
||||
replay(message);
|
||||
|
||||
PayloadMatcher matcher = PayloadMatcher.createStringPayloadMatcher(xml);
|
||||
matcher.match(message);
|
||||
|
||||
verify(message);
|
||||
}
|
||||
|
||||
@Test(expected = AssertionError.class)
|
||||
public void nonMatch() throws Exception {
|
||||
String actual = "<element1 xmlns='http://example.com'/>";
|
||||
WebServiceMessage message = createMock(WebServiceMessage.class);
|
||||
expect(message.getPayloadSource()).andReturn(new StringSource(actual));
|
||||
replay(message);
|
||||
|
||||
String expected = "<element2 xmlns='http://example.com'/>";
|
||||
PayloadMatcher matcher = PayloadMatcher.createStringPayloadMatcher(expected);
|
||||
matcher.match(message);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resourcePayload() throws Exception {
|
||||
String xml = "<element xmlns='http://example.com'/>";
|
||||
WebServiceMessage message = createMock(WebServiceMessage.class);
|
||||
expect(message.getPayloadSource()).andReturn(new StringSource(xml));
|
||||
replay(message);
|
||||
|
||||
Resource resource = new ByteArrayResource(xml.getBytes());
|
||||
PayloadMatcher matcher = PayloadMatcher.createResourcePayloadMatcher(resource);
|
||||
matcher.match(message);
|
||||
|
||||
verify(message);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void sourcePayloadMatcher() throws Exception {
|
||||
String xml = "<element xmlns='http://example.com'/>";
|
||||
WebServiceMessage message = createMock(WebServiceMessage.class);
|
||||
expect(message.getPayloadSource()).andReturn(new StringSource(xml));
|
||||
replay(message);
|
||||
|
||||
Resource resource = new ByteArrayResource(xml.getBytes());
|
||||
PayloadMatcher matcher = PayloadMatcher.createSourcePayloadMatcher(new StringSource(xml));
|
||||
matcher.match(message);
|
||||
|
||||
verify(message);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
/*
|
||||
* 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.client;
|
||||
|
||||
import javax.xml.namespace.QName;
|
||||
import javax.xml.soap.MessageFactory;
|
||||
import javax.xml.soap.SOAPMessage;
|
||||
|
||||
import org.springframework.ws.WebServiceMessage;
|
||||
import org.springframework.ws.soap.SoapMessage;
|
||||
import org.springframework.ws.soap.saaj.SaajSoapMessage;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.easymock.EasyMock.createMock;
|
||||
|
||||
public class SoapHeaderMatcherTest {
|
||||
|
||||
private SoapHeaderMatcher matcher;
|
||||
|
||||
private QName expectedHeaderName;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
expectedHeaderName = new QName("http://example.com", "header");
|
||||
matcher = new SoapHeaderMatcher(expectedHeaderName);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void match() throws Exception {
|
||||
MessageFactory messageFactory = MessageFactory.newInstance();
|
||||
SOAPMessage saajMessage = messageFactory.createMessage();
|
||||
saajMessage.getSOAPHeader().addHeaderElement(expectedHeaderName);
|
||||
SoapMessage soapMessage = new SaajSoapMessage(saajMessage);
|
||||
|
||||
matcher.match(soapMessage);
|
||||
}
|
||||
|
||||
@Test(expected = AssertionError.class)
|
||||
public void nonMatch() throws Exception {
|
||||
MessageFactory messageFactory = MessageFactory.newInstance();
|
||||
SOAPMessage saajMessage = messageFactory.createMessage();
|
||||
SoapMessage soapMessage = new SaajSoapMessage(saajMessage);
|
||||
|
||||
matcher.match(soapMessage);
|
||||
}
|
||||
|
||||
@Test(expected = AssertionError.class)
|
||||
public void nonSoap() throws Exception {
|
||||
WebServiceMessage message = createMock(WebServiceMessage.class);
|
||||
|
||||
matcher.match(message);
|
||||
}
|
||||
|
||||
}
|
||||
6
test/src/test/resources/log4j.properties
Normal file
6
test/src/test/resources/log4j.properties
Normal file
@@ -0,0 +1,6 @@
|
||||
log4j.rootCategory=INFO, stdout
|
||||
log4j.logger.org.springframework.ws=DEBUG
|
||||
|
||||
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
|
||||
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
|
||||
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - <%m>%n
|
||||
@@ -4,9 +4,18 @@ Bundle-Vendor: SpringSource
|
||||
Bundle-Version: ${osgi.version}
|
||||
Bundle-ManifestVersion: 2
|
||||
Import-Template:
|
||||
javax.xml.namespace.*;version="0",
|
||||
javax.xml.parsers.*;version="0",
|
||||
javax.xml.transform.*;version="0",
|
||||
org.custommonkey.xmlunit.*;version="[1.3.0, 2.0.0)",
|
||||
org.junit.*;version="[4.7.0, 5.0.0)",
|
||||
org.springframework.core.*;version=${spring.framework.osgi.range},
|
||||
org.springframework.util.*;version=${spring.framework.osgi.range},
|
||||
org.springframework.ws.*;version=${osgi.range},
|
||||
org.springframework.xml.*;version=${osgi.range}
|
||||
Ignored-Existing-Headers:
|
||||
org.springframework.xml.*;version=${osgi.range},
|
||||
org.w3c.dom.*;version="0",
|
||||
org.xml.sax.*;version="0"
|
||||
Ignored-Existing-Headers:
|
||||
Bnd-LastModified,
|
||||
Import-Package,
|
||||
Export-Package,
|
||||
|
||||
Reference in New Issue
Block a user