mirror of
https://github.com/jenkinsci/jenkins.git
synced 2026-11-16 21:42:27 +00:00
fixed a test case.
git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@20903 71c3de6d-444a-0410-be80-ed276b4c234a
This commit is contained in:
1 parent
5b70aeb427
commit
64ba5fcfa8
3 files changed
+87
-1
No files matched your search
@@ -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<UUID,Runnable> runnables = Collections.synchronizedMap(new HashMap<UUID, Runnable>());
|
||||
|
||||
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";
|
||||
}
|
||||
}
|
||||
@@ -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}.
|
||||
*
|
||||
* <p>
|
||||
* 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.
|
||||
|
||||
Reference in new issue
Block a user