Coverage Report - org.jbehave.core.steps.spring.SpringStepsFactory
 
Classes in this File Line Coverage Branch Coverage Complexity
SpringStepsFactory
93%
14/15
92%
13/14
3
 
 1  
 package org.jbehave.core.steps.spring;
 2  
 
 3  
 import java.lang.reflect.Modifier;
 4  
 import java.util.ArrayList;
 5  
 import java.util.List;
 6  
 
 7  
 import org.jbehave.core.configuration.Configuration;
 8  
 import org.jbehave.core.steps.AbstractStepsFactory;
 9  
 import org.jbehave.core.steps.InjectableStepsFactory;
 10  
 import org.springframework.context.ApplicationContext;
 11  
 
 12  
 /**
 13  
  * An {@link InjectableStepsFactory} that uses Spring's
 14  
  * {@link ApplicationContext} for the composition and instantiation of all
 15  
  * components that contain JBehave annotated methods.
 16  
  * 
 17  
  * @author Paul Hammant
 18  
  * @author Mauro Talevi
 19  
  */
 20  
 public class SpringStepsFactory extends AbstractStepsFactory {
 21  
 
 22  
     private final ApplicationContext context;
 23  
 
 24  
     public SpringStepsFactory(Configuration configuration, ApplicationContext context) {
 25  6
         super(configuration);
 26  6
         this.context = context;
 27  6
     }
 28  
 
 29  
     @Override
 30  
     protected List<Class<?>> stepsTypes() {
 31  6
         List<Class<?>> types = new ArrayList<Class<?>>();
 32  33
         for (String name : context.getBeanDefinitionNames()) {
 33  27
             Class<?> type = context.getType(name);
 34  27
             if (isAllowed(type) && hasAnnotatedMethods(type)) {
 35  5
                 types.add(type);
 36  
             }
 37  
         }
 38  6
         return types;
 39  
     }
 40  
 
 41  
     /**
 42  
      * Checks if type returned from context is allowed, i.e. not null and not
 43  
      * abstract.
 44  
      * 
 45  
      * @param type the Class of the bean
 46  
      * @return A boolean, <code>true</code> if allowed
 47  
      */
 48  
     protected boolean isAllowed(Class<?> type) {
 49  27
         return type != null && !Modifier.isAbstract(type.getModifiers());
 50  
     }
 51  
 
 52  
     public Object createInstanceOfType(Class<?> type) {
 53  20
         for (String name : context.getBeanDefinitionNames()) {
 54  20
             Class<?> beanType = context.getType(name);
 55  20
             if (type.equals(beanType)) {
 56  6
                 return context.getBean(name);
 57  
             }
 58  
         }
 59  
 
 60  0
         throw new StepsInstanceNotFound(type, this);
 61  
     }
 62  
 
 63  
 }