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

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

There is a newer version: 4.4.0
Show newest version
package org.togglz.junit.vary;

import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;

import org.junit.runner.Runner;
import org.junit.runners.Suite;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.InitializationError;
import org.junit.runners.model.TestClass;
import org.togglz.core.Feature;

/**
 * 

* This class is custom JUnit runner that allows to run tests against different feature combinations. A usecase for this runner * would be for example to test feature toggles that enable caching behavior. The runner can be configured to run the tests two * times. Once with the flag enabled and once with the flag being disabled. *

* *

* Tests executed with this runner must implement a public static method annotated with {@link Variations}. This method must * return a {@link VariationSet} set specifies which features should be varied. *

* *

* Usage example: *

* *
 * @RunWith(FeatureVariations.class)
 * public class FeatureVariationsTest {
 * 
 *     @Variations
 *     public static VariationSet<MyFeatures> getPermutations() {
 *         return VariationSetBuilder.create(MyFeatures.class)
 *                 .enable(MyFeatures.FEATURE_ONE)
 *                 .vary(MyFeatures.FEATURE_TWO);
 *     }
 * 
 *     @Test
 *     public void test() {
 *         // do the tests
 *     }
 * 
 * }
 * 
* *

* In this example the test is executed two times. Once with FEATURE_TWO being active and once with * FEATURE_TWO being inactive. In both runs FEATURE_ONE is enabled. * * @author Christian Kaltepoth */ public class FeatureVariations extends Suite { protected List runners = new ArrayList(); public FeatureVariations(Class clazz) throws InitializationError { super(clazz, Collections. emptyList()); TestClass testClass = new TestClass(clazz); VariationSetBuilder permutation = getPermutationFromMethod(testClass); if (permutation == null) { throw new IllegalStateException("You have to place a @" + Variations.class.getSimpleName() + " annotation one the class: " + clazz.getName()); } for (Set activeFeatures : permutation.getVariants()) { runners.add(new VariationRunner(clazz, permutation.getFeatureClass(), activeFeatures)); } } private VariationSetBuilder getPermutationFromMethod(TestClass testClass) { List methods = testClass.getAnnotatedMethods(Variations.class); for (FrameworkMethod method : methods) { int modifiers = method.getMethod().getModifiers(); if (Modifier.isStatic(modifiers) && Modifier.isPublic(modifiers)) { try { return (VariationSetBuilder) method.invokeExplosively(null); } catch (Throwable e) { throw new IllegalStateException(e); } } } throw new IllegalStateException("Could not find public static method annotated with @" + Variations.class.getSimpleName() + " on class: " + testClass.getName()); } @Override protected List getChildren() { return runners; } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy