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 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
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
47 mapping.setKey("endpoint");
48 Assert.assertNotNull("No endpoint returned", mapping.getEndpointInternal(null));
49
50
51 mapping.setKey("alias");
52 Assert.assertNotNull("No endpoint returned", mapping.getEndpointInternal(null));
53
54
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
89 mapping.setKey("endpoint1");
90 Assert.assertNotNull("No endpoint returned", mapping.getEndpointInternal(null));
91
92
93 mapping.setKey("endpoint2");
94 Assert.assertNotNull("No endpoint returned", mapping.getEndpointInternal(null));
95
96
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 }