diff --git a/core/src/main/java/hudson/cli/ListJobsCommand.java b/core/src/main/java/hudson/cli/ListJobsCommand.java index 9e8bdda4bd74c7ec744fc1f2c10a64e2b15593cf..809bd328b4db45aae272d50056e19b0f647de85f 100644 --- a/core/src/main/java/hudson/cli/ListJobsCommand.java +++ b/core/src/main/java/hudson/cli/ListJobsCommand.java @@ -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 getViewItems(View view) { + + final Collection jobs = new LinkedHashSet( + view.getItems() + ); + + if (view instanceof ViewGroup) { + + for(View subview: ((ViewGroup) view).getViews()) { + + jobs.addAll(getViewItems(subview)); + } + } + return jobs; + } } diff --git a/core/src/test/java/hudson/cli/ListJobsCommandTest.java b/core/src/test/java/hudson/cli/ListJobsCommandTest.java index 66362fe2d2263ecc718276ec06607f911c67cb03..490de8d94fa0705abf70eff78c6a88e3a11df923 100644 --- a/core/src/test/java/hudson/cli/ListJobsCommandTest.java +++ b/core/src/test/java/hudson/cli/ListJobsCommandTest.java @@ -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 BaseMatcher empty() { + private TypeSafeMatcher empty() { - return new BaseMatcher() { + return new TypeSafeMatcher() { @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 listsJobs(final String... expected) { + private TypeSafeMatcher listsJobs(final String... expected) { - return new BaseMatcher() { + return new TypeSafeMatcher() { @Override - public boolean matches(Object item) { - - if (!(item instanceof ByteArrayOutputStream)) return false; - - final ByteArrayOutputStream actual = (ByteArrayOutputStream) item; + protected boolean matchesSafely(ByteArrayOutputStream item) { final HashSet jobs = new HashSet( - Arrays.asList(actual.toString().split("\n")) + Arrays.asList(item.toString().split("\n")) ); return new HashSet(Arrays.asList(expected)).equals(jobs);