SWS-164
This commit is contained in:
@@ -78,6 +78,9 @@ public abstract class AbstractStaxEventPayloadEndpoint extends AbstractStaxPaylo
|
||||
}
|
||||
|
||||
private XMLEventReader getEventReader(Source source) throws XMLStreamException, TransformerException {
|
||||
if (source == null) {
|
||||
return null;
|
||||
}
|
||||
XMLEventReader eventReader = null;
|
||||
if (source instanceof StaxSource) {
|
||||
StaxSource staxSource = (StaxSource) source;
|
||||
|
||||
@@ -54,6 +54,9 @@ public abstract class AbstractStaxStreamPayloadEndpoint extends AbstractStaxPayl
|
||||
}
|
||||
|
||||
private XMLStreamReader getStreamReader(Source source) throws XMLStreamException, TransformerException {
|
||||
if (source == null) {
|
||||
return null;
|
||||
}
|
||||
XMLStreamReader streamReader = null;
|
||||
if (source instanceof StaxSource) {
|
||||
streamReader = ((StaxSource) source).getXMLStreamReader();
|
||||
|
||||
@@ -18,6 +18,7 @@ package org.springframework.ws.support;
|
||||
|
||||
import java.io.IOException;
|
||||
import javax.activation.DataHandler;
|
||||
import javax.xml.transform.Source;
|
||||
|
||||
import org.springframework.oxm.Marshaller;
|
||||
import org.springframework.oxm.Unmarshaller;
|
||||
@@ -41,6 +42,9 @@ public abstract class MarshallingUtils {
|
||||
|
||||
/**
|
||||
* Unmarshals the payload of the given message using the provided {@link Unmarshaller}.
|
||||
* <p/>
|
||||
* If the request message has no payload (i.e. {@link WebServiceMessage#getPayloadSource()} returns
|
||||
* <code>null</code>), this method will return <code>null</code>.
|
||||
*
|
||||
* @param unmarshaller the unmarshaller
|
||||
* @param message the message of which the payload is to be unmarshalled
|
||||
@@ -48,13 +52,17 @@ public abstract class MarshallingUtils {
|
||||
* @throws IOException in case of I/O errors
|
||||
*/
|
||||
public static Object unmarshal(Unmarshaller unmarshaller, WebServiceMessage message) throws IOException {
|
||||
if (unmarshaller instanceof MimeUnmarshaller && message instanceof MimeMessage) {
|
||||
Source payload = message.getPayloadSource();
|
||||
if (payload == null) {
|
||||
return null;
|
||||
}
|
||||
else if (unmarshaller instanceof MimeUnmarshaller && message instanceof MimeMessage) {
|
||||
MimeUnmarshaller mimeUnmarshaller = (MimeUnmarshaller) unmarshaller;
|
||||
MimeMessageContainer container = new MimeMessageContainer((MimeMessage) message);
|
||||
return mimeUnmarshaller.unmarshal(message.getPayloadSource(), container);
|
||||
return mimeUnmarshaller.unmarshal(payload, container);
|
||||
}
|
||||
else {
|
||||
return unmarshaller.unmarshal(message.getPayloadSource());
|
||||
return unmarshaller.unmarshal(payload);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -45,14 +45,13 @@ import org.springframework.xml.transform.StringSource;
|
||||
*/
|
||||
public class MockWebServiceMessage implements FaultAwareWebServiceMessage {
|
||||
|
||||
private final StringBuffer content;
|
||||
private StringBuffer content;
|
||||
|
||||
private boolean fault = false;
|
||||
|
||||
private String faultReason;
|
||||
|
||||
public MockWebServiceMessage() {
|
||||
content = new StringBuffer();
|
||||
}
|
||||
|
||||
public MockWebServiceMessage(Source source) throws TransformerException {
|
||||
@@ -71,14 +70,17 @@ public class MockWebServiceMessage implements FaultAwareWebServiceMessage {
|
||||
}
|
||||
|
||||
public MockWebServiceMessage(String content) {
|
||||
this.content = new StringBuffer(content);
|
||||
if (content != null) {
|
||||
this.content = new StringBuffer(content);
|
||||
}
|
||||
}
|
||||
|
||||
public String getPayloadAsString() {
|
||||
return content.toString();
|
||||
return content != null ? content.toString() : null;
|
||||
}
|
||||
|
||||
public void setPayload(InputStreamSource inputStreamSource) throws IOException {
|
||||
checkContent();
|
||||
InputStream is = null;
|
||||
try {
|
||||
is = inputStreamSource.getInputStream();
|
||||
@@ -93,16 +95,24 @@ public class MockWebServiceMessage implements FaultAwareWebServiceMessage {
|
||||
}
|
||||
|
||||
public void setPayload(String content) {
|
||||
checkContent();
|
||||
this.content.replace(0, this.content.length(), content);
|
||||
}
|
||||
|
||||
private void checkContent() {
|
||||
if (content == null) {
|
||||
content = new StringBuffer();
|
||||
}
|
||||
}
|
||||
|
||||
public Result getPayloadResult() {
|
||||
checkContent();
|
||||
content.setLength(0);
|
||||
return new StreamResult(new StringBufferWriter());
|
||||
}
|
||||
|
||||
public Source getPayloadSource() {
|
||||
return new StringSource(content.toString());
|
||||
return content != null ? new StringSource(content.toString()) : null;
|
||||
}
|
||||
|
||||
public boolean hasFault() {
|
||||
@@ -122,13 +132,17 @@ public class MockWebServiceMessage implements FaultAwareWebServiceMessage {
|
||||
}
|
||||
|
||||
public void writeTo(OutputStream outputStream) throws IOException {
|
||||
PrintWriter writer = new PrintWriter(outputStream);
|
||||
writer.write(content.toString());
|
||||
if (content != null) {
|
||||
PrintWriter writer = new PrintWriter(outputStream);
|
||||
writer.write(content.toString());
|
||||
}
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuffer buffer = new StringBuffer("MockWebServiceMessage {");
|
||||
buffer.append(content);
|
||||
if (content != null) {
|
||||
buffer.append(content);
|
||||
}
|
||||
buffer.append('}');
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
@@ -42,6 +42,15 @@ public abstract class AbstractMessageEndpointTestCase extends AbstractEndpointTe
|
||||
assertFalse("Response message created", context.hasResponse());
|
||||
}
|
||||
|
||||
public void testNoRequestPayload() throws Exception {
|
||||
endpoint = createNoRequestPayloadEndpoint();
|
||||
|
||||
MessageContext context = new DefaultMessageContext(new MockWebServiceMessage((StringBuffer) null),
|
||||
new MockWebServiceMessageFactory());
|
||||
endpoint.invoke(context);
|
||||
assertFalse("Response message created", context.hasResponse());
|
||||
}
|
||||
|
||||
protected final void testSource(Source requestSource) throws Exception {
|
||||
MessageContext context =
|
||||
new DefaultMessageContext(new MockWebServiceMessage(requestSource), new MockWebServiceMessageFactory());
|
||||
@@ -52,6 +61,8 @@ public abstract class AbstractMessageEndpointTestCase extends AbstractEndpointTe
|
||||
|
||||
protected abstract MessageEndpoint createNoResponseEndpoint();
|
||||
|
||||
protected abstract MessageEndpoint createNoRequestPayloadEndpoint();
|
||||
|
||||
protected abstract MessageEndpoint createResponseEndpoint();
|
||||
|
||||
}
|
||||
|
||||
@@ -143,6 +143,25 @@ public class MarshallingPayloadEndpointTest extends XMLTestCase {
|
||||
factoryControl.verify();
|
||||
}
|
||||
|
||||
public void testInvokeNoRequest() throws Exception {
|
||||
MockWebServiceMessage request = new MockWebServiceMessage((StringBuffer) null);
|
||||
context = new DefaultMessageContext(request, factoryMock);
|
||||
AbstractMarshallingPayloadEndpoint endpoint = new AbstractMarshallingPayloadEndpoint() {
|
||||
|
||||
protected Object invokeInternal(Object requestObject) throws Exception {
|
||||
assertNull("No request expected", requestObject);
|
||||
return null;
|
||||
}
|
||||
};
|
||||
endpoint.setMarshaller(new SimpleMarshaller());
|
||||
endpoint.setUnmarshaller(new SimpleMarshaller());
|
||||
endpoint.afterPropertiesSet();
|
||||
factoryControl.replay();
|
||||
endpoint.invoke(context);
|
||||
assertFalse("Response created", context.hasResponse());
|
||||
factoryControl.verify();
|
||||
}
|
||||
|
||||
public void testInvokeMimeMarshaller() throws Exception {
|
||||
MockControl unmarshallerControl = MockControl.createControl(MimeUnmarshaller.class);
|
||||
MimeUnmarshaller unmarshaller = (MimeUnmarshaller) unmarshallerControl.getMock();
|
||||
@@ -187,7 +206,7 @@ public class MarshallingPayloadEndpointTest extends XMLTestCase {
|
||||
messageControl.verify();
|
||||
}
|
||||
|
||||
private abstract static class SimpleMarshaller implements Marshaller, Unmarshaller {
|
||||
private static class SimpleMarshaller implements Marshaller, Unmarshaller {
|
||||
|
||||
public void marshal(Object graph, Result result) throws XmlMappingException, IOException {
|
||||
fail("Not expected");
|
||||
|
||||
@@ -37,6 +37,18 @@ public class StaxEventPayloadEndpointTest extends AbstractMessageEndpointTestCas
|
||||
protected void invokeInternal(XMLEventReader eventReader,
|
||||
XMLEventConsumer eventWriter,
|
||||
XMLEventFactory eventFactory) throws Exception {
|
||||
assertNotNull("No EventReader passed", eventReader);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
protected MessageEndpoint createNoRequestPayloadEndpoint() {
|
||||
return new AbstractStaxEventPayloadEndpoint() {
|
||||
|
||||
protected void invokeInternal(XMLEventReader eventReader,
|
||||
XMLEventConsumer eventWriter,
|
||||
XMLEventFactory eventFactory) throws Exception {
|
||||
assertNull("EventReader passed", eventReader);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@@ -42,6 +42,15 @@ public class StaxStreamPayloadEndpointTest extends AbstractMessageEndpointTestCa
|
||||
protected MessageEndpoint createNoResponseEndpoint() {
|
||||
return new AbstractStaxStreamPayloadEndpoint() {
|
||||
protected void invokeInternal(XMLStreamReader streamReader, XMLStreamWriter streamWriter) throws Exception {
|
||||
assertNotNull("No StreamReader passed", streamReader);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
protected MessageEndpoint createNoRequestPayloadEndpoint() {
|
||||
return new AbstractStaxStreamPayloadEndpoint() {
|
||||
protected void invokeInternal(XMLStreamReader streamReader, XMLStreamWriter streamWriter) throws Exception {
|
||||
assertNull("StreamReader passed", streamReader);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import junit.framework.TestCase;
|
||||
import org.easymock.MockControl;
|
||||
import org.springframework.oxm.Marshaller;
|
||||
import org.springframework.oxm.Unmarshaller;
|
||||
import org.springframework.ws.MockWebServiceMessage;
|
||||
import org.springframework.ws.MockWebServiceMessageFactory;
|
||||
import org.springframework.ws.context.DefaultMessageContext;
|
||||
import org.springframework.ws.context.MessageContext;
|
||||
@@ -38,7 +39,8 @@ public class MarshallingMethodEndpointAdapterTest extends TestCase {
|
||||
unmarshallerMock = (Unmarshaller) unmarshallerControl.getMock();
|
||||
adapter.setUnmarshaller(unmarshallerMock);
|
||||
adapter.afterPropertiesSet();
|
||||
messageContext = new DefaultMessageContext(new MockWebServiceMessageFactory());
|
||||
MockWebServiceMessage request = new MockWebServiceMessage("<request/>");
|
||||
messageContext = new DefaultMessageContext(request, new MockWebServiceMessageFactory());
|
||||
}
|
||||
|
||||
public void testNoResponse() throws Exception {
|
||||
@@ -56,6 +58,20 @@ public class MarshallingMethodEndpointAdapterTest extends TestCase {
|
||||
unmarshallerControl.verify();
|
||||
}
|
||||
|
||||
public void testNoRequestPayload() throws Exception {
|
||||
MockWebServiceMessage request = new MockWebServiceMessage();
|
||||
messageContext = new DefaultMessageContext(request, new MockWebServiceMessageFactory());
|
||||
Method noResponse = getClass().getMethod("noResponse", new Class[]{MyType.class});
|
||||
MethodEndpoint methodEndpoint = new MethodEndpoint(this, noResponse);
|
||||
marshallerControl.replay();
|
||||
unmarshallerControl.replay();
|
||||
assertFalse("Method invoked", noResponseInvoked);
|
||||
adapter.invoke(messageContext, methodEndpoint);
|
||||
assertTrue("Method not invoked", noResponseInvoked);
|
||||
marshallerControl.verify();
|
||||
unmarshallerControl.verify();
|
||||
}
|
||||
|
||||
public void testResponse() throws Exception {
|
||||
Method response = getClass().getMethod("response", new Class[]{MyType.class});
|
||||
MethodEndpoint methodEndpoint = new MethodEndpoint(this, response);
|
||||
|
||||
@@ -76,6 +76,24 @@ public class MarshallingUtilsTest extends TestCase {
|
||||
messageControl.verify();
|
||||
}
|
||||
|
||||
public void testUnmarshalNoPayload() throws Exception {
|
||||
MockControl unmarshallerControl = MockControl.createControl(MimeUnmarshaller.class);
|
||||
MimeUnmarshaller unmarshallerMock = (MimeUnmarshaller) unmarshallerControl.getMock();
|
||||
MockControl messageControl = MockControl.createControl(MimeMessage.class);
|
||||
MimeMessage messageMock = (MimeMessage) messageControl.getMock();
|
||||
|
||||
messageControl.expectAndReturn(messageMock.getPayloadSource(), null);
|
||||
|
||||
unmarshallerControl.replay();
|
||||
messageControl.replay();
|
||||
|
||||
Object result = MarshallingUtils.unmarshal(unmarshallerMock, messageMock);
|
||||
assertNull("Invalid unmarshalled object", result);
|
||||
|
||||
unmarshallerControl.verify();
|
||||
messageControl.verify();
|
||||
}
|
||||
|
||||
public void testMarshal() throws Exception {
|
||||
MockControl marshallerControl = MockControl.createControl(Marshaller.class);
|
||||
Marshaller marshallerMock = (Marshaller) marshallerControl.getMock();
|
||||
|
||||
Reference in New Issue
Block a user