1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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
44 mapping.setKey("endpoint");
45 assertNotNull("No endpoint returned", mapping.getEndpointInternal(null));
46
47
48 mapping.setKey("alias");
49 assertNotNull("No endpoint returned", mapping.getEndpointInternal(null));
50
51
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
84 mapping.setKey("endpoint1");
85 assertNotNull("No endpoint returned", mapping.getEndpointInternal(null));
86
87
88 mapping.setKey("endpoint2");
89 assertNotNull("No endpoint returned", mapping.getEndpointInternal(null));
90
91
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 }