提交 bdea18f5 编写于 作者: O Oliver Gondža

List Jobs recursively in hudson.cli.ListJobsCommand

上级 5457d1e8
......@@ -24,12 +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;
......@@ -59,7 +61,7 @@ public class ListJobsCommand extends CLICommand {
View view = h.getView(name);
if (view != null) {
jobs = view.getItems();
jobs = getViewItems(view);
}
// If no view was found, try with an item group.
else {
......@@ -89,4 +91,20 @@ public class ListJobsCommand extends CLICommand {
return 0;
}
private Collection<TopLevelItem> getViewItems(View view) {
final Collection<TopLevelItem> jobs = new LinkedHashSet<TopLevelItem>(
view.getItems()
);
if (view instanceof ViewGroup) {
for(View subview: ((ViewGroup) view).getViews()) {
jobs.addAll(getViewItems(subview));
}
}
return jobs;
}
}
......@@ -20,8 +20,8 @@ import java.util.List;
import jenkins.model.Jenkins;
import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import org.hamcrest.TypeSafeMatcher;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
......@@ -94,6 +94,30 @@ public class ListJobsCommandTest {
assertThat(stdout, listsJobs("some-job", "some-other-job"));
}
@Test
public void getJobsRecursivelyFromViewGroup() throws Exception {
final CompositeView rootView = mock(CompositeView.class);
final View leftView = mock(View.class);
final View rightView = mock(View.class);
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 TopLevelItem job(final String name) {
final TopLevelItem item = mock(TopLevelItem.class);
......@@ -110,16 +134,14 @@ public class ListJobsCommandTest {
return command.run();
}
private <T> BaseMatcher<ByteArrayOutputStream> empty() {
private TypeSafeMatcher<ByteArrayOutputStream> empty() {
return new BaseMatcher<ByteArrayOutputStream>() {
return new TypeSafeMatcher<ByteArrayOutputStream>() {
@Override
public boolean matches(Object item) {
protected boolean matchesSafely(ByteArrayOutputStream item) {
if (!(item instanceof ByteArrayOutputStream)) throw new IllegalArgumentException();
return ((ByteArrayOutputStream) item).toString().isEmpty();
return item.toString().isEmpty();
}
@Override
......@@ -130,19 +152,15 @@ public class ListJobsCommandTest {
};
}
private BaseMatcher<ByteArrayOutputStream> listsJobs(final String... expected) {
private TypeSafeMatcher<ByteArrayOutputStream> listsJobs(final String... expected) {
return new BaseMatcher<ByteArrayOutputStream>() {
return new TypeSafeMatcher<ByteArrayOutputStream>() {
@Override
public boolean matches(Object item) {
if (!(item instanceof ByteArrayOutputStream)) return false;
final ByteArrayOutputStream actual = (ByteArrayOutputStream) item;
protected boolean matchesSafely(ByteArrayOutputStream item) {
final HashSet<String> jobs = new HashSet<String>(
Arrays.asList(actual.toString().split("\n"))
Arrays.asList(item.toString().split("\n"))
);
return new HashSet<String>(Arrays.asList(expected)).equals(jobs);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册