1 | |
package org.jbehave.core.configuration.groovy; |
2 | |
|
3 | |
import groovy.lang.GroovyClassLoader; |
4 | |
|
5 | |
import org.jbehave.core.annotations.groovy.UsingGroovy; |
6 | |
import org.jbehave.core.configuration.AnnotationBuilder; |
7 | |
import org.jbehave.core.configuration.AnnotationMonitor; |
8 | |
import org.jbehave.core.configuration.AnnotationRequired; |
9 | |
import org.jbehave.core.configuration.Configuration; |
10 | |
import org.jbehave.core.steps.CompositeStepsFactory; |
11 | |
import org.jbehave.core.steps.InjectableStepsFactory; |
12 | |
import org.jbehave.core.steps.groovy.GroovyStepsFactory; |
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
public class GroovyAnnotationBuilder extends AnnotationBuilder { |
21 | |
|
22 | |
private GroovyContext context; |
23 | |
|
24 | |
public GroovyAnnotationBuilder(Class<?> annotatedClass) { |
25 | 4 | super(annotatedClass); |
26 | 4 | } |
27 | |
|
28 | |
public GroovyAnnotationBuilder(Class<?> annotatedClass, AnnotationMonitor annotationMonitor) { |
29 | 0 | super(annotatedClass, annotationMonitor); |
30 | 0 | } |
31 | |
|
32 | |
@SuppressWarnings("unchecked") |
33 | |
@Override |
34 | |
public Configuration buildConfiguration() throws AnnotationRequired { |
35 | 3 | if (annotationFinder().isAnnotationPresent(UsingGroovy.class)) { |
36 | 2 | Class<GroovyClassLoader> classLoaderClass = annotationFinder().getAnnotatedValue(UsingGroovy.class, |
37 | |
Class.class, "classLoader"); |
38 | 2 | Class<GroovyResourceFinder> resourceFinderClass = annotationFinder().getAnnotatedValue(UsingGroovy.class, |
39 | |
Class.class, "resourceFinder"); |
40 | |
try { |
41 | 2 | GroovyClassLoader classLoader = super.instanceOf(classLoaderClass, classLoaderClass); |
42 | 2 | GroovyResourceFinder resourceFinder = super.instanceOf(resourceFinderClass, resourceFinderClass); |
43 | 2 | context = createGroovyContext(classLoader, resourceFinder); |
44 | 0 | } catch (Exception e) { |
45 | 0 | annotationMonitor().elementCreationFailed(GroovyContext.class, e); |
46 | 2 | } |
47 | 2 | } else { |
48 | 1 | annotationMonitor().annotationNotFound(UsingGroovy.class, annotatedClass()); |
49 | |
} |
50 | 3 | return super.buildConfiguration(); |
51 | |
} |
52 | |
|
53 | |
@Override |
54 | |
public InjectableStepsFactory buildStepsFactory(Configuration configuration) { |
55 | 1 | InjectableStepsFactory factoryUsingSteps = super.buildStepsFactory(configuration); |
56 | 1 | if (context != null) { |
57 | 1 | return new CompositeStepsFactory(new GroovyStepsFactory(configuration, context), factoryUsingSteps); |
58 | |
} |
59 | 0 | return factoryUsingSteps; |
60 | |
} |
61 | |
|
62 | |
@Override |
63 | |
protected <T, V extends T> T instanceOf(Class<T> type, Class<V> ofClass) { |
64 | 36 | if (context != null) { |
65 | |
try { |
66 | 36 | return context.getInstanceOfType(type); |
67 | 36 | } catch (Exception e) { |
68 | |
|
69 | |
} |
70 | |
} |
71 | 36 | return super.instanceOf(type, ofClass); |
72 | |
} |
73 | |
|
74 | |
protected GroovyContext createGroovyContext(GroovyClassLoader classLoader, GroovyResourceFinder resourceFinder) { |
75 | 2 | if (context != null) { |
76 | 0 | return context; |
77 | |
} |
78 | 2 | return new GroovyContext(classLoader, resourceFinder); |
79 | |
} |
80 | |
|
81 | |
} |