From 69275965a7ead437704b4e4f0d3a39f75fbd4a5e Mon Sep 17 00:00:00 2001 From: Kohsuke Kawaguchi Date: Wed, 23 Feb 2011 17:12:56 +0900 Subject: [PATCH] executeOnServer needs to be available on WebClient so that it can access a particular session --- .../org/jvnet/hudson/test/HudsonTestCase.java | 71 +++++++++++++------ 1 file changed, 48 insertions(+), 23 deletions(-) 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 854e75f2c1..d44b8635a6 100644 --- a/test/src/main/java/org/jvnet/hudson/test/HudsonTestCase.java +++ b/test/src/main/java/org/jvnet/hudson/test/HudsonTestCase.java @@ -1383,29 +1383,8 @@ public abstract class HudsonTestCase extends TestCase implements RootAction { * @throws Exception * If a closure throws any exception, that exception will be carried forward. */ - public V executeOnServer(final Callable c) throws Exception { - final Exception[] t = new Exception[1]; - final List r = new ArrayList(1); // size 1 list - - ClosureExecuterAction cea = hudson.getExtensionList(RootAction.class).get(ClosureExecuterAction.class); - UUID id = UUID.randomUUID(); - cea.add(id,new Runnable() { - public void run() { - try { - StaplerResponse rsp = Stapler.getCurrentResponse(); - rsp.setStatus(200); - rsp.setContentType("text/html"); - r.add(c.call()); - } catch (Exception e) { - t[0] = e; - } - } - }); - createWebClient().goTo("closures/?uuid="+id); - - if (t[0]!=null) - throw t[0]; - return r.get(0); + public V executeOnServer(Callable c) throws Exception { + return createWebClient().executeOnServer(c); } /** @@ -1505,6 +1484,52 @@ public abstract class HudsonTestCase extends TestCase implements RootAction { return this; } + /** + * Executes the given closure on the server, by the servlet request handling thread, + * in the context of an HTTP request. + * + *

+ * In {@link HudsonTestCase}, a thread that's executing the test code is different from the thread + * that carries out HTTP requests made through {@link WebClient}. But sometimes you want to + * make assertions and other calls with side-effect from within the request handling thread. + * + *

+ * This method allows you to do just that. It is useful for testing some methods that + * require {@link StaplerRequest} and {@link StaplerResponse}, or getting the credential + * of the current user (via {@link Hudson#getAuthentication()}, and so on. + * + * @param c + * The closure to be executed on the server. + * @return + * The return value from the closure. + * @throws Exception + * If a closure throws any exception, that exception will be carried forward. + */ + public V executeOnServer(final Callable c) throws Exception { + final Exception[] t = new Exception[1]; + final List r = new ArrayList(1); // size 1 list + + ClosureExecuterAction cea = hudson.getExtensionList(RootAction.class).get(ClosureExecuterAction.class); + UUID id = UUID.randomUUID(); + cea.add(id,new Runnable() { + public void run() { + try { + StaplerResponse rsp = Stapler.getCurrentResponse(); + rsp.setStatus(200); + rsp.setContentType("text/html"); + r.add(c.call()); + } catch (Exception e) { + t[0] = e; + } + } + }); + goTo("closures/?uuid="+id); + + if (t[0]!=null) + throw t[0]; + return r.get(0); + } + public HtmlPage search(String q) throws IOException, SAXException { HtmlPage top = goTo(""); HtmlForm search = top.getFormByName("search");