Created support.creator package, with support for generic WebService creation (irrespective of whether it's a request or response).

This commit is contained in:
Arjen Poutsma
2010-11-04 11:00:40 +00:00
parent 7a38ad1402
commit 5500ae79b8
8 changed files with 148 additions and 77 deletions

View File

@@ -1,52 +0,0 @@
/*
* Copyright 2005-2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.ws.test.client;
import java.io.IOException;
import java.net.URI;
import javax.xml.transform.Source;
import javax.xml.transform.TransformerException;
import org.springframework.ws.WebServiceMessage;
import org.springframework.xml.transform.TransformerHelper;
/**
* Implementation of {@link ResponseCreator} that writes a {@link Source} response.
*
* @author Arjen Poutsma
* @since 2.0
*/
class PayloadResponseCreator extends AbstractResponseCreator {
private final Source payload;
private TransformerHelper transformerHelper = new TransformerHelper();
PayloadResponseCreator(Source payload) {
this.payload = payload;
}
@Override
protected void doWithResponse(URI uri, WebServiceMessage request, WebServiceMessage response) throws IOException {
try {
transformerHelper.transform(payload, response.getPayloadResult());
}
catch (TransformerException ex) {
throw new AssertionError("Could not transform response payload to message: " + ex.getMessage());
}
}
}

View File

@@ -17,11 +17,16 @@
package org.springframework.ws.test.client;
import java.io.IOException;
import java.net.URI;
import java.util.Locale;
import javax.xml.transform.Source;
import org.springframework.core.io.Resource;
import org.springframework.util.Assert;
import org.springframework.ws.WebServiceMessage;
import org.springframework.ws.WebServiceMessageFactory;
import org.springframework.ws.test.support.creator.PayloadMessageCreator;
import org.springframework.ws.test.support.creator.WebServiceMessageCreator;
import org.springframework.xml.transform.ResourceSource;
/**
@@ -40,7 +45,7 @@ public abstract class ResponseCreators {
*/
public static ResponseCreator withPayload(Source payload) {
Assert.notNull(payload, "'payload' must not be null");
return new PayloadResponseCreator(payload);
return new WebServiceMessageCreatorAdapter(new PayloadMessageCreator(payload));
}
/**
@@ -137,4 +142,23 @@ public abstract class ResponseCreators {
return SoapFaultResponseCreator.createVersionMismatchFault(faultStringOrReason, locale);
}
/**
* Adapts a {@link WebServiceMessageCreator} to the {@link ResponseCreator} contract.
*/
private static class WebServiceMessageCreatorAdapter implements ResponseCreator {
private final WebServiceMessageCreator adaptee;
private WebServiceMessageCreatorAdapter(WebServiceMessageCreator adaptee) {
this.adaptee = adaptee;
}
public WebServiceMessage createResponse(URI uri,
WebServiceMessage request,
WebServiceMessageFactory messageFactory) throws IOException {
return adaptee.createMessage(messageFactory);
}
}
}

View File

@@ -21,6 +21,10 @@ import javax.xml.transform.Source;
import org.springframework.core.io.Resource;
import org.springframework.util.Assert;
import org.springframework.ws.WebServiceMessage;
import org.springframework.ws.WebServiceMessageFactory;
import org.springframework.ws.test.support.creator.PayloadMessageCreator;
import org.springframework.ws.test.support.creator.WebServiceMessageCreator;
import org.springframework.xml.transform.ResourceSource;
/**
@@ -43,7 +47,7 @@ public abstract class RequestCreators {
*/
public static RequestCreator withPayload(Source payload) {
Assert.notNull(payload, "'payload' must not be null");
return new PayloadRequestCreator(payload);
return new WebServiceMessageCreatorAdapter(new PayloadMessageCreator(payload));
}
/**
@@ -57,5 +61,21 @@ public abstract class RequestCreators {
return withPayload(new ResourceSource(payload));
}
/**
* Adapts a {@link WebServiceMessageCreator} to the {@link RequestCreator} contract.
*/
private static class WebServiceMessageCreatorAdapter implements RequestCreator {
private final WebServiceMessageCreator adaptee;
private WebServiceMessageCreatorAdapter(WebServiceMessageCreator adaptee) {
this.adaptee = adaptee;
}
public WebServiceMessage createRequest(WebServiceMessageFactory messageFactory) throws IOException {
return adaptee.createMessage(messageFactory);
}
}
}

View File

@@ -0,0 +1,50 @@
/*
* Copyright 2005-2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.ws.test.support.creator;
import java.io.IOException;
import org.springframework.ws.WebServiceMessage;
import org.springframework.ws.WebServiceMessageFactory;
/**
* Abstract base class for the {@link WebServiceMessageCreator} interface.
* <p/>
* Creates a message using the given {@link WebServiceMessageFactory}, and passes it on to {@link
* #doWithMessage(WebServiceMessage)}.
*
* @author Arjen Poutsma
* @since 2.0
*/
public abstract class AbstractMessageCreator implements WebServiceMessageCreator {
public final WebServiceMessage createMessage(WebServiceMessageFactory messageFactory) throws IOException {
WebServiceMessage message = messageFactory.createWebServiceMessage();
doWithMessage(message);
return message;
}
/**
* Abstract template method, invoked by {@link #createMessage(WebServiceMessageFactory)} after a message has been
* created.
*
* @param message the message
* @throws IOException in case of I/O errors
*/
protected abstract void doWithMessage(WebServiceMessage message) throws IOException;
}

