com.github.iskrenyp.spockdbrepo.api.repo.DbRepoAnnotationDrivenExtension.groovy Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of spock-db-repo Show documentation
Show all versions of spock-db-repo Show documentation
A simple annotation driven local extension for Spock framework, which enables you to screen record your Specifications
The newest version!
package com.github.iskrenyp.spockdbrepo.api.repo
import org.spockframework.runtime.extension.AbstractAnnotationDrivenExtension
import org.spockframework.runtime.extension.ExtensionException
import org.spockframework.runtime.extension.IMethodInterceptor
import org.spockframework.runtime.extension.IMethodInvocation
import org.spockframework.runtime.model.FieldInfo
import org.spockframework.runtime.model.SpecInfo
class DbRepoAnnotationDrivenExtension extends AbstractAnnotationDrivenExtension {
@Override
void visitFieldAnnotation(Repo annotation, FieldInfo fieldInfo) {
if (!fieldInfo.shared) throw new ExtensionException("All fields with @Repo should be declared as @Shared as well.")
SpecInfo specInfo = fieldInfo.parent
specInfo.addSetupSpecInterceptor(new DbRepoInterceptor(fieldInfo, annotation.name()))
}
private static class DbRepoInterceptor implements IMethodInterceptor {
FieldInfo field
String repoName
DbRepoInterceptor(FieldInfo field, String repoName) {
this.field = field
this.repoName = repoName
}
@Override
void intercept(IMethodInvocation invocation) throws Throwable {
field.writeValue(invocation.instance, new SqlDataStore(repoName))
}
}
}