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.mapping;
18  
19  import junit.framework.TestCase;
20  import org.easymock.MockControl;
21  
22  import org.springframework.context.support.StaticApplicationContext;
23  import org.springframework.ws.context.MessageContext;
24  import org.springframework.ws.server.EndpointInterceptor;
25  import org.springframework.ws.server.EndpointInvocationChain;
26  import org.springframework.ws.server.endpoint.interceptor.EndpointInterceptorAdapter;
27  
28  public class EndpointMappingTest extends TestCase {
29  
30      private MessageContext mockContext;
31  
32      private MockControl contextControl;
33  
34      protected void setUp() throws Exception {
35          contextControl = MockControl.createControl(MessageContext.class);
36          mockContext = (MessageContext) contextControl.getMock();
37      }
38  
39      public void testDefaultEndpoint() throws Exception {
40          Object defaultEndpoint = new Object();
41          AbstractEndpointMapping mapping = new AbstractEndpointMapping() {
42              protected Object getEndpointInternal(MessageContext givenRequest) throws Exception {
43                  assertEquals("Invalid request passed", mockContext, givenRequest);
44                  return null;
45              }
46          };
47          mapping.setDefaultEndpoint(defaultEndpoint);
48          contextControl.replay();
49  
50          EndpointInvocationChain result = mapping.getEndpoint(mockContext);
51          assertNotNull("No EndpointInvocatioChain returned", result);
52          assertEquals("Default Endpoint not returned", defaultEndpoint, result.getEndpoint());
53          contextControl.verify();
54      }
55  
56      public void testEndpoint() throws Exception {
57          final Object endpoint = new Object();
58          AbstractEndpointMapping mapping = new AbstractEndpointMapping() {
59              protected Object getEndpointInternal(MessageContext givenRequest) throws Exception {
60                  assertEquals("Invalid request passed", mockContext, givenRequest);
61                  return endpoint;
62              }
63          };
64          contextControl.replay();
65  
66          EndpointInvocationChain result = mapping.getEndpoint(mockContext);
67          assertNotNull("No EndpointInvocatioChain returned", result);
68          assertEquals("Unexpected Endpoint returned", endpoint, result.getEndpoint());
69          contextControl.verify();
70      }
71  
72      public void testEndpointInterceptors() throws Exception {
73          final Object endpoint = new Object();
74          EndpointInterceptor interceptor = new EndpointInterceptorAdapter();
75          AbstractEndpointMapping mapping = new AbstractEndpointMapping() {
76              protected Object getEndpointInternal(MessageContext givenRequest) throws Exception {
77                  assertEquals("Invalid request passed", mockContext, givenRequest);
78                  return endpoint;
79              }
80          };
81          contextControl.replay();
82          mapping.setInterceptors(new EndpointInterceptor[]{interceptor});
83          EndpointInvocationChain result = mapping.getEndpoint(mockContext);
84          assertEquals("Unexpected amount of EndpointInterceptors returned", 1, result.getInterceptors().length);
85          assertEquals("Unexpected EndpointInterceptor returned", interceptor, result.getInterceptors()[0]);
86          contextControl.verify();
87      }
88  
89      public void testEndpointBeanName() throws Exception {
90          StaticApplicationContext applicationContext = new StaticApplicationContext();
91          applicationContext.registerSingleton("endpoint", Object.class);
92  
93          AbstractEndpointMapping mapping = new AbstractEndpointMapping() {
94  
95              protected Object getEndpointInternal(MessageContext message) throws Exception {
96                  assertEquals("Invalid request", mockContext, message);
97                  return "endpoint";
98              }
99          };
100         mapping.setApplicationContext(applicationContext);
101         contextControl.replay();
102 
103         EndpointInvocationChain result = mapping.getEndpoint(mockContext);
104         assertNotNull("No endpoint returned", result);
105         contextControl.verify();
106     }
107 
108     public void testEndpointInvalidBeanName() throws Exception {
109         StaticApplicationContext applicationContext = new StaticApplicationContext();
110         applicationContext.registerSingleton("endpoint", Object.class);
111 
112         AbstractEndpointMapping mapping = new AbstractEndpointMapping() {
113 
114             protected Object getEndpointInternal(MessageContext message) throws Exception {
115                 assertEquals("Invalid request", mockContext, message);
116                 return "noSuchBean";
117             }
118         };
119         mapping.setApplicationContext(applicationContext);
120         contextControl.replay();
121 
122         EndpointInvocationChain result = mapping.getEndpoint(mockContext);
123 
124         assertNull("No endpoint returned", result);
125         contextControl.verify();
126     }
127 
128     public void testEndpointPrototype() throws Exception {
129         StaticApplicationContext applicationContext = new StaticApplicationContext();
130         applicationContext.registerPrototype("endpoint", MyEndpoint.class);
131 
132         AbstractEndpointMapping mapping = new AbstractEndpointMapping() {
133 
134             protected Object getEndpointInternal(MessageContext message) throws Exception {
135                 assertEquals("Invalid request", mockContext, message);
136                 return "endpoint";
137             }
138         };
139         mapping.setApplicationContext(applicationContext);
140         contextControl.replay();
141 
142         EndpointInvocationChain result = mapping.getEndpoint(mockContext);
143         assertNotNull("No endpoint returned", result);
144         result = mapping.getEndpoint(mockContext);
145         assertNotNull("No endpoint returned", result);
146         assertEquals("Prototype endpoint was not constructed twice", 2, MyEndpoint.constrCount);
147         contextControl.verify();
148     }
149 
150     private static class MyEndpoint {
151 
152         private static int constrCount;
153 
154         private MyEndpoint() {
155             constrCount++;
156         }
157     }
158 
159 }