diff --git a/test/src/main/java/org/jvnet/hudson/test/HudsonHomeLoader.java b/test/src/main/java/org/jvnet/hudson/test/HudsonHomeLoader.java index f9e98a8f37..922d2261d7 100644 --- a/test/src/main/java/org/jvnet/hudson/test/HudsonHomeLoader.java +++ b/test/src/main/java/org/jvnet/hudson/test/HudsonHomeLoader.java @@ -5,6 +5,7 @@ import hudson.FilePath; import java.io.File; import java.io.IOException; import java.net.URL; +import java.lang.reflect.Method; /** * Controls how a {@link HudsonTestCase} initializes HUDSON_HOME. @@ -32,7 +33,7 @@ public interface HudsonHomeLoader { /** * Allocates a new directory by copying from an existing directory, or unzipping from a zip file. */ - public static class CopyExisting implements HudsonHomeLoader { + public static final class CopyExisting implements HudsonHomeLoader { private final File source; /** @@ -66,4 +67,43 @@ public interface HudsonHomeLoader { return target; } } + + /** + * Allocates a new directory by copying from a test resource + */ + public static final class Local implements HudsonHomeLoader { + private final Method testMethod; + + public Local(Method testMethod) { + this.testMethod = testMethod; + } + + public File allocate() throws Exception { + URL res = findDataResource(); + if(!res.getProtocol().equals("file")) + throw new AssertionError("Test data is not available in the file system: "+res); + // if we picked up a directory, it's one level above config.xml + File home = new File(res.getPath()); + if(!home.getName().endsWith(".zip")) + home = home.getParentFile(); + + return new CopyExisting(home).allocate(); + } + + private URL findDataResource() { + // first, check method specific resource + Class clazz = testMethod.getDeclaringClass(); + + for( String middle : new String[]{ '/'+testMethod.getName(), "" }) { + for( String suffix : SUFFIXES ) { + URL res = clazz.getResource(clazz.getSimpleName() + middle+suffix); + if(res!=null) return res; + } + } + + throw new AssertionError("No test resource was found for "+testMethod); + } + + private static final String[] SUFFIXES = {"/config.xml",".zip"}; + } } 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 aa972c5829..d9bf80d4f1 100644 --- a/test/src/main/java/org/jvnet/hudson/test/HudsonTestCase.java +++ b/test/src/main/java/org/jvnet/hudson/test/HudsonTestCase.java @@ -162,13 +162,11 @@ public abstract class HudsonTestCase extends TestCase { } public HudsonTestCase withNewHome() { - homeLoader = HudsonHomeLoader.NEW; - return this; + return with(HudsonHomeLoader.NEW); } public HudsonTestCase withExistingHome(File source) { - homeLoader = new CopyExisting(source); - return this; + return with(new CopyExisting(source)); } /** @@ -181,7 +179,11 @@ public abstract class HudsonTestCase extends TestCase { URL res = getClass().getResource(name); if(res==null) throw new IllegalArgumentException("No such data set found: "+name); - homeLoader = new CopyExisting(res); + return with(new CopyExisting(res)); + } + + public HudsonTestCase with(HudsonHomeLoader homeLoader) { + this.homeLoader = homeLoader; return this; } diff --git a/test/src/main/java/org/jvnet/hudson/test/recipes/LocalData.java b/test/src/main/java/org/jvnet/hudson/test/recipes/LocalData.java new file mode 100644 index 0000000000..6b0d244879 --- /dev/null +++ b/test/src/main/java/org/jvnet/hudson/test/recipes/LocalData.java @@ -0,0 +1,57 @@ +package org.jvnet.hudson.test.recipes; + +import org.jvnet.hudson.test.HudsonTestCase; +import org.jvnet.hudson.test.HudsonHomeLoader.Local; + +import java.lang.annotation.Documented; +import java.lang.annotation.Target; +import java.lang.annotation.Retention; +import static java.lang.annotation.RetentionPolicy.RUNTIME; +import static java.lang.annotation.ElementType.METHOD; + +/** + * Runs a test case with a data set local to test method or the test class. + * + *

+ * This recipe allows your test case to start with the preset HUDSON_HOME data loaded + * either from your test method or from the test class. + * + *

+ * For example, if the test method if org.acme.FooTest.testBar(), then + * you can have your test data in one of the following places in resources folder + * (typically src/test/resources): + * + *

    + *
  1. + * Under org/acme/FooTest/testBar directory (that is, you'll have + * org/acme/FooTest/testBar/config.xml), in the same layout as in the real HUDSON_HOME directory. + *
  2. + * In org/acme/FooTest/testBar.zip as a zip file. + *
  3. + * Under org/acme/FooTest directory (that is, you'll have + * org/acme/FooTest/config.xml), in the same layout as in the real HUDSON_HOME directory. + *
  4. + * In org/acme/FooTest.zip as a zip file. + *
+ * + *

+ * Search is performed in this specific order. The fall back mechanism allows you to write + * one test class that interacts with different aspects of the same data set, by associating + * the dataset with a test class, or have a data set local to a specific test method. + * + *

+ * The choice of zip and directory depends on the nature of the test data, as well as the size of it. + * + * @author Kohsuke Kawaguchi + */ +@Documented +@Recipe(LocalData.RunnerImpl.class) +@Target(METHOD) +@Retention(RUNTIME) +public @interface LocalData { + public class RunnerImpl extends Recipe.Runner { + public void setup(HudsonTestCase testCase, LocalData recipe) throws Exception { + testCase.with(new Local(testCase.getClass().getMethod(testCase.getName()))); + } + } +}