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