View File

@@ -14,39 +14,47 @@
* limitations under the License.
*/
package org.springframework.ws.test.server;
package org.springframework.ws.test.support.creator;
import java.io.IOException;
import javax.xml.transform.Source;
import javax.xml.transform.TransformerException;
import org.springframework.util.Assert;
import org.springframework.ws.WebServiceMessage;
import org.springframework.xml.transform.TransformerHelper;
import static org.springframework.ws.test.support.AssertionErrors.fail;
/**
* Implementation of {@link org.springframework.ws.test.server.RequestCreator} that creates a request based on a {@link
* javax.xml.transform.Source}.
* Implementation of {@link WebServiceMessageCreator} that creates a request based on a {@link Source}.
*
* @author Arjen Poutsma
* @since 2.0
*/
class PayloadRequestCreator extends AbstractRequestCreator {
public class PayloadMessageCreator extends AbstractMessageCreator {
private final Source payload;
private TransformerHelper transformerHelper = new TransformerHelper();
PayloadRequestCreator(Source payload) {
/**
* Creates a new instance of the {@code PayloadMessageCreator} with the given payload source.
*
* @param payload the payload source
*/
public PayloadMessageCreator(Source payload) {
Assert.notNull(payload, "'payload' must not be null");
this.payload = payload;
}
@Override
protected void doWithRequest(WebServiceMessage request) throws IOException {
protected void doWithMessage(WebServiceMessage message) throws IOException {
try {
transformerHelper.transform(payload, request.getPayloadResult());
transformerHelper.transform(payload, message.getPayloadResult());
}
catch (TransformerException ex) {
throw new AssertionError("Could not transform request payload to message: " + ex.getMessage());
fail("Could not transform request payload to message: " + ex.getMessage());
}
}
}

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.ws.test.server;
package org.springframework.ws.test.support.creator;
import java.io.IOException;
@@ -22,22 +22,20 @@ import org.springframework.ws.WebServiceMessage;
import org.springframework.ws.WebServiceMessageFactory;
/**
* Abstract base class for the {@link org.springframework.ws.test.server.RequestCreator} interface.
* <p/>
* Creates a response using the given {@link org.springframework.ws.WebServiceMessageFactory}, and passes it on to
* {@link #doWithRequest(org.springframework.ws.WebServiceMessage)}.
* Defines the general contract for creating messages used in test scenarios.
*
* @author Arjen Poutsma
* @since 2.0
*/
abstract class AbstractRequestCreator implements RequestCreator {
public interface WebServiceMessageCreator {
public final WebServiceMessage createRequest(WebServiceMessageFactory messageFactory) throws IOException {
WebServiceMessage request = messageFactory.createWebServiceMessage();
doWithRequest(request);
return request;
}
/**
* Create a message.
*
* @param messageFactory the message that can be used to create the message
* @throws IOException in case of I/O errors
*/
WebServiceMessage createMessage(WebServiceMessageFactory messageFactory) throws IOException;
protected abstract void doWithRequest(WebServiceMessage request) throws IOException;
}

View File

@@ -0,0 +1,21 @@
/*
* Copyright 2005-2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Provides the generic {@link org.springframework.ws.test.support.creator.WebServiceMessageCreator WebServiceMessageCreator}
* interface, and implementations.
*/
package org.springframework.ws.test.support.creator;

View File

@@ -23,6 +23,8 @@ import org.springframework.xml.transform.StringSource;
import org.junit.Test;
import static org.junit.Assert.*;
import static org.springframework.ws.test.client.ResponseCreators.withError;
import static org.springframework.ws.test.client.ResponseCreators.withPayload;
public class MockSenderConnectionTest {
@@ -30,7 +32,7 @@ public class MockSenderConnectionTest {
public void error() throws IOException {
String testErrorMessage = "Test Error Message";
MockSenderConnection connection = new MockSenderConnection();
connection.andRespond(new ErrorResponseCreator(testErrorMessage));
connection.andRespond(withError(testErrorMessage));
assertTrue(connection.hasError());
assertEquals(testErrorMessage, connection.getErrorMessage());
}
@@ -38,7 +40,7 @@ public class MockSenderConnectionTest {
@Test
public void normal() throws IOException {
MockSenderConnection connection = new MockSenderConnection();
connection.andRespond(new PayloadResponseCreator(new StringSource("<response/>")));
connection.andRespond(withPayload(new StringSource("<response/>")));
assertFalse(connection.hasError());
assertNull(connection.getErrorMessage());
}
@@ -46,7 +48,7 @@ public class MockSenderConnectionTest {
@Test(expected = AssertionError.class)
public void noRequestMatchers() throws IOException {
MockSenderConnection connection = new MockSenderConnection();
connection.andRespond(new PayloadResponseCreator(new StringSource("<response/>")));
connection.andRespond(withPayload(new StringSource("<response/>")));
connection.send(null);
}
}