Moving client2 to client
This commit is contained in:
@@ -1,59 +0,0 @@
|
||||
/*
|
||||
* Copyright 2005-2010 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.ws.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;
|
||||
|
||||
}
|
||||
@@ -1,47 +0,0 @@
|
||||
/*
|
||||
* Copyright 2005-2010 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.ws.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 request, WebServiceMessage response) throws IOException {
|
||||
if (exception instanceof IOException) {
|
||||
throw (IOException) exception;
|
||||
}
|
||||
else {
|
||||
throw (RuntimeException) exception;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,221 +0,0 @@
|
||||
/*
|
||||
* Copyright 2005-2010 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.ws.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.ResourceSource;
|
||||
import org.springframework.xml.transform.StringSource;
|
||||
|
||||
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 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 WebServiceMessage request;
|
||||
|
||||
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(new PayloadMatcher(new StringSource(payload)));
|
||||
}
|
||||
|
||||
public ResponseActions expectPayload(Source payload) {
|
||||
Assert.notNull(payload, "'payload' must not be null");
|
||||
return addRequestMatcher(new PayloadMatcher(payload));
|
||||
}
|
||||
|
||||
public ResponseActions expectPayload(Resource payload) {
|
||||
Assert.notNull(payload, "'payload' must not be null");
|
||||
try {
|
||||
return addRequestMatcher(new PayloadMatcher(new ResourceSource(payload)));
|
||||
}
|
||||
catch (IOException ex) {
|
||||
throw new IllegalArgumentException(payload + " could not be opened", ex);
|
||||
}
|
||||
}
|
||||
|
||||
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 + "]");
|
||||
}
|
||||
this.request = message;
|
||||
}
|
||||
|
||||
public WebServiceMessage receive(WebServiceMessageFactory messageFactory) throws IOException {
|
||||
if (responseCallback != null) {
|
||||
WebServiceMessage response = messageFactory.createWebServiceMessage();
|
||||
responseCallback.doWithResponse(request, 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();
|
||||
request = null;
|
||||
responseCallback = null;
|
||||
errorMessage = null;
|
||||
uri = null;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,192 +0,0 @@
|
||||
/*
|
||||
* Copyright 2005-2010 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.ws.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());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,67 +0,0 @@
|
||||
/*
|
||||
* Copyright 2005-2010 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.ws.mock.client;
|
||||
|
||||
import javax.xml.transform.Source;
|
||||
import javax.xml.transform.TransformerException;
|
||||
import javax.xml.transform.dom.DOMResult;
|
||||
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.ws.WebServiceMessage;
|
||||
|
||||
import org.custommonkey.xmlunit.Diff;
|
||||
import org.w3c.dom.Document;
|
||||
|
||||
import static junit.framework.Assert.fail;
|
||||
|
||||
/**
|
||||
* Abstract base class that matches payloads.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @author Lukas Krecan
|
||||
* @since 2.0
|
||||
*/
|
||||
class PayloadMatcher extends DiffMatcher {
|
||||
|
||||
private final Source expected;
|
||||
|
||||
PayloadMatcher(Source expected) {
|
||||
Assert.notNull(expected, "'expected' must not be null");
|
||||
this.expected = expected;
|
||||
}
|
||||
|
||||
@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 Diff createDiff(Source payload) throws TransformerException {
|
||||
Document expectedDocument = createDocumentFromSource(expected);
|
||||
Document actualDocument = createDocumentFromSource(payload);
|
||||
return new Diff(expectedDocument, actualDocument);
|
||||
}
|
||||
|
||||
private Document createDocumentFromSource(Source source) throws TransformerException {
|
||||
DOMResult result = new DOMResult();
|
||||
transform(source, result);
|
||||
return (Document) result.getNode();
|
||||
}
|
||||
}
|
||||
@@ -1,54 +0,0 @@
|
||||
/*
|
||||
* Copyright 2005-2010 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.ws.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 request, WebServiceMessage response) throws IOException {
|
||||
try {
|
||||
transform(payload, response.getPayloadResult());
|
||||
}
|
||||
catch (TransformerException ex) {
|
||||
throw new AssertionError("Could not transform response payload to message: " + ex.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,78 +0,0 @@
|
||||
/*
|
||||
* Copyright 2005-2010 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.ws.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);
|
||||
}
|
||||
@@ -1,42 +0,0 @@
|
||||
/*
|
||||
* Copyright 2005-2010 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.ws.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;
|
||||
|
||||
}
|
||||
@@ -1,120 +0,0 @@
|
||||
/*
|
||||
* Copyright 2005-2010 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.ws.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);
|
||||
|
||||
}
|
||||
@@ -1,42 +0,0 @@
|
||||
/*
|
||||
* Copyright 2005-2010 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.ws.mock.client;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.springframework.ws.WebServiceMessage;
|
||||
|
||||
/**
|
||||
* Callback interface for code that operates on response {@link WebServiceMessage}s. Defines the contract for creating
|
||||
* responses in test scenarios.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @author Lukas Krecan
|
||||
* @since 2.0
|
||||
*/
|
||||
public interface ResponseCallback {
|
||||
|
||||
/**
|
||||
* Execute any number of operations on the supplied response, given the request.
|
||||
*
|
||||
* @param request the request message
|
||||
* @param response the response message
|
||||
* @throws IOException in case of I/O errors
|
||||
*/
|
||||
void doWithResponse(WebServiceMessage request, WebServiceMessage response) throws IOException;
|
||||
|
||||
}
|
||||
@@ -1,87 +0,0 @@
|
||||
/*
|
||||
* Copyright 2005-2010 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.ws.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 request, WebServiceMessage response) throws IOException {
|
||||
Assert.isInstanceOf(SoapMessage.class, response);
|
||||
SoapMessage soapMessage = (SoapMessage) response;
|
||||
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);
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,66 +0,0 @@
|
||||
/*
|
||||
* Copyright 2005-2010 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.ws.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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user