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

org.togglz.junit.vary.VariationRunner Maven / Gradle / Ivy

package org.togglz.junit.vary;

import java.util.Set;

import org.junit.runners.BlockJUnit4ClassRunner;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.InitializationError;
import org.junit.runners.model.Statement;
import org.togglz.core.Feature;
import org.togglz.core.context.FeatureContext;
import org.togglz.testing.TestFeatureManager;
import org.togglz.testing.TestFeatureManagerProvider;

/**
 * Internal class that executes a test for a test class for a single feature variation.
 * 
 * @author Christian Kaltepoth
 */
class VariationRunner extends BlockJUnit4ClassRunner {

    private final Class featureClass;

    private final Set activeFeatures;

    public VariationRunner(Class testClass, Class featureClass,
            Set activeFeatures) throws InitializationError {
        super(testClass);
        this.featureClass = featureClass;
        this.activeFeatures = activeFeatures;
    }

    @Override
    protected Statement methodInvoker(FrameworkMethod method, Object test) {

        final Statement delegate = super.methodInvoker(method, test);

        return new Statement() {

            @Override
            public void evaluate() throws Throwable {
                try {

                    // create blank instance and set initial state
                    TestFeatureManager featureManager = new TestFeatureManager(featureClass);
                    for (Feature feature : activeFeatures) {
                        featureManager.enable(feature);
                    }

                    // register the test instance
                    TestFeatureManagerProvider.setFeatureManager(featureManager);
                    FeatureContext.clearCache();

                    // run the test
                    delegate.evaluate();

                }

                finally {
                    TestFeatureManagerProvider.setFeatureManager(null);
                }
            }
        };
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy