Skip to content

Commit

Permalink
Merge pull request #11 from MarkEWaite/JENKINS-20660
Browse files Browse the repository at this point in the history
Fix JENKINS-20660 - null pointer exception on first run after upgrade 0.8 to 0.9
  • Loading branch information
mambu committed Nov 28, 2013
2 parents 87601f9 + 4ea7f14 commit 5fa0853
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 5 deletions.
17 changes: 15 additions & 2 deletions pom.xml
Expand Up @@ -54,6 +54,19 @@
<url>http://repo.jenkins-ci.org/public/</url>
</pluginRepository>
</pluginRepositories>
</project>


<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
<version>1.3</version>
</dependency>
</dependencies>

</project>
6 changes: 3 additions & 3 deletions src/main/java/hudson/plugins/xshell/XShellBuilder.java
Expand Up @@ -74,7 +74,7 @@ public String getTimeAllocated() {
public XShellBuilder(final String commandLine, final Boolean executeFromWorkingDir, final String regexToKill, final String timeAllocated) {
this.commandLine = Util.fixEmptyAndTrim(commandLine);
this.executeFromWorkingDir = executeFromWorkingDir;
this.regexToKill = regexToKill;
this.regexToKill = regexToKill == null ? "" : regexToKill;
this.timeAllocated = timeAllocated;
}

Expand Down Expand Up @@ -119,7 +119,7 @@ public boolean perform(final AbstractBuild<?, ?> build, final Launcher launcher,
LOG.log(Level.FINE, "Command line: " + args.toStringWithQuote());
LOG.log(Level.FINE, "Working directory: " + build.getWorkspace());

Pattern r = Pattern.compile(this.regexToKill);
Pattern r = Pattern.compile(this.regexToKill == null ? "" : this.regexToKill);
Long timeAllowed;

try{
Expand All @@ -146,7 +146,7 @@ public boolean perform(final AbstractBuild<?, ?> build, final Launcher launcher,
listener.getLogger().print(s);
listener.getLogger().flush();

if ((this.regexToKill.length() > 0) && (r.matcher(s).find())){
if ((this.regexToKill != null) && (this.regexToKill.length() > 0) && (r.matcher(s).find())){
LOG.log(Level.FINEST, "Matched failure in log");
child.kill();
listener.getLogger().println("Matched <" + this.regexToKill +"> in output. Terminated");
Expand Down
67 changes: 67 additions & 0 deletions src/test/java/hudson/plugins/xshell/UpgradeTest.java
@@ -0,0 +1,67 @@
package hudson.plugins.xshell;

import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.not;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;

import java.io.IOException;
import java.util.concurrent.ExecutionException;

import org.apache.commons.io.FileUtils;

import org.junit.Rule;
import org.junit.Test;

import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

import org.jvnet.hudson.test.Bug;
import org.jvnet.hudson.test.HudsonTestCase;
import org.jvnet.hudson.test.JenkinsRule;

import hudson.model.FreeStyleBuild;
import hudson.model.FreeStyleProject;

import hudson.plugins.xshell.XShellBuilder;

/**
* Test upgrade from one version of XShell to another.
*
* @author MarkEWaite
*/
@RunWith(JUnit4.class)
public class UpgradeTest extends HudsonTestCase {
@Rule public JenkinsRule j = new JenkinsRule();

/**
* XShell upgrade from 0.8 to 0.9 reported a null pointer
* exception when the job was first executed after the upgrade.
* The null pointer exception was in the constructor for a regular
* expression.
*
* Creating a new job with a null pointer for the regular
* expression to kill the job shows the same bug, so this test
* creates and executes a new job with a null regexToKill.
*/
@Test @Bug(20660)
public void testXShellBuilderNullAsRegExToKill() throws IOException, InterruptedException, ExecutionException {

FreeStyleProject project = j.createFreeStyleProject();

final String arguments = "hello world";

final boolean execFromWorkingDir = false;
final String regexToKill = null;
final String timeAllocated = null;

project.getBuildersList().add(new XShellBuilder("echo " + arguments, execFromWorkingDir, regexToKill, timeAllocated));

FreeStyleBuild build = project.scheduleBuild2(0).get();
String s = FileUtils.readFileToString(build.getLogFile());

assertThat(s, not(containsString("java.lang.NullPointerException")));
assertThat(s, containsString(arguments));
assertThat(s, containsString("SUCCESS"));
}
}

0 comments on commit 5fa0853

Please sign in to comment.