1   /*
2    * Copyright 2005 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
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;
18  
19  import java.io.IOException;
20  import java.io.StringReader;
21  import java.io.StringWriter;
22  import javax.xml.transform.Result;
23  import javax.xml.transform.Source;
24  import javax.xml.transform.Transformer;
25  import javax.xml.transform.TransformerException;
26  import javax.xml.transform.TransformerFactory;
27  import javax.xml.transform.stream.StreamResult;
28  import javax.xml.transform.stream.StreamSource;
29  
30  import org.custommonkey.xmlunit.XMLTestCase;
31  import org.easymock.MockControl;
32  import org.springframework.oxm.Marshaller;
33  import org.springframework.oxm.Unmarshaller;
34  import org.springframework.oxm.XmlMappingException;
35  import org.springframework.oxm.mime.MimeMarshaller;
36  import org.springframework.oxm.mime.MimeUnmarshaller;
37  import org.springframework.ws.MockWebServiceMessage;
38  import org.springframework.ws.WebServiceMessageFactory;
39  import org.springframework.ws.context.DefaultMessageContext;
40  import org.springframework.ws.context.MessageContext;
41  import org.springframework.ws.mime.MimeMessage;
42  import org.springframework.xml.transform.StringResult;
43  import org.springframework.xml.transform.StringSource;
44  
45  public class MarshallingPayloadEndpointTest extends XMLTestCase {
46  
47      private Transformer transformer;
48  
49      private MessageContext context;
50  
51      private MockControl factoryControl;
52  
53      private WebServiceMessageFactory factoryMock;
54  
55      protected void setUp() throws Exception {
56          MockWebServiceMessage request = new MockWebServiceMessage("<request/>");
57          transformer = TransformerFactory.newInstance().newTransformer();
58          factoryControl = MockControl.createControl(WebServiceMessageFactory.class);
59          factoryMock = (WebServiceMessageFactory) factoryControl.getMock();
60  
61          context = new DefaultMessageContext(request, factoryMock);
62      }
63  
64      public void testInvoke() throws Exception {
65          Unmarshaller unmarshaller = new SimpleMarshaller() {
66              public Object unmarshal(Source source) throws XmlMappingException {
67                  try {
68                      StringWriter writer = new StringWriter();
69                      transformer.transform(source, new StreamResult(writer));
70                      assertXMLEqual("Invalid source", "<request/>", writer.toString());
71                      return new Long(42);
72                  }
73                  catch (Exception e) {
74                      fail(e.getMessage());
75                      return null;
76                  }
77              }
78          };
79          Marshaller marshaller = new SimpleMarshaller() {
80              public void marshal(Object graph, Result result) throws XmlMappingException {
81                  assertEquals("Invalid graph", "result", graph);
82                  try {
83                      transformer.transform(new StreamSource(new StringReader("<result/>")), result);
84                  }
85                  catch (TransformerException e) {
86                      fail(e.getMessage());
87                  }
88              }
89          };
90          AbstractMarshallingPayloadEndpoint endpoint = new AbstractMarshallingPayloadEndpoint() {
91              protected Object invokeInternal(Object requestObject) throws Exception {
92                  assertEquals("Invalid request object", new Long(42), requestObject);
93                  return "result";
94              }
95          };
96          endpoint.setMarshaller(marshaller);
97          endpoint.setUnmarshaller(unmarshaller);
98          endpoint.afterPropertiesSet();
99  
100         factoryControl.expectAndReturn(factoryMock.createWebServiceMessage(), new MockWebServiceMessage());
101         factoryControl.replay();
102 
103         endpoint.invoke(context);
104         MockWebServiceMessage response = (MockWebServiceMessage) context.getResponse();
105         assertNotNull("Invalid result", response);
106         assertXMLEqual("Invalid response", "<result/>", response.getPayloadAsString());
107 
108         factoryControl.verify();
109     }
110 
111     public void testInvokeNullResponse() throws Exception {
112         Unmarshaller unmarshaller = new SimpleMarshaller() {
113             public Object unmarshal(Source source) throws XmlMappingException {
114                 try {
115                     StringWriter writer = new StringWriter();
116                     transformer.transform(source, new StreamResult(writer));
117                     assertXMLEqual("Invalid source", "<request/>", writer.toString());
118                     return new Long(42);
119                 }
120                 catch (Exception e) {
121                     fail(e.getMessage());
122                     return null;
123                 }
124             }
125         };
126         Marshaller marshaller = new SimpleMarshaller() {
127             public void marshal(Object graph, Result result) throws XmlMappingException {
128                 fail("marshal not expected");
129             }
130         };
131         AbstractMarshallingPayloadEndpoint endpoint = new AbstractMarshallingPayloadEndpoint() {
132             protected Object invokeInternal(Object requestObject) throws Exception {
133                 assertEquals("Invalid request object", new Long(42), requestObject);
134                 return null;
135             }
136         };
137         endpoint.setMarshaller(marshaller);
138         endpoint.setUnmarshaller(unmarshaller);
139         endpoint.afterPropertiesSet();
140         factoryControl.replay();
141         endpoint.invoke(context);
142         assertFalse("Response created", context.hasResponse());
143         factoryControl.verify();
144     }
145 
146     public void testInvokeNoRequest() throws Exception {
147         MockWebServiceMessage request = new MockWebServiceMessage((StringBuffer) null);
148         context = new DefaultMessageContext(request, factoryMock);
149         AbstractMarshallingPayloadEndpoint endpoint = new AbstractMarshallingPayloadEndpoint() {
150 
151             protected Object invokeInternal(Object requestObject) throws Exception {
152                 assertNull("No request expected", requestObject);
153                 return null;
154             }
155         };
156         endpoint.setMarshaller(new SimpleMarshaller());
157         endpoint.setUnmarshaller(new SimpleMarshaller());
158         endpoint.afterPropertiesSet();
159         factoryControl.replay();
160         endpoint.invoke(context);
161         assertFalse("Response created", context.hasResponse());
162         factoryControl.verify();
163     }
164 
165     public void testInvokeMimeMarshaller() throws Exception {
166         MockControl unmarshallerControl = MockControl.createControl(MimeUnmarshaller.class);
167         MimeUnmarshaller unmarshaller = (MimeUnmarshaller) unmarshallerControl.getMock();
168         MockControl marshallerControl = MockControl.createControl(MimeMarshaller.class);
169         MimeMarshaller marshaller = (MimeMarshaller) marshallerControl.getMock();
170         MockControl messageControl = MockControl.createControl(MimeMessage.class);
171         MimeMessage request = (MimeMessage) messageControl.getMock();
172         MimeMessage response = (MimeMessage) messageControl.getMock();
173         Source requestSource = new StringSource("<request/>");
174         messageControl.expectAndReturn(request.getPayloadSource(), requestSource);
175         factoryControl.expectAndReturn(factoryMock.createWebServiceMessage(), response);
176         unmarshaller.unmarshal(requestSource, null);
177         unmarshallerControl.setMatcher(MockControl.ALWAYS_MATCHER);
178         unmarshallerControl.setReturnValue(new Long(42));
179         Result responseResult = new StringResult();
180         messageControl.expectAndReturn(response.getPayloadResult(), responseResult);
181         marshaller.marshal("result", responseResult, null);
182         marshallerControl.setMatcher(MockControl.ALWAYS_MATCHER);
183 
184         factoryControl.replay();
185         unmarshallerControl.replay();
186         marshallerControl.replay();
187         messageControl.replay();
188 
189         AbstractMarshallingPayloadEndpoint endpoint = new AbstractMarshallingPayloadEndpoint() {
190             protected Object invokeInternal(Object requestObject) throws Exception {
191                 assertEquals("Invalid request object", new Long(42), requestObject);
192                 return "result";
193             }
194         };
195         endpoint.setMarshaller(marshaller);
196         endpoint.setUnmarshaller(unmarshaller);
197         endpoint.afterPropertiesSet();
198 
199         context = new DefaultMessageContext(request, factoryMock);
200         endpoint.invoke(context);
201         assertNotNull("Invalid result", response);
202 
203         factoryControl.verify();
204         unmarshallerControl.verify();
205         marshallerControl.verify();
206         messageControl.verify();
207     }
208 
209     private static class SimpleMarshaller implements Marshaller, Unmarshaller {
210 
211         public void marshal(Object graph, Result result) throws XmlMappingException, IOException {
212             fail("Not expected");
213         }
214 
215         public Object unmarshal(Source source) throws XmlMappingException, IOException {
216             fail("Not expected");
217             return null;
218         }
219 
220         public boolean supports(Class clazz) {
221             return false;
222         }
223     }
224 
225 }