From 64ba5fcfa82796ceb57078fb33bc62189cdead62 Mon Sep 17 00:00:00 2001 From: kohsuke Date: Wed, 19 Aug 2009 18:48:02 +0000 Subject: [PATCH] fixed a test case. git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@20903 71c3de6d-444a-0410-be80-ed276b4c234a --- .../hudson/test/ClosureExecuterAction.java | 49 +++++++++++++++++++ .../org/jvnet/hudson/test/HudsonTestCase.java | 35 +++++++++++++ .../test/groovy/hudson/model/SlaveTest.groovy | 4 +- 3 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 test/src/main/java/org/jvnet/hudson/test/ClosureExecuterAction.java diff --git a/test/src/main/java/org/jvnet/hudson/test/ClosureExecuterAction.java b/test/src/main/java/org/jvnet/hudson/test/ClosureExecuterAction.java new file mode 100644 index 0000000000..bf9ab247ca --- /dev/null +++ b/test/src/main/java/org/jvnet/hudson/test/ClosureExecuterAction.java @@ -0,0 +1,49 @@ +package org.jvnet.hudson.test; + +import groovy.lang.Closure; +import hudson.Extension; +import hudson.model.RootAction; +import org.kohsuke.stapler.QueryParameter; +import org.kohsuke.stapler.StaplerResponse; + +import java.io.IOException; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; +import java.util.UUID; + +/** + * Server-side logic that implements {@link HudsonTestCase#onServer(Closure)}. + * + * @author Kohsuke Kawaguchi + */ +@Extension +public final class ClosureExecuterAction implements RootAction { + private final Map runnables = Collections.synchronizedMap(new HashMap()); + + public void add(UUID uuid, Runnable r) { + runnables.put(uuid,r); + } + + public void doIndex(StaplerResponse rsp, @QueryParameter("uuid") String uuid) throws IOException { + Runnable r = runnables.get(UUID.fromString(uuid)); + if (r!=null) { + r.run(); + rsp.sendError(200); + } else { + rsp.sendError(404); + } + } + + public String getIconFileName() { + return null; + } + + public String getDisplayName() { + return null; + } + + public String getUrlName() { + return "closures"; + } +} 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 0b786b2201..870d072d1c 100644 --- a/test/src/main/java/org/jvnet/hudson/test/HudsonTestCase.java +++ b/test/src/main/java/org/jvnet/hudson/test/HudsonTestCase.java @@ -53,6 +53,7 @@ import hudson.model.TaskListener; import hudson.model.UpdateCenter; import hudson.model.AbstractProject; import hudson.model.View; +import hudson.model.RootAction; import hudson.model.UpdateCenter.UpdateCenterConfiguration; import hudson.model.Node.Mode; import hudson.security.csrf.CrumbIssuer; @@ -86,6 +87,7 @@ import java.util.List; import java.util.Locale; import java.util.Arrays; import java.util.Collections; +import java.util.UUID; import java.util.jar.Manifest; import java.util.logging.Filter; import java.util.logging.Level; @@ -111,6 +113,9 @@ import org.jvnet.hudson.test.rhino.JavaScriptDebugger; import org.kohsuke.stapler.Dispatcher; import org.kohsuke.stapler.MetaClass; import org.kohsuke.stapler.MetaClassLoader; +import org.kohsuke.stapler.StaplerRequest; +import org.kohsuke.stapler.StaplerResponse; +import org.kohsuke.stapler.Stapler; import org.mortbay.jetty.Server; import org.mortbay.jetty.bio.SocketConnector; import org.mortbay.jetty.security.HashUserRealm; @@ -140,6 +145,7 @@ import com.gargoylesoftware.htmlunit.html.HtmlInput; import com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine; import com.gargoylesoftware.htmlunit.javascript.host.Stylesheet; import com.gargoylesoftware.htmlunit.javascript.host.XMLHttpRequest; +import groovy.lang.Closure; /** * Base class for all Hudson test cases. @@ -833,6 +839,35 @@ public abstract class HudsonTestCase extends TestCase { return this; } + /** + * Executes the given closure on the server, in the context of an HTTP request. + * This is useful for testing some methods that require {@link StaplerRequest} and {@link StaplerResponse}. + * + *

+ * The closure will get the request and response as parameters. + */ + public Object executeOnServer(final Closure c) throws Throwable { + final Throwable[] t = new Throwable[1]; + final Object[] r = new Object[1]; + + ClosureExecuterAction cea = hudson.getExtensionList(RootAction.class).get(ClosureExecuterAction.class); + UUID id = UUID.randomUUID(); + cea.add(id,new Runnable() { + public void run() { + try { + r[0] = c.call(new Object[]{Stapler.getCurrentRequest(),Stapler.getCurrentResponse()}); + } catch (Throwable e) { + t[0] = e; + } + } + }); + createWebClient().goTo("closures/?uuid="+id); + + if (t[0]!=null) + throw t[0]; + return r[0]; + } + /** * Sometimes a part of a test case may ends up creeping into the serialization tree of {@link Saveable#save()}, * so detect that and flag that as an error. diff --git a/test/src/test/groovy/hudson/model/SlaveTest.groovy b/test/src/test/groovy/hudson/model/SlaveTest.groovy index 8a85ddc19f..2622044346 100644 --- a/test/src/test/groovy/hudson/model/SlaveTest.groovy +++ b/test/src/test/groovy/hudson/model/SlaveTest.groovy @@ -36,6 +36,8 @@ public class SlaveTest extends HudsonTestCase { * Makes sure that a form validation method gets inherited. */ void testFormValidation() { - assertNotNull(hudson.getDescriptor(DumbSlave.class).getCheckUrl("remoteFS")) + onServer { + assertNotNull(hudson.getDescriptor(DumbSlave.class).getCheckUrl("remoteFS")) + } } } \ No newline at end of file