##### Coverage Report - org.jbehave.core.steps.spring.SpringStepsFactory

Classes in this File | Line Coverage | Branch Coverage | Complexity
SpringStepsFactory |
93% | 14/15
92% | 13/14
| 3.0;3

1 |  |
     package org.jbehave.core.steps.spring;
2 |  |
3 |  |
     import java.lang.reflect.Modifier;
4 |  |
     import java.util.ArrayList;
5 |  |
     import java.util.List;
6 |  |
7 |  |
     import org.jbehave.core.configuration.Configuration;
8 |  |
     import org.jbehave.core.steps.AbstractStepsFactory;
9 |  |
     import org.jbehave.core.steps.InjectableStepsFactory;
10 |  |
     import org.springframework.context.ApplicationContext;
11 |  |
12 |  |
     /**
13 |  |
      * An {@link InjectableStepsFactory} that uses Spring's
14 |  |
      * {@link ApplicationContext} for the composition and instantiation of all
15 |  |
      * components that contain JBehave annotated methods.
16 |  |
      *
17 |  |
      * @author Paul Hammant
18 |  |
      * @author Mauro Talevi
19 |  |
      */
20 |  |
     public class SpringStepsFactory extends AbstractStepsFactory {
21 |  |
22 |  |
         private final ApplicationContext context;
23 |  |
24 |  |
         public SpringStepsFactory(Configuration configuration, ApplicationContext context) {
25 | 8 |
             super(configuration);
26 | 8 |
             this.context = context;
27 | 8 |
         }
28 |  |
29 |  |
         @Override
30 |  |
         protected List<Class<?>> stepsTypes() {
31 | 8 |
             List<Class<?>> types = new ArrayList<Class<?>>();
32 | 52 |
             for (String name : context.getBeanDefinitionNames()) {
33 | 44 |
                 Class<?> type = context.getType(name);
34 | 44 |
                 if (isAllowed(type) && hasAnnotatedMethods(type)) {
35 | 7 |
                     types.add(type);
36 |  |
                 }
37 |  |
             }
38 | 8 |
             return types;
39 |  |
         }
40 |  |
41 |  |
         /**
42 |  |
          * Checks if type returned from context is allowed, i.e. not null and not
43 |  |
          * abstract.
44 |  |
          *
45 |  |
          * @param type the Class of the bean
46 |  |
          * @return A boolean, <code>true</code> if allowed
47 |  |
          */
48 |  |
         protected boolean isAllowed(Class<?> type) {
49 | 44 |
             return type != null && !Modifier.isAbstract(type.getModifiers());
50 |  |
         }
51 |  |
52 |  |
         public Object createInstanceOfType(Class<?> type) {
53 | 43 |
             for (String name : context.getBeanDefinitionNames()) {
54 | 43 |
                 Class<?> beanType = context.getType(name);
55 | 43 |
                 if (type.equals(beanType)) {
56 | 9 |
                     return context.getBean(name);
57 |  |
                 }
58 |  |
             }
59 |  |
60 | 0 |
             throw new StepsInstanceNotFound(type, this);
61 |  |
         }
62 |  |
63 |  |
     }