##### Coverage Report - org.jbehave.core.steps.AbstractStepsFactory

Classes in this File | Line Coverage | Branch Coverage | Complexity
AbstractStepsFactory |
100% | 20/20
85% | 12/14
| 2.3333333333333335;2.333
AbstractStepsFactory$StepsInstanceNotFound |
0% | 0/2
N/A | N/A
| 2.3333333333333335;2.333

1 |  |
     package org.jbehave.core.steps;
2 |  |
3 |  |
     import java.lang.annotation.Annotation;
4 |  |
     import java.lang.reflect.Method;
5 |  |
     import java.lang.reflect.Type;
6 |  |
     import java.util.ArrayList;
7 |  |
     import java.util.List;
8 |  |
9 |  |
     import org.jbehave.core.annotations.AsParameterConverter;
10 |  |
     import org.jbehave.core.configuration.Configuration;
11 |  |
     import org.jbehave.core.steps.ParameterConverters.MethodReturningConverter;
12 |  |
     import org.jbehave.core.steps.ParameterConverters.ParameterConverter;
13 |  |
14 |  |
     /**
15 |  |
      * <p>
16 |  |
      * An abstract implementation of {@link InjectableStepsFactory} that is provided
17 |  |
      * by concrete subclasses Object instances which contain the candidate steps
18 |  |
      * methods. The Object instances are wrapped by {@link Steps}.
19 |  |
      * </p>
20 |  |
      * <p>
21 |  |
      * The object instances are also inspected for methods annotated by {@link AsParameterConverter}
22 |  |
      * and the {@link ParameterConverter} is configured accordingly.
23 |  |
      * </p>
24 |  |
      */
25 |  |
     public abstract class AbstractStepsFactory implements InjectableStepsFactory {
26 |  |
27 |  |
         private final Configuration configuration;
28 |  |
29 | 190 |
             public AbstractStepsFactory(Configuration configuration) {
30 | 190 |
                     this.configuration = configuration;
31 | 190 |
             }
32 |  |
33 |  |
             public List<CandidateSteps> createCandidateSteps() {
34 | 16 |
                     List<Class<?>> types = stepsTypes();
35 | 16 |
                     List<CandidateSteps> steps = new ArrayList<CandidateSteps>();
36 | 16 |
                     for (Class<?> type : types) {
37 | 17 |
                             configuration.parameterConverters().addConverters(
38 |  |
                                             methodReturningConverters(type));
39 | 17 |
                             steps.add(new Steps(configuration, type, this));
40 |  |
                     }
41 | 16 |
                     return steps;
42 |  |
             }
43 |  |
44 |  |
             protected abstract List<Class<?>> stepsTypes();
45 |  |
46 |  |
             /**
47 |  |
              * Create parameter converters from methods annotated with @AsParameterConverter
48 |  |
              */
49 |  |
             private List<ParameterConverter> methodReturningConverters(Class<?> type) {
50 | 17 |
                     List<ParameterConverter> converters = new ArrayList<ParameterConverter>();
51 |  |
52 | 184 |
                     for (Method method : type.getMethods()) {
53 | 167 |
                             if (method.isAnnotationPresent(AsParameterConverter.class)) {
54 | 1 |
                                     converters.add(new MethodReturningConverter(method, type, this));
55 |  |
                             }
56 |  |
                     }
57 |  |
58 | 17 |
                     return converters;
59 |  |
             }
60 |  |
61 |  |
             /**
62 |  |
              * Determines if the given type is a {@link Class} containing at least one method
63 |  |
          * annotated with annotations from package "org.jbehave.core.annotations".
64 |  |
              *
65 |  |
              * @param type the Type of the steps instance
66 |  |
              * @return A boolean, <code>true</code> if at least one annotated method is found.
67 |  |
              */
68 |  |
         protected boolean hasAnnotatedMethods(Type type) {
69 | 2 |
                     if (type instanceof Class<?>) {
70 | 11 |
                             for (Method method : ((Class<?>) type).getMethods()) {
71 | 10 |
                                     for (Annotation annotation : method.getAnnotations()) {
72 | 1 |
                                             if (annotation.annotationType().getName().startsWith(
73 |  |
                                                             "org.jbehave.core.annotations")) {
74 | 1 |
                                                     return true;
75 |  |
                                             }
76 |  |
                                     }
77 |  |
                             }
78 |  |
                     }
79 | 1 |
                     return false;
80 |  |
             }
81 |  |
82 |  |
         @SuppressWarnings("serial")
83 |  |
         public static class StepsInstanceNotFound extends RuntimeException {
84 |  |
85 |  |
             public StepsInstanceNotFound(Class<?> type, InjectableStepsFactory stepsFactory) {
86 | 0 |
                 super("Steps instance not found for type "+type+" in factory "+stepsFactory);
87 | 0 |
             }
88 |  |
89 |  |
         }
90 |  |
     }