1 | |
package org.jbehave.core.configuration.needle; |
2 | |
|
3 | |
import java.util.HashSet; |
4 | |
import java.util.List; |
5 | |
import java.util.Set; |
6 | |
|
7 | |
import org.jbehave.core.annotations.UsingSteps; |
8 | |
import org.jbehave.core.annotations.needle.UsingNeedle; |
9 | |
import org.jbehave.core.configuration.AnnotationBuilder; |
10 | |
import org.jbehave.core.configuration.AnnotationFinder; |
11 | |
import org.jbehave.core.configuration.AnnotationMonitor; |
12 | |
import org.jbehave.core.configuration.AnnotationRequired; |
13 | |
import org.jbehave.core.configuration.Configuration; |
14 | |
import org.jbehave.core.configuration.PrintStreamAnnotationMonitor; |
15 | |
import org.jbehave.core.steps.InjectableStepsFactory; |
16 | |
import org.jbehave.core.steps.needle.NeedleStepsFactory; |
17 | |
import org.jbehave.core.steps.needle.configuration.CreateInstanceByDefaultConstructor; |
18 | |
import org.needle4j.injection.InjectionProvider; |
19 | |
import org.needle4j.injection.InjectionProviderInstancesSupplier; |
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
public class NeedleAnnotationBuilder extends AnnotationBuilder { |
30 | |
|
31 | 12 | private final Set<InjectionProvider<?>> provider = new HashSet<InjectionProvider<?>>(); |
32 | 12 | private final Set<Class<?>> stepsClasses = new HashSet<Class<?>>(); |
33 | |
private NeedleStepsFactory factory; |
34 | |
|
35 | |
public NeedleAnnotationBuilder(Class<?> annotatedClass, AnnotationMonitor annotationMonitor) { |
36 | 12 | super(annotatedClass, annotationMonitor); |
37 | 12 | } |
38 | |
|
39 | |
public NeedleAnnotationBuilder(Class<?> annotatedClass) { |
40 | 12 | this(annotatedClass, new PrintStreamAnnotationMonitor()); |
41 | 12 | } |
42 | |
|
43 | |
@Override |
44 | |
public Configuration buildConfiguration() throws AnnotationRequired { |
45 | |
|
46 | 11 | final AnnotationFinder finder = annotationFinder(); |
47 | |
|
48 | 11 | if (finder.isAnnotationPresent(UsingSteps.class)) { |
49 | 6 | stepsClasses.addAll(finder.getAnnotatedClasses(UsingSteps.class, Object.class, "instances")); |
50 | |
} |
51 | |
|
52 | 11 | if (finder.isAnnotationPresent(UsingNeedle.class)) { |
53 | |
|
54 | |
@SuppressWarnings("rawtypes") |
55 | 9 | final List<Class> supplierClasses = finder.getAnnotatedValues(UsingNeedle.class, Class.class, "supplier"); |
56 | 9 | for (Class<InjectionProviderInstancesSupplier> supplierClass : supplierClasses) { |
57 | 0 | provider.addAll(CreateInstanceByDefaultConstructor.INSTANCE.apply(supplierClass).get()); |
58 | 0 | } |
59 | |
|
60 | |
@SuppressWarnings("rawtypes") |
61 | 9 | final List<Class> providerClasses = finder.getAnnotatedValues(UsingNeedle.class, Class.class, "provider"); |
62 | 9 | for (Class<InjectionProvider<?>> providerClass : providerClasses) { |
63 | 5 | provider.add(CreateInstanceByDefaultConstructor.INSTANCE.apply(providerClass)); |
64 | 5 | } |
65 | |
|
66 | 9 | } else { |
67 | 2 | annotationMonitor().annotationNotFound(UsingNeedle.class, annotatedClass()); |
68 | |
} |
69 | 11 | return super.buildConfiguration(); |
70 | |
} |
71 | |
|
72 | |
@Override |
73 | |
public InjectableStepsFactory buildStepsFactory(Configuration configuration) { |
74 | 14 | this.factory = new NeedleStepsFactory(configuration, provider, stepsClasses.toArray(new Class<?>[stepsClasses |
75 | 7 | .size()])); |
76 | 7 | return factory; |
77 | |
} |
78 | |
|
79 | |
@Override |
80 | |
@SuppressWarnings("unchecked") |
81 | |
protected <T, V extends T> T instanceOf(Class<T> type, Class<V> ofClass) { |
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | 177 | if (factory != null) { |
88 | 0 | return (T) factory.createInstanceOfType(ofClass); |
89 | |
} |
90 | 177 | return super.instanceOf(type, ofClass); |
91 | |
} |
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
public Set<InjectionProvider<?>> getProvider() { |
99 | 1 | return provider; |
100 | |
} |
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
public Set<Class<?>> getStepsClasses() { |
108 | 0 | return stepsClasses; |
109 | |
} |
110 | |
} |