1
2
3
4
5
6
7
8
9
10
11
12
13
14
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 org.junit.Assert;
31 import org.junit.Test;
32
33 public class DefaultStrategiesHelperTest {
34
35 @Test
36 public void testGetDefaultStrategies() throws Exception {
37
38 Properties strategies = new Properties();
39 strategies.put(Strategy.class.getName(),
40 StrategyImpl.class.getName() + "," + ContextAwareStrategyImpl.class.getName());
41 DefaultStrategiesHelper helper = new DefaultStrategiesHelper(strategies);
42
43 StaticApplicationContext applicationContext = new StaticApplicationContext();
44 applicationContext.registerSingleton("strategy1", StrategyImpl.class);
45 applicationContext.registerSingleton("strategy2", ContextAwareStrategyImpl.class);
46
47 List<Strategy> result = helper.getDefaultStrategies(Strategy.class, applicationContext);
48 Assert.assertNotNull("No result", result);
49 Assert.assertEquals("Invalid amount of strategies", 2, result.size());
50 Assert.assertTrue("Result not a Strategy implementation", result.get(0) != null);
51 Assert.assertTrue("Result not a Strategy implementation", result.get(1) != null);
52 Assert.assertTrue("Result not a StrategyImpl implementation", result.get(0) instanceof StrategyImpl);
53 Assert.assertTrue("Result not a StrategyImpl implementation", result.get(1) instanceof ContextAwareStrategyImpl);
54 ContextAwareStrategyImpl impl = (ContextAwareStrategyImpl) result.get(1);
55 Assert.assertNotNull("No application context injected", impl.getApplicationContext());
56 }
57
58 @Test
59 public void testGetDefaultStrategy() throws Exception {
60 Properties strategies = new Properties();
61 strategies.put(Strategy.class.getName(), StrategyImpl.class.getName());
62 DefaultStrategiesHelper helper = new DefaultStrategiesHelper(strategies);
63
64 StaticApplicationContext applicationContext = new StaticApplicationContext();
65 applicationContext.registerSingleton("strategy1", StrategyImpl.class);
66 applicationContext.registerSingleton("strategy2", ContextAwareStrategyImpl.class);
67
68 Object result = helper.getDefaultStrategy(Strategy.class, applicationContext);
69 Assert.assertNotNull("No result", result);
70 Assert.assertTrue("Result not a Strategy implementation", result instanceof Strategy);
71 Assert.assertTrue("Result not a StrategyImpl implementation", result instanceof StrategyImpl);
72 }
73
74 @Test
75 public void testGetDefaultStrategyMoreThanOne() throws Exception {
76 Properties strategies = new Properties();
77 strategies.put(Strategy.class.getName(),
78 StrategyImpl.class.getName() + "," + ContextAwareStrategyImpl.class.getName());
79 DefaultStrategiesHelper helper = new DefaultStrategiesHelper(strategies);
80
81 StaticApplicationContext applicationContext = new StaticApplicationContext();
82 applicationContext.registerSingleton("strategy1", StrategyImpl.class);
83 applicationContext.registerSingleton("strategy2", ContextAwareStrategyImpl.class);
84
85 try {
86 helper.getDefaultStrategy(Strategy.class, applicationContext);
87 Assert.fail("Expected BeanInitializationException");
88 }
89 catch (BeanInitializationException ex) {
90
91 }
92 }
93
94 @Test
95 public void testResourceConstructor() throws Exception {
96 Resource resource = new ClassPathResource("strategies.properties", getClass());
97 new DefaultStrategiesHelper(resource);
98 }
99
100 public interface Strategy {
101
102 }
103
104 private static class StrategyImpl implements Strategy {
105
106 }
107
108 private static class ContextAwareStrategyImpl implements Strategy, ApplicationContextAware {
109
110 private ApplicationContext applicationContext;
111
112 public ApplicationContext getApplicationContext() {
113 return applicationContext;
114 }
115
116 public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
117 this.applicationContext = applicationContext;
118 }
119 }
120 }