Search in sources :

Example 1 with FieldInfo

use of org.spockframework.runtime.model.FieldInfo in project spock by spockframework.

the class AutoCleanupInterceptor method intercept.

public void intercept(IMethodInvocation invocation) throws Throwable {
    List<Throwable> exceptions = new ArrayList<Throwable>();
    try {
        invocation.proceed();
    } catch (Throwable t) {
        exceptions.add(t);
    }
    for (FieldInfo field : fields) {
        AutoCleanup annotation = field.getAnnotation(AutoCleanup.class);
        try {
            Object value = field.readValue(invocation.getInstance());
            if (value == null)
                continue;
            GroovyRuntimeUtil.invokeMethod(value, annotation.value());
        } catch (Throwable t) {
            if (!annotation.quiet())
                exceptions.add(t);
        }
    }
    ExtensionUtil.throwAll(exceptions);
}
Also used : AutoCleanup(spock.lang.AutoCleanup) FieldInfo(org.spockframework.runtime.model.FieldInfo)

Example 2 with FieldInfo

use of org.spockframework.runtime.model.FieldInfo in project spock by spockframework.

the class ClassRuleInterceptor method intercept.

public void intercept(final IMethodInvocation invocation) throws Throwable {
    Statement stat = createBaseStatement(invocation);
    for (FieldInfo field : ruleFields) {
        TestRule rule = (TestRule) getRuleInstance(field, field.isShared() ? invocation.getSharedInstance() : invocation.getInstance());
        stat = rule.apply(stat, invocation.getSpec().getDescription());
    }
    stat.evaluate();
}
Also used : TestRule(org.junit.rules.TestRule) Statement(org.junit.runners.model.Statement) FieldInfo(org.spockframework.runtime.model.FieldInfo)

Example 3 with FieldInfo

use of org.spockframework.runtime.model.FieldInfo in project spock by spockframework.

the class MethodRuleInterceptor method intercept.

public void intercept(final IMethodInvocation invocation) throws Throwable {
    Statement statement = createBaseStatement(invocation);
    FrameworkMethod method = createFrameworkMethod(invocation);
    for (FieldInfo field : ruleFields) {
        MethodRule rule = (MethodRule) getRuleInstance(field, invocation.getInstance());
        statement = rule.apply(statement, method, invocation.getInstance());
    }
    statement.evaluate();
}
Also used : MethodRule(org.junit.rules.MethodRule) Statement(org.junit.runners.model.Statement) FrameworkMethod(org.junit.runners.model.FrameworkMethod) FieldInfo(org.spockframework.runtime.model.FieldInfo)

Example 4 with FieldInfo

use of org.spockframework.runtime.model.FieldInfo in project spock by spockframework.

the class TestRuleInterceptor method intercept.

public void intercept(final IMethodInvocation invocation) throws Throwable {
    Statement stat = createBaseStatement(invocation);
    for (FieldInfo field : ruleFields) {
        TestRule rule = (TestRule) getRuleInstance(field, invocation.getInstance());
        stat = rule.apply(stat, invocation.getIteration().getDescription());
    }
    stat.evaluate();
}
Also used : TestRule(org.junit.rules.TestRule) Statement(org.junit.runners.model.Statement) FieldInfo(org.spockframework.runtime.model.FieldInfo)

Example 5 with FieldInfo

use of org.spockframework.runtime.model.FieldInfo in project spock by spockframework.

the class RuleExtension method visitSpec.

public void visitSpec(SpecInfo spec) {
    if (ruleClass == null)
        return;
    List<FieldInfo> methodRuleFields = new ArrayList<FieldInfo>();
    List<FieldInfo> testRuleFields = new ArrayList<FieldInfo>();
    for (FieldInfo field : spec.getAllFields()) {
        if (!field.isAnnotationPresent(ruleClass))
            continue;
        checkIsInstanceField(field);
        if (hasFieldType(field, methodRuleClass)) {
            methodRuleFields.add(field);
        } else if (hasFieldType(field, testRuleClass)) {
            testRuleFields.add(field);
        } else {
            invalidFieldType(field);
        }
    }
    if (!methodRuleFields.isEmpty())
        MethodRuleInterceptorInstaller.install(spec, methodRuleFields);
    if (!testRuleFields.isEmpty())
        TestRuleInterceptorInstaller.install(spec, testRuleFields);
}
Also used : ArrayList(java.util.ArrayList) FieldInfo(org.spockframework.runtime.model.FieldInfo)

Aggregations

FieldInfo (org.spockframework.runtime.model.FieldInfo)6 Statement (org.junit.runners.model.Statement)3 TestRule (org.junit.rules.TestRule)2 Field (java.lang.reflect.Field)1 ArrayList (java.util.ArrayList)1 Autobuild (org.apache.tapestry5.ioc.annotations.Autobuild)1 InjectService (org.apache.tapestry5.ioc.annotations.InjectService)1 MethodRule (org.junit.rules.MethodRule)1 FrameworkMethod (org.junit.runners.model.FrameworkMethod)1 AutoCleanup (spock.lang.AutoCleanup)1