001: /*
002: * Copyright 2005 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.ws.server.endpoint.mapping;
018:
019: import java.util.Arrays;
020: import java.util.Map;
021: import java.util.TreeMap;
022:
023: import junit.framework.TestCase;
024: import org.springframework.context.support.StaticApplicationContext;
025: import org.springframework.ws.context.MessageContext;
026:
027: /**
028: * Test case for AbstractMapBasedEndpointMapping.
029: */
030: public class MapBasedSoapEndpointMappingTest extends TestCase {
031:
032: public void testBeanNames() throws Exception {
033: StaticApplicationContext context = new StaticApplicationContext();
034: context.registerSingleton("endpointMapping",
035: MyMapBasedEndpointMapping.class);
036: context.registerSingleton("endpoint", Object.class);
037: context.registerAlias("endpoint", "alias");
038: MyMapBasedEndpointMapping mapping = new MyMapBasedEndpointMapping();
039: mapping.setValidKeys(new String[] { "endpoint", "alias" });
040:
041: mapping.setRegisterBeanNames(true);
042: mapping.setApplicationContext(context);
043:
044: // try bean
045: mapping.setKey("endpoint");
046: assertNotNull("No endpoint returned", mapping
047: .getEndpointInternal(null));
048:
049: // try alias
050: mapping.setKey("alias");
051: assertNotNull("No endpoint returned", mapping
052: .getEndpointInternal(null));
053:
054: // try non-mapped values
055: mapping.setKey("endpointMapping");
056: assertNull("Endpoint returned", mapping
057: .getEndpointInternal(null));
058:
059: }
060:
061: public void testDisabledBeanNames() throws Exception {
062: StaticApplicationContext context = new StaticApplicationContext();
063: context.registerSingleton("endpoint", Object.class);
064:
065: MyMapBasedEndpointMapping mapping = new MyMapBasedEndpointMapping();
066:
067: mapping.setRegisterBeanNames(true);
068: mapping.setApplicationContext(context);
069:
070: mapping.setKey("endpoint");
071: assertNull("Endpoint returned", mapping
072: .getEndpointInternal(null));
073: }
074:
075: public void testEndpointMap() throws Exception {
076: Map endpointMap = new TreeMap();
077: Object endpoint1 = new Object();
078: Object endpoint2 = new Object();
079: endpointMap.put("endpoint1", endpoint1);
080: endpointMap.put("endpoint2", endpoint2);
081:
082: MyMapBasedEndpointMapping mapping = new MyMapBasedEndpointMapping();
083: mapping.setValidKeys(new String[] { "endpoint1", "endpoint2" });
084:
085: mapping.setEndpointMap(endpointMap);
086: mapping.initApplicationContext();
087:
088: // try endpoint1
089: mapping.setKey("endpoint1");
090: assertNotNull("No endpoint returned", mapping
091: .getEndpointInternal(null));
092:
093: // try endpoint2
094: mapping.setKey("endpoint2");
095: assertNotNull("No endpoint returned", mapping
096: .getEndpointInternal(null));
097:
098: // try non-mapped values
099: mapping.setKey("endpoint3");
100: assertNull("Endpoint returned", mapping
101: .getEndpointInternal(null));
102: }
103:
104: private static class MyMapBasedEndpointMapping extends
105: AbstractMapBasedEndpointMapping {
106:
107: private String key;
108:
109: private String[] validKeys = new String[0];
110:
111: public void setKey(String key) {
112: this .key = key;
113: }
114:
115: public void setValidKeys(String[] validKeys) {
116: this .validKeys = validKeys;
117: Arrays.sort(this .validKeys);
118: }
119:
120: protected boolean validateLookupKey(String key) {
121: return Arrays.binarySearch(validKeys, key) >= 0;
122: }
123:
124: protected String getLookupKeyForMessage(
125: MessageContext messageContext) throws Exception {
126: return key;
127: }
128: }
129:
130: }
|