Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
xxadev
jenkins
提交
34850200
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,发现更多精彩内容 >>
提交
34850200
编写于
11月 06, 2014
作者:
O
Oliver Gondža
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Introduce FormValidation#aggregate
上级
12ff0b24
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
75 addition
and
1 deletion
+75
-1
core/src/main/java/hudson/util/FormValidation.java
core/src/main/java/hudson/util/FormValidation.java
+27
-1
core/src/test/java/hudson/util/FormValidationTest.java
core/src/test/java/hudson/util/FormValidationTest.java
+48
-0
未找到文件。
core/src/main/java/hudson/util/FormValidation.java
浏览文件 @
34850200
...
...
@@ -34,9 +34,9 @@ import hudson.model.AbstractBuild;
import
hudson.model.BuildListener
;
import
hudson.model.Descriptor
;
import
hudson.tasks.Builder
;
import
hudson.util.ReflectionUtils.Parameter
;
import
jenkins.model.Jenkins
;
import
org.kohsuke.stapler.HttpResponse
;
import
org.kohsuke.stapler.QueryParameter
;
import
org.kohsuke.stapler.StaplerRequest
;
...
...
@@ -44,7 +44,9 @@ import org.kohsuke.stapler.StaplerResponse;
import
org.kohsuke.stapler.Stapler
;
import
org.springframework.util.StringUtils
;
import
javax.annotation.Nonnull
;
import
javax.servlet.ServletException
;
import
java.io.File
;
import
java.io.IOException
;
import
java.io.BufferedReader
;
...
...
@@ -53,6 +55,7 @@ import java.lang.reflect.Method;
import
java.net.URL
;
import
java.net.URLConnection
;
import
java.util.ArrayList
;
import
java.util.Collection
;
import
java.util.List
;
import
java.util.Locale
;
...
...
@@ -209,7 +212,30 @@ public abstract class FormValidation extends IOException implements HttpResponse
return
warning
(
e
,
String
.
format
(
format
,
args
));
}
/**
* Aggregate multiple validations into one.
*
* @return Validation of the least successful kind aggregating all child messages.
* @since TODO
*/
public
static
@Nonnull
FormValidation
aggregate
(
@Nonnull
Collection
<
FormValidation
>
validations
)
{
if
(
validations
==
null
||
validations
.
isEmpty
())
return
FormValidation
.
ok
();
if
(
validations
.
size
()
==
1
)
return
validations
.
iterator
().
next
();
final
StringBuilder
sb
=
new
StringBuilder
(
"<ul style='list-style-type: none; padding-left: 0; margin: 0'>"
);
FormValidation
.
Kind
worst
=
Kind
.
OK
;
for
(
FormValidation
validation:
validations
)
{
sb
.
append
(
"<li>"
).
append
(
validation
.
renderHtml
()).
append
(
"</li>"
);
if
(
validation
.
kind
.
ordinal
()
>
worst
.
ordinal
())
{
worst
=
validation
.
kind
;
}
}
sb
.
append
(
"</ul>"
);
return
respond
(
worst
,
sb
.
toString
());
}
/**
* Sends out an HTML fragment that indicates an error.
...
...
core/src/test/java/hudson/util/FormValidationTest.java
浏览文件 @
34850200
...
...
@@ -25,6 +25,8 @@ package hudson.util;
import
static
org
.
junit
.
Assert
.*;
import
java.util.Arrays
;
import
org.junit.Test
;
/**
...
...
@@ -57,4 +59,50 @@ public class FormValidationTest {
public
void
testMessage
()
{
assertEquals
(
"test msg"
,
FormValidation
.
errorWithMarkup
(
"test msg"
).
getMessage
());
}
@Test
public
void
aggregateZeroValidations
()
{
assertEquals
(
FormValidation
.
ok
(),
aggregate
());
}
@Test
public
void
aggregateSingleValidations
()
{
FormValidation
ok
=
FormValidation
.
ok
();
FormValidation
warning
=
FormValidation
.
warning
(
""
);
FormValidation
error
=
FormValidation
.
error
(
""
);
assertEquals
(
ok
,
aggregate
(
ok
));
assertEquals
(
warning
,
aggregate
(
warning
));
assertEquals
(
error
,
aggregate
(
error
));
}
@Test
public
void
aggregateSeveralValidations
()
{
FormValidation
ok
=
FormValidation
.
ok
(
"ok_message"
);
FormValidation
warning
=
FormValidation
.
warning
(
"warning_message"
);
FormValidation
error
=
FormValidation
.
error
(
"error_message"
);
final
FormValidation
ok_ok
=
aggregate
(
ok
,
ok
);
assertEquals
(
FormValidation
.
Kind
.
OK
,
ok_ok
.
kind
);
assertTrue
(
ok_ok
.
renderHtml
().
contains
(
ok
.
getMessage
()));
final
FormValidation
ok_warning
=
aggregate
(
ok
,
warning
);
assertEquals
(
FormValidation
.
Kind
.
WARNING
,
ok_warning
.
kind
);
assertTrue
(
ok_warning
.
renderHtml
().
contains
(
ok
.
getMessage
()));
assertTrue
(
ok_warning
.
renderHtml
().
contains
(
warning
.
getMessage
()));
final
FormValidation
ok_error
=
aggregate
(
ok
,
error
);
assertEquals
(
FormValidation
.
Kind
.
ERROR
,
ok_error
.
kind
);
assertTrue
(
ok_error
.
renderHtml
().
contains
(
ok
.
getMessage
()));
assertTrue
(
ok_error
.
renderHtml
().
contains
(
error
.
getMessage
()));
final
FormValidation
warninig_error
=
aggregate
(
warning
,
error
);
assertEquals
(
FormValidation
.
Kind
.
ERROR
,
warninig_error
.
kind
);
assertTrue
(
warninig_error
.
renderHtml
().
contains
(
error
.
getMessage
()));
assertTrue
(
warninig_error
.
renderHtml
().
contains
(
warning
.
getMessage
()));
}
private
FormValidation
aggregate
(
FormValidation
...
fvs
)
{
return
FormValidation
.
aggregate
(
Arrays
.
asList
(
fvs
));
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录