cucumber.runtime.java.JavaBackend Maven / Gradle / Ivy
package cucumber.runtime.java;
import cucumber.annotation.After;
import cucumber.annotation.Before;
import cucumber.annotation.Order;
import cucumber.fallback.runtime.java.DefaultJavaObjectFactory;
import cucumber.io.ClasspathResourceLoader;
import cucumber.io.ResourceLoader;
import cucumber.runtime.Backend;
import cucumber.runtime.CucumberException;
import cucumber.runtime.Glue;
import cucumber.runtime.UnreportedStepExecutor;
import cucumber.runtime.snippets.SnippetGenerator;
import gherkin.formatter.model.Step;
import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;
import java.util.regex.Pattern;
public class JavaBackend implements Backend {
private final SnippetGenerator snippetGenerator = new SnippetGenerator(new JavaSnippet());
private final ObjectFactory objectFactory;
private final ClasspathResourceLoader classpathResourceLoader;
private final ClasspathMethodScanner classpathMethodScanner;
private Glue glue;
public JavaBackend(ResourceLoader ignored) {
classpathResourceLoader = new ClasspathResourceLoader(Thread.currentThread().getContextClassLoader());
classpathMethodScanner = new ClasspathMethodScanner(classpathResourceLoader);
objectFactory = loadObjectFactory();
}
public JavaBackend(ObjectFactory objectFactory) {
classpathResourceLoader = new ClasspathResourceLoader(Thread.currentThread().getContextClassLoader());
classpathMethodScanner = new ClasspathMethodScanner(classpathResourceLoader);
this.objectFactory = objectFactory;
}
private ObjectFactory loadObjectFactory() {
ObjectFactory foundOF;
if (ObjectFactoryHolder.getFactory() != null) {
foundOF = ObjectFactoryHolder.getFactory();
} else {
try {
foundOF = classpathResourceLoader.instantiateExactlyOneSubclass(ObjectFactory.class, "cucumber.runtime", new Class[0], new Object[0]);
} catch (CucumberException ce) {
foundOF = new DefaultJavaObjectFactory();
}
}
return foundOF;
}
@Override
public void loadGlue(Glue glue, List gluePaths) {
this.glue = glue;
classpathMethodScanner.scan(this, gluePaths);
}
/**
* Convenience method for frameworks that wish to load glue from methods explicitly (possibly
* found with a different mechanism than Cucumber's built-in classpath scanning).
*
* @param glue where stepdefs and hooks will be added.
* @param method a candidate method.
*/
public void loadGlue(Glue glue, Method method) {
this.glue = glue;
classpathMethodScanner.scan(this, method);
}
@Override
public void setUnreportedStepExecutor(UnreportedStepExecutor executor) {
//Not used here yet
}
@Override
public void buildWorld() {
objectFactory.createInstances();
}
@Override
public void disposeWorld() {
objectFactory.disposeInstances();
}
@Override
public String getSnippet(Step step) {
return snippetGenerator.getSnippet(step);
}
void addStepDefinition(Annotation annotation, Method method) {
try {
Method regexpMethod = annotation.getClass().getMethod("value");
String regexpString = (String) regexpMethod.invoke(annotation);
if (regexpString != null) {
Pattern pattern = Pattern.compile(regexpString);
objectFactory.addClass(method.getDeclaringClass());
glue.addStepDefinition(new JavaStepDefinition(method, pattern, objectFactory));
}
} catch (NoSuchMethodException e) {
throw new CucumberException(e);
} catch (InvocationTargetException e) {
throw new CucumberException(e.getTargetException());
} catch (IllegalAccessException e) {
throw new CucumberException(e);
}
}
void addHook(Annotation annotation, Method method) {
objectFactory.addClass(method.getDeclaringClass());
Order order = method.getAnnotation(Order.class);
int hookOrder = (order == null) ? Integer.MAX_VALUE : order.value();
if (annotation.annotationType().equals(Before.class)) {
String[] tagExpressions = ((Before) annotation).value();
glue.addBeforeHook(new JavaHookDefinition(method, tagExpressions, hookOrder, objectFactory));
} else {
String[] tagExpressions = ((After) annotation).value();
glue.addAfterHook(new JavaHookDefinition(method, tagExpressions, hookOrder, objectFactory));
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy