##### Coverage Report - org.jbehave.core.configuration.spring.SpringAnnotationBuilder

Classes in this File | Line Coverage | Branch Coverage | Complexity
SpringAnnotationBuilder |
97% | 37/38
95% | 19/20
| 2.7777777777777777;2.778

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.AnnotationRequired;
12 |  |
     import org.jbehave.core.configuration.Configuration;
13 |  |
     import org.jbehave.core.steps.CompositeStepsFactory;
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 InjectableStepsFactory buildStepsFactory(Configuration configuration) {
60 | 6 |
             InjectableStepsFactory factoryUsingSteps = super.buildStepsFactory(configuration);
61 | 6 |
             if (context != null) {
62 | 3 |
                 return new CompositeStepsFactory(new SpringStepsFactory(configuration, context), factoryUsingSteps);
63 |  |
             }
64 | 3 |
             return factoryUsingSteps;
65 |  |
         }
66 |  |
67 |  |
         @Override
68 |  |
         protected ParameterConverters parameterConverters(AnnotationFinder annotationFinder) {
69 | 9 |
             ParameterConverters converters = super.parameterConverters(annotationFinder);
70 | 9 |
             if (context != null) {
71 | 6 |
                 return converters.addConverters(getBeansOfType(context, ParameterConverter.class));
72 |  |
             }
73 | 3 |
             return converters;
74 |  |
         }
75 |  |
76 |  |
         private List<ParameterConverter> getBeansOfType(ApplicationContext context, Class<ParameterConverter> type) {
77 | 6 |
             Map<String, ParameterConverter> beansOfType = context.getBeansOfType(type);
78 | 6 |
             List<ParameterConverter> converters = new ArrayList<ParameterConverter>();
79 | 6 |
             for (ParameterConverter converter : beansOfType.values()) {
80 | 6 |
                 converters.add(converter);
81 |  |
             }
82 | 6 |
             return converters;
83 |  |
         }
84 |  |
85 |  |
         @Override
86 |  |
         protected <T, V extends T> T instanceOf(Class<T> type, Class<V> ofClass) {
87 | 173 |
             if (context != null) {
88 | 116 |
                 if (!type.equals(Object.class)) {
89 | 114 |
                     if (context.getBeansOfType(type).size() > 0) {
90 | 36 |
                         return context.getBeansOfType(type).values().iterator().next();
91 |  |
                     }
92 |  |
                 } else {
93 | 2 |
                     if (context.getBeansOfType(ofClass).size() > 0) {
94 | 0 |
                         return context.getBeansOfType(ofClass).values().iterator().next();
95 |  |
                     }
96 |  |
                 }
97 |  |
             }
98 | 137 |
             return super.instanceOf(type, ofClass);
99 |  |
         }
100 |  |
101 |  |
         protected ApplicationContext createApplicationContext(ClassLoader classLoader, List<String> resources) {
102 | 7 |
             if (context != null) {
103 | 1 |
                 return context;
104 |  |
             }
105 | 6 |
             return new SpringApplicationContextFactory(classLoader, resources.toArray(new String[resources.size()]))
106 |  |
                     .createApplicationContext();
107 |  |
         }
108 |  |
109 |  |
         protected ApplicationContext applicationContext() {
110 | 2 |
             return context;
111 |  |
         }
112 |  |
113 |  |
     }