提交 571de9c1 编写于 作者: J Jesse Glick

JUnit 4; introduced GroovyJenkinsRule.

上级 ed03fbbc
...@@ -15,7 +15,9 @@ import java.io.IOException; ...@@ -15,7 +15,9 @@ import java.io.IOException;
* {@link HudsonTestCase} with more convenience methods for Groovy. * {@link HudsonTestCase} with more convenience methods for Groovy.
* *
* @author Kohsuke Kawaguchi * @author Kohsuke Kawaguchi
* @deprecated Use {@link GroovyJenkinsRule} instead.
*/ */
@Deprecated
public abstract class GroovyHudsonTestCase extends HudsonTestCase { public abstract class GroovyHudsonTestCase extends HudsonTestCase {
/** /**
* Executes the given closure on the server, in the context of an HTTP request. * Executes the given closure on the server, in the context of an HTTP request.
......
/*
* The MIT License
*
* Copyright 2013 Jesse Glick.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.jvnet.hudson.test;
import groovy.lang.Closure;
import hudson.Launcher;
import hudson.model.AbstractBuild;
import hudson.model.BuildListener;
import hudson.tasks.Builder;
import java.io.IOException;
import java.util.concurrent.Callable;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;
/**
* {@link JenkinsRule} variant with special options for tests written in Groovy.
* @since 1.535
*/
public class GroovyJenkinsRule extends JenkinsRule {
/**
* 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 Exception {
return executeOnServer(new Callable<Object>() {
@Override public Object call() throws Exception {
return c.call();
}
});
}
/**
* Wraps a closure as a {@link Builder}.
*/
public Builder builder(final Closure c) {
return new TestBuilder() {
@Override public boolean perform(AbstractBuild<?,?> build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException {
Object r = c.call(new Object[] {build, launcher, listener});
if (r instanceof Boolean) {
return (Boolean) r;
}
return true;
}
};
}
}
...@@ -29,21 +29,28 @@ import hudson.slaves.EnvironmentVariablesNodeProperty ...@@ -29,21 +29,28 @@ import hudson.slaves.EnvironmentVariablesNodeProperty
import hudson.slaves.EnvironmentVariablesNodeProperty.Entry import hudson.slaves.EnvironmentVariablesNodeProperty.Entry
import org.jvnet.hudson.test.CaptureEnvironmentBuilder import org.jvnet.hudson.test.CaptureEnvironmentBuilder
import org.jvnet.hudson.test.GroovyHudsonTestCase import org.jvnet.hudson.test.GroovyJenkinsRule
import org.jvnet.hudson.test.FakeChangeLogSCM import org.jvnet.hudson.test.FakeChangeLogSCM
import org.jvnet.hudson.test.FailureBuilder import org.jvnet.hudson.test.FailureBuilder
import org.jvnet.hudson.test.UnstableBuilder import org.jvnet.hudson.test.UnstableBuilder
public class AbstractBuildTest extends GroovyHudsonTestCase { import static org.junit.Assert.*
void testVariablesResolved() { import org.junit.Rule
def project = createFreeStyleProject(); import org.junit.Test
jenkins.nodeProperties.replaceBy([
public class AbstractBuildTest {
@Rule public GroovyJenkinsRule j = new GroovyJenkinsRule();
@Test void variablesResolved() {
def project = j.createFreeStyleProject();
j.jenkins.nodeProperties.replaceBy([
new EnvironmentVariablesNodeProperty(new Entry("KEY1", "value"), new Entry("KEY2",'$KEY1'))]); new EnvironmentVariablesNodeProperty(new Entry("KEY1", "value"), new Entry("KEY2",'$KEY1'))]);
def builder = new CaptureEnvironmentBuilder(); def builder = new CaptureEnvironmentBuilder();
project.buildersList.add(builder); project.buildersList.add(builder);
buildAndAssertSuccess(project); j.buildAndAssertSuccess(project);
def envVars = builder.getEnvVars(); def envVars = builder.getEnvVars();
assertEquals("value", envVars["KEY1"]); assertEquals("value", envVars["KEY1"]);
...@@ -53,15 +60,15 @@ public class AbstractBuildTest extends GroovyHudsonTestCase { ...@@ -53,15 +60,15 @@ public class AbstractBuildTest extends GroovyHudsonTestCase {
/** /**
* Makes sure that raw console output doesn't get affected by XML escapes. * Makes sure that raw console output doesn't get affected by XML escapes.
*/ */
void testRawConsoleOutput() { @Test void rawConsoleOutput() {
def out = "<test>&</test>"; def out = "<test>&</test>";
def p = createFreeStyleProject(); def p = j.createFreeStyleProject();
p.buildersList.add(builder { builder,launcher,BuildListener listener -> p.buildersList.add(j.builder { builder,launcher,BuildListener listener ->
listener.logger.println(out); listener.logger.println(out);
}) })
def b = buildAndAssertSuccess(p); def b = j.buildAndAssertSuccess(p);
Page rsp = createWebClient().goTo("${b.url}/consoleText", "text/plain"); Page rsp = j.createWebClient().goTo("${b.url}/consoleText", "text/plain");
println "Output:\n"+rsp.webResponse.contentAsString println "Output:\n"+rsp.webResponse.contentAsString
assertTrue(rsp.webResponse.contentAsString.contains(out)); assertTrue(rsp.webResponse.contentAsString.contains(out));
} }
...@@ -70,48 +77,48 @@ public class AbstractBuildTest extends GroovyHudsonTestCase { ...@@ -70,48 +77,48 @@ public class AbstractBuildTest extends GroovyHudsonTestCase {
assertEquals(expectedIds as Set, b.culprits*.id as Set); assertEquals(expectedIds as Set, b.culprits*.id as Set);
} }
void testCulprits() { @Test void culprits() {
def p = createFreeStyleProject(); def p = j.createFreeStyleProject();
def scm = new FakeChangeLogSCM() def scm = new FakeChangeLogSCM()
p.scm = scm p.scm = scm
// 1st build, successful, no culprits // 1st build, successful, no culprits
scm.addChange().withAuthor("alice") scm.addChange().withAuthor("alice")
def b = assertBuildStatus(Result.SUCCESS,p.scheduleBuild2(0).get()) def b = j.assertBuildStatus(Result.SUCCESS,p.scheduleBuild2(0).get())
assertCulprits(b,["alice"]) assertCulprits(b,["alice"])
// 2nd build // 2nd build
scm.addChange().withAuthor("bob") scm.addChange().withAuthor("bob")
p.buildersList.add(new FailureBuilder()) p.buildersList.add(new FailureBuilder())
b = assertBuildStatus(Result.FAILURE,p.scheduleBuild2(0).get()) b = j.assertBuildStatus(Result.FAILURE,p.scheduleBuild2(0).get())
assertCulprits(b,["bob"]) assertCulprits(b,["bob"])
// 3rd build. bob continues to be in culprit // 3rd build. bob continues to be in culprit
scm.addChange().withAuthor("charlie") scm.addChange().withAuthor("charlie")
b = assertBuildStatus(Result.FAILURE,p.scheduleBuild2(0).get()) b = j.assertBuildStatus(Result.FAILURE,p.scheduleBuild2(0).get())
assertCulprits(b,["bob","charlie"]) assertCulprits(b,["bob","charlie"])
// 4th build, unstable. culprit list should continue // 4th build, unstable. culprit list should continue
scm.addChange().withAuthor("dave") scm.addChange().withAuthor("dave")
p.buildersList.replaceBy([new UnstableBuilder()]) p.buildersList.replaceBy([new UnstableBuilder()])
b = assertBuildStatus(Result.UNSTABLE,p.scheduleBuild2(0).get()) b = j.assertBuildStatus(Result.UNSTABLE,p.scheduleBuild2(0).get())
assertCulprits(b,["bob","charlie","dave"]) assertCulprits(b,["bob","charlie","dave"])
// 5th build, unstable. culprit list should continue // 5th build, unstable. culprit list should continue
scm.addChange().withAuthor("eve") scm.addChange().withAuthor("eve")
b = assertBuildStatus(Result.UNSTABLE,p.scheduleBuild2(0).get()) b = j.assertBuildStatus(Result.UNSTABLE,p.scheduleBuild2(0).get())
assertCulprits(b,["bob","charlie","dave","eve"]) assertCulprits(b,["bob","charlie","dave","eve"])
// 6th build, success, accumulation continues up to this point // 6th build, success, accumulation continues up to this point
scm.addChange().withAuthor("fred") scm.addChange().withAuthor("fred")
p.buildersList.replaceBy([]) p.buildersList.replaceBy([])
b = assertBuildStatus(Result.SUCCESS,p.scheduleBuild2(0).get()) b = j.assertBuildStatus(Result.SUCCESS,p.scheduleBuild2(0).get())
assertCulprits(b,["bob","charlie","dave","eve","fred"]) assertCulprits(b,["bob","charlie","dave","eve","fred"])
// 7th build, back to empty culprits // 7th build, back to empty culprits
scm.addChange().withAuthor("george") scm.addChange().withAuthor("george")
b = assertBuildStatus(Result.SUCCESS,p.scheduleBuild2(0).get()) b = j.assertBuildStatus(Result.SUCCESS,p.scheduleBuild2(0).get())
assertCulprits(b,["george"]) assertCulprits(b,["george"])
} }
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册