1 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 23 import org.springframework.beans.factory.NoSuchBeanDefinitionException; 24 import org.springframework.beans.factory.config.AutowireCapableBeanFactory; 25 26 55 public abstract class AbstractDependencyInjectionSpringContextTests extends AbstractSingleSpringContextTests { 56 57 61 public static final int AUTOWIRE_NO = 0; 62 63 67 public static final int AUTOWIRE_BY_NAME = AutowireCapableBeanFactory.AUTOWIRE_BY_NAME; 68 69 73 public static final int AUTOWIRE_BY_TYPE = AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE; 74 75 76 private boolean populateProtectedVariables = false; 77 78 private int autowireMode = AUTOWIRE_BY_TYPE; 79 80 private boolean dependencyCheck = true; 81 82 private String [] managedVariableNames; 83 84 85 88 public AbstractDependencyInjectionSpringContextTests() { 89 } 90 91 95 public AbstractDependencyInjectionSpringContextTests(String name) { 96 super(name); 97 } 98 99 100 104 public final void setPopulateProtectedVariables(boolean populateFields) { 105 this.populateProtectedVariables = populateFields; 106 } 107 108 111 public final boolean isPopulateProtectedVariables() { 112 return this.populateProtectedVariables; 113 } 114 115 123 public final void setAutowireMode(int autowireMode) { 124 this.autowireMode = autowireMode; 125 } 126 127 130 public final int getAutowireMode() { 131 return this.autowireMode; 132 } 133 134 140 public final void setDependencyCheck(boolean dependencyCheck) { 141 this.dependencyCheck = dependencyCheck; 142 } 143 144 148 public final boolean isDependencyCheck() { 149 return this.dependencyCheck; 150 } 151 152 153 157 protected void prepareTestInstance() throws Exception { 158 injectDependencies(); 159 } 160 161 171 protected void injectDependencies() throws Exception { 172 if (isPopulateProtectedVariables()) { 173 if (this.managedVariableNames == null) { 174 initManagedVariableNames(); 175 } 176 populateProtectedVariables(); 177 } 178 else if (getAutowireMode() != AUTOWIRE_NO) { 179 getApplicationContext().getBeanFactory().autowireBeanProperties( 180 this, getAutowireMode(), isDependencyCheck()); 181 } 182 } 183 184 private void initManagedVariableNames() throws IllegalAccessException { 185 LinkedList managedVarNames = new LinkedList (); 186 Class clazz = getClass(); 187 188 do { 189 Field [] fields = clazz.getDeclaredFields(); 190 if (logger.isDebugEnabled()) { 191 logger.debug("Found " + fields.length + " fields on " + clazz); 192 } 193 194 for (int i = 0; i < fields.length; i++) { 195 Field field = fields[i]; 196 field.setAccessible(true); 197 if (logger.isDebugEnabled()) { 198 logger.debug("Candidate field: " + field); 199 } 200 if (isProtectedInstanceField(field)) { 201 Object oldValue = field.get(this); 202 if (oldValue == null) { 203 managedVarNames.add(field.getName()); 204 if (logger.isDebugEnabled()) { 205 logger.debug("Added managed variable '" + field.getName() + "'"); 206 } 207 } 208 else { 209 if (logger.isDebugEnabled()) { 210 logger.debug("Rejected managed variable '" + field.getName() + "'"); 211 } 212 } 213 } 214 } 215 clazz = clazz.getSuperclass(); 216 } 217 while (!clazz.equals(AbstractDependencyInjectionSpringContextTests.class)); 218 219 this.managedVariableNames = (String []) managedVarNames.toArray(new String [managedVarNames.size()]); 220 } 221 222 private boolean isProtectedInstanceField(Field field) { 223 int modifiers = field.getModifiers(); 224 return !Modifier.isStatic(modifiers) && Modifier.isProtected(modifiers); 225 } 226 227 private void populateProtectedVariables() throws IllegalAccessException { 228 for (int i = 0; i < this.managedVariableNames.length; i++) { 229 String varName = this.managedVariableNames[i]; 230 Object bean = null; 231 try { 232 Field field = findField(getClass(), varName); 233 bean = getApplicationContext().getBean(varName, field.getType()); 234 field.setAccessible(true); 235 field.set(this, bean); 236 if (logger.isDebugEnabled()) { 237 logger.debug("Populated field: " + field); 238 } 239 } 240 catch (NoSuchFieldException ex) { 241 if (logger.isWarnEnabled()) { 242 logger.warn("No field with name '" + varName + "'"); 243 } 244 } 245 catch (NoSuchBeanDefinitionException ex) { 246 if (logger.isWarnEnabled()) { 247 logger.warn("No bean with name '" + varName + "'"); 248 } 249 } 250 } 251 } 252 253 private Field findField(Class clazz, String name) throws NoSuchFieldException { 254 try { 255 return clazz.getDeclaredField(name); 256 } 257 catch (NoSuchFieldException ex) { 258 Class superclass = clazz.getSuperclass(); 259 if (superclass != AbstractSpringContextTests.class) { 260 return findField(superclass, name); 261 } 262 else { 263 throw ex; 264 } 265 } 266 } 267 268 } 269 | Popular Tags |