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