1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springframework.test;
18
19 import java.lang.reflect.Field;
20 import java.lang.reflect.Modifier;
21 import java.util.LinkedList;
22 import java.util.List;
23
24 import org.springframework.beans.factory.NoSuchBeanDefinitionException;
25 import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
26 import org.springframework.context.ApplicationContext;
27 import org.springframework.util.Assert;
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67 @Deprecated
68 public abstract class AbstractDependencyInjectionSpringContextTests extends AbstractSingleSpringContextTests {
69
70
71
72
73
74
75 public static final int AUTOWIRE_NO = 0;
76
77
78
79
80
81
82 public static final int AUTOWIRE_BY_NAME = AutowireCapableBeanFactory.AUTOWIRE_BY_NAME;
83
84
85
86
87
88
89 public static final int AUTOWIRE_BY_TYPE = AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE;
90
91 private boolean populateProtectedVariables = false;
92
93 private int autowireMode = AUTOWIRE_BY_TYPE;
94
95 private boolean dependencyCheck = true;
96
97 private String[] managedVariableNames;
98
99
100
101
102
103 public AbstractDependencyInjectionSpringContextTests() {
104 }
105
106
107
108
109
110
111 public AbstractDependencyInjectionSpringContextTests(String name) {
112 super(name);
113 }
114
115
116
117
118
119
120 public final void setPopulateProtectedVariables(boolean populateFields) {
121 this.populateProtectedVariables = populateFields;
122 }
123
124
125
126
127 public final boolean isPopulateProtectedVariables() {
128 return this.populateProtectedVariables;
129 }
130
131
132
133
134
135
136
137
138
139 public final void setAutowireMode(final int autowireMode) {
140 this.autowireMode = autowireMode;
141 }
142
143
144
145
146 public final int getAutowireMode() {
147 return this.autowireMode;
148 }
149
150
151
152
153
154
155
156 public final void setDependencyCheck(final boolean dependencyCheck) {
157 this.dependencyCheck = dependencyCheck;
158 }
159
160
161
162
163
164 public final boolean isDependencyCheck() {
165 return this.dependencyCheck;
166 }
167
168
169
170
171
172
173
174
175
176
177 @Override
178 protected void prepareTestInstance() throws Exception {
179 if (getApplicationContext() == null) {
180 if (this.logger.isInfoEnabled()) {
181 this.logger.info("ApplicationContext has not been configured for test [" + getClass().getName()
182 + "]: dependency injection will NOT be performed.");
183 }
184 }
185 else {
186 injectDependencies();
187 }
188 }
189
190
191
192
193
194
195
196
197
198
199
200
201
202 protected void injectDependencies() throws Exception {
203 Assert.state(getApplicationContext() != null,
204 "injectDependencies() called without first configuring an ApplicationContext");
205 if (isPopulateProtectedVariables()) {
206 if (this.managedVariableNames == null) {
207 initManagedVariableNames();
208 }
209 populateProtectedVariables();
210 }
211 getApplicationContext().getBeanFactory().autowireBeanProperties(this, getAutowireMode(), isDependencyCheck());
212 }
213
214 private void initManagedVariableNames() throws IllegalAccessException {
215 List managedVarNames = new LinkedList();
216 Class clazz = getClass();
217 do {
218 Field[] fields = clazz.getDeclaredFields();
219 if (this.logger.isDebugEnabled()) {
220 this.logger.debug("Found " + fields.length + " fields on " + clazz);
221 }
222 for (int i = 0; i < fields.length; i++) {
223 Field field = fields[i];
224 field.setAccessible(true);
225 if (this.logger.isDebugEnabled()) {
226 this.logger.debug("Candidate field: " + field);
227 }
228 if (isProtectedInstanceField(field)) {
229 Object oldValue = field.get(this);
230 if (oldValue == null) {
231 managedVarNames.add(field.getName());
232 if (this.logger.isDebugEnabled()) {
233 this.logger.debug("Added managed variable '" + field.getName() + "'");
234 }
235 }
236 else {
237 if (this.logger.isDebugEnabled()) {
238 this.logger.debug("Rejected managed variable '" + field.getName() + "'");
239 }
240 }
241 }
242 }
243 clazz = clazz.getSuperclass();
244 } while (!clazz.equals(AbstractDependencyInjectionSpringContextTests.class));
245
246 this.managedVariableNames = (String[]) managedVarNames.toArray(new String[managedVarNames.size()]);
247 }
248
249 private boolean isProtectedInstanceField(Field field) {
250 int modifiers = field.getModifiers();
251 return !Modifier.isStatic(modifiers) && Modifier.isProtected(modifiers);
252 }
253
254 private void populateProtectedVariables() throws IllegalAccessException {
255 for (int i = 0; i < this.managedVariableNames.length; i++) {
256 String varName = this.managedVariableNames[i];
257 Object bean = null;
258 try {
259 Field field = findField(getClass(), varName);
260 bean = getApplicationContext().getBean(varName, field.getType());
261 field.setAccessible(true);
262 field.set(this, bean);
263 if (this.logger.isDebugEnabled()) {
264 this.logger.debug("Populated field: " + field);
265 }
266 }
267 catch (NoSuchFieldException ex) {
268 if (this.logger.isWarnEnabled()) {
269 this.logger.warn("No field with name '" + varName + "'");
270 }
271 }
272 catch (NoSuchBeanDefinitionException ex) {
273 if (this.logger.isWarnEnabled()) {
274 this.logger.warn("No bean with name '" + varName + "'");
275 }
276 }
277 }
278 }
279
280 private Field findField(Class clazz, String name) throws NoSuchFieldException {
281 try {
282 return clazz.getDeclaredField(name);
283 }
284 catch (NoSuchFieldException ex) {
285 Class superclass = clazz.getSuperclass();
286 if (superclass != AbstractSpringContextTests.class) {
287 return findField(superclass, name);
288 }
289 else {
290 throw ex;
291 }
292 }
293 }
294
295 }