Skip to content
Permalink
Browse files
Merge pull request #823 from olivergondza/jenkins18393
[FIXED JENKINS-18393] Do not list any ItemGroup in list-jobs CLI command
  • Loading branch information
olivergondza committed Oct 14, 2013
2 parents ec33380 + 6af228e commit f8ec806d74ae31b96e904a1572dfff389c94632d
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 12 deletions.
@@ -24,16 +24,13 @@
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.Items;
import hudson.model.TopLevelItem;
import hudson.model.ViewGroup;
import hudson.model.View;
import hudson.Extension;
import jenkins.model.ModifiableTopLevelItemGroup;
import jenkins.model.Jenkins;
import org.kohsuke.args4j.Argument;

@@ -68,9 +65,8 @@ protected int run() throws Exception {
final Item item = h.getItemByFullName(name);

// If item group was found use it's jobs.
if (item instanceof ItemGroup) {
ItemGroup itemGroup = (ItemGroup) item;
jobs = itemGroup.getItems();
if (item instanceof ModifiableTopLevelItemGroup) {
jobs = Items.getAllItems((ModifiableTopLevelItemGroup) item, TopLevelItem.class);
}
// No view and no item group with the given name found.
else {
@@ -2,30 +2,47 @@

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.containsString;
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.matrix.MatrixConfiguration;
import hudson.matrix.MatrixProject;
import hudson.model.AbstractProject;
import hudson.model.Item;
import hudson.model.ItemGroup;
import hudson.model.TopLevelItem;
import hudson.model.TopLevelItemDescriptor;
import hudson.model.ViewGroup;
import hudson.model.ViewTest.CompositeView;
import hudson.model.View;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;

import javax.annotation.CheckForNull;
import javax.servlet.ServletException;

import jenkins.model.Jenkins;
import jenkins.model.ModifiableTopLevelItemGroup;

import org.hamcrest.Description;
import org.hamcrest.TypeSafeMatcher;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.jvnet.hudson.test.Bug;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;
import org.mockito.Mockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor;
@@ -53,14 +70,59 @@ public void setUp() {
}

@Test
public void getNullForNonexistingName() throws Exception {
public void failForNonexistingName() throws Exception {

when(jenkins.getView(null)).thenReturn(null);
when(jenkins.getItemByFullName(null)).thenReturn(null);
when(jenkins.getView("NoSuchViewOrItemGroup")).thenReturn(null);
when(jenkins.getItemByFullName("NoSuchViewOrItemGroup")).thenReturn(null);

assertThat(runWith("NoSuchViewOrItemGroup"), equalTo(-1));
assertThat(stdout, is(empty()));
assertThat(stderr, is(not(empty())));
assertThat(stderr.toString(), containsString("No view or item group with the given name found"));
}

@Test
@Bug(18393)
public void failForMatrixProject() throws Exception {

final MatrixProject matrix = mock(MatrixProject.class);
final MatrixConfiguration config = mock(MatrixConfiguration.class);
when(matrix.getItems()).thenReturn(Arrays.asList(config));

when(jenkins.getView("MatrixJob")).thenReturn(null);
when(jenkins.getItemByFullName("MatrixJob")).thenReturn(matrix);

assertThat(runWith("MatrixJob"), equalTo(-1));
assertThat(stdout, is(empty()));
assertThat(stderr.toString(), containsString("No view or item group with the given name found"));
}

@Test
public void getAllJobsFromFolders() throws Exception {

abstract class Folder implements ModifiableTopLevelItemGroup, TopLevelItem {
}

final Folder folder = mock(Folder.class);
final Folder nestedFolder = mock(Folder.class);
when(folder.getDisplayName()).thenReturn("Folder");
when(nestedFolder.getDisplayName()).thenReturn("NestedFolder");

final TopLevelItem job = job("job");
final TopLevelItem nestedJob = job("nestedJob");
when(job.hasPermission(Item.READ)).thenReturn(true);
when(nestedJob.hasPermission(Item.READ)).thenReturn(true);
when(job.getRelativeNameFrom((ItemGroup<TopLevelItem>) folder)).thenReturn("job");
when(nestedJob.getRelativeNameFrom((ItemGroup<TopLevelItem>) folder)).thenReturn("nestedJob");

when(folder.getItems()).thenReturn(Arrays.asList(nestedFolder, job));
when(nestedFolder.getItems()).thenReturn(Arrays.asList(nestedJob));

when(jenkins.getView("OuterFolder")).thenReturn(null);
when(jenkins.getItemByFullName("OuterFolder")).thenReturn(folder);

assertThat(runWith("OuterFolder"), equalTo(0));
assertThat(stdout, listsJobs("job", "nestedJob"));
assertThat(stderr, is(empty()));
}

@Test

0 comments on commit f8ec806

Please sign in to comment.