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