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  import org.springframework.context.support.StaticApplicationContext;
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.EndpointInterceptorAdapter;
26  
27  public class EndpointMappingTest extends TestCase {
28  
29      private MessageContext mockContext;
30  
31      private MockControl contextControl;
32  
33      protected void setUp() throws Exception {
34          contextControl = MockControl.createControl(MessageContext.class);
35          mockContext = (MessageContext) contextControl.getMock();
36      }
37  
38      public void testDefaultEndpoint() throws Exception {
39          Object defaultEndpoint = new Object();
40          AbstractEndpointMapping mapping = new AbstractEndpointMapping() {
41              protected Object getEndpointInternal(MessageContext givenRequest) throws Exception {
42                  assertEquals("Invalid request passed", mockContext, givenRequest);
43                  return null;
44              }
45          };
46          mapping.setDefaultEndpoint(defaultEndpoint);
47          contextControl.replay();
48  
49          EndpointInvocationChain result = mapping.getEndpoint(mockContext);
50          assertNotNull("No EndpointInvocatioChain returned", result);
51          assertEquals("Default Endpoint not returned", defaultEndpoint, result.getEndpoint());
52          contextControl.verify();
53      }
54  
55      public void testEndpoint() throws Exception {
56          final Object endpoint = new Object();
57          AbstractEndpointMapping mapping = new AbstractEndpointMapping() {
58              protected Object getEndpointInternal(MessageContext givenRequest) throws Exception {
59                  assertEquals("Invalid request passed", mockContext, givenRequest);
60                  return endpoint;
61              }
62          };
63          contextControl.replay();
64  
65          EndpointInvocationChain result = mapping.getEndpoint(mockContext);
66          assertNotNull("No EndpointInvocatioChain returned", result);
67          assertEquals("Unexpected Endpoint returned", endpoint, result.getEndpoint());
68          contextControl.verify();
69      }
70  
71      public void testEndpointInterceptors() throws Exception {
72          final Object endpoint = new Object();
73          EndpointInterceptor interceptor = new EndpointInterceptorAdapter();
74          AbstractEndpointMapping mapping = new AbstractEndpointMapping() {
75              protected Object getEndpointInternal(MessageContext givenRequest) throws Exception {
76                  assertEquals("Invalid request passed", mockContext, givenRequest);
77                  return endpoint;
78              }
79          };
80          contextControl.replay();
81          mapping.setInterceptors(new EndpointInterceptor[]{interceptor});
82          EndpointInvocationChain result = mapping.getEndpoint(mockContext);
83          assertEquals("Unexpected amount of EndpointInterceptors returned", 1, result.getInterceptors().length);
84          assertEquals("Unexpected EndpointInterceptor returned", interceptor, result.getInterceptors()[0]);
85          contextControl.verify();
86      }
87  
88      public void testEndpointBeanName() throws Exception {
89          StaticApplicationContext applicationContext = new StaticApplicationContext();
90          applicationContext.registerSingleton("endpoint", Object.class);
91  
92          AbstractEndpointMapping mapping = new AbstractEndpointMapping() {
93  
94              protected Object getEndpointInternal(MessageContext message) throws Exception {
95                  assertEquals("Invalid request", mockContext, message);
96                  return "endpoint";
97              }
98          };
99          mapping.setApplicationContext(applicationContext);
100         contextControl.replay();
101 
102         EndpointInvocationChain result = mapping.getEndpoint(mockContext);
103         assertNotNull("No endpoint returned", result);
104         contextControl.verify();
105     }
106 
107     public void testEndpointInvalidBeanName() throws Exception {
108         StaticApplicationContext applicationContext = new StaticApplicationContext();
109         applicationContext.registerSingleton("endpoint", Object.class);
110 
111         AbstractEndpointMapping mapping = new AbstractEndpointMapping() {
112 
113             protected Object getEndpointInternal(MessageContext message) throws Exception {
114                 assertEquals("Invalid request", mockContext, message);
115                 return "noSuchBean";
116             }
117         };
118         mapping.setApplicationContext(applicationContext);
119         contextControl.replay();
120 
121         EndpointInvocationChain result = mapping.getEndpoint(mockContext);
122 
123         assertNull("No endpoint returned", result);
124         contextControl.verify();
125     }
126 
127 
128 }