Search in sources :

Example 11 with CucumberException

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);
    }
}
Also used : Closure(groovy.lang.Closure) ArrayList(java.util.ArrayList) CucumberException(cucumber.runtime.CucumberException) TagExpression(gherkin.TagExpression)

Example 12 with CucumberException

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;
}
Also used : TooManyInstancesException(cucumber.runtime.TooManyInstancesException) ObjectFactory(cucumber.api.java.ObjectFactory) CucumberException(cucumber.runtime.CucumberException) NoInstancesException(cucumber.runtime.NoInstancesException) Reflections(cucumber.runtime.Reflections)

Example 13 with CucumberException

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);
    }
}
Also used : CucumberException(cucumber.runtime.CucumberException) CucumberException(cucumber.runtime.CucumberException)

Example 14 with CucumberException

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;
}
Also used : CucumberException(cucumber.runtime.CucumberException)

Example 15 with CucumberException

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());
        }
    }
}
Also used : CucumberException(cucumber.runtime.CucumberException) Annotation(java.lang.annotation.Annotation)

Aggregations

CucumberException (cucumber.runtime.CucumberException)37 Test (org.junit.Test)7 ArrayList (java.util.ArrayList)6 SingleValueConverter (cucumber.deps.com.thoughtworks.xstream.converters.SingleValueConverter)4 IOException (java.io.IOException)4 Step (gherkin.formatter.model.Step)3 ObjectFactory (cucumber.api.java.ObjectFactory)2 StepdefBody (cucumber.api.java8.StepdefBody)2 RuntimeOptionsFactory (cucumber.runtime.RuntimeOptionsFactory)2 Resource (cucumber.runtime.io.Resource)2 Java8StepDefinition (cucumber.runtime.java.Java8StepDefinition)2 AssetManager (android.content.res.AssetManager)1 DataTable (cucumber.api.DataTable)1 PendingException (cucumber.api.PendingException)1 Scenario (cucumber.api.Scenario)1 After (cucumber.api.java.After)1 Cucumber (cucumber.api.junit.Cucumber)1 OsgiClassFinder (cucumber.java.runtime.osgi.OsgiClassFinder)1 PaxExamObjectFactory (cucumber.java.runtime.osgi.PaxExamObjectFactory)1 Backend (cucumber.runtime.Backend)1