Coverage Report - org.jbehave.core.steps.guice.GuiceStepsFactory
 
Classes in this File Line Coverage Branch Coverage Complexity
GuiceStepsFactory
96%
27/28
92%
13/14
2.6
 
 1  
 package org.jbehave.core.steps.guice;
 2  
 
 3  
 import java.lang.reflect.Type;
 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  
 
 11  
 import com.google.inject.Binding;
 12  
 import com.google.inject.Injector;
 13  
 import com.google.inject.Key;
 14  
 
 15  
 /**
 16  
  * An {@link InjectableStepsFactory} that uses a Guice {@link Injector} for the
 17  
  * composition and instantiation of all components that contain JBehave
 18  
  * annotated methods.
 19  
  * 
 20  
  * @author Cristiano GaviĆ£o
 21  
  * @author Paul Hammant
 22  
  * @author Mauro Talevi
 23  
  */
 24  
 public class GuiceStepsFactory extends AbstractStepsFactory {
 25  
 
 26  
     private final Injector injector;
 27  
 
 28  
     public GuiceStepsFactory(Configuration configuration, Injector injector) {
 29  6
         super(configuration);
 30  6
         this.injector = injector;
 31  6
     }
 32  
 
 33  
     @Override
 34  
     protected List<Class<?>> stepsTypes() {
 35  6
         List<Class<?>> types = new ArrayList<Class<?>>();
 36  6
         addTypes(injector, types);
 37  6
         return types;
 38  
     }
 39  
 
 40  
     /**
 41  
      * Adds steps types from given injector and recursively its parent
 42  
      * 
 43  
      * @param injector the current Inject
 44  
      * @param types the List of steps types
 45  
      */
 46  
     private void addTypes(Injector injector, List<Class<?>> types) {
 47  62
         for (Binding<?> binding : injector.getBindings().values()) {
 48  220
             Key<?> key = binding.getKey();
 49  220
             Type type = key.getTypeLiteral().getType();
 50  220
             if (hasAnnotatedMethods(type)) {
 51  4
                 types.add(((Class<?>)type));
 52  
             }
 53  220
         }
 54  62
         if (injector.getParent() != null) {
 55  56
             addTypes(injector.getParent(), types);
 56  
         }
 57  62
     }
 58  
 
 59  
     public Object createInstanceOfType(Class<?> type) {
 60  5
         List<Object> instances = new ArrayList<Object>();
 61  5
         addInstances(injector, type, instances);
 62  5
         if ( !instances.isEmpty() ){
 63  5
             return instances.iterator().next();
 64  
         }
 65  0
         return new StepsInstanceNotFound(type, this);
 66  
     }
 67  
 
 68  
     private void addInstances(Injector injector, Class<?> type, List<Object> instances) {
 69  33
         for (Binding<?> binding : injector.getBindings().values()) {
 70  124
             Key<?> key = binding.getKey();
 71  124
             if (type.equals(key.getTypeLiteral().getType())) {
 72  5
                 instances.add(injector.getInstance(key));
 73  
             }
 74  124
         }
 75  33
         if (injector.getParent() != null) {
 76  28
             addInstances(injector.getParent(), type, instances);
 77  
         }
 78  33
     }
 79  
 }