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;
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.Marshaller;
24  import org.springframework.oxm.Unmarshaller;
25  import org.springframework.ws.MockWebServiceMessage;
26  import org.springframework.ws.MockWebServiceMessageFactory;
27  import org.springframework.ws.context.DefaultMessageContext;
28  import org.springframework.ws.context.MessageContext;
29  import org.springframework.ws.server.endpoint.MethodEndpoint;
30  
31  import org.junit.Assert;
32  import org.junit.Before;
33  import org.junit.Test;
34  
35  import static org.easymock.EasyMock.*;
36  
37  public class MarshallingMethodEndpointAdapterTest {
38  
39      private MarshallingMethodEndpointAdapter adapter;
40  
41      private boolean noResponseInvoked;
42  
43      private Marshaller marshallerMock;
44  
45      private Unmarshaller unmarshallerMock;
46  
47      private MessageContext messageContext;
48  
49      private boolean responseInvoked;
50  
51      @Before
52      public void setUp() throws Exception {
53          adapter = new MarshallingMethodEndpointAdapter();
54          marshallerMock = createMock(Marshaller.class);
55          adapter.setMarshaller(marshallerMock);
56          unmarshallerMock = createMock(Unmarshaller.class);
57          adapter.setUnmarshaller(unmarshallerMock);
58          adapter.afterPropertiesSet();
59          MockWebServiceMessage request = new MockWebServiceMessage("<request/>");
60          messageContext = new DefaultMessageContext(request, new MockWebServiceMessageFactory());
61      }
62  
63      @Test
64      public void testNoResponse() throws Exception {
65          Method noResponse = getClass().getMethod("noResponse", new Class[]{MyType.class});
66          MethodEndpoint methodEndpoint = new MethodEndpoint(this, noResponse);
67          expect(unmarshallerMock.unmarshal(isA(Source.class))).andReturn(new MyType());
68  
69          replay(marshallerMock, unmarshallerMock);
70  
71          Assert.assertFalse("Method invoked", noResponseInvoked);
72          adapter.invoke(messageContext, methodEndpoint);
73          Assert.assertTrue("Method not invoked", noResponseInvoked);
74  
75          verify(marshallerMock, unmarshallerMock);
76      }
77  
78      @Test
79      public void testNoRequestPayload() throws Exception {
80          MockWebServiceMessage request = new MockWebServiceMessage();
81          messageContext = new DefaultMessageContext(request, new MockWebServiceMessageFactory());
82          Method noResponse = getClass().getMethod("noResponse", new Class[]{MyType.class});
83          MethodEndpoint methodEndpoint = new MethodEndpoint(this, noResponse);
84  
85          replay(marshallerMock, unmarshallerMock);
86  
87          Assert.assertFalse("Method invoked", noResponseInvoked);
88          adapter.invoke(messageContext, methodEndpoint);
89          Assert.assertTrue("Method not invoked", noResponseInvoked);
90  
91          verify(marshallerMock, unmarshallerMock);
92      }
93  
94      @Test
95      public void testResponse() throws Exception {
96          Method response = getClass().getMethod("response", new Class[]{MyType.class});
97          MethodEndpoint methodEndpoint = new MethodEndpoint(this, response);
98          expect(unmarshallerMock.unmarshal(isA(Source.class))).andReturn(new MyType());
99          marshallerMock.marshal(isA(MyType.class), isA(Result.class));
100 
101         replay(marshallerMock, unmarshallerMock);
102 
103         Assert.assertFalse("Method invoked", responseInvoked);
104         adapter.invoke(messageContext, methodEndpoint);
105         Assert.assertTrue("Method not invoked", responseInvoked);
106 
107         verify(marshallerMock, unmarshallerMock);
108     }
109 
110     @Test
111     public void testSupportedNoResponse() throws NoSuchMethodException {
112         Method noResponse = getClass().getMethod("noResponse", new Class[]{MyType.class});
113         MethodEndpoint methodEndpoint = new MethodEndpoint(this, noResponse);
114         expect(unmarshallerMock.supports(MyType.class)).andReturn(true);
115 
116         replay(marshallerMock, unmarshallerMock);
117 
118         Assert.assertTrue("Method unsupported", adapter.supportsInternal(methodEndpoint));
119 
120         verify(marshallerMock, unmarshallerMock);
121     }
122 
123     @Test
124     public void testSupportedResponse() throws NoSuchMethodException {
125         Method response = getClass().getMethod("response", new Class[]{MyType.class});
126         MethodEndpoint methodEndpoint = new MethodEndpoint(this, response);
127         expect(unmarshallerMock.supports(MyType.class)).andReturn(true);
128         expect(marshallerMock.supports(MyType.class)).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 testUnsupportedMethodMultipleParams() throws NoSuchMethodException {
139         Method unsupported = getClass().getMethod("unsupportedMultipleParams", new Class[]{String.class, String.class});
140 
141         replay(marshallerMock, unmarshallerMock);
142 
143         Assert.assertFalse("Method supported", adapter.supportsInternal(new MethodEndpoint(this, unsupported)));
144 
145         verify(marshallerMock, unmarshallerMock);
146     }
147 
148     @Test
149     public void testUnsupportedMethodWrongParam() throws NoSuchMethodException {
150         Method unsupported = getClass().getMethod("unsupportedWrongParam", new Class[]{String.class});
151         expect(unmarshallerMock.supports(String.class)).andReturn(false);
152         expect(marshallerMock.supports(String.class)).andReturn(true);
153 
154         replay(marshallerMock, unmarshallerMock);
155 
156         Assert.assertFalse("Method supported", adapter.supportsInternal(new MethodEndpoint(this, unsupported)));
157 
158         verify(marshallerMock, unmarshallerMock);
159     }
160 
161     @Test
162     public void testUnsupportedMethodWrongReturnType() throws NoSuchMethodException {
163         Method unsupported = getClass().getMethod("unsupportedWrongParam", new Class[]{String.class});
164         expect(marshallerMock.supports(String.class)).andReturn(false);
165 
166         replay(marshallerMock, unmarshallerMock);
167 
168         Assert.assertFalse("Method supported", adapter.supportsInternal(new MethodEndpoint(this, unsupported)));
169 
170         verify(marshallerMock, unmarshallerMock);
171     }
172 
173     public void noResponse(MyType type) {
174         noResponseInvoked = true;
175 
176     }
177 
178     public MyType response(MyType type) {
179         responseInvoked = true;
180         return new MyType();
181     }
182 
183     public void unsupportedMultipleParams(String s1, String s2) {
184     }
185 
186     public String unsupportedWrongParam(String s) {
187         return s;
188     }
189 
190     private static class MyType {
191 
192     }
193 
194 }