From 6ffa28d1f7ce84089fc16fbc935d5e3c8c45eac4 Mon Sep 17 00:00:00 2001 From: kiy0taka Date: Wed, 28 Dec 2011 08:10:07 +0900 Subject: [PATCH] JUnit4 supports recipes. --- .../org/jvnet/hudson/test/JenkinsRule.java | 17 ++-- .../jvnet/hudson/test/recipes/LocalData.java | 12 +++ .../jvnet/hudson/test/recipes/PresetData.java | 8 ++ .../jvnet/hudson/test/recipes/WithPlugin.java | 21 +++++ .../test/recipes/WithPluginManager.java | 17 ++++ .../main/UseRecipesWithJenkinsRuleTest.java | 79 +++++++++++++++++++ .../testGetItemFromLocalData/config.xml | 15 ++++ .../jobs/testJob/config.xml | 22 ++++++ 8 files changed, 185 insertions(+), 6 deletions(-) create mode 100644 test/src/test/junit4/org/jvnet/hudson/main/UseRecipesWithJenkinsRuleTest.java create mode 100644 test/src/test/resources/org/jvnet/hudson/main/UseRecipesWithJenkinsRuleTest/testGetItemFromLocalData/config.xml create mode 100644 test/src/test/resources/org/jvnet/hudson/main/UseRecipesWithJenkinsRuleTest/testGetItemFromLocalData/jobs/testJob/config.xml diff --git a/test/src/main/java/org/jvnet/hudson/test/JenkinsRule.java b/test/src/main/java/org/jvnet/hudson/test/JenkinsRule.java index a60b5278e2..b02bad6c23 100644 --- a/test/src/main/java/org/jvnet/hudson/test/JenkinsRule.java +++ b/test/src/main/java/org/jvnet/hudson/test/JenkinsRule.java @@ -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; + } } 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 index 237e1bf91e..1de6011852 100644 --- a/test/src/main/java/org/jvnet/hudson/test/recipes/LocalData.java +++ b/test/src/main/java/org/jvnet/hudson/test/recipes/LocalData.java @@ -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 { + public void setup(JenkinsRule jenkinsRule, LocalData recipe) throws Exception { + Description desc = jenkinsRule.getTestDescription(); + jenkinsRule.with(new Local(desc.getTestClass().getMethod(desc.getMethodName()))); + } + } } 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 index 3929789e52..f233ad7e9e 100644 --- a/test/src/main/java/org/jvnet/hudson/test/recipes/PresetData.java +++ b/test/src/main/java/org/jvnet/hudson/test/recipes/PresetData.java @@ -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 { + public void setup(JenkinsRule jenkinsRule, PresetData recipe) { + jenkinsRule.withPresetData(recipe.value().name().toLowerCase(Locale.ENGLISH).replace('_','-')); + } + } } diff --git a/test/src/main/java/org/jvnet/hudson/test/recipes/WithPlugin.java b/test/src/main/java/org/jvnet/hudson/test/recipes/WithPlugin.java index 50df05ab59..9901de1a50 100644 --- a/test/src/main/java/org/jvnet/hudson/test/recipes/WithPlugin.java +++ b/test/src/main/java/org/jvnet/hudson/test/recipes/WithPlugin.java @@ -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 { + 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())); + } + } } diff --git a/test/src/main/java/org/jvnet/hudson/test/recipes/WithPluginManager.java b/test/src/main/java/org/jvnet/hudson/test/recipes/WithPluginManager.java index f7d72d736f..0517e9267c 100644 --- a/test/src/main/java/org/jvnet/hudson/test/recipes/WithPluginManager.java +++ b/test/src/main/java/org/jvnet/hudson/test/recipes/WithPluginManager.java @@ -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 { + 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 c = recipe.value(); + Constructor ctr = c.getDeclaredConstructor(File.class); + jenkinsRule.setPluginManager((PluginManager)ctr.newInstance(home)); + } + } } diff --git a/test/src/test/junit4/org/jvnet/hudson/main/UseRecipesWithJenkinsRuleTest.java b/test/src/test/junit4/org/jvnet/hudson/main/UseRecipesWithJenkinsRuleTest.java new file mode 100644 index 0000000000..34af67ee3c --- /dev/null +++ b/test/src/test/junit4/org/jvnet/hudson/main/UseRecipesWithJenkinsRuleTest.java @@ -0,0 +1,79 @@ +package org.jvnet.hudson.main; + +import static javax.servlet.http.HttpServletResponse.SC_UNAUTHORIZED; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.fail; +import hudson.LocalPluginManager; + +import java.io.File; +import java.io.IOException; +import java.net.URL; + +import org.junit.Rule; +import org.junit.Test; +import org.jvnet.hudson.test.JenkinsRule; +import org.jvnet.hudson.test.JenkinsRule.WebClient; +import org.jvnet.hudson.test.recipes.LocalData; +import org.jvnet.hudson.test.recipes.PresetData; +import org.jvnet.hudson.test.recipes.PresetData.DataSet; +import org.jvnet.hudson.test.recipes.WithPlugin; +import org.jvnet.hudson.test.recipes.WithPluginManager; +import org.xml.sax.SAXException; + +import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException; +import com.gargoylesoftware.htmlunit.html.HtmlPage; + +public class UseRecipesWithJenkinsRuleTest { + + @Rule + public JenkinsRule rule = new JenkinsRule(); + + @Test + @LocalData + public void testGetItemFromLocalData() { + assertNotNull(rule.jenkins.getItem("testJob")); + } + + @Test + @WithPlugin("tasks.hpi") + public void testWithPlugin() { + assertNotNull(rule.jenkins.getPlugin("tasks")); + } + + @Test + @PresetData(DataSet.ANONYMOUS_READONLY) + public void testPresetData() throws Exception { + WebClient wc = rule.createWebClient(); + try { + wc.goTo("loginError"); + fail("Expecting a 401 error"); + } catch (FailingHttpStatusCodeException e) { + e.printStackTrace(); + assertEquals(SC_UNAUTHORIZED,e.getStatusCode()); + } + + // but not once the user logs in. + verifyNotError(wc.login("alice")); + } + + @Test + @WithPluginManager(MyPluginManager.class) + public void testWithPluginManager() { + assertEquals(MyPluginManager.class, rule.jenkins.pluginManager.getClass()); + } + + private void verifyNotError(WebClient wc) throws IOException, SAXException { + HtmlPage p = wc.goTo("loginError"); + URL url = p.getWebResponse().getUrl(); + System.out.println(url); + assertFalse(url.toExternalForm().contains("login")); + } + + public static class MyPluginManager extends LocalPluginManager { + public MyPluginManager(File rootDir) { + super(rootDir); + } + } +} diff --git a/test/src/test/resources/org/jvnet/hudson/main/UseRecipesWithJenkinsRuleTest/testGetItemFromLocalData/config.xml b/test/src/test/resources/org/jvnet/hudson/main/UseRecipesWithJenkinsRuleTest/testGetItemFromLocalData/config.xml new file mode 100644 index 0000000000..d364ef8a29 --- /dev/null +++ b/test/src/test/resources/org/jvnet/hudson/main/UseRecipesWithJenkinsRuleTest/testGetItemFromLocalData/config.xml @@ -0,0 +1,15 @@ + + + 1.355 + 1 + NORMAL + true + + hudson.model.Hudson.Read:anonymous + hudson.model.Item.Read:anonymous + + + + + 5 + diff --git a/test/src/test/resources/org/jvnet/hudson/main/UseRecipesWithJenkinsRuleTest/testGetItemFromLocalData/jobs/testJob/config.xml b/test/src/test/resources/org/jvnet/hudson/main/UseRecipesWithJenkinsRuleTest/testGetItemFromLocalData/jobs/testJob/config.xml new file mode 100644 index 0000000000..7f0268d9f1 --- /dev/null +++ b/test/src/test/resources/org/jvnet/hudson/main/UseRecipesWithJenkinsRuleTest/testGetItemFromLocalData/jobs/testJob/config.xml @@ -0,0 +1,22 @@ + + + + + + + true + hudson.model.Item.ExtendedRead:alice + hudson.model.Item.Configure:alice + hudson.model.Item.Configure:bob + hudson.model.Item.ExtendedRead:charlie + + + + true + false + (Default) + + + + +