1   /*
2    * Copyright 2007 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.transport.support;
18  
19  import java.util.List;
20  import java.util.Properties;
21  
22  import junit.framework.TestCase;
23  import org.springframework.beans.BeansException;
24  import org.springframework.beans.factory.BeanInitializationException;
25  import org.springframework.context.ApplicationContext;
26  import org.springframework.context.ApplicationContextAware;
27  import org.springframework.context.support.StaticApplicationContext;
28  import org.springframework.core.io.ClassPathResource;
29  import org.springframework.core.io.Resource;
30  
31  public class DefaultStrategiesHelperTest extends TestCase {
32  
33      public void testGetDefaultStrategies() throws Exception {
34  
35          Properties strategies = new Properties();
36          strategies.put(Strategy.class.getName(),
37                  StrategyImpl.class.getName() + "," + ContextAwareStrategyImpl.class.getName());
38          DefaultStrategiesHelper helper = new DefaultStrategiesHelper(strategies);
39  
40          StaticApplicationContext applicationContext = new StaticApplicationContext();
41          applicationContext.registerSingleton("strategy1", StrategyImpl.class);
42          applicationContext.registerSingleton("strategy2", ContextAwareStrategyImpl.class);
43  
44          List result = helper.getDefaultStrategies(Strategy.class, applicationContext);
45          assertNotNull("No result", result);
46          assertEquals("Invalid amount of strategies", 2, result.size());
47          assertTrue("Result not a Strategy implementation", result.get(0) instanceof Strategy);
48          assertTrue("Result not a Strategy implementation", result.get(1) instanceof Strategy);
49          assertTrue("Result not a StrategyImpl implementation", result.get(0) instanceof StrategyImpl);
50          assertTrue("Result not a StrategyImpl implementation", result.get(1) instanceof ContextAwareStrategyImpl);
51          ContextAwareStrategyImpl impl = (ContextAwareStrategyImpl) result.get(1);
52          assertNotNull("No application context injected", impl.getApplicationContext());
53      }
54  
55      public void testGetDefaultStrategy() throws Exception {
56          Properties strategies = new Properties();
57          strategies.put(Strategy.class.getName(), StrategyImpl.class.getName());
58          DefaultStrategiesHelper helper = new DefaultStrategiesHelper(strategies);
59  
60          StaticApplicationContext applicationContext = new StaticApplicationContext();
61          applicationContext.registerSingleton("strategy1", StrategyImpl.class);
62          applicationContext.registerSingleton("strategy2", ContextAwareStrategyImpl.class);
63  
64          Object result = helper.getDefaultStrategy(Strategy.class, applicationContext);
65          assertNotNull("No result", result);
66          assertTrue("Result not a Strategy implementation", result instanceof Strategy);
67          assertTrue("Result not a StrategyImpl implementation", result instanceof StrategyImpl);
68      }
69  
70      public void testGetDefaultStrategyMoreThanOne() throws Exception {
71          Properties strategies = new Properties();
72          strategies.put(Strategy.class.getName(),
73                  StrategyImpl.class.getName() + "," + ContextAwareStrategyImpl.class.getName());
74          DefaultStrategiesHelper helper = new DefaultStrategiesHelper(strategies);
75  
76          StaticApplicationContext applicationContext = new StaticApplicationContext();
77          applicationContext.registerSingleton("strategy1", StrategyImpl.class);
78          applicationContext.registerSingleton("strategy2", ContextAwareStrategyImpl.class);
79  
80          try {
81              helper.getDefaultStrategy(Strategy.class, applicationContext);
82              fail("Expected BeanInitializationException");
83          }
84          catch (BeanInitializationException ex) {
85              // expected
86          }
87      }
88  
89      public void testResourceConstructor() throws Exception {
90          Resource resource = new ClassPathResource("strategies.properties", getClass());
91          new DefaultStrategiesHelper(resource);
92      }
93  
94      public interface Strategy {
95  
96      }
97  
98      private static class StrategyImpl implements Strategy {
99  
100     }
101 
102     private static class ContextAwareStrategyImpl implements Strategy, ApplicationContextAware {
103 
104         private ApplicationContext applicationContext;
105 
106         public ApplicationContext getApplicationContext() {
107             return applicationContext;
108         }
109 
110         public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
111             this.applicationContext = applicationContext;
112         }
113     }
114 }