01: /*
02: * Copyright 2005 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16:
17: package org.springframework.ws.server.endpoint.adapter;
18:
19: import java.io.StringReader;
20: import java.io.StringWriter;
21: import javax.xml.transform.Source;
22: import javax.xml.transform.Transformer;
23: import javax.xml.transform.TransformerFactory;
24: import javax.xml.transform.stream.StreamResult;
25: import javax.xml.transform.stream.StreamSource;
26:
27: import org.custommonkey.xmlunit.XMLTestCase;
28: import org.easymock.MockControl;
29: import org.springframework.ws.MockWebServiceMessage;
30: import org.springframework.ws.MockWebServiceMessageFactory;
31: import org.springframework.ws.context.DefaultMessageContext;
32: import org.springframework.ws.context.MessageContext;
33: import org.springframework.ws.server.endpoint.PayloadEndpoint;
34:
35: public class PayloadEndpointAdapterTest extends XMLTestCase {
36:
37: private PayloadEndpointAdapter adapter;
38:
39: private PayloadEndpoint endpointMock;
40:
41: private MockControl endpointControl;
42:
43: protected void setUp() throws Exception {
44: adapter = new PayloadEndpointAdapter();
45: endpointControl = MockControl
46: .createControl(PayloadEndpoint.class);
47: endpointMock = (PayloadEndpoint) endpointControl.getMock();
48: }
49:
50: public void testSupports() throws Exception {
51: assertTrue(
52: "PayloadEndpointAdapter does not support PayloadEndpoint",
53: adapter.supports(endpointMock));
54: }
55:
56: public void testInvoke() throws Exception {
57: MockWebServiceMessage request = new MockWebServiceMessage(
58: "<request/>");
59: final Transformer transformer = TransformerFactory
60: .newInstance().newTransformer();
61: PayloadEndpoint endpoint = new PayloadEndpoint() {
62: public Source invoke(Source request) throws Exception {
63: StringWriter writer = new StringWriter();
64: transformer
65: .transform(request, new StreamResult(writer));
66: assertXMLEqual("Invalid request", "<request/>", writer
67: .toString());
68: return new StreamSource(new StringReader("<response/>"));
69: }
70: };
71: endpoint.invoke(request.getPayloadSource());
72: MessageContext messageContext = new DefaultMessageContext(
73: request, new MockWebServiceMessageFactory());
74: adapter.invoke(messageContext, endpoint);
75: MockWebServiceMessage response = (MockWebServiceMessage) messageContext
76: .getResponse();
77: assertNotNull("No response created", response);
78: assertXMLEqual("Invalid payload", "<response/>", response
79: .getPayloadAsString());
80: }
81:
82: public void testInvokeNoResponse() throws Exception {
83: MessageContext messageContext = new DefaultMessageContext(
84: new MockWebServiceMessageFactory());
85: endpointMock.invoke(messageContext.getRequest()
86: .getPayloadSource());
87: endpointControl.setMatcher(MockControl.ALWAYS_MATCHER);
88: endpointControl.setReturnValue(null);
89: endpointControl.replay();
90: adapter.invoke(messageContext, endpointMock);
91: endpointControl.verify();
92: assertFalse("Response created", messageContext.hasResponse());
93: }
94:
95: }
|