001: /*
002: * Copyright 2005 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.ws.server.endpoint;
018:
019: import java.io.IOException;
020: import java.io.StringReader;
021: import java.io.StringWriter;
022: import javax.xml.transform.Result;
023: import javax.xml.transform.Source;
024: import javax.xml.transform.Transformer;
025: import javax.xml.transform.TransformerException;
026: import javax.xml.transform.TransformerFactory;
027: import javax.xml.transform.stream.StreamResult;
028: import javax.xml.transform.stream.StreamSource;
029:
030: import org.custommonkey.xmlunit.XMLTestCase;
031: import org.easymock.MockControl;
032: import org.springframework.oxm.Marshaller;
033: import org.springframework.oxm.Unmarshaller;
034: import org.springframework.oxm.XmlMappingException;
035: import org.springframework.oxm.mime.MimeMarshaller;
036: import org.springframework.oxm.mime.MimeUnmarshaller;
037: import org.springframework.ws.MockWebServiceMessage;
038: import org.springframework.ws.WebServiceMessageFactory;
039: import org.springframework.ws.context.DefaultMessageContext;
040: import org.springframework.ws.context.MessageContext;
041: import org.springframework.ws.mime.MimeMessage;
042: import org.springframework.xml.transform.StringResult;
043: import org.springframework.xml.transform.StringSource;
044:
045: public class MarshallingPayloadEndpointTest extends XMLTestCase {
046:
047: private Transformer transformer;
048:
049: private MessageContext context;
050:
051: private MockControl factoryControl;
052:
053: private WebServiceMessageFactory factoryMock;
054:
055: protected void setUp() throws Exception {
056: MockWebServiceMessage request = new MockWebServiceMessage(
057: "<request/>");
058: transformer = TransformerFactory.newInstance().newTransformer();
059: factoryControl = MockControl
060: .createControl(WebServiceMessageFactory.class);
061: factoryMock = (WebServiceMessageFactory) factoryControl
062: .getMock();
063:
064: context = new DefaultMessageContext(request, factoryMock);
065: }
066:
067: public void testInvoke() throws Exception {
068: Unmarshaller unmarshaller = new SimpleMarshaller() {
069: public Object unmarshal(Source source)
070: throws XmlMappingException {
071: try {
072: StringWriter writer = new StringWriter();
073: transformer.transform(source, new StreamResult(
074: writer));
075: assertXMLEqual("Invalid source", "<request/>",
076: writer.toString());
077: return new Long(42);
078: } catch (Exception e) {
079: fail(e.getMessage());
080: return null;
081: }
082: }
083: };
084: Marshaller marshaller = new SimpleMarshaller() {
085: public void marshal(Object graph, Result result)
086: throws XmlMappingException {
087: assertEquals("Invalid graph", "result", graph);
088: try {
089: transformer.transform(new StreamSource(
090: new StringReader("<result/>")), result);
091: } catch (TransformerException e) {
092: fail(e.getMessage());
093: }
094: }
095: };
096: AbstractMarshallingPayloadEndpoint endpoint = new AbstractMarshallingPayloadEndpoint() {
097: protected Object invokeInternal(Object requestObject)
098: throws Exception {
099: assertEquals("Invalid request object", new Long(42),
100: requestObject);
101: return "result";
102: }
103: };
104: endpoint.setMarshaller(marshaller);
105: endpoint.setUnmarshaller(unmarshaller);
106: endpoint.afterPropertiesSet();
107:
108: factoryControl
109: .expectAndReturn(factoryMock.createWebServiceMessage(),
110: new MockWebServiceMessage());
111: factoryControl.replay();
112:
113: endpoint.invoke(context);
114: MockWebServiceMessage response = (MockWebServiceMessage) context
115: .getResponse();
116: assertNotNull("Invalid result", response);
117: assertXMLEqual("Invalid response", "<result/>", response
118: .getPayloadAsString());
119:
120: factoryControl.verify();
121: }
122:
123: public void testInvokeNullResponse() throws Exception {
124: Unmarshaller unmarshaller = new SimpleMarshaller() {
125: public Object unmarshal(Source source)
126: throws XmlMappingException {
127: try {
128: StringWriter writer = new StringWriter();
129: transformer.transform(source, new StreamResult(
130: writer));
131: assertXMLEqual("Invalid source", "<request/>",
132: writer.toString());
133: return new Long(42);
134: } catch (Exception e) {
135: fail(e.getMessage());
136: return null;
137: }
138: }
139: };
140: Marshaller marshaller = new SimpleMarshaller() {
141: public void marshal(Object graph, Result result)
142: throws XmlMappingException {
143: fail("marshal not expected");
144: }
145: };
146: AbstractMarshallingPayloadEndpoint endpoint = new AbstractMarshallingPayloadEndpoint() {
147: protected Object invokeInternal(Object requestObject)
148: throws Exception {
149: assertEquals("Invalid request object", new Long(42),
150: requestObject);
151: return null;
152: }
153: };
154: endpoint.setMarshaller(marshaller);
155: endpoint.setUnmarshaller(unmarshaller);
156: endpoint.afterPropertiesSet();
157: factoryControl.replay();
158: endpoint.invoke(context);
159: assertFalse("Response created", context.hasResponse());
160: factoryControl.verify();
161: }
162:
163: public void testInvokeMimeMarshaller() throws Exception {
164: MockControl unmarshallerControl = MockControl
165: .createControl(MimeUnmarshaller.class);
166: MimeUnmarshaller unmarshaller = (MimeUnmarshaller) unmarshallerControl
167: .getMock();
168: MockControl marshallerControl = MockControl
169: .createControl(MimeMarshaller.class);
170: MimeMarshaller marshaller = (MimeMarshaller) marshallerControl
171: .getMock();
172: MockControl messageControl = MockControl
173: .createControl(MimeMessage.class);
174: MimeMessage request = (MimeMessage) messageControl.getMock();
175: MimeMessage response = (MimeMessage) messageControl.getMock();
176: Source requestSource = new StringSource("<request/>");
177: messageControl.expectAndReturn(request.getPayloadSource(),
178: requestSource);
179: factoryControl.expectAndReturn(factoryMock
180: .createWebServiceMessage(), response);
181: unmarshaller.unmarshal(requestSource, null);
182: unmarshallerControl.setMatcher(MockControl.ALWAYS_MATCHER);
183: unmarshallerControl.setReturnValue(new Long(42));
184: Result responseResult = new StringResult();
185: messageControl.expectAndReturn(response.getPayloadResult(),
186: responseResult);
187: marshaller.marshal("result", responseResult, null);
188: marshallerControl.setMatcher(MockControl.ALWAYS_MATCHER);
189:
190: factoryControl.replay();
191: unmarshallerControl.replay();
192: marshallerControl.replay();
193: messageControl.replay();
194:
195: AbstractMarshallingPayloadEndpoint endpoint = new AbstractMarshallingPayloadEndpoint() {
196: protected Object invokeInternal(Object requestObject)
197: throws Exception {
198: assertEquals("Invalid request object", new Long(42),
199: requestObject);
200: return "result";
201: }
202: };
203: endpoint.setMarshaller(marshaller);
204: endpoint.setUnmarshaller(unmarshaller);
205: endpoint.afterPropertiesSet();
206:
207: context = new DefaultMessageContext(request, factoryMock);
208: endpoint.invoke(context);
209: assertNotNull("Invalid result", response);
210:
211: factoryControl.verify();
212: unmarshallerControl.verify();
213: marshallerControl.verify();
214: messageControl.verify();
215: }
216:
217: private abstract static class SimpleMarshaller implements
218: Marshaller, Unmarshaller {
219:
220: public void marshal(Object graph, Result result)
221: throws XmlMappingException, IOException {
222: fail("Not expected");
223: }
224:
225: public Object unmarshal(Source source)
226: throws XmlMappingException, IOException {
227: fail("Not expected");
228: return null;
229: }
230:
231: public boolean supports(Class clazz) {
232: return false;
233: }
234: }
235:
236: }
|