use of cucumber.runtime.CucumberException in project cucumber-jvm by cucumber.
the class Hooks method addHook.
private static void addHook(Object[] tagsExpressionsAndBody, boolean before) {
long timeoutMillis = DEFAULT_TIMEOUT;
int order = DEFAULT_ORDER;
boolean timeoutSet = false;
boolean orderSet = false;
Closure body = null;
List<String> tagExpressions = new ArrayList<String>();
for (Object o : tagsExpressionsAndBody) {
if (o instanceof String) {
tagExpressions.add((String) o);
} else if (o instanceof Long) {
if (timeoutSet) {
throw new CucumberException("Two timeout (Long) arguments found; " + Long.toString(timeoutMillis) + ", and; " + Long.toString((Long) o));
}
timeoutMillis = (Long) o;
timeoutSet = true;
} else if (o instanceof Integer) {
if (orderSet) {
throw new CucumberException("Two order (Integer) arguments found; " + Integer.toString(order) + ", and; " + Integer.toString((Integer) o));
}
order = (Integer) o;
orderSet = true;
} else if (o instanceof Closure) {
body = (Closure) o;
} else {
throw new CucumberException("An argument of the type " + o.getClass().getName() + " found, " + (before ? "Before" : "After") + " only allows the argument types " + "String - Tag, Long - timeout, Integer - order, and Closure");
}
}
TagExpression tagExpression = new TagExpression(tagExpressions);
if (before) {
GroovyBackend.getInstance().addBeforeHook(tagExpression, timeoutMillis, order, body);
} else {
GroovyBackend.getInstance().addAfterHook(tagExpression, timeoutMillis, order, body);
}
}
use of cucumber.runtime.CucumberException in project cucumber-jvm by cucumber.
the class ObjectFactoryLoader method loadObjectFactory.
/**
* Loads an instance of {@link ObjectFactory}. The class name can be explicit, or it can be null.
* When it's null, the implementation is searched for in the <pre>cucumber.runtime</pre> packahe.
*
* @param classFinder where to load classes from
* @param objectFactoryClassName specific class name of {@link ObjectFactory} implementation. May be null.
* @return an instance of {@link ObjectFactory}
*/
public static ObjectFactory loadObjectFactory(ClassFinder classFinder, String objectFactoryClassName) {
ObjectFactory objectFactory;
try {
Reflections reflections = new Reflections(classFinder);
if (objectFactoryClassName != null) {
Class<ObjectFactory> objectFactoryClass = (Class<ObjectFactory>) classFinder.loadClass(objectFactoryClassName);
objectFactory = reflections.newInstance(new Class[0], new Object[0], objectFactoryClass);
} else {
objectFactory = reflections.instantiateExactlyOneSubclass(ObjectFactory.class, "cucumber.runtime", new Class[0], new Object[0]);
}
} catch (TooManyInstancesException e) {
System.out.println(e.getMessage());
System.out.println(getMultipleObjectFactoryLogMessage());
objectFactory = new DefaultJavaObjectFactory();
} catch (NoInstancesException e) {
objectFactory = new DefaultJavaObjectFactory();
} catch (ClassNotFoundException e) {
throw new CucumberException("Couldn't instantiate custom ObjectFactory", e);
}
return objectFactory;
}
use of cucumber.runtime.CucumberException in project cucumber-jvm by cucumber.
the class OsgiObjectFactoryBase method cacheNewInstance.
private <T> T cacheNewInstance(Class<T> type) {
try {
Constructor<T> constructor = type.getConstructor();
T instance = constructor.newInstance();
instances.put(type, instance);
return instance;
} catch (NoSuchMethodException e) {
throw new CucumberException(String.format("%s doesn't have an empty constructor. If you need DI, put cucumber-picocontainer on the classpath", type), e);
} catch (Exception e) {
throw new CucumberException(String.format("Failed to instantiate %s", type), e);
}
}
use of cucumber.runtime.CucumberException in project cucumber-jvm by cucumber.
the class RhinoBackend method registerWorld.
public void registerWorld(Function buildWorldFn, Function disposeWorldFn) {
if (this.buildWorldFn != null)
throw new CucumberException("World is already set");
if (buildWorldFn == null)
throw new CucumberException("World requires at least a build function");
this.buildWorldFn = buildWorldFn;
this.disposeWorldFn = disposeWorldFn;
}
use of cucumber.runtime.CucumberException in project cucumber-jvm by cucumber.
the class CucumberTestContextManager method checkAnnotationsEqual.
private void checkAnnotationsEqual(Class<?> stepClassWithSpringContext, Class<?> stepClass) {
Annotation[] annotations1 = stepClassWithSpringContext.getAnnotations();
Annotation[] annotations2 = stepClass.getAnnotations();
if (annotations1.length != annotations2.length) {
throw new CucumberException("Annotations differs on glue classes found: " + stepClassWithSpringContext.getName() + ", " + stepClass.getName());
}
for (Annotation annotation : annotations1) {
if (!isAnnotationInArray(annotation, annotations2)) {
throw new CucumberException("Annotations differs on glue classes found: " + stepClassWithSpringContext.getName() + ", " + stepClass.getName());
}
}
}
Aggregations