Coverage Report - org.kuali.student.common.test.spring.DaoTestDependencyInjectorListener
 
Classes in this File Line Coverage Branch Coverage Complexity
DaoTestDependencyInjectorListener
0%
0/14
0%
0/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  0
 public class DaoTestDependencyInjectorListener extends
 29  
                 DependencyInjectionTestExecutionListener {
 30  
 
 31  
         @Override
 32  
         protected void injectDependencies(TestContext testContext) throws Exception {
 33  
 
 34  0
                 ApplicationContext context = testContext.getApplicationContext();
 35  
 
 36  0
                 BeanDefinitionRegistry registry = ((BeanDefinitionRegistry) context);
 37  
 
 38  0
                 Field[] fields = testContext.getTestClass().getDeclaredFields();
 39  0
                 for (Field f : fields) {
 40  0
                         Dao a = f.getAnnotation(Dao.class);
 41  0
                         if (a != null) {
 42  0
                                 Class<?> clazz = Class.forName(a.value());
 43  0
                                 BeanDefinition definition = new RootBeanDefinition(clazz);
 44  0
                                 registry.registerBeanDefinition(f.getName(), definition);
 45  0
                                 Field.setAccessible(new AccessibleObject[]{f}, true);
 46  0
                                 f.set(testContext.getTestInstance(), testContext
 47  
                                                 .getApplicationContext().getBean(f.getName()));
 48  
                         }
 49  
                 }
 50  
 
 51  0
                 super.injectDependencies(testContext);
 52  
 
 53  0
         }
 54  
 }