Coverage Report - org.jbehave.core.configuration.spring.SpringAnnotationBuilder
 
Classes in this File Line Coverage Branch Coverage Complexity
SpringAnnotationBuilder
100%
39/39
100%
18/18
2.444
 
 1  
 package org.jbehave.core.configuration.spring;
 2  
 
 3  
 import java.util.ArrayList;
 4  
 import java.util.List;
 5  
 import java.util.Map;
 6  
 
 7  
 import org.jbehave.core.annotations.spring.UsingSpring;
 8  
 import org.jbehave.core.configuration.AnnotationBuilder;
 9  
 import org.jbehave.core.configuration.AnnotationFinder;
 10  
 import org.jbehave.core.configuration.AnnotationMonitor;
 11  
 import org.jbehave.core.configuration.Configuration;
 12  
 import org.jbehave.core.configuration.AnnotationRequired;
 13  
 import org.jbehave.core.steps.CandidateSteps;
 14  
 import org.jbehave.core.steps.InjectableStepsFactory;
 15  
 import org.jbehave.core.steps.ParameterConverters;
 16  
 import org.jbehave.core.steps.ParameterConverters.ParameterConverter;
 17  
 import org.jbehave.core.steps.spring.SpringApplicationContextFactory;
 18  
 import org.jbehave.core.steps.spring.SpringStepsFactory;
 19  
 import org.springframework.context.ApplicationContext;
 20  
 
 21  
 /**
 22  
  * Extends {@link AnnotationBuilder} to provide Spring-based dependency
 23  
  * injection if {@link UsingSpring} annotation is present.
 24  
  * 
 25  
  * @author Cristiano GaviĆ£o
 26  
  * @author Mauro Talevi
 27  
  */
 28  
 public class SpringAnnotationBuilder extends AnnotationBuilder {
 29  
 
 30  
     private ApplicationContext context;
 31  
 
 32  
     public SpringAnnotationBuilder(Class<?> annotatedClass) {
 33  10
         super(annotatedClass);
 34  10
     }
 35  
 
 36  
     public SpringAnnotationBuilder(Class<?> annotatedClass, AnnotationMonitor annotationMonitor) {
 37  1
         super(annotatedClass, annotationMonitor);
 38  1
     }
 39  
 
 40  
     @Override
 41  
     public Configuration buildConfiguration() throws AnnotationRequired {
 42  11
         if (annotationFinder().isAnnotationPresent(UsingSpring.class)) {
 43  9
             List<String> resources = annotationFinder()
 44  
                     .getAnnotatedValues(UsingSpring.class, String.class, "resources");
 45  9
             if (resources.size() > 0) {
 46  
                 try {
 47  7
                     context = createApplicationContext(annotatedClass().getClassLoader(), resources);
 48  1
                 } catch ( Exception e ){
 49  1
                     annotationMonitor().elementCreationFailed(ApplicationContext.class, e);
 50  6
                 }
 51  
             }
 52  9
         } else {
 53  2
             annotationMonitor().annotationNotFound(UsingSpring.class, annotatedClass());
 54  
         }
 55  11
         return super.buildConfiguration();
 56  
     }
 57  
 
 58  
     @Override
 59  
     public List<CandidateSteps> buildCandidateSteps(Configuration configuration) {
 60  6
         List<CandidateSteps> steps = super.buildCandidateSteps(configuration);
 61  6
         if (context != null) {
 62  3
             InjectableStepsFactory factory = new SpringStepsFactory(configuration, context);
 63  3
             steps.addAll(0, factory.createCandidateSteps());
 64  
         }
 65  6
         return steps;
 66  
     }
 67  
 
 68  
     @Override
 69  
     protected ParameterConverters parameterConverters(AnnotationFinder annotationFinder) {
 70  9
         ParameterConverters converters = super.parameterConverters(annotationFinder);
 71  9
         if (context != null) {
 72  6
             return converters.addConverters(getBeansOfType(context, ParameterConverter.class));
 73  
         }
 74  3
         return converters;
 75  
     }
 76  
 
 77  
     @SuppressWarnings("unchecked")
 78  
     private List<ParameterConverter> getBeansOfType(ApplicationContext context, Class<ParameterConverter> type) {
 79  6
         Map<String, ParameterConverter> beansOfType = context.getBeansOfType(type);
 80  6
         List<ParameterConverter> converters = new ArrayList<ParameterConverter>();
 81  6
         for (ParameterConverter converter : beansOfType.values()) {
 82  6
             converters.add(converter);
 83  
         }
 84  6
         return converters;
 85  
     }
 86  
 
 87  
     @Override
 88  
     @SuppressWarnings("unchecked")
 89  
     protected <T, V extends T> T instanceOf(Class<T> type, Class<V> ofClass) {
 90  155
         if (context != null) {
 91  
             Map<String, Object> beansOfType;
 92  104
             if (!type.equals(Object.class)) {
 93  102
                 beansOfType = context.getBeansOfType(type);
 94  
             } else {
 95  2
                 beansOfType = context.getBeansOfType(ofClass);
 96  
             }
 97  104
             if (beansOfType.size() > 0) {
 98  30
                 return (T) beansOfType.values().iterator().next();
 99  
             }
 100  
         }
 101  125
         return super.instanceOf(type, ofClass);
 102  
     }
 103  
 
 104  
     protected ApplicationContext createApplicationContext(ClassLoader classLoader, List<String> resources) {
 105  7
         if ( context != null ){
 106  1
             return context;
 107  
         }
 108  6
         return new SpringApplicationContextFactory(classLoader, resources.toArray(new String[resources.size()]))
 109  
                 .createApplicationContext();
 110  
     }
 111  
 
 112  
     protected ApplicationContext applicationContext() {
 113  2
         return context;
 114  
     }
 115  
 
 116  
 }