Coverage Report - org.kuali.student.common.test.spring.DaoTestDependencyInjectorListener
 
Classes in this File Line Coverage Branch Coverage Complexity
DaoTestDependencyInjectorListener
100%
14/14
100%
4/4
3
 
 1  
 /**
 2  
  * Copyright 2010 The Kuali Foundation Licensed under the
 3  
  * Educational Community License, Version 2.0 (the "License"); you may
 4  
  * not use this file except in compliance with the License. You may
 5  
  * obtain a copy of the License at
 6  
  *
 7  
  * http://www.osedu.org/licenses/ECL-2.0
 8  
  *
 9  
  * Unless required by applicable law or agreed to in writing,
 10  
  * software distributed under the License is distributed on an "AS IS"
 11  
  * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 12  
  * or implied. See the License for the specific language governing
 13  
  * permissions and limitations under the License.
 14  
  */
 15  
 
 16  
 package org.kuali.student.common.test.spring;
 17  
 
 18  
 import java.lang.reflect.AccessibleObject;
 19  
 import java.lang.reflect.Field;
 20  
 
 21  
 import org.springframework.beans.factory.config.BeanDefinition;
 22  
 import org.springframework.beans.factory.support.BeanDefinitionRegistry;
 23  
 import org.springframework.beans.factory.support.RootBeanDefinition;
 24  
 import org.springframework.context.ApplicationContext;
 25  
 import org.springframework.test.context.TestContext;
 26  
 import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
 27  
 
 28  1
 public class DaoTestDependencyInjectorListener extends
 29  
                 DependencyInjectionTestExecutionListener {
 30  
 
 31  
         @Override
 32  
         protected void injectDependencies(TestContext testContext) throws Exception {
 33  
 
 34  2
                 ApplicationContext context = testContext.getApplicationContext();
 35  
 
 36  2
                 BeanDefinitionRegistry registry = ((BeanDefinitionRegistry) context);
 37  
 
 38  2
                 Field[] fields = testContext.getTestClass().getDeclaredFields();
 39  6
                 for (Field f : fields) {
 40  4
                         Dao a = f.getAnnotation(Dao.class);
 41  4
                         if (a != null) {
 42  2
                                 Class<?> clazz = Class.forName(a.value());
 43  2
                                 BeanDefinition definition = new RootBeanDefinition(clazz);
 44  2
                                 registry.registerBeanDefinition(f.getName(), definition);
 45  2
                                 Field.setAccessible(new AccessibleObject[]{f}, true);
 46  2
                                 f.set(testContext.getTestInstance(), testContext
 47  
                                                 .getApplicationContext().getBean(f.getName()));
 48  
                         }
 49  
                 }
 50  
 
 51  2
                 super.injectDependencies(testContext);
 52  
 
 53  2
         }
 54  
 }