Coverage Report - org.jbehave.core.steps.guice.GuiceStepsFactory
 
Classes in this File Line Coverage Branch Coverage Complexity
GuiceStepsFactory
100%
14/14
100%
6/6
2
 
 1  
 package org.jbehave.core.steps.guice;
 2  
 
 3  
 import java.util.ArrayList;
 4  
 import java.util.List;
 5  
 
 6  
 import org.jbehave.core.configuration.Configuration;
 7  
 import org.jbehave.core.steps.AbstractStepsFactory;
 8  
 import org.jbehave.core.steps.InjectableStepsFactory;
 9  
 
 10  
 import com.google.inject.Binding;
 11  
 import com.google.inject.Injector;
 12  
 import com.google.inject.Key;
 13  
 
 14  
 /**
 15  
  * An {@link InjectableStepsFactory} that uses a Guice {@link Injector} for the
 16  
  * composition and instantiation of all components that contain JBehave
 17  
  * annotated methods.
 18  
  * 
 19  
  * @author Cristiano GaviĆ£o
 20  
  * @author Paul Hammant
 21  
  * @author Mauro Talevi
 22  
  */
 23  
 public class GuiceStepsFactory extends AbstractStepsFactory {
 24  
 
 25  
     private final Injector injector;
 26  
 
 27  
     public GuiceStepsFactory(Configuration configuration, Injector injector) {
 28  6
         super(configuration);
 29  6
         this.injector = injector;
 30  6
     }
 31  
 
 32  
     @Override
 33  
     protected List<Object> stepsInstances() {
 34  6
         List<Object> steps = new ArrayList<Object>();
 35  6
         addInstances(injector, steps);
 36  6
         return steps;
 37  
     }
 38  
 
 39  
     /**
 40  
      * Adds steps instances from given injector and recursively its parent
 41  
      * 
 42  
      * @param injector the current Inject
 43  
      * @param steps the List of steps instances
 44  
      */
 45  
     private void addInstances(Injector injector, List<Object> steps) {
 46  54
         for (Binding<?> binding : injector.getBindings().values()) {
 47  192
             Key<?> key = binding.getKey();
 48  192
             if (hasAnnotatedMethods(key.getTypeLiteral().getType())) {
 49  4
                 steps.add(injector.getInstance(key));
 50  
             }
 51  192
         }
 52  54
         if (injector.getParent() != null) {
 53  48
             addInstances(injector.getParent(), steps);
 54  
         }
 55  54
     }
 56  
 }