diff --git a/test/src/main/java/org/jvnet/hudson/test/HudsonTestCase.java b/test/src/main/java/org/jvnet/hudson/test/HudsonTestCase.java index fe34bbe998..20015a93c7 100644 --- a/test/src/main/java/org/jvnet/hudson/test/HudsonTestCase.java +++ b/test/src/main/java/org/jvnet/hudson/test/HudsonTestCase.java @@ -105,12 +105,12 @@ public abstract class HudsonTestCase extends TestCase { withNewHome(); } - protected HudsonTestCase withNewHome() { + public HudsonTestCase withNewHome() { homeLoader = HudsonHomeLoader.NEW; return this; } - protected HudsonTestCase withExistingHome(File source) { + public HudsonTestCase withExistingHome(File source) { homeLoader = new CopyExisting(source); return this; } @@ -120,7 +120,7 @@ public abstract class HudsonTestCase extends TestCase { * See https://svn.dev.java.net/svn/hudson/trunk/hudson/main/test/src/main/preset-data/ * for available datasets and what they mean. */ - protected HudsonTestCase withPresetData(String name) { + public HudsonTestCase withPresetData(String name) { name = "/" + name + ".zip"; URL res = getClass().getResource(name); if(res==null) throw new IllegalArgumentException("No such data set found: "+name); diff --git a/test/src/main/java/org/jvnet/hudson/test/recipes/PresetData.java b/test/src/main/java/org/jvnet/hudson/test/recipes/PresetData.java new file mode 100644 index 0000000000..2e8c92cca9 --- /dev/null +++ b/test/src/main/java/org/jvnet/hudson/test/recipes/PresetData.java @@ -0,0 +1,39 @@ +package org.jvnet.hudson.test.recipes; + +import org.jvnet.hudson.test.HudsonTestCase; + +import java.lang.annotation.Documented; +import static java.lang.annotation.ElementType.METHOD; +import java.lang.annotation.Retention; +import static java.lang.annotation.RetentionPolicy.RUNTIME; +import java.lang.annotation.Target; + +/** + * Runs a test case with one of the preset HUDSON_HOME data set. + * + * @author Kohsuke Kawaguchi + */ +@Documented +@Recipe(@PresetData.RunnerImpl.class) +@Target(METHOD) +@Retention(RUNTIME) +public @interface PresetData { + /** + * One of the preset data to choose from. + */ + DataSet value(); + + public enum DataSet { + /** + * Secured Hudson that has no anonymous read access. + * Any logged in user can do anything. + */ + NO_ANONYMOUS_READACCESS + } + + public class RunnerImpl extends Recipe.Runner { + public void setup(HudsonTestCase testCase, PresetData recipe) { + testCase.withPresetData(recipe.value().name().toLowerCase().replace('_','-')); + } + } +} diff --git a/test/src/main/java/org/jvnet/hudson/test/recipes/Recipe.java b/test/src/main/java/org/jvnet/hudson/test/recipes/Recipe.java new file mode 100644 index 0000000000..ae9252d2a9 --- /dev/null +++ b/test/src/main/java/org/jvnet/hudson/test/recipes/Recipe.java @@ -0,0 +1,48 @@ +package org.jvnet.hudson.test.recipes; + +import org.jvnet.hudson.test.HudsonTestCase; + +import java.lang.annotation.Documented; +import static java.lang.annotation.ElementType.ANNOTATION_TYPE; +import java.lang.annotation.Retention; +import static java.lang.annotation.RetentionPolicy.RUNTIME; +import java.lang.annotation.Target; +import java.lang.annotation.Annotation; + +import junit.framework.TestCase; + +/** + * Meta-annotation for recipe annotations, which controls + * the test environment set up. + * + * @author Kohsuke Kawaguchi + */ +@Retention(RUNTIME) +@Documented +@Target(ANNOTATION_TYPE) +public @interface Recipe { + /** + * Specifies the class that sets up the test environment. + * + *

+ * When a recipe annotation is placed on a test method, + */ + Class value(); + + /** + * The code that implements the recipe semantics. + * + * @param + * The recipe annotation associated with this runner. + */ + public abstract class Runner { + /** + * Called during {@link TestCase#setUp()} to prepare the test environment. + */ + public void setup(HudsonTestCase testCase, T recipe) {} + /** + * Called during {@link TestCase#tearDown()} to shut down the test environment. + */ + public void tearDown(HudsonTestCase testCase, T recipe) {} + } +}