1   /*
2    * Copyright 2005-2010 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.adapter.method;
18  
19  import java.lang.reflect.Type;
20  import javax.xml.transform.Result;
21  import javax.xml.transform.Source;
22  
23  import org.springframework.core.MethodParameter;
24  import org.springframework.oxm.GenericMarshaller;
25  import org.springframework.oxm.GenericUnmarshaller;
26  import org.springframework.ws.context.MessageContext;
27  import org.springframework.ws.server.endpoint.annotation.RequestPayload;
28  import org.springframework.ws.server.endpoint.annotation.ResponsePayload;
29  
30  import org.junit.Before;
31  import org.junit.Test;
32  
33  import static org.easymock.EasyMock.*;
34  import static org.junit.Assert.*;
35  
36  public class MarshallingPayloadMethodProcessorTest extends AbstractMethodArgumentResolverTestCase {
37  
38      private MarshallingPayloadMethodProcessor processor;
39  
40      private GenericMarshaller marshaller;
41  
42      private GenericUnmarshaller unmarshaller;
43  
44      private MethodParameter supportedParameter;
45  
46      private MethodParameter supportedReturnType;
47  
48      @Before
49      public void setUp() throws Exception {
50          marshaller = createMock("marshaller", GenericMarshaller.class);
51          unmarshaller = createMock("unmarshaller", GenericUnmarshaller.class);
52          processor = new MarshallingPayloadMethodProcessor(marshaller, unmarshaller);
53          supportedParameter = new MethodParameter(getClass().getMethod("method", MyObject.class), 0);
54          supportedReturnType = new MethodParameter(getClass().getMethod("method", MyObject.class), -1);
55      }
56  
57      @Test
58      public void supportsParameterSupported() {
59          expect(unmarshaller.supports(isA(Type.class))).andReturn(true);
60  
61          replay(marshaller, unmarshaller);
62  
63          assertTrue("processor does not support supported parameter", processor.supportsParameter(supportedParameter));
64  
65          verify(marshaller, unmarshaller);
66      }
67  
68      @Test
69      public void supportsParameterUnsupported() {
70          expect(unmarshaller.supports(isA(Type.class))).andReturn(false);
71  
72          replay(marshaller, unmarshaller);
73  
74          assertFalse("processor supports unsupported parameter", processor.supportsParameter(supportedParameter));
75  
76          verify(marshaller, unmarshaller);
77      }
78  
79      @Test
80      public void supportsParameterNoUnmarshallerSupported() {
81          processor = new MarshallingPayloadMethodProcessor();
82          processor.setMarshaller(marshaller);
83  
84          replay(marshaller, unmarshaller);
85  
86          assertFalse("processor supports parameter with no unmarshaller set",
87                  processor.supportsParameter(supportedParameter));
88  
89          verify(marshaller, unmarshaller);
90      }
91  
92      @Test
93      public void supportsReturnTypeSupported() {
94          expect(marshaller.supports(isA(Type.class))).andReturn(true);
95  
96          replay(marshaller, unmarshaller);
97  
98          assertTrue("processor does not support supported return type", processor.supportsReturnType(supportedReturnType));
99  
100         verify(marshaller, unmarshaller);
101     }
102 
103     @Test
104     public void supportsReturnTypeUnsupported() {
105         expect(marshaller.supports(isA(Type.class))).andReturn(false);
106 
107         replay(marshaller, unmarshaller);
108 
109         assertFalse("processor supports unsupported parameter", processor.supportsReturnType(supportedReturnType));
110 
111         verify(marshaller, unmarshaller);
112     }
113 
114     @Test
115     public void supportsReturnTypeNoMarshaller() {
116         processor = new MarshallingPayloadMethodProcessor();
117         processor.setUnmarshaller(unmarshaller);
118 
119         replay(marshaller, unmarshaller);
120 
121         assertFalse("processor supports return type with no marshaller set",
122                 processor.supportsReturnType(supportedReturnType));
123 
124         verify(marshaller, unmarshaller);
125     }
126 
127 
128     @Test
129     public void resolveArgument() throws Exception {
130         MyObject expected = new MyObject();
131 
132         expect(unmarshaller.unmarshal(isA(Source.class))).andReturn(expected);
133 
134         replay(marshaller, unmarshaller);
135         MessageContext messageContext = createMockMessageContext();
136 
137         Object result = processor.resolveArgument(messageContext, supportedParameter);
138         assertEquals("Invalid return argument", expected, result);
139 
140         verify(marshaller, unmarshaller);
141     }
142 
143     @Test(expected = IllegalStateException.class)
144     public void resolveArgumentNoUnmarshaller() throws Exception {
145         processor = new MarshallingPayloadMethodProcessor();
146         processor.setMarshaller(marshaller);
147 
148         replay(marshaller, unmarshaller);
149         MessageContext messageContext = createMockMessageContext();
150 
151         processor.resolveArgument(messageContext, supportedParameter);
152     }
153 
154     @Test
155     public void handleReturnValue() throws Exception {
156         MyObject returnValue = new MyObject();
157 
158         marshaller.marshal(eq(returnValue), isA(Result.class));
159 
160         replay(marshaller, unmarshaller);
161         MessageContext messageContext = createMockMessageContext();
162 
163         processor.handleReturnValue(messageContext, supportedReturnType, returnValue);
164 
165         verify(marshaller, unmarshaller);
166     }
167 
168     @Test(expected = IllegalStateException.class)
169     public void handleReturnValueNoMarshaller() throws Exception {
170         processor = new MarshallingPayloadMethodProcessor();
171         processor.setUnmarshaller(unmarshaller);
172 
173         MyObject returnValue = new MyObject();
174 
175         replay(marshaller, unmarshaller);
176         MessageContext messageContext = createMockMessageContext();
177 
178         processor.handleReturnValue(messageContext, supportedReturnType, returnValue);
179     }
180 
181     @ResponsePayload
182     public MyObject method(@RequestPayload MyObject object) {
183         return object;
184     }
185 
186     public static class MyObject {
187 
188     }
189 }