Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
xxadev
jenkins
提交
571de9c1
J
jenkins
项目概览
xxadev
/
jenkins
与 Fork 源项目一致
从无法访问的项目Fork
通知
3
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
J
jenkins
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
571de9c1
编写于
10月 07, 2013
作者:
J
Jesse Glick
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
JUnit 4; introduced GroovyJenkinsRule.
上级
ed03fbbc
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
102 addition
and
20 deletion
+102
-20
test/src/main/java/org/jvnet/hudson/test/GroovyHudsonTestCase.java
...main/java/org/jvnet/hudson/test/GroovyHudsonTestCase.java
+2
-0
test/src/main/java/org/jvnet/hudson/test/GroovyJenkinsRule.java
...rc/main/java/org/jvnet/hudson/test/GroovyJenkinsRule.java
+73
-0
test/src/test/groovy/hudson/model/AbstractBuildTest.groovy
test/src/test/groovy/hudson/model/AbstractBuildTest.groovy
+27
-20
未找到文件。
test/src/main/java/org/jvnet/hudson/test/GroovyHudsonTestCase.java
浏览文件 @
571de9c1
...
@@ -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.
...
...
test/src/main/java/org/jvnet/hudson/test/GroovyJenkinsRule.java
0 → 100644
浏览文件 @
571de9c1
/*
* 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
;
}
};
}
}
test/src/test/groovy/hudson/model/AbstractBuildTest.groovy
浏览文件 @
571de9c1
...
@@ -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.Groovy
HudsonTestCas
e
import
org.jvnet.hudson.test.Groovy
JenkinsRul
e
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
testR
awConsoleOutput
()
{
@Test
void
r
awConsoleOutput
()
{
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
testC
ulprits
()
{
@Test
void
c
ulprits
()
{
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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录