001: /*
002: * Copyright 2007 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.transport.support;
018:
019: import java.util.List;
020: import java.util.Properties;
021:
022: import junit.framework.TestCase;
023: import org.springframework.beans.BeansException;
024: import org.springframework.beans.factory.BeanInitializationException;
025: import org.springframework.context.ApplicationContext;
026: import org.springframework.context.ApplicationContextAware;
027: import org.springframework.context.support.StaticApplicationContext;
028: import org.springframework.core.io.ClassPathResource;
029: import org.springframework.core.io.Resource;
030:
031: public class DefaultStrategiesHelperTest extends TestCase {
032:
033: public void testGetDefaultStrategies() throws Exception {
034:
035: Properties strategies = new Properties();
036: strategies.put(Strategy.class.getName(), StrategyImpl.class
037: .getName()
038: + "," + ContextAwareStrategyImpl.class.getName());
039: DefaultStrategiesHelper helper = new DefaultStrategiesHelper(
040: strategies);
041:
042: StaticApplicationContext applicationContext = new StaticApplicationContext();
043: applicationContext.registerSingleton("strategy1",
044: StrategyImpl.class);
045: applicationContext.registerSingleton("strategy2",
046: ContextAwareStrategyImpl.class);
047:
048: List result = helper.getDefaultStrategies(Strategy.class,
049: applicationContext);
050: assertNotNull("No result", result);
051: assertEquals("Invalid amount of strategies", 2, result.size());
052: assertTrue("Result not a Strategy implementation", result
053: .get(0) instanceof Strategy);
054: assertTrue("Result not a Strategy implementation", result
055: .get(1) instanceof Strategy);
056: assertTrue("Result not a StrategyImpl implementation", result
057: .get(0) instanceof StrategyImpl);
058: assertTrue("Result not a StrategyImpl implementation", result
059: .get(1) instanceof ContextAwareStrategyImpl);
060: ContextAwareStrategyImpl impl = (ContextAwareStrategyImpl) result
061: .get(1);
062: assertNotNull("No application context injected", impl
063: .getApplicationContext());
064: }
065:
066: public void testGetDefaultStrategy() throws Exception {
067: Properties strategies = new Properties();
068: strategies.put(Strategy.class.getName(), StrategyImpl.class
069: .getName());
070: DefaultStrategiesHelper helper = new DefaultStrategiesHelper(
071: strategies);
072:
073: StaticApplicationContext applicationContext = new StaticApplicationContext();
074: applicationContext.registerSingleton("strategy1",
075: StrategyImpl.class);
076: applicationContext.registerSingleton("strategy2",
077: ContextAwareStrategyImpl.class);
078:
079: Object result = helper.getDefaultStrategy(Strategy.class,
080: applicationContext);
081: assertNotNull("No result", result);
082: assertTrue("Result not a Strategy implementation",
083: result instanceof Strategy);
084: assertTrue("Result not a StrategyImpl implementation",
085: result instanceof StrategyImpl);
086: }
087:
088: public void testGetDefaultStrategyMoreThanOne() throws Exception {
089: Properties strategies = new Properties();
090: strategies.put(Strategy.class.getName(), StrategyImpl.class
091: .getName()
092: + "," + ContextAwareStrategyImpl.class.getName());
093: DefaultStrategiesHelper helper = new DefaultStrategiesHelper(
094: strategies);
095:
096: StaticApplicationContext applicationContext = new StaticApplicationContext();
097: applicationContext.registerSingleton("strategy1",
098: StrategyImpl.class);
099: applicationContext.registerSingleton("strategy2",
100: ContextAwareStrategyImpl.class);
101:
102: try {
103: helper.getDefaultStrategy(Strategy.class,
104: applicationContext);
105: fail("Expected BeanInitializationException");
106: } catch (BeanInitializationException ex) {
107: // expected
108: }
109: }
110:
111: public void testResourceConstructor() throws Exception {
112: Resource resource = new ClassPathResource(
113: "strategies.properties", getClass());
114: new DefaultStrategiesHelper(resource);
115: }
116:
117: public interface Strategy {
118:
119: }
120:
121: private static class StrategyImpl implements Strategy {
122:
123: }
124:
125: private static class ContextAwareStrategyImpl implements Strategy,
126: ApplicationContextAware {
127:
128: private ApplicationContext applicationContext;
129:
130: public ApplicationContext getApplicationContext() {
131: return applicationContext;
132: }
133:
134: public void setApplicationContext(
135: ApplicationContext applicationContext)
136: throws BeansException {
137: this.applicationContext = applicationContext;
138: }
139: }
140: }
|