Skip to content

Commit fa2df40

Browse files
committedJan 5, 2013
Fixed: JENKINS-12046 - TestNG shouldn't look for result files if build was aborted (again!)
Also added a test for this.
1 parent b71d891 commit fa2df40

File tree

4 files changed

+92
-64
lines changed

4 files changed

+92
-64
lines changed
 

‎README

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Release Notes
2020
* JENKINS-9838 - Action --> HealthReportingAction
2121
* Upped compatible Jenkins version to v1.447
2222
* Fixed: JENKINS-15119 - Duration sorting is incorrect in TestNG report
23+
* Fixed: JENKINS-12046 - TestNG shouldn't look for result files if build was aborted (again!)
2324

2425
### v0.32
2526
* Fixed: JENKINS-12648 - Results should be reported for failed/aborted builds as well (Reverted fix for JENKINS-12046 in v0.30)

‎pom.xml

+6
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@
1717
<groupId>org.jenkins-ci.main</groupId>
1818
<artifactId>maven-plugin</artifactId>
1919
</dependency>
20+
<dependency>
21+
<groupId>org.mockito</groupId>
22+
<artifactId>mockito-core</artifactId>
23+
<version>1.8.5</version>
24+
<scope>test</scope>
25+
</dependency>
2026
</dependencies>
2127

2228
<issueManagement>

‎src/main/java/hudson/plugins/testng/Publisher.java

+10-18
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,14 @@
33
import hudson.Extension;
44
import hudson.FilePath;
55
import hudson.Launcher;
6-
import hudson.model.Action;
7-
import hudson.model.BuildListener;
8-
import hudson.model.Result;
9-
import hudson.model.AbstractBuild;
10-
import hudson.model.AbstractProject;
6+
import hudson.model.*;
117
import hudson.plugins.testng.results.TestResults;
128
import hudson.tasks.BuildStepDescriptor;
139
import hudson.tasks.BuildStepMonitor;
1410
import hudson.tasks.Recorder;
11+
import net.sf.json.JSONObject;
12+
import org.kohsuke.stapler.DataBoundConstructor;
13+
import org.kohsuke.stapler.StaplerRequest;
1514

1615
import java.io.File;
1716
import java.io.IOException;
@@ -21,11 +20,6 @@
2120
import java.util.Collection;
2221
import java.util.List;
2322

