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