Skip to content

Commit

Permalink
[JENKINS-30742] Fixed possible NPE in AbstractProject.resolveForCLI()
Browse files Browse the repository at this point in the history
  • Loading branch information
pjanouse committed Oct 2, 2015
1 parent 41b8294 commit 37c5656
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
7 changes: 5 additions & 2 deletions core/src/main/java/hudson/model/AbstractProject.java
Expand Up @@ -2248,8 +2248,11 @@ public int compare(Integer o1, Integer o2) {
public static AbstractProject resolveForCLI(
@Argument(required=true,metaVar="NAME",usage="Job name") String name) throws CmdLineException {
AbstractProject item = Jenkins.getInstance().getItemByFullName(name, AbstractProject.class);
if (item==null)
throw new CmdLineException(null,Messages.AbstractItem_NoSuchJobExists(name,AbstractProject.findNearest(name).getFullName()));
if (item==null) {
AbstractProject project = AbstractProject.findNearest(name);
throw new CmdLineException(null, project == null ? Messages.AbstractItem_NoSuchJobExistsWithoutSuggestion(name)
: Messages.AbstractItem_NoSuchJobExists(name, project.getFullName()));
}
return item;
}

Expand Down
23 changes: 22 additions & 1 deletion test/src/test/groovy/hudson/model/AbstractProjectTest.groovy
Expand Up @@ -47,7 +47,8 @@ import hudson.triggers.Trigger
import hudson.triggers.TriggerDescriptor;
import hudson.util.StreamTaskListener;
import hudson.util.OneShotEvent
import jenkins.model.Jenkins;
import jenkins.model.Jenkins
import org.junit.Assert;
import org.jvnet.hudson.test.HudsonTestCase
import org.jvnet.hudson.test.Issue;
import org.jvnet.hudson.test.TestExtension;
Expand All @@ -56,6 +57,7 @@ import org.jvnet.hudson.test.recipes.PresetData.DataSet
import org.apache.commons.io.FileUtils;
import org.junit.Assume;
import org.jvnet.hudson.test.MockFolder
import org.kohsuke.args4j.CmdLineException

/**
* @author Kohsuke Kawaguchi
Expand Down Expand Up @@ -588,6 +590,25 @@ public class AbstractProjectTest extends HudsonTestCase {
assert project.triggers().size() == 1
}

@Issue("JENKINS-30742")
public void testResolveForCLI() {
try {
AbstractProject not_found = AbstractProject.resolveForCLI("never_created");
fail("Exception should occur before!");
} catch (CmdLineException e) {
assert e.getMessage().contentEquals("No such job \u2018never_created\u2019 exists.");
}

AbstractProject project = jenkins.createProject(FreeStyleProject.class, "never_created");
try {
AbstractProject not_found = AbstractProject.resolveForCLI("never_created1");
fail("Exception should occur before!");
} catch (CmdLineException e) {
assert e.getMessage().contentEquals("No such job \u2018never_created1\u2019 exists. Perhaps you meant \u2018never_created\u2019?")
}

}

static class MockBuildTriggerThrowsNPEOnStart<Item> extends Trigger {
@Override
public void start(hudson.model.Item project, boolean newInstance) { throw new NullPointerException(); }
Expand Down

0 comments on commit 37c5656

Please sign in to comment.