##### Coverage Report - org.jbehave.core.configuration.pico.PicoAnnotationBuilder

Classes in this File | Line Coverage | Branch Coverage | Complexity
PicoAnnotationBuilder |
100% | 43/43
100% | 18/18
| 2.875;2.875

1 |  |
     package org.jbehave.core.configuration.pico;
2 |  |
3 |  |
     import java.util.ArrayList;
4 |  |
     import java.util.List;
5 |  |
6 |  |
     import org.jbehave.core.annotations.pico.UsingPico;
7 |  |
     import org.jbehave.core.configuration.AnnotationBuilder;
8 |  |
     import org.jbehave.core.configuration.AnnotationFinder;
9 |  |
     import org.jbehave.core.configuration.AnnotationMonitor;
10 |  |
     import org.jbehave.core.configuration.AnnotationRequired;
11 |  |
     import org.jbehave.core.configuration.Configuration;
12 |  |
     import org.jbehave.core.steps.CompositeStepsFactory;
13 |  |
     import org.jbehave.core.steps.InjectableStepsFactory;
14 |  |
     import org.jbehave.core.steps.ParameterConverters;
15 |  |
     import org.jbehave.core.steps.ParameterConverters.ParameterConverter;
16 |  |
     import org.jbehave.core.steps.pico.PicoStepsFactory;
17 |  |
     import org.picocontainer.MutablePicoContainer;
18 |  |
     import org.picocontainer.PicoContainer;
19 |  |
     import org.picocontainer.injectors.AbstractInjector.AmbiguousComponentResolutionException;
20 |  |
21 |  |
     /**
22 |  |
      * Extends {@link AnnotationBuilder} to provide PicoContainer-based
23 |  |
      * dependency injection if {@link UsingPico} annotation is present.
24 |  |
      *
25 |  |
      * @author Cristiano Gavião
26 |  |
      * @author Mauro Talevi
27 |  |
      */
28 |  |
     public class PicoAnnotationBuilder extends AnnotationBuilder {
29 |  |
30 |  |
         private PicoContainer container;
31 |  |
32 |  |
         public PicoAnnotationBuilder(Class<?> annotatedClass) {
33 | 10 |
             super(annotatedClass);
34 | 10 |
         }
35 |  |
36 |  |
         public PicoAnnotationBuilder(Class<?> annotatedClass, AnnotationMonitor annotationMonitor) {
37 | 1 |
             super(annotatedClass, annotationMonitor);
38 | 1 |
         }
39 |  |
40 |  |
         public Configuration buildConfiguration() throws AnnotationRequired {
41 | 11 |
             AnnotationFinder finder = annotationFinder();
42 | 11 |
             if (finder.isAnnotationPresent(UsingPico.class)) {
43 |  |
                 @SuppressWarnings("rawtypes")
44 | 9 |
                 List<Class> moduleClasses = finder.getAnnotatedValues(UsingPico.class, Class.class, "modules");
45 | 9 |
                 List<PicoModule> modules = new ArrayList<PicoModule>();
46 | 9 |
                 for (Class<PicoModule> moduleClass : moduleClasses) {
47 |  |
                     try {
48 | 9 |
                         modules.add(moduleClass.newInstance());
49 | 1 |
                     } catch (Exception e) {
50 | 1 |
                         annotationMonitor().elementCreationFailed(moduleClass, e);
51 | 17 |
                     }
52 |  |
                 }
53 | 9 |
                 if ( modules.size() > 0 ){
54 | 6 |
                     container = createPicoContainer(modules);
55 |  |
                 }
56 | 9 |
             } else {
57 | 2 |
                 annotationMonitor().annotationNotFound(UsingPico.class, annotatedClass());
58 |  |
             }
59 | 11 |
             return super.buildConfiguration();
60 |  |
         }
61 |  |
62 |  |
         @Override
63 |  |
         public InjectableStepsFactory buildStepsFactory(Configuration configuration) {
64 | 6 |
             InjectableStepsFactory factoryUsingSteps = super.buildStepsFactory(configuration);
65 | 6 |
             if (container != null) {
66 | 3 |
                 return new CompositeStepsFactory(new PicoStepsFactory(configuration, container), factoryUsingSteps);
67 |  |
             }
68 | 3 |
             return factoryUsingSteps;
69 |  |
         }
70 |  |
71 |  |
         @Override
72 |  |
         protected ParameterConverters parameterConverters(AnnotationFinder annotationFinder) {
73 | 9 |
             ParameterConverters converters = super.parameterConverters(annotationFinder);
74 | 9 |
             if (container != null) {
75 | 6 |
                 return converters.addConverters(container.getComponents(ParameterConverter.class));
76 |  |
             }
77 | 3 |
             return converters;
78 |  |
         }
79 |  |
80 |  |
         @Override
81 |  |
         protected <T, V extends T> T instanceOf(final Class<T> type, final Class<V> ofClass) {
82 | 178 |
             if (container != null) {
83 | 116 |
                 T instance = null;
84 |  |
                 try {
85 | 116 |
                     instance = container.getComponent(type);
86 | 2 |
                 } catch (AmbiguousComponentResolutionException e) {
87 | 2 |
                     instance = container.getComponent(ofClass);
88 | 114 |
                 }
89 | 116 |
                 if ( instance != null ){
90 | 30 |
                     return instance;
91 |  |
                 }
92 |  |
             }
93 | 148 |
             return super.instanceOf(type, ofClass);
94 |  |
         }
95 |  |
96 |  |
         @SuppressWarnings("unchecked")
97 |  |
         protected PicoContainer createPicoContainer(List<PicoModule> modules) {
98 | 6 |
             if ( container != null ){
99 | 1 |
                 return container;
100 |  |
             }
101 |  |
             @SuppressWarnings("rawtypes")
102 | 5 |
             Class containerClass = annotationFinder().getAnnotatedValue(UsingPico.class, Class.class, "container");
103 | 5 |
             MutablePicoContainer container = instanceOf(MutablePicoContainer.class, containerClass);
104 | 5 |
             for (PicoModule module : modules) {
105 | 7 |
                 module.configure(container);
106 |  |
             }
107 | 5 |
             return container;
108 |  |
         }
109 |  |
110 |  |
         protected PicoContainer picoContainer() {
111 | 2 |
             return container;
112 |  |
         }
113 |  |
114 |  |
     }