提交 1ef68951 编写于 作者: K kohsuke

implemented recipe execution

git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@11509 71c3de6d-444a-0410-be80-ed276b4c234a
上级 dc7f18fb
......@@ -6,6 +6,8 @@ import hudson.model.Hudson;
import hudson.model.Item;
import junit.framework.TestCase;
import org.jvnet.hudson.test.HudsonHomeLoader.CopyExisting;
import org.jvnet.hudson.test.recipes.Recipe;
import org.jvnet.hudson.test.recipes.Recipe.Runner;
import org.kohsuke.stapler.Stapler;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.bio.SocketConnector;
......@@ -16,7 +18,11 @@ import org.xml.sax.SAXException;
import javax.servlet.ServletContext;
import java.io.File;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
/**
* Base class for all Hudson test cases.
......@@ -27,7 +33,7 @@ public abstract class HudsonTestCase extends TestCase {
protected Hudson hudson;
protected final TestEnvironment env = new TestEnvironment();
protected HudsonHomeLoader homeLoader;
protected HudsonHomeLoader homeLoader = HudsonHomeLoader.NEW;
/**
* TCP/IP port that the server is listening on.
*/
......@@ -39,6 +45,11 @@ public abstract class HudsonTestCase extends TestCase {
*/
protected String contextPath = "/";
/**
* {@link Runnable}s to be invoked at {@link #tearDown()}.
*/
protected List<LenientRunnable> tearDowns = new ArrayList<LenientRunnable>();
protected HudsonTestCase(String name) {
super(name);
}
......@@ -51,6 +62,8 @@ public abstract class HudsonTestCase extends TestCase {
}
protected void tearDown() throws Exception {
for (LenientRunnable r : tearDowns)
r.run();
hudson.cleanUp();
env.dispose();
server.stop();
......@@ -101,8 +114,20 @@ public abstract class HudsonTestCase extends TestCase {
* <p>
* From here, call a series of {@code withXXX} methods.
*/
protected void recipe() {
withNewHome();
protected void recipe() throws Exception {
// look for recipe meta-annotation
Method runMethod= getClass().getMethod(getName());
for( final Annotation a : runMethod.getAnnotations() ) {
Recipe r = a.getClass().getAnnotation(Recipe.class);
if(r==null) continue;
final Runner runner = r.value().newInstance();
tearDowns.add(new LenientRunnable() {
public void run() throws Exception {
runner.tearDown(HudsonTestCase.this,a);
}
});
runner.setup(this,a);
}
}
public HudsonTestCase withNewHome() {
......
package org.jvnet.hudson.test;
/**
* Like {@link Runnable} but can throw any exception.
*
* @author Kohsuke Kawaguchi
*/
public interface LenientRunnable {
public void run() throws Exception;
}
......@@ -39,10 +39,10 @@ public @interface Recipe {
/**
* Called during {@link TestCase#setUp()} to prepare the test environment.
*/
public void setup(HudsonTestCase testCase, T recipe) {}
public void setup(HudsonTestCase testCase, T recipe) throws Exception {}
/**
* Called during {@link TestCase#tearDown()} to shut down the test environment.
*/
public void tearDown(HudsonTestCase testCase, T recipe) {}
public void tearDown(HudsonTestCase testCase, T recipe) throws Exception {}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册