Permalink
Browse files

Fixed: JENKINS-12046 - TestNG shouldn't look for result files if buil…

…d was aborted (again!)

Also added a test for this.
  • Loading branch information...
1 parent b71d891 commit fa2df404a99ef2fd27ca48ef2fb3bac5337f539c @nullin nullin committed Jan 5, 2013
Showing with 92 additions and 64 deletions.
  1. +1 −0 README
  2. +6 −0 pom.xml
  3. +10 −18 src/main/java/hudson/plugins/testng/Publisher.java
  4. +75 −46 src/test/java/hudson/plugins/testng/PublisherTest.java
View
1 README
@@ -20,6 +20,7 @@ Release Notes
* JENKINS-9838 - Action --> HealthReportingAction
* Upped compatible Jenkins version to v1.447
* Fixed: JENKINS-15119 - Duration sorting is incorrect in TestNG report
+* Fixed: JENKINS-12046 - TestNG shouldn't look for result files if build was aborted (again!)
### v0.32
* Fixed: JENKINS-12648 - Results should be reported for failed/aborted builds as well (Reverted fix for JENKINS-12046 in v0.30)
View
@@ -17,6 +17,12 @@
<groupId>org.jenkins-ci.main</groupId>
<artifactId>maven-plugin</artifactId>
</dependency>
+ <dependency>
+ <groupId>org.mockito</groupId>
+ <artifactId>mockito-core</artifactId>
+ <version>1.8.5</version>
+ <scope>test</scope>
+ </dependency>
</dependencies>
<issueManagement>
@@ -3,15 +3,14 @@
import hudson.Extension;
import hudson.FilePath;
import hudson.Launcher;
-import hudson.model.Action;
-import hudson.model.BuildListener;
-import hudson.model.Result;
-import hudson.model.AbstractBuild;
-import hudson.model.AbstractProject;
+import hudson.model.*;
import hudson.plugins.testng.results.TestResults;
import hudson.tasks.BuildStepDescriptor;
import hudson.tasks.BuildStepMonitor;
import hudson.tasks.Recorder;
+import net.sf.json.JSONObject;
+import org.kohsuke.stapler.DataBoundConstructor;
+import org.kohsuke.stapler.StaplerRequest;
import java.io.File;
import java.io.IOException;
@@ -21,11 +20,6 @@
import java.util.Collection;
import java.util.List;
-import net.sf.json.JSONObject;
-
-import org.kohsuke.stapler.DataBoundConstructor;
-import org.kohsuke.stapler.StaplerRequest;
-
/**
* This class defines a @Publisher and @Extension
*
@@ -84,6 +78,12 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher,
throws InterruptedException, IOException {
PrintStream logger = listener.getLogger();
+
+ if (build.getResult().equals(Result.ABORTED)) {
+ logger.println("Build Aborted. Not looking for any TestNG results.");
+ return true;
+ }
+
logger.println("TestNG Reports Processing: START");
logger.println("Looking for TestNG results report in workspace using pattern: "
+ reportFilenamePattern);
@@ -230,14 +230,6 @@ static boolean saveReports(FilePath testngDir, FilePath[] paths, PrintStream log
return true;
}
- /**
- * {@inheritDoc}
- */
- @Override
- public boolean prebuild(AbstractBuild<?, ?> build, BuildListener listener) {
- return true;
- }
-
public static final class DescriptorImpl extends BuildStepDescriptor<hudson.tasks.Publisher> {
/**
@@ -1,57 +1,86 @@
package hudson.plugins.testng;
import hudson.FilePath;
+import hudson.Launcher;
+import hudson.model.AbstractBuild;
+import hudson.model.BuildListener;
+import hudson.model.Result;
+import junit.framework.Assert;
+import org.junit.Test;
+import org.jvnet.hudson.test.HudsonTestCase;
+import java.io.ByteArrayOutputStream;
import java.io.File;
+import java.io.PrintStream;
-import junit.framework.Assert;
-import junit.framework.TestCase;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
/**
* @autor nullin
*/
-public class PublisherTest extends TestCase
-{
- public void testLocateReports() throws Exception
- {
- // Create a temporary workspace in the system
- File w = File.createTempFile("workspace", ".test");
- w.delete();
- w.mkdir();
- w.deleteOnExit();
- FilePath workspace = new FilePath(w);
- // Create 4 files in the workspace
- File f1 = File.createTempFile("testng-results", ".xml", w);
- f1.deleteOnExit();
- File f2 = File.createTempFile("anyname", ".xml", w);
- f2.deleteOnExit();
- File f3 = File.createTempFile("testng-results", ".xml", w);
- f3.deleteOnExit();
- File f4 = File.createTempFile("anyname", ".xml", w);
- f4.deleteOnExit();
- // Create a folder and move there 2 files
- File d1 = new File(workspace.child("subdir").getRemote());
- d1.mkdir();
- d1.deleteOnExit();
- File f5 = new File(workspace.child(d1.getName()).child(f3.getName()).getRemote());
- File f6 = new File(workspace.child(d1.getName()).child(f4.getName()).getRemote());
- f3.renameTo(f5);
- f4.renameTo(f6);
- f5.deleteOnExit();
- f6.deleteOnExit();
- // Look for files in the entire workspace recursively without providing
- // the includes parameter
- FilePath[] reports = Publisher.locateReports(workspace, "**/testng*.xml");
- Assert.assertEquals(2, reports.length);
- // Generate a includes string and look for files
- String includes = f1.getName() + "; " + f2.getName() + "; " + d1.getName();
- reports = Publisher.locateReports(workspace, includes);
- Assert.assertEquals(3, reports.length);
- // Save files in local workspace
- FilePath local = workspace.child("coverage_localfolder");
- boolean saved = Publisher.saveReports(local, reports, System.out);
- Assert.assertTrue(saved);
- Assert.assertEquals(3, local.list().size());
- local.deleteRecursive();
- }
+public class PublisherTest extends HudsonTestCase {
+
+ @Test
+ public void testLocateReports() throws Exception {
+ // Create a temporary workspace in the system
+ File w = File.createTempFile("workspace", ".test");
+ w.delete();
+ w.mkdir();
+ w.deleteOnExit();
+ FilePath workspace = new FilePath(w);
+ // Create 4 files in the workspace
+ File f1 = File.createTempFile("testng-results", ".xml", w);
+ f1.deleteOnExit();
+ File f2 = File.createTempFile("anyname", ".xml", w);
+ f2.deleteOnExit();
+ File f3 = File.createTempFile("testng-results", ".xml", w);
+ f3.deleteOnExit();
+ File f4 = File.createTempFile("anyname", ".xml", w);
+ f4.deleteOnExit();
+ // Create a folder and move 2 files there
+ File d1 = new File(workspace.child("subdir").getRemote());
+ d1.mkdir();
+ d1.deleteOnExit();
+ File f5 = new File(workspace.child(d1.getName()).child(f3.getName()).getRemote());
+ File f6 = new File(workspace.child(d1.getName()).child(f4.getName()).getRemote());
+ f3.renameTo(f5);
+ f4.renameTo(f6);
+ f5.deleteOnExit();
+ f6.deleteOnExit();
+ // Look for files in the entire workspace recursively without providing
+ // the includes parameter
+ FilePath[] reports = Publisher.locateReports(workspace, "**/testng*.xml");
+ Assert.assertEquals(2, reports.length);
+ // Generate a includes string and look for files
+ String includes = f1.getName() + "; " + f2.getName() + "; " + d1.getName();
+ reports = Publisher.locateReports(workspace, includes);
+ Assert.assertEquals(3, reports.length);
+ // Save files in local workspace
+ FilePath local = workspace.child("publishertest_localfolder");
+ boolean saved = Publisher.saveReports(local, reports, System.out);
+ Assert.assertTrue(saved);
+ Assert.assertEquals(3, local.list().size());
+ local.deleteRecursive();
+ }
+
+ @Test
+ public void testBuildAborted() throws Exception {
+ Publisher publisher = new Publisher("**/testng-results.xml", false, false);
+ Launcher launcherMock = mock(Launcher.class);
+ AbstractBuild buildMock = mock(AbstractBuild.class);
+ BuildListener listenerMock = mock(BuildListener.class);
+
+ ByteArrayOutputStream os = new ByteArrayOutputStream();
+ PrintStream ps = new PrintStream(os);
+
+ when(buildMock.getResult()).thenReturn(Result.ABORTED);
+ when(listenerMock.getLogger()).thenReturn(ps);
+
+ Assert.assertTrue(publisher.perform(buildMock, launcherMock, listenerMock));
+
+ String str = os.toString();
+ Assert.assertTrue(str.contains("Build Aborted"));
+ }
+
}

0 comments on commit fa2df40

Please sign in to comment.