Coverage Report - org.jbehave.core.steps.spring.SpringStepsFactory
 
Classes in this File Line Coverage Branch Coverage Complexity
SpringStepsFactory
91%
11/12
100%
10/10
2.667
 
 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.beans.BeansException;
 11  
 import org.springframework.context.ApplicationContext;
 12  
 
 13  
 /**
 14  
  * An {@link InjectableStepsFactory} that uses Spring's
 15  
  * {@link ApplicationContext} for the composition and instantiation of all
 16  
  * components that contain JBehave annotated methods.
 17  
  * 
 18  
  * @author Paul Hammant
 19  
  * @author Mauro Talevi
 20  
  */
 21  
 public class SpringStepsFactory extends AbstractStepsFactory {
 22  
 
 23  
     private final ApplicationContext context;
 24  
 
 25  
     public SpringStepsFactory(Configuration configuration, ApplicationContext context) {
 26  6
         super(configuration);
 27  6
         this.context = context;
 28  6
     }
 29  
 
 30  
     @Override
 31  
     protected List<Object> stepsInstances() {
 32  6
         List<Object> steps = new ArrayList<Object>();
 33  33
         for (String name : context.getBeanDefinitionNames()) {
 34  27
             Class<?> type = context.getType(name);            
 35  27
             if ( isAllowed(type) && hasAnnotatedMethods(type)) {
 36  
                 try {
 37  5
                     steps.add(context.getBean(name));
 38  0
                 } catch ( BeansException e ){
 39  
                     // failed to get bean instance for whatever reason
 40  
                     // we ignore it and move on
 41  5
                 }
 42  
             }
 43  
         }
 44  6
         return steps;
 45  
     }
 46  
 
 47  
     /**
 48  
      * Checks if type returned from context is allowed,
 49  
      * i.e. not null and not abstract.
 50  
      * 
 51  
      * @param type the Class of the bean
 52  
      * @return A boolean, <code>true</code> if allowed
 53  
      */
 54  
     protected boolean isAllowed(Class<?> type) {
 55  27
         return type != null && !Modifier.isAbstract(type.getModifiers());
 56  
     }
 57  
 
 58  
 }