Coverage Report - org.jbehave.core.configuration.needle.NeedleAnnotationBuilder
 
Classes in this File Line Coverage Branch Coverage Complexity
NeedleAnnotationBuilder
85%
24/28
80%
8/10
1.857
 
 1  
 package org.jbehave.core.configuration.needle;
 2  
 
 3  
 import java.util.HashSet;
 4  
 import java.util.List;
 5  
 import java.util.Set;
 6  
 
 7  
 import org.jbehave.core.annotations.UsingSteps;
 8  
 import org.jbehave.core.annotations.needle.UsingNeedle;
 9  
 import org.jbehave.core.configuration.AnnotationBuilder;
 10  
 import org.jbehave.core.configuration.AnnotationFinder;
 11  
 import org.jbehave.core.configuration.AnnotationMonitor;
 12  
 import org.jbehave.core.configuration.AnnotationRequired;
 13  
 import org.jbehave.core.configuration.Configuration;
 14  
 import org.jbehave.core.configuration.PrintStreamAnnotationMonitor;
 15  
 import org.jbehave.core.steps.InjectableStepsFactory;
 16  
 import org.jbehave.core.steps.needle.NeedleStepsFactory;
 17  
 import org.jbehave.core.steps.needle.configuration.CreateInstanceByDefaultConstructor;
 18  
 
 19  
 import de.akquinet.jbosscc.needle.injection.InjectionProvider;
 20  
 import de.holisticon.toolbox.needle.provider.InjectionProviderInstancesSupplier;
 21  
 
 22  
 /**
 23  
  * Extends {@link AnnotationBuilder} to provide Needle-based dependency injection if {@link UsingNeedle} annotation is
 24  
  * present.
 25  
  * 
 26  
  * @author Simon Zambrovski (simon.zambrovski@holisticon.de)
 27  
  * @author Jan Galinski (jan.galinski@holisticon.de)
 28  
  * 
 29  
  */
 30  
 public class NeedleAnnotationBuilder extends AnnotationBuilder {
 31  
 
 32  12
         private final Set<InjectionProvider<?>> provider = new HashSet<InjectionProvider<?>>();
 33  12
         private final Set<Class<?>> stepsClasses = new HashSet<Class<?>>();
 34  
         private NeedleStepsFactory factory;
 35  
 
 36  
         public NeedleAnnotationBuilder(Class<?> annotatedClass, AnnotationMonitor annotationMonitor) {
 37  12
                 super(annotatedClass, annotationMonitor);
 38  12
         }
 39  
 
 40  
         public NeedleAnnotationBuilder(Class<?> annotatedClass) {
 41  12
                 this(annotatedClass, new PrintStreamAnnotationMonitor());
 42  12
         }
 43  
 
 44  
         @Override
 45  
         public Configuration buildConfiguration() throws AnnotationRequired {
 46  
 
 47  11
                 final AnnotationFinder finder = annotationFinder();
 48  
 
 49  11
                 if (finder.isAnnotationPresent(UsingSteps.class)) {
 50  6
                         stepsClasses.addAll(finder.getAnnotatedClasses(UsingSteps.class, Object.class, "instances"));
 51  
                 }
 52  
 
 53  11
                 if (finder.isAnnotationPresent(UsingNeedle.class)) {
 54  
 
 55  
                         @SuppressWarnings("rawtypes")
 56  9
                         final List<Class> supplierClasses = finder.getAnnotatedValues(UsingNeedle.class, Class.class, "supplier");
 57  9
                         for (Class<InjectionProviderInstancesSupplier> supplierClass : supplierClasses) {
 58  0
                                 provider.addAll(CreateInstanceByDefaultConstructor.INSTANCE.apply(supplierClass).get());
 59  0
                         }
 60  
 
 61  
                         @SuppressWarnings("rawtypes")
 62  9
                         final List<Class> providerClasses = finder.getAnnotatedValues(UsingNeedle.class, Class.class, "provider");
 63  9
                         for (Class<InjectionProvider<?>> providerClass : providerClasses) {
 64  5
                                 provider.add(CreateInstanceByDefaultConstructor.INSTANCE.apply(providerClass));
 65  5
                         }
 66  
 
 67  9
                 } else {
 68  2
                         annotationMonitor().annotationNotFound(UsingNeedle.class, annotatedClass());
 69  
                 }
 70  11
                 return super.buildConfiguration();
 71  
         }
 72  
 
 73  
         @Override
 74  
         public InjectableStepsFactory buildStepsFactory(Configuration configuration) {
 75  7
                 this.factory = new NeedleStepsFactory(configuration, provider, stepsClasses.toArray(new Class<?>[stepsClasses
 76  
                                 .size()]));
 77  7
                 return factory;
 78  
         }
 79  
 
 80  
         @Override
 81  
         @SuppressWarnings("unchecked")
 82  
         protected <T, V extends T> T instanceOf(Class<T> type, Class<V> ofClass) {
 83  
                 /*
 84  
                  * This allow usage of the factory only after step factory is constructed. Current implementation only supports
 85  
                  * creation injection into steps. Further improvement will be to provide a needle factory capable of creating
 86  
                  * configuration parts.
 87  
                  */
 88  177
                 if (factory != null) {
 89  0
                         return (T) factory.createInstanceOfType(ofClass);
 90  
                 }
 91  177
                 return super.instanceOf(type, ofClass);
 92  
         }
 93  
 
 94  
         /**
 95  
          * Retrieves the set of injection providers.
 96  
          * 
 97  
          * @return set of providers.
 98  
          */
 99  
         public Set<InjectionProvider<?>> getProvider() {
 100  1
                 return provider;
 101  
         }
 102  
 
 103  
         /**
 104  
          * Retrieve step classes.
 105  
          * 
 106  
          * @return set of step classes.
 107  
          */
 108  
         public Set<Class<?>> getStepsClasses() {
 109  0
                 return stepsClasses;
 110  
         }
 111  
 }