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.mapping;
18  
19  import org.springframework.context.support.StaticApplicationContext;
20  import org.springframework.ws.MockWebServiceMessageFactory;
21  import org.springframework.ws.context.DefaultMessageContext;
22  import org.springframework.ws.context.MessageContext;
23  import org.springframework.ws.server.EndpointInterceptor;
24  import org.springframework.ws.server.EndpointInvocationChain;
25  import org.springframework.ws.server.endpoint.interceptor.DelegatingSmartEndpointInterceptor;
26  import org.springframework.ws.server.endpoint.interceptor.EndpointInterceptorAdapter;
27  
28  import org.junit.Before;
29  import org.junit.Test;
30  
31  import static org.junit.Assert.*;
32  
33  /**
34   * Test case for [email protected] AbstractEndpointMapping}.
35   */
36  public class EndpointMappingTest {
37  
38      private MessageContext messageContext;
39  
40      @Before
41      public void setUp() throws Exception {
42          messageContext = new DefaultMessageContext(new MockWebServiceMessageFactory());
43      }
44  
45      @Test
46      public void defaultEndpoint() throws Exception {
47          Object defaultEndpoint = new Object();
48          AbstractEndpointMapping mapping = new AbstractEndpointMapping() {
49              @Override
50              protected Object getEndpointInternal(MessageContext givenRequest) throws Exception {
51                  assertEquals("Invalid request passed", messageContext, givenRequest);
52                  return null;
53              }
54          };
55          mapping.setDefaultEndpoint(defaultEndpoint);
56  
57          EndpointInvocationChain result = mapping.getEndpoint(messageContext);
58          assertNotNull("No EndpointInvocatioChain returned", result);
59          assertEquals("Default Endpoint not returned", defaultEndpoint, result.getEndpoint());
60      }
61  
62      @Test
63      public void endpoint() throws Exception {
64          final Object endpoint = new Object();
65          AbstractEndpointMapping mapping = new AbstractEndpointMapping() {
66              @Override
67              protected Object getEndpointInternal(MessageContext givenRequest) throws Exception {
68                  assertEquals("Invalid request passed", messageContext, givenRequest);
69                  return endpoint;
70              }
71          };
72  
73          EndpointInvocationChain result = mapping.getEndpoint(messageContext);
74          assertNotNull("No EndpointInvocationChain returned", result);
75          assertEquals("Unexpected Endpoint returned", endpoint, result.getEndpoint());
76      }
77  
78      @Test
79      public void endpointInterceptors() throws Exception {
80          final Object endpoint = new Object();
81          EndpointInterceptor interceptor = new EndpointInterceptorAdapter();
82          AbstractEndpointMapping mapping = new AbstractEndpointMapping() {
83              @Override
84              protected Object getEndpointInternal(MessageContext givenRequest) throws Exception {
85                  assertEquals("Invalid request passed", messageContext, givenRequest);
86                  return endpoint;
87              }
88          };
89  
90          mapping.setInterceptors(new EndpointInterceptor[]{interceptor});
91          EndpointInvocationChain result = mapping.getEndpoint(messageContext);
92          assertEquals("Unexpected amount of EndpointInterceptors returned", 1, result.getInterceptors().length);
93          assertEquals("Unexpected EndpointInterceptor returned", interceptor, result.getInterceptors()[0]);
94      }
95  
96      @Test
97      public void smartEndpointInterceptors() throws Exception {
98          StaticApplicationContext applicationContext = new StaticApplicationContext();
99          applicationContext.registerSingleton("smartInterceptor", MySmartEndpointInterceptor.class);
100 
101         final Object endpoint = new Object();
102         EndpointInterceptor interceptor = new EndpointInterceptorAdapter();
103         AbstractEndpointMapping mapping = new AbstractEndpointMapping() {
104             @Override
105             protected Object getEndpointInternal(MessageContext givenRequest) throws Exception {
106                 assertEquals("Invalid request passed", messageContext, givenRequest);
107                 return endpoint;
108             }
109         };
110         mapping.setApplicationContext(applicationContext);
111         mapping.setInterceptors(new EndpointInterceptor[]{interceptor});
112         
113         EndpointInvocationChain result = mapping.getEndpoint(messageContext);
114         assertEquals("Unexpected amount of EndpointInterceptors returned", 2, result.getInterceptors().length);
115         assertEquals("Unexpected EndpointInterceptor returned", interceptor, result.getInterceptors()[0]);
116         assertTrue("Unexpected EndpointInterceptor returned",
117                 result.getInterceptors()[1] instanceof MySmartEndpointInterceptor);
118     }
119 
120     @Test
121     public void endpointBeanName() throws Exception {
122         StaticApplicationContext applicationContext = new StaticApplicationContext();
123         applicationContext.registerSingleton("endpoint", Object.class);
124 
125         AbstractEndpointMapping mapping = new AbstractEndpointMapping() {
126 
127             @Override
128             protected Object getEndpointInternal(MessageContext message) throws Exception {
129                 assertEquals("Invalid request", messageContext, message);
130                 return "endpoint";
131             }
132         };
133         mapping.setApplicationContext(applicationContext);
134 
135         EndpointInvocationChain result = mapping.getEndpoint(messageContext);
136         assertNotNull("No endpoint returned", result);
137     }
138 
139     @Test
140     public void endpointInvalidBeanName() throws Exception {
141         StaticApplicationContext applicationContext = new StaticApplicationContext();
142         applicationContext.registerSingleton("endpoint", Object.class);
143 
144         AbstractEndpointMapping mapping = new AbstractEndpointMapping() {
145 
146             @Override
147             protected Object getEndpointInternal(MessageContext message) throws Exception {
148                 assertEquals("Invalid request", messageContext, message);
149                 return "noSuchBean";
150             }
151         };
152         mapping.setApplicationContext(applicationContext);
153 
154         EndpointInvocationChain result = mapping.getEndpoint(messageContext);
155 
156         assertNull("No endpoint returned", result);
157     }
158 
159     @Test
160     public void endpointPrototype() throws Exception {
161         StaticApplicationContext applicationContext = new StaticApplicationContext();
162         applicationContext.registerPrototype("endpoint", MyEndpoint.class);
163 
164         AbstractEndpointMapping mapping = new AbstractEndpointMapping() {
165 
166             @Override
167             protected Object getEndpointInternal(MessageContext message) throws Exception {
168                 assertEquals("Invalid request", messageContext, message);
169                 return "endpoint";
170             }
171         };
172         mapping.setApplicationContext(applicationContext);
173 
174         EndpointInvocationChain result = mapping.getEndpoint(messageContext);
175         assertNotNull("No endpoint returned", result);
176         result = mapping.getEndpoint(messageContext);
177         assertNotNull("No endpoint returned", result);
178         assertEquals("Prototype endpoint was not constructed twice", 2, MyEndpoint.constructorCount);
179     }
180 
181     private static class MyEndpoint {
182 
183         private static int constructorCount;
184 
185         private MyEndpoint() {
186             constructorCount++;
187         }
188     }
189 
190     private static class MySmartEndpointInterceptor extends DelegatingSmartEndpointInterceptor {
191 
192         private MySmartEndpointInterceptor() {
193             super(new EndpointInterceptorAdapter());
194         }
195     }
196 
197 }