1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package ch.elca.el4j.tests.core;
18
19 import java.util.HashSet;
20 import java.util.Set;
21
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24 import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
25 import org.springframework.beans.factory.support.BeanDefinitionRegistry;
26 import org.springframework.context.ConfigurableApplicationContext;
27 import org.springframework.context.annotation.AnnotationConfigUtils;
28 import org.springframework.test.context.ContextConfiguration;
29 import org.springframework.test.context.support.AbstractContextLoader;
30 import org.springframework.util.StringUtils;
31
32 import ch.elca.el4j.core.context.ModuleApplicationContext;
33 import ch.elca.el4j.core.context.ModuleApplicationContextConfiguration;
34 import ch.elca.el4j.core.context.ModuleApplicationContextCreationListener;
35 import ch.elca.el4j.tests.core.context.ExtendedContextConfiguration;
36 import ch.elca.el4j.tests.core.context.junit4.EL4JJunit4ClassRunner;
37
38
39
40
41
42
43
44
45
46 public class ModuleTestContextLoader extends AbstractContextLoader
47 implements ModuleApplicationContextCreationListener {
48
49
50
51
52 private static final Logger s_logger = LoggerFactory.getLogger(ModuleTestContextLoader.class);
53
54
55
56
57
58
59 private static ThreadLocal<Class<?>> s_testedClass = new ThreadLocal<Class<?>>();
60
61
62
63
64
65
66
67
68 @Override
69 public ConfigurableApplicationContext loadContext(String... locations) throws Exception {
70 if (s_logger.isDebugEnabled()) {
71 s_logger.debug("Loading ModuleApplicationContext for locations ["
72 + StringUtils.arrayToCommaDelimitedString(locations) + "].");
73 }
74 ModuleApplicationContextConfiguration config = createModuleApplicationContextConfiguration();
75 config.setInclusiveConfigLocations(locations);
76 config.setModuleApplicationContextCreationListener(this);
77 customizeModuleApplicationContextConfiguration(config);
78 ModuleApplicationContext context = new ModuleApplicationContext(config);
79 return context;
80 }
81
82
83
84
85
86
87
88
89
90
91 private ModuleApplicationContextConfiguration createModuleApplicationContextConfiguration()
92 throws IllegalStateException {
93 ModuleApplicationContextConfiguration resultingConfiguration = new ModuleApplicationContextConfiguration();
94 recursivelyConfigure(s_testedClass.get(), resultingConfiguration);
95 return resultingConfiguration;
96 }
97
98
99
100
101
102
103
104 private void recursivelyConfigure(Class<?> annotatedClass, ModuleApplicationContextConfiguration config) {
105
106 if (annotatedClass == null) {
107 return;
108 }
109
110
111 recursivelyConfigure(annotatedClass.getSuperclass(), config);
112
113 ContextConfiguration configuration
114 = annotatedClass.getAnnotation(ContextConfiguration.class);
115 ExtendedContextConfiguration extendedConfiguration
116 = annotatedClass.getAnnotation(ExtendedContextConfiguration.class);
117
118 if (extendedConfiguration != null && configuration == null) {
119 throw new IllegalStateException(
120 "@ExtendedContextConfiguration without @ContextConfiguration in class "
121 + annotatedClass.getName());
122 }
123
124 if (extendedConfiguration == null) {
125
126 return;
127 }
128
129
130 TernaryBoolean b;
131
132
133
134 if (configuration.inheritLocations()) {
135 Set<String> exclusiveLocations = new HashSet<String>();
136 for (String path : config.getExclusiveConfigLocations()) {
137 exclusiveLocations.add(path);
138 }
139 for (String path : extendedConfiguration.exclusiveConfigLocations()) {
140 exclusiveLocations.add(path);
141 }
142 config.setExclusiveConfigLocations(exclusiveLocations.toArray(new String[0]));
143 } else {
144 config.setExclusiveConfigLocations(extendedConfiguration.exclusiveConfigLocations());
145 }
146
147
148 b = new TernaryBoolean(extendedConfiguration.allowBeanDefinitionOverriding());
149 if (b.isSet) {
150 config.setAllowBeanDefinitionOverriding(b.value);
151 }
152
153 b = new TernaryBoolean(extendedConfiguration.mergeWithOuterResources());
154 if (b.isSet) {
155 config.setMergeWithOuterResources(b.value);
156 }
157
158 b = new TernaryBoolean(extendedConfiguration.mostSpecificBeanDefinitionCounts());
159 if (b.isSet) {
160 config.setMostSpecificBeanDefinitionCounts(b.value);
161 }
162
163 b = new TernaryBoolean(extendedConfiguration.mostSpecificResourceLast());
164 if (b.isSet) {
165 config.setMostSpecificResourceLast(b.value);
166 }
167
168 }
169
170
171
172
173 private class TernaryBoolean {
174
175 public TernaryBoolean(String sValue) {
176 if ("true".equals(sValue) || "false".equals(sValue)) {
177 isSet = true;
178 value = "true".equals(sValue);
179 } else {
180 isSet = false;
181 }
182 }
183
184 public boolean isSet;
185 public boolean value;
186 }
187
188
189
190
191
192
193
194 protected void customizeModuleApplicationContextConfiguration(ModuleApplicationContextConfiguration config) { }
195
196
197
198
199
200
201 @Override
202 public String getResourceSuffix() {
203 return "-context.xml";
204 }
205
206
207
208
209 @Override
210 public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) {
211 AnnotationConfigUtils.registerAnnotationConfigProcessors((BeanDefinitionRegistry) beanFactory);
212 }
213
214
215
216
217 @Override
218 public void finishRefresh(ModuleApplicationContext context) {
219 context.registerShutdownHook();
220 }
221
222
223
224
225
226 public static void setTestedClass(Class<?> testClass) {
227 s_testedClass.set(testClass);
228 }
229
230 }