1 package org.apache.maven.surefire.its;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 import org.apache.maven.it.VerificationException;
22 import org.apache.maven.it.Verifier;
23 import org.apache.maven.it.util.ResourceExtractor;
24 import org.apache.maven.surefire.its.misc.HelperAssertions;
25
26 import java.io.BufferedReader;
27 import java.io.File;
28 import java.io.FileReader;
29 import java.io.IOException;
30 import java.util.List;
31
32
33
34
35
36
37 public class ForkModeIT
38 extends AbstractSurefireIntegrationTestClass
39 {
40 public void testForkModeAlways()
41 throws Exception
42 {
43 String[] pids = doTest( "always" );
44
45 assertDifferentPids( pids );
46 }
47
48 public void testForkModePerTest()
49 throws Exception
50 {
51 String[] pids = doTest( "pertest" );
52
53 assertDifferentPids( pids );
54 }
55
56 public void testForkModeNever()
57 throws Exception
58 {
59 String[] pids = doTest( "never" );
60
61 assertSamePids( pids );
62 }
63
64 public void testForkModeNone()
65 throws Exception
66 {
67 String[] pids = doTest( "none" );
68
69 assertSamePids( pids );
70 }
71
72 public void testForkModeOnce()
73 throws Exception
74 {
75 String[] pids = doTest( "once" );
76
77
78
79
80 assertSamePids( pids );
81 }
82
83 private void assertSamePids( String[] pids )
84 {
85 assertEquals( "pid 1 didn't match pid 2", pids[0], pids[1] );
86 assertEquals( "pid 1 didn't match pid 3", pids[0], pids[2] );
87 }
88
89 private void assertDifferentPids( String[] pids )
90 {
91 if ( pids[0].equals( pids[1] ) )
92 {
93 fail( "pid 1 matched pid 2: " + pids[0] );
94 }
95
96 if ( pids[0].equals( pids[2] ) )
97 {
98 fail( "pid 1 matched pid 3: " + pids[0] );
99 }
100
101 if ( pids[1].equals( pids[2] ) )
102 {
103 fail( "pid 2 matched pid 3: " + pids[0] );
104 }
105 }
106
107 private String[] doTest( String forkMode )
108 throws IOException, VerificationException
109 {
110 File testDir = ResourceExtractor.simpleExtractResources( getClass(), "/fork-mode" );
111
112 Verifier verifier = new Verifier( testDir.getAbsolutePath() );
113 List<String> goals = this.getInitialGoals();
114 goals.add( "test" );
115 goals.add( "-DforkMode=" + forkMode );
116 executeGoals( verifier, goals );
117 verifier.verifyErrorFreeLog();
118 verifier.resetStreams();
119
120 HelperAssertions.assertTestSuiteResults( 3, 0, 0, 0, testDir );
121
122 File targetDir = new File( testDir, "target" );
123 String[] pids = new String[3];
124 for ( int i = 1; i <= pids.length; i++ )
125 {
126 File pidFile = new File( targetDir, "test" + i + "-pid" );
127 String pid = slurpFile( pidFile );
128 pids[i - 1] = pid;
129 }
130 return pids;
131 }
132
133 private String slurpFile( File textFile )
134 throws IOException
135 {
136 StringBuffer sb = new StringBuffer();
137 BufferedReader reader = new BufferedReader( new FileReader( textFile ) );
138 for ( String line = reader.readLine(); line != null; line = reader.readLine() )
139 {
140 sb.append( line );
141 }
142 reader.close();
143 return sb.toString();
144 }
145 }