1
1
package hudson .plugins .testng ;
2
2
3
3
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 ;
4
11
12
+ import java .io .ByteArrayOutputStream ;
5
13
import java .io .File ;
14
+ import java .io .PrintStream ;
6
15
7
- import junit . framework . Assert ;
8
- import junit . framework . TestCase ;
16
+ import static org . mockito . Mockito . mock ;
17
+ import static org . mockito . Mockito . when ;
9
18
10
19
/**
11
20
* @autor nullin
12
21
*/
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
+
57
86
}
0 commit comments