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 java.util.Arrays;
20  import java.util.Map;
21  import java.util.TreeMap;
22  
23  import junit.framework.TestCase;
24  import org.springframework.context.support.StaticApplicationContext;
25  import org.springframework.ws.context.MessageContext;
26  
27  /**
28   * Test case for AbstractMapBasedEndpointMapping.
29   */
30  public class MapBasedSoapEndpointMappingTest extends TestCase {
31  
32      public void testBeanNames() throws Exception {
33          StaticApplicationContext context = new StaticApplicationContext();
34          context.registerSingleton("endpointMapping", MyMapBasedEndpointMapping.class);
35          context.registerSingleton("endpoint", Object.class);
36          context.registerAlias("endpoint", "alias");
37          MyMapBasedEndpointMapping mapping = new MyMapBasedEndpointMapping();
38          mapping.setValidKeys(new String[]{"endpoint", "alias"});
39  
40          mapping.setRegisterBeanNames(true);
41          mapping.setApplicationContext(context);
42  
43          // try bean
44          mapping.setKey("endpoint");
45          assertNotNull("No endpoint returned", mapping.getEndpointInternal(null));
46  
47          // try alias
48          mapping.setKey("alias");
49          assertNotNull("No endpoint returned", mapping.getEndpointInternal(null));
50  
51          // try non-mapped values
52          mapping.setKey("endpointMapping");
53          assertNull("Endpoint returned", mapping.getEndpointInternal(null));
54  
55      }
56  
57      public void testDisabledBeanNames() throws Exception {
58          StaticApplicationContext context = new StaticApplicationContext();
59          context.registerSingleton("endpoint", Object.class);
60  
61          MyMapBasedEndpointMapping mapping = new MyMapBasedEndpointMapping();
62  
63          mapping.setRegisterBeanNames(true);
64          mapping.setApplicationContext(context);
65  
66          mapping.setKey("endpoint");
67          assertNull("Endpoint returned", mapping.getEndpointInternal(null));
68      }
69  
70      public void testEndpointMap() throws Exception {
71          Map endpointMap = new TreeMap();
72          Object endpoint1 = new Object();
73          Object endpoint2 = new Object();
74          endpointMap.put("endpoint1", endpoint1);
75          endpointMap.put("endpoint2", endpoint2);
76  
77          MyMapBasedEndpointMapping mapping = new MyMapBasedEndpointMapping();
78          mapping.setValidKeys(new String[]{"endpoint1", "endpoint2"});
79  
80          mapping.setEndpointMap(endpointMap);
81          mapping.initApplicationContext();
82  
83          // try endpoint1
84          mapping.setKey("endpoint1");
85          assertNotNull("No endpoint returned", mapping.getEndpointInternal(null));
86  
87          // try endpoint2
88          mapping.setKey("endpoint2");
89          assertNotNull("No endpoint returned", mapping.getEndpointInternal(null));
90  
91          // try non-mapped values
92          mapping.setKey("endpoint3");
93          assertNull("Endpoint returned", mapping.getEndpointInternal(null));
94      }
95  
96      private static class MyMapBasedEndpointMapping extends AbstractMapBasedEndpointMapping {
97  
98          private String key;
99  
100         private String[] validKeys = new String[0];
101 
102         public void setKey(String key) {
103             this.key = key;
104         }
105 
106         public void setValidKeys(String[] validKeys) {
107             this.validKeys = validKeys;
108             Arrays.sort(this.validKeys);
109         }
110 
111         protected boolean validateLookupKey(String key) {
112             return Arrays.binarySearch(validKeys, key) >= 0;
113         }
114 
115         protected String getLookupKeyForMessage(MessageContext messageContext) throws Exception {
116             return key;
117         }
118     }
119 
120 }