1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springframework.ws.server.endpoint.adapter;
18
19 import java.lang.reflect.Method;
20 import javax.xml.transform.Result;
21 import javax.xml.transform.Source;
22
23 import org.springframework.oxm.GenericMarshaller;
24 import org.springframework.oxm.GenericUnmarshaller;
25 import org.springframework.ws.WebServiceMessage;
26 import org.springframework.ws.WebServiceMessageFactory;
27 import org.springframework.ws.context.DefaultMessageContext;
28 import org.springframework.ws.context.MessageContext;
29 import org.springframework.ws.server.endpoint.MethodEndpoint;
30 import org.springframework.xml.transform.StringResult;
31 import org.springframework.xml.transform.StringSource;
32
33 import org.junit.Assert;
34 import org.junit.Before;
35 import org.junit.Test;
36
37 import static org.easymock.EasyMock.*;
38
39 public class GenericMarshallingMethodEndpointAdapterTest {
40
41 private GenericMarshallingMethodEndpointAdapter adapter;
42
43 private boolean noResponseInvoked;
44
45 private GenericMarshaller marshallerMock;
46
47 private GenericUnmarshaller unmarshallerMock;
48
49 private boolean responseInvoked;
50
51 @Before
52 public void setUp() throws Exception {
53 adapter = new GenericMarshallingMethodEndpointAdapter();
54 marshallerMock = createMock(GenericMarshaller.class);
55 adapter.setMarshaller(marshallerMock);
56 unmarshallerMock = createMock(GenericUnmarshaller.class);
57 adapter.setUnmarshaller(unmarshallerMock);
58 adapter.afterPropertiesSet();
59 }
60
61 @Test
62 public void testNoResponse() throws Exception {
63 WebServiceMessage messageMock = createMock(WebServiceMessage.class);
64 expect(messageMock.getPayloadSource()).andReturn(new StringSource("<request/>"));
65 WebServiceMessageFactory factoryMock = createMock(WebServiceMessageFactory.class);
66 MessageContext messageContext = new DefaultMessageContext(messageMock, factoryMock);
67
68 Method noResponse = getClass().getMethod("noResponse", MyGenericType.class);
69 MethodEndpoint methodEndpoint = new MethodEndpoint(this, noResponse);
70 expect(unmarshallerMock.unmarshal(isA(Source.class))).andReturn(new MyGenericType<MyType>());
71
72 replay(marshallerMock, unmarshallerMock, messageMock, factoryMock);
73
74 Assert.assertFalse("Method invoked", noResponseInvoked);
75 adapter.invoke(messageContext, methodEndpoint);
76 Assert.assertTrue("Method not invoked", noResponseInvoked);
77
78 verify(marshallerMock, unmarshallerMock, messageMock, factoryMock);
79 }
80
81 @Test
82 public void testNoRequestPayload() throws Exception {
83 WebServiceMessage messageMock = createMock(WebServiceMessage.class);
84 expect(messageMock.getPayloadSource()).andReturn(null);
85 WebServiceMessageFactory factoryMock = createMock(WebServiceMessageFactory.class);
86 MessageContext messageContext = new DefaultMessageContext(messageMock, factoryMock);
87
88 Method noResponse = getClass().getMethod("noResponse", MyGenericType.class);
89 MethodEndpoint methodEndpoint = new MethodEndpoint(this, noResponse);
90
91 replay(marshallerMock, unmarshallerMock, messageMock, factoryMock);
92
93 Assert.assertFalse("Method invoked", noResponseInvoked);
94 adapter.invoke(messageContext, methodEndpoint);
95 Assert.assertTrue("Method not invoked", noResponseInvoked);
96
97 verify(marshallerMock, unmarshallerMock, messageMock, factoryMock);
98 }
99
100 @Test
101 public void testResponse() throws Exception {
102 WebServiceMessage requestMock = createMock(WebServiceMessage.class);
103 expect(requestMock.getPayloadSource()).andReturn(new StringSource("<request/>"));
104 WebServiceMessage responseMock = createMock(WebServiceMessage.class);
105 expect(responseMock.getPayloadResult()).andReturn(new StringResult());
106 WebServiceMessageFactory factoryMock = createMock(WebServiceMessageFactory.class);
107 expect(factoryMock.createWebServiceMessage()).andReturn(responseMock);
108 MessageContext messageContext = new DefaultMessageContext(requestMock, factoryMock);
109
110 Method response = getClass().getMethod("response", MyGenericType.class);
111 MethodEndpoint methodEndpoint = new MethodEndpoint(this, response);
112 expect(unmarshallerMock.unmarshal(isA(Source.class))).andReturn(new MyGenericType<MyType>());
113 marshallerMock.marshal(isA(MyGenericType.class), isA(Result.class));
114
115 replay(marshallerMock, unmarshallerMock, requestMock, responseMock, factoryMock);
116
117 Assert.assertFalse("Method invoked", responseInvoked);
118 adapter.invoke(messageContext, methodEndpoint);
119 Assert.assertTrue("Method not invoked", responseInvoked);
120
121 verify(marshallerMock, unmarshallerMock, requestMock, responseMock, factoryMock);
122 }
123
124 @Test
125 public void testSupportedNoResponse() throws NoSuchMethodException {
126 Method noResponse = getClass().getMethod("noResponse", MyGenericType.class);
127 MethodEndpoint methodEndpoint = new MethodEndpoint(this, noResponse);
128 expect(unmarshallerMock.supports(noResponse.getGenericParameterTypes()[0])).andReturn(true);
129
130 replay(marshallerMock, unmarshallerMock);
131
132 Assert.assertTrue("Method unsupported", adapter.supportsInternal(methodEndpoint));
133
134 verify(marshallerMock, unmarshallerMock);
135 }
136
137 @Test
138 public void testSupportedResponse() throws NoSuchMethodException {
139 Method response = getClass().getMethod("response", MyGenericType.class);
140 MethodEndpoint methodEndpoint = new MethodEndpoint(this, response);
141
142 expect(unmarshallerMock.supports(response.getGenericParameterTypes()[0])).andReturn(true);
143 expect(marshallerMock.supports(response.getGenericReturnType())).andReturn(true);
144
145 replay(marshallerMock, unmarshallerMock);
146
147 Assert.assertTrue("Method unsupported", adapter.supportsInternal(methodEndpoint));
148
149 verify(marshallerMock, unmarshallerMock);
150 }
151
152 @Test
153 public void testUnsupportedMethodMultipleParams() throws NoSuchMethodException {
154 Method unsupported = getClass().getMethod("unsupportedMultipleParams", String.class, String.class);
155
156 replay(marshallerMock, unmarshallerMock);
157
158 Assert.assertFalse("Method supported", adapter.supportsInternal(new MethodEndpoint(this, unsupported)));
159
160 verify(marshallerMock, unmarshallerMock);
161 }
162
163 @Test
164 public void testUnsupportedMethodWrongParam() throws NoSuchMethodException {
165 Method unsupported = getClass().getMethod("unsupportedWrongParam", String.class);
166 expect(unmarshallerMock.supports(unsupported.getGenericParameterTypes()[0])).andReturn(false);
167 expect(marshallerMock.supports(unsupported.getGenericReturnType())).andReturn(true);
168
169 replay(marshallerMock, unmarshallerMock);
170
171 Assert.assertFalse("Method supported", adapter.supportsInternal(new MethodEndpoint(this, unsupported)));
172
173 verify(marshallerMock, unmarshallerMock);
174 }
175
176 @Test
177 public void testUnsupportedMethodWrongReturnType() throws NoSuchMethodException {
178 Method unsupported = getClass().getMethod("unsupportedWrongParam", String.class);
179 expect(marshallerMock.supports(unsupported.getGenericReturnType())).andReturn(false);
180
181 replay(marshallerMock, unmarshallerMock);
182
183 Assert.assertFalse("Method supported", adapter.supportsInternal(new MethodEndpoint(this, unsupported)));
184
185 verify(marshallerMock, unmarshallerMock);
186 }
187
188 public void noResponse(MyGenericType<MyType> type) {
189 noResponseInvoked = true;
190
191 }
192
193 public MyGenericType<MyType> response(MyGenericType<MyType> type) {
194 responseInvoked = true;
195 return type;
196 }
197
198 public void unsupportedMultipleParams(String s1, String s2) {
199 }
200
201 public String unsupportedWrongParam(String s) {
202 return s;
203 }
204
205 private static class MyType {
206
207 }
208
209 private static class MyGenericType<T> {
210
211 }
212
213
214 }