Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
野花太放肆
jenkins
提交
6b79233f
J
jenkins
项目概览
野花太放肆
/
jenkins
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
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,发现更多精彩内容 >>
提交
6b79233f
编写于
6月 16, 2013
作者:
C
Christoph Kutzinski
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #793 from olivergondza/list_jobs_recursively_fix
List Jobs recursively in hudson.cli.ListJobsCommand
上级
8678aceb
67827c7e
变更
4
显示空白变更内容
内联
并排
Showing
4 changed file
with
256 addition
and
1 deletion
+256
-1
core/src/main/java/hudson/cli/ListJobsCommand.java
core/src/main/java/hudson/cli/ListJobsCommand.java
+5
-1
core/src/main/java/hudson/model/View.java
core/src/main/java/hudson/model/View.java
+22
-0
core/src/test/java/hudson/cli/ListJobsCommandTest.java
core/src/test/java/hudson/cli/ListJobsCommandTest.java
+187
-0
core/src/test/java/hudson/model/ViewTest.java
core/src/test/java/hudson/model/ViewTest.java
+42
-0
未找到文件。
core/src/main/java/hudson/cli/ListJobsCommand.java
浏览文件 @
6b79233f
...
...
@@ -24,10 +24,14 @@
package
hudson.cli
;
import
java.util.Collection
;
import
java.util.LinkedHashSet
;
import
java.util.Collections
;
import
hudson.model.Item
;
import
hudson.model.ItemGroup
;
import
hudson.model.TopLevelItem
;
import
hudson.model.ViewGroup
;
import
hudson.model.View
;
import
hudson.Extension
;
import
jenkins.model.Jenkins
;
...
...
@@ -57,7 +61,7 @@ public class ListJobsCommand extends CLICommand {
View
view
=
h
.
getView
(
name
);
if
(
view
!=
null
)
{
jobs
=
view
.
getItems
();
jobs
=
view
.
get
All
Items
();
}
// If no view was found, try with an item group.
else
{
...
...
core/src/main/java/hudson/model/View.java
浏览文件 @
6b79233f
...
...
@@ -93,6 +93,7 @@ import java.util.Comparator;
import
java.util.GregorianCalendar
;
import
java.util.HashMap
;
import
java.util.HashSet
;
import
java.util.LinkedHashSet
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.Set
;
...
...
@@ -176,6 +177,27 @@ public abstract class View extends AbstractModelObject implements AccessControll
@Exported
(
name
=
"jobs"
)
public
abstract
Collection
<
TopLevelItem
>
getItems
();
/**
* Gets all the items recursively contained in this collection in a read-only view.
* @since 1.520
*/
public
Collection
<
TopLevelItem
>
getAllItems
()
{
final
Collection
<
TopLevelItem
>
items
=
new
LinkedHashSet
<
TopLevelItem
>(
getItems
()
);
if
(
this
instanceof
ViewGroup
)
{
for
(
final
View
view:
((
ViewGroup
)
this
).
getViews
())
{
items
.
addAll
(
view
.
getAllItems
());
}
}
return
Collections
.
unmodifiableCollection
(
items
);
}
/**
* Gets the {@link TopLevelItem} of the given name.
*/
...
...
core/src/test/java/hudson/cli/ListJobsCommandTest.java
0 → 100644
浏览文件 @
6b79233f
package
hudson.cli
;
import
static
org
.
hamcrest
.
CoreMatchers
.
equalTo
;
import
static
org
.
hamcrest
.
CoreMatchers
.
is
;
import
static
org
.
hamcrest
.
CoreMatchers
.
not
;
import
static
org
.
hamcrest
.
MatcherAssert
.
assertThat
;
import
static
org
.
powermock
.
api
.
mockito
.
PowerMockito
.
mock
;
import
static
org
.
powermock
.
api
.
mockito
.
PowerMockito
.
mockStatic
;
import
static
org
.
powermock
.
api
.
mockito
.
PowerMockito
.
when
;
import
hudson.model.TopLevelItem
;
import
hudson.model.ViewGroup
;
import
hudson.model.ViewTest.CompositeView
;
import
hudson.model.View
;
import
java.io.ByteArrayOutputStream
;
import
java.io.PrintStream
;
import
java.util.Arrays
;
import
java.util.Collection
;
import
java.util.HashSet
;
import
java.util.List
;
import
jenkins.model.Jenkins
;
import
org.hamcrest.Description
;
import
org.hamcrest.TypeSafeMatcher
;
import
org.junit.Before
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.mockito.Mockito
;
import
org.powermock.core.classloader.annotations.PrepareForTest
;
import
org.powermock.core.classloader.annotations.SuppressStaticInitializationFor
;
import
org.powermock.modules.junit4.PowerMockRunner
;
@RunWith
(
PowerMockRunner
.
class
)
@PrepareForTest
(
Jenkins
.
class
)
@SuppressStaticInitializationFor
(
"hudson.cli.CLICommand"
)
public
class
ListJobsCommandTest
{
private
/*final*/
Jenkins
jenkins
;
private
/*final*/
ListJobsCommand
command
;
private
final
ByteArrayOutputStream
stdout
=
new
ByteArrayOutputStream
();
private
final
ByteArrayOutputStream
stderr
=
new
ByteArrayOutputStream
();
@Before
public
void
setUp
()
{
jenkins
=
mock
(
Jenkins
.
class
);
mockStatic
(
Jenkins
.
class
);
when
(
Jenkins
.
getInstance
()).
thenReturn
(
jenkins
);
command
=
mock
(
ListJobsCommand
.
class
,
Mockito
.
CALLS_REAL_METHODS
);
command
.
stdout
=
new
PrintStream
(
stdout
);
command
.
stderr
=
new
PrintStream
(
stderr
);
}
@Test
public
void
getNullForNonexistingName
()
throws
Exception
{
when
(
jenkins
.
getView
(
null
)).
thenReturn
(
null
);
when
(
jenkins
.
getItemByFullName
(
null
)).
thenReturn
(
null
);
// TODO: One would expect -1 here
assertThat
(
runWith
(
"NoSuchViewOrItemGroup"
),
equalTo
(
0
));
assertThat
(
stdout
,
is
(
empty
()));
assertThat
(
stderr
,
is
(
not
(
empty
())));
}
@Test
public
void
getAllJobsForEmptyName
()
throws
Exception
{
final
Collection
<
TopLevelItem
>
jenkinsJobs
=
Arrays
.
asList
(
job
(
"some-job"
),
job
(
"some-other-job"
)
);
when
(
jenkins
.
getItems
()).
thenReturn
((
List
<
TopLevelItem
>)
jenkinsJobs
);
assertThat
(
runWith
(
null
),
equalTo
(
0
));
assertThat
(
stderr
,
is
(
empty
()));
assertThat
(
stdout
,
listsJobs
(
"some-job"
,
"some-other-job"
));
}
@Test
public
void
getJobsFromView
()
throws
Exception
{
final
Collection
<
TopLevelItem
>
viewJobs
=
Arrays
.
asList
(
job
(
"some-job"
),
job
(
"some-other-job"
)
);
final
View
customView
=
view
();
when
(
customView
.
getItems
()).
thenReturn
(
viewJobs
);
when
(
jenkins
.
getView
(
"CustomView"
)).
thenReturn
(
customView
);
assertThat
(
runWith
(
"CustomView"
),
equalTo
(
0
));
assertThat
(
stderr
,
is
(
empty
()));
assertThat
(
stdout
,
listsJobs
(
"some-job"
,
"some-other-job"
));
}
@Test
public
void
getJobsRecursivelyFromViewGroup
()
throws
Exception
{
final
CompositeView
rootView
=
mock
(
CompositeView
.
class
);
when
(
rootView
.
getAllItems
()).
thenCallRealMethod
();
final
View
leftView
=
view
();
final
View
rightView
=
view
();
final
TopLevelItem
rootJob
=
job
(
"rootJob"
);
final
TopLevelItem
leftJob
=
job
(
"leftJob"
);
final
TopLevelItem
rightJob
=
job
(
"rightJob"
);
final
TopLevelItem
sharedJob
=
job
(
"sharedJob"
);
when
(
rootView
.
getViews
()).
thenReturn
(
Arrays
.
asList
(
leftView
,
rightView
));
when
(
rootView
.
getItems
()).
thenReturn
(
Arrays
.
asList
(
rootJob
,
sharedJob
));
when
(
leftView
.
getItems
()).
thenReturn
(
Arrays
.
asList
(
leftJob
,
sharedJob
));
when
(
rightView
.
getItems
()).
thenReturn
(
Arrays
.
asList
(
rightJob
));
when
(
jenkins
.
getView
(
"Root"
)).
thenReturn
(
rootView
);
assertThat
(
runWith
(
"Root"
),
equalTo
(
0
));
assertThat
(
stderr
,
is
(
empty
()));
assertThat
(
stdout
,
listsJobs
(
"rootJob"
,
"leftJob"
,
"rightJob"
,
"sharedJob"
));
}
private
View
view
()
{
final
View
view
=
mock
(
View
.
class
);
when
(
view
.
getAllItems
()).
thenCallRealMethod
();
return
view
;
}
private
TopLevelItem
job
(
final
String
name
)
{
final
TopLevelItem
item
=
mock
(
TopLevelItem
.
class
);
when
(
item
.
getDisplayName
()).
thenReturn
(
name
);
return
item
;
}
private
int
runWith
(
final
String
name
)
throws
Exception
{
command
.
name
=
name
;
return
command
.
run
();
}
private
TypeSafeMatcher
<
ByteArrayOutputStream
>
empty
()
{
return
new
TypeSafeMatcher
<
ByteArrayOutputStream
>()
{
@Override
protected
boolean
matchesSafely
(
ByteArrayOutputStream
item
)
{
return
item
.
toString
().
isEmpty
();
}
@Override
public
void
describeTo
(
Description
description
)
{
description
.
appendText
(
"Empty output"
);
}
};
}
private
TypeSafeMatcher
<
ByteArrayOutputStream
>
listsJobs
(
final
String
...
expected
)
{
return
new
TypeSafeMatcher
<
ByteArrayOutputStream
>()
{
@Override
protected
boolean
matchesSafely
(
ByteArrayOutputStream
item
)
{
final
HashSet
<
String
>
jobs
=
new
HashSet
<
String
>(
Arrays
.
asList
(
item
.
toString
().
split
(
"\n"
))
);
return
new
HashSet
<
String
>(
Arrays
.
asList
(
expected
)).
equals
(
jobs
);
}
@Override
public
void
describeTo
(
Description
description
)
{
description
.
appendText
(
"Job listing of "
+
Arrays
.
toString
(
expected
));
}
};
}
}
core/src/test/java/hudson/model/ViewTest.java
浏览文件 @
6b79233f
...
...
@@ -5,6 +5,7 @@ import hudson.search.SearchIndexBuilder;
import
hudson.search.SearchItem
;
import
java.util.ArrayList
;
import
java.util.Arrays
;
import
java.util.Collection
;
import
java.util.List
;
...
...
@@ -65,4 +66,45 @@ public class ViewTest {
Assert
.
assertEquals
(
actual
.
getSearchName
(),
item2
.
getDisplayName
());
Assert
.
assertEquals
(
actual
.
getSearchUrl
(),
item2
.
getSearchUrl
());
}
/*
* Get all items recursively when View implements ViewGroup at the same time
*/
@Test
public
void
getAllItems
()
throws
Exception
{
final
CompositeView
rootView
=
Mockito
.
mock
(
CompositeView
.
class
);
final
View
leftView
=
Mockito
.
mock
(
View
.
class
);
final
View
rightView
=
Mockito
.
mock
(
View
.
class
);
Mockito
.
when
(
rootView
.
getAllItems
()).
thenCallRealMethod
();
Mockito
.
when
(
leftView
.
getAllItems
()).
thenCallRealMethod
();
Mockito
.
when
(
rightView
.
getAllItems
()).
thenCallRealMethod
();
final
TopLevelItem
rootJob
=
Mockito
.
mock
(
TopLevelItem
.
class
);
final
TopLevelItem
leftJob
=
Mockito
.
mock
(
TopLevelItem
.
class
);
final
TopLevelItem
rightJob
=
Mockito
.
mock
(
TopLevelItem
.
class
);
final
TopLevelItem
sharedJob
=
Mockito
.
mock
(
TopLevelItem
.
class
);
Mockito
.
when
(
rootJob
.
getDisplayName
()).
thenReturn
(
"rootJob"
);
Mockito
.
when
(
leftJob
.
getDisplayName
()).
thenReturn
(
"leftJob"
);
Mockito
.
when
(
rightJob
.
getDisplayName
()).
thenReturn
(
"rightJob"
);
Mockito
.
when
(
sharedJob
.
getDisplayName
()).
thenReturn
(
"sharedJob"
);
Mockito
.
when
(
rootView
.
getViews
()).
thenReturn
(
Arrays
.
asList
(
leftView
,
rightView
));
Mockito
.
when
(
rootView
.
getItems
()).
thenReturn
(
Arrays
.
asList
(
rootJob
,
sharedJob
));
Mockito
.
when
(
leftView
.
getItems
()).
thenReturn
(
Arrays
.
asList
(
leftJob
,
sharedJob
));
Mockito
.
when
(
rightView
.
getItems
()).
thenReturn
(
Arrays
.
asList
(
rightJob
));
final
TopLevelItem
[]
expected
=
new
TopLevelItem
[]
{
rootJob
,
sharedJob
,
leftJob
,
rightJob
};
Assert
.
assertArrayEquals
(
expected
,
rootView
.
getAllItems
().
toArray
());
}
public
static
abstract
class
CompositeView
extends
View
implements
ViewGroup
{
protected
CompositeView
(
final
String
name
)
{
super
(
name
);
}
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录