JUnit4 supports recipes.

This commit is contained in:
kiy0taka authored and Kohsuke Kawaguchi committed 2012-01-03 11:41:01 -08:00
1 parent 104a058186
commit 6ffa28d1f7
8 files changed
+185 -6

No files matched your search

@@ -215,7 +215,7 @@ import static org.junit.matchers.JUnitMatchers.containsString;
*/
public class JenkinsRule implements TestRule, RootAction {
private final TemporaryFolder tempFolder = new TemporaryFolder();
private final TestEnvironment env = new TestEnvironment(null);
private Description testDescription;
@@ -299,6 +299,7 @@ public class JenkinsRule implements TestRule, RootAction {
* @throws Throwable if setup fails (which will disable {@code after}
*/
protected void before() throws Throwable {
env.pin();
recipe();
AbstractProject.WORKSPACE.toString();
User.clear();
@@ -414,9 +415,9 @@ public class JenkinsRule implements TestRule, RootAction {
public Statement apply(final Statement base, final Description description) {
if (description.getAnnotation(WithoutJenkins.class) != null) {
// request has been made to not create the instance for this test method
return tempFolder.apply(base, description);
return base;
}
return tempFolder.apply(new Statement() {
return new Statement() {
@Override
public void evaluate() throws Throwable {
testDescription = description;
@@ -453,7 +454,7 @@ public class JenkinsRule implements TestRule, RootAction {
CURRENT = null;
}
}
}, description);
};
}
public static class BreakException extends Exception {}
@@ -476,7 +477,7 @@ public class JenkinsRule implements TestRule, RootAction {
*/
protected Hudson newHudson() throws Exception {
ServletContext webServer = createWebServer();
File home = tempFolder.newFolder("jenkins-home-" + testDescription.getDisplayName());
File home = homeLoader.allocate();
for (JenkinsRecipe.Runner r : recipes)
r.decorateHome(this,home);
return new Hudson(home, webServer, getPluginManager());
@@ -715,7 +716,7 @@ public class JenkinsRule implements TestRule, RootAction {
* Allocates a new temporary directory for the duration of this test.
*/
public File createTmpDir() throws IOException {
return tempFolder.newFolder("jenkins-"+testDescription.getDisplayName());
return env.temporaryDirectoryAllocator.allocate();
}
public DumbSlave createSlave() throws Exception {
@@ -1987,4 +1988,8 @@ public class JenkinsRule implements TestRule, RootAction {
}
}
}
public Description getTestDescription() {
return testDescription;
}
}
@@ -23,8 +23,13 @@
*/
package org.jvnet.hudson.test.recipes;
import org.junit.runner.Description;
import org.jvnet.hudson.test.HudsonTestCase;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.HudsonHomeLoader.Local;
import org.jvnet.hudson.test.JenkinsRecipe;
import hudson.util.JenkinsReloadFailed;
import java.lang.annotation.Documented;
import java.lang.annotation.Target;
@@ -69,6 +74,7 @@ import static java.lang.annotation.ElementType.METHOD;
*/
@Documented
@Recipe(LocalData.RunnerImpl.class)
@JenkinsRecipe(LocalData.RuleRunnerImpl.class)
@Target(METHOD)
@Retention(RUNTIME)
public @interface LocalData {
@@ -77,4 +83,10 @@ public @interface LocalData {
testCase.with(new Local(testCase.getClass().getMethod(testCase.getName())));
}
}
class RuleRunnerImpl extends JenkinsRecipe.Runner<LocalData> {
public void setup(JenkinsRule jenkinsRule, LocalData recipe) throws Exception {
Description desc = jenkinsRule.getTestDescription();
jenkinsRule.with(new Local(desc.getTestClass().getMethod(desc.getMethodName())));
}
}
}
@@ -24,6 +24,8 @@
package org.jvnet.hudson.test.recipes;
import org.jvnet.hudson.test.HudsonTestCase;
import org.jvnet.hudson.test.JenkinsRecipe;
import org.jvnet.hudson.test.JenkinsRule;
import java.lang.annotation.Documented;
import static java.lang.annotation.ElementType.METHOD;
@@ -40,6 +42,7 @@ import java.util.Locale;
*/
@Documented
@Recipe(PresetData.RunnerImpl.class)
@JenkinsRecipe(PresetData.RuleRunnerImpl.class)
@Target(METHOD)
@Retention(RUNTIME)
public @interface PresetData {
@@ -66,4 +69,9 @@ public @interface PresetData {
testCase.withPresetData(recipe.value().name().toLowerCase(Locale.ENGLISH).replace('_','-'));
}
}
class RuleRunnerImpl extends JenkinsRecipe.Runner<PresetData> {
public void setup(JenkinsRule jenkinsRule, PresetData recipe) {
jenkinsRule.withPresetData(recipe.value().name().toLowerCase(Locale.ENGLISH).replace('_','-'));
}
}
}
@@ -24,8 +24,12 @@
package org.jvnet.hudson.test.recipes;
import org.jvnet.hudson.test.HudsonTestCase;
import org.jvnet.hudson.test.JenkinsRecipe;
import org.jvnet.hudson.test.JenkinsRule;
import org.apache.commons.io.FileUtils;
import hudson.util.JenkinsReloadFailed;
import java.io.File;
import java.lang.annotation.Documented;
import static java.lang.annotation.ElementType.METHOD;
@@ -41,6 +45,7 @@ import java.net.URL;
*/
@Documented
@Recipe(WithPlugin.RunnerImpl.class)
@JenkinsRecipe(WithPlugin.RuleRunnerImpl.class)
@Target(METHOD)
@Retention(RUNTIME)
public @interface WithPlugin {
@@ -67,4 +72,20 @@ public @interface WithPlugin {
FileUtils.copyURLToFile(res,new File(home,"plugins/"+a.value()));
}
}
class RuleRunnerImpl extends JenkinsRecipe.Runner<WithPlugin> {
private WithPlugin a;
@Override
public void setup(JenkinsRule jenkinsRule, WithPlugin recipe) throws Exception {
a = recipe;
jenkinsRule.useLocalPluginManager = true;
}
@Override
public void decorateHome(JenkinsRule jenkinsRule, File home) throws Exception {
URL res = getClass().getClassLoader().getResource("plugins/" + a.value());
FileUtils.copyURLToFile(res,new File(home,"plugins/"+a.value()));
}
}
}
@@ -25,6 +25,8 @@ package org.jvnet.hudson.test.recipes;
import hudson.PluginManager;
import org.jvnet.hudson.test.HudsonTestCase;
import org.jvnet.hudson.test.JenkinsRecipe;
import org.jvnet.hudson.test.JenkinsRule;
import java.io.File;
import java.lang.annotation.Documented;
@@ -42,6 +44,7 @@ import static java.lang.annotation.RetentionPolicy.*;
*/
@Documented
@Recipe(WithPluginManager.RunnerImpl.class)
@JenkinsRecipe(WithPluginManager.RuleRunnerImpl.class)
@Target(METHOD)
@Retention(RUNTIME)
public @interface WithPluginManager {
@@ -78,4 +81,18 @@ public @interface WithPluginManager {
}
}
class RuleRunnerImpl extends JenkinsRecipe.Runner<WithPluginManager> {
private WithPluginManager recipe;
@Override
public void setup(JenkinsRule jenkinsRule, WithPluginManager recipe) throws Exception {
this.recipe = recipe;
}
@Override
public void decorateHome(JenkinsRule jenkinsRule, File home) throws Exception {
Class<? extends PluginManager> c = recipe.value();
Constructor ctr = c.getDeclaredConstructor(File.class);
jenkinsRule.setPluginManager((PluginManager)ctr.newInstance(home));
}
}
}