24-
import net.sf.json.JSONObject;
25-
26-
import org.kohsuke.stapler.DataBoundConstructor;
27-
import org.kohsuke.stapler.StaplerRequest;
28-
2923
/**
3024
* This class defines a @Publisher and @Extension
3125
*
@@ -84,6 +78,12 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher,
8478
throws InterruptedException, IOException {
8579

8680
PrintStream logger = listener.getLogger();
81+
82+
if (build.getResult().equals(Result.ABORTED)) {
83+
logger.println("Build Aborted. Not looking for any TestNG results.");
84+
return true;
85+
}
86+
8787
logger.println("TestNG Reports Processing: START");
8888
logger.println("Looking for TestNG results report in workspace using pattern: "
8989
+ reportFilenamePattern);
@@ -230,14 +230,6 @@ static boolean saveReports(FilePath testngDir, FilePath[] paths, PrintStream log
230230
return true;
231231
}
232232

233-
/**
234-
* {@inheritDoc}
235-
*/
236-
@Override
237-
public boolean prebuild(AbstractBuild<?, ?> build, BuildListener listener) {
238-
return true;
239-
}
240-
241233
public static final class DescriptorImpl extends BuildStepDescriptor<hudson.tasks.Publisher> {
242234

243235
/**
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,86 @@
11
package hudson.plugins.testng;
22

33
import hudson.FilePath;
4+
import hudson.Launcher;
5+
import hudson.model.AbstractBuild;
6+
import hudson.model.BuildListener;
7+
import hudson.model.Result;
8+
import junit.framework.Assert;
9+
import org.junit.Test;
10+
import org.jvnet.hudson.test.HudsonTestCase;
411

12+
import java.io.ByteArrayOutputStream;
513
import java.io.File;
14+
import java.io.PrintStream;
615

7-
import junit.framework.Assert;
8-
import junit.framework.TestCase;
16+
import static org.mockito.Mockito.mock;
17+
import static org.mockito.Mockito.when;
918

1019
/**
1120
* @autor nullin
1221
*/
13-
public class PublisherTest extends TestCase
14-
{
15-
public void testLocateReports() throws Exception
16-
{
17-
// Create a temporary workspace in the system
18-
File w = File.createTempFile("workspace", ".test");
19-
w.delete();
20-
w.mkdir();
21-
w.deleteOnExit();
22-
FilePath workspace = new FilePath(w);
23-
// Create 4 files in the workspace
24-
File f1 = File.createTempFile("testng-results", ".xml", w);
25-
f1.deleteOnExit();
26-
File f2 = File.createTempFile("anyname", ".xml", w);
27-
f2.deleteOnExit();
28-
File f3 = File.createTempFile("testng-results", ".xml", w);
29-
f3.deleteOnExit();
30-
File f4 = File.createTempFile("anyname", ".xml", w);
31-
f4.deleteOnExit();
32-
// Create a folder and move there 2 files
33-
File d1 = new File(workspace.child("subdir").getRemote());
34-
d1.mkdir();
35-
d1.deleteOnExit();
36-
File f5 = new File(workspace.child(d1.getName()).child(f3.getName()).getRemote());
37-
File f6 = new File(workspace.child(d1.getName()).child(f4.getName()).getRemote());
38-
f3.renameTo(f5);
39-
f4.renameTo(f6);
40-
f5.deleteOnExit();
41-
f6.deleteOnExit();
42-
// Look for files in the entire workspace recursively without providing
43-
// the includes parameter
44-
FilePath[] reports = Publisher.locateReports(workspace, "**/testng*.xml");
45-
Assert.assertEquals(2, reports.length);
46-
// Generate a includes string and look for files
47-
String includes = f1.getName() + "; " + f2.getName() + "; " + d1.getName();
48-
reports = Publisher.locateReports(workspace, includes);
49-
Assert.assertEquals(3, reports.length);
50-
// Save files in local workspace
51-
FilePath local = workspace.child("coverage_localfolder");
52-
boolean saved = Publisher.saveReports(local, reports, System.out);
53-
Assert.assertTrue(saved);
54-
Assert.assertEquals(3, local.list().size());
55-
local.deleteRecursive();
56-
}
22+
public class PublisherTest extends HudsonTestCase {
23+
24+
@Test
25+
public void testLocateReports() throws Exception {
26+
// Create a temporary workspace in the system
27+
File w = File.createTempFile("workspace", ".test");
28+
w.delete();
29+
w.mkdir();
30+
w.deleteOnExit();
31+
FilePath workspace = new FilePath(w);
32+
// Create 4 files in the workspace
33+
File f1 = File.createTempFile("testng-results", ".xml", w);
34+
f1.deleteOnExit();
35+
File f2 = File.createTempFile("anyname", ".xml", w);
36+
f2.deleteOnExit();
37+
File f3 = File.createTempFile("testng-results", ".xml", w);
38+
f3.deleteOnExit();
39+
File f4 = File.createTempFile("anyname", ".xml", w);
40+
f4.deleteOnExit();
41+
// Create a folder and move 2 files there
42+
File d1 = new File(workspace.child("subdir").getRemote());
43+
d1.mkdir();
44+
d1.deleteOnExit();
45+
File f5 = new File(workspace.child(d1.getName()).child(f3.getName()).getRemote());
46+
File f6 = new File(workspace.child(d1.getName()).child(f4.getName()).getRemote());
47+
f3.renameTo(f5);
48+
f4.renameTo(f6);
49+
f5.deleteOnExit();
50+
f6.deleteOnExit();
51+
// Look for files in the entire workspace recursively without providing
52+
// the includes parameter
53+
FilePath[] reports = Publisher.locateReports(workspace, "**/testng*.xml");
54+
Assert.assertEquals(2, reports.length);
55+
// Generate a includes string and look for files
56+
String includes = f1.getName() + "; " + f2.getName() + "; " + d1.getName();
57+
reports = Publisher.locateReports(workspace, includes);
58+
Assert.assertEquals(3, reports.length);
59+
// Save files in local workspace
60+
FilePath local = workspace.child("publishertest_localfolder");
61+
boolean saved = Publisher.saveReports(local, reports, System.out);
62+
Assert.assertTrue(saved);
63+
Assert.assertEquals(3, local.list().size());
64+
local.deleteRecursive();
65+
}
66+
67+
@Test
68+
public void testBuildAborted() throws Exception {
69+
Publisher publisher = new Publisher("**/testng-results.xml", false, false);
70+
Launcher launcherMock = mock(Launcher.class);
71+
AbstractBuild buildMock = mock(AbstractBuild.class);
72+
BuildListener listenerMock = mock(BuildListener.class);
73+
74+
ByteArrayOutputStream os = new ByteArrayOutputStream();
75+
PrintStream ps = new PrintStream(os);
76+
77+
when(buildMock.getResult()).thenReturn(Result.ABORTED);
78+
when(listenerMock.getLogger()).thenReturn(ps);
79+
80+
Assert.assertTrue(publisher.perform(buildMock, launcherMock, listenerMock));
81+
82+
String str = os.toString();
83+
Assert.assertTrue(str.contains("Build Aborted"));
84+
}
85+
5786
}

0 commit comments

Comments
 (0)
Please sign in to comment.