All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.iskrenyp.spockdbrepo.api.withviews.WithViewsAnnotationDrivenExtension.groovy Maven / Gradle / Ivy

Go to download

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.withviews

import com.github.iskrenyp.spockdbrepo.api.repo.SqlDataStore
import com.github.iskrenyp.spockdbrepo.exception.DbRepoException
import org.spockframework.runtime.AbstractRunListener
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.FeatureInfo
import org.spockframework.runtime.model.SpecInfo

class WithViewsAnnotationDrivenExtension extends AbstractAnnotationDrivenExtension {

    SqlDataStore dataStore
    ViewsManager viewsManager

    @Override
    void visitSpecAnnotation(WithViews annotation, SpecInfo spec) {

        spec.addListener(new AbstractRunListener() {

            @Override
            void afterSpec(SpecInfo specInfo) {
                try {
                    if (viewsManager) viewsManager.dropMatchingViews()
                } catch (DbRepoException e) {
                    throw new ExtensionException("There was an error while dropping views from ${annotation.repo()}", e)
                }
            }

            @Override
            void beforeSpec(SpecInfo specInfo) {
                dataStore = new SqlDataStore(annotation.repo())
                viewsManager = new ViewsManager(dataStore, annotation.views())
                try {
                    viewsManager.createMatchingViews()
                } catch(DbRepoException e) {
                    throw new ExtensionException("There was an error while creating views for ${annotation.repo()}", e)
                }
            }
        })
    }

    @Override
    void visitFeatureAnnotation(WithViews annotation, FeatureInfo feature) {
        feature.addInterceptor(
                new WithViewsMethodInterceptor(repoName: annotation.repo(), requiredViews: annotation.views())
        )
    }

    private static class WithViewsMethodInterceptor implements IMethodInterceptor {

        String repoName
        String[] requiredViews

        @Override
        void intercept(IMethodInvocation invocation) throws Throwable {
            SqlDataStore dataStore = new SqlDataStore(repoName)
            ViewsManager viewsManager = new ViewsManager(dataStore, requiredViews)
            try {
                viewsManager.createMatchingViews()
            } catch(DbRepoException e) {
                throw new ExtensionException("There was an error while creating views for ${repoName}", e)
            }
            try {
                invocation.proceed()
            } finally {
                try {
                    if (viewsManager) viewsManager.dropMatchingViews()
                } catch (DbRepoException e) {
                    throw new ExtensionException("There was an error while dropping views from ${repoName}", e)
                }
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy