mirror of
https://github.com/jenkinsci/jenkins.git
synced 2026-11-20 07:22:32 +00:00
adding a new recipe to load a dataset colocated with tests
git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@11622 71c3de6d-444a-0410-be80-ed276b4c234a
This commit is contained in:
1 parent
b4656b69be
commit
349a5777c7
3 files changed
+105
-6
No files matched your search
@@ -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 <tt>HUDSON_HOME</tt>.
|
||||
@@ -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"};
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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.
|
||||
*
|
||||
* <p>
|
||||
* This recipe allows your test case to start with the preset <tt>HUDSON_HOME</tt> data loaded
|
||||
* either from your test method or from the test class.
|
||||
*
|
||||
* <p>
|
||||
* For example, if the test method if <tt>org.acme.FooTest.testBar()</tt>, then
|
||||
* you can have your test data in one of the following places in resources folder
|
||||
* (typically <tt>src/test/resources</tt>):
|
||||
*
|
||||
* <ol>
|
||||
* <li>
|
||||
* Under <tt>org/acme/FooTest/testBar</tt> directory (that is, you'll have
|
||||
* <tt>org/acme/FooTest/testBar/config.xml</tt>), in the same layout as in the real <tt>HUDSON_HOME</tt> directory.
|
||||
* <li>
|
||||
* In <tt>org/acme/FooTest/testBar.zip</tt> as a zip file.
|
||||
* <li>
|
||||
* Under <tt>org/acme/FooTest</tt> directory (that is, you'll have
|
||||
* <tt>org/acme/FooTest/config.xml</tt>), in the same layout as in the real <tt>HUDSON_HOME</tt> directory.
|
||||
* <li>
|
||||
* In <tt>org/acme/FooTest.zip</tt> as a zip file.
|
||||
* </ol>
|
||||
*
|
||||
* <p>
|
||||
* 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.
|
||||
*
|
||||
* <p>
|
||||
* 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<LocalData> {
|
||||
public void setup(HudsonTestCase testCase, LocalData recipe) throws Exception {
|
||||
testCase.with(new Local(testCase.getClass().getMethod(testCase.getName())));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user