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
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertFalse;
24 import static org.junit.Assert.fail;
25
26 import java.util.Arrays;
27 import java.util.HashSet;
28 import java.util.Set;
29
30 import org.apache.maven.surefire.its.fixture.OutputValidator;
31 import org.apache.maven.surefire.its.fixture.SurefireJUnit4IntegrationTestCase;
32 import org.apache.maven.surefire.its.fixture.SurefireLauncher;
33 import org.apache.maven.surefire.its.fixture.TestFile;
34 import org.junit.BeforeClass;
35 import org.junit.Test;
36
37
38
39
40
41
42 public class ForkModeIT
43 extends SurefireJUnit4IntegrationTestCase
44 {
45
46 private OutputValidator outputValidator;
47
48 @BeforeClass
49 public static void installDumpPidPlugin()
50 throws Exception
51 {
52 unpack( ForkModeIT.class, "test-helper-dump-pid-plugin", "plugin" ).executeInstall();
53 }
54
55 @Test
56 public void testForkModeAlways()
57 {
58 String[] pids = doTest( unpack( getProject() ).setForkJvm().forkAlways() );
59 assertDifferentPids( pids );
60 assertEndWith( pids, "_1_1", 3 );
61 assertFalse( "pid 1 is not the same as the main process' pid", pids[0].equals( getMainPID() ) );
62 }
63
64 @Test
65 public void testForkModePerTest()
66 {
67 String[] pids = doTest( unpack( getProject() ).setForkJvm().forkPerTest() );
68 assertDifferentPids( pids );
69 assertEndWith( pids, "_1_1", 3 );
70 assertFalse( "pid 1 is not the same as the main process' pid", pids[0].equals( getMainPID() ) );
71 }
72
73 @Test
74 public void testForkModeNever()
75 {
76 String[] pids = doTest( unpack( getProject() ).forkNever() );
77 assertSamePids( pids );
78 assertEndWith( pids, "_1_1", 3 );
79 assertEquals( "my pid is equal to pid 1 of the test", getMainPID(), pids[0] );
80 }
81
82 @Test
83 public void testForkModeNone()
84 {
85 String[] pids = doTest( unpack( getProject() ).forkMode( "none" ) );
86 assertSamePids( pids );
87 assertEndWith( pids, "_1_1", 3 );
88 assertEquals( "my pid is equal to pid 1 of the test", getMainPID(), pids[0] );
89 }
90
91 @Test
92 public void testForkModeOncePerThreadSingleThread()
93 {
94 String[] pids = doTest( unpack( getProject() ).setForkJvm().forkOncePerThread().threadCount( 1 ) );
95 assertSamePids( pids );
96 assertEndWith( pids, "_1_1", 3 );
97 assertFalse( "pid 1 is not the same as the main process' pid", pids[0].equals( getMainPID() ) );
98 }
99
100 @Test
101 public void testForkModeOncePerThreadTwoThreads()
102 {
103 String[] pids =
104 doTest( unpack( getProject() ).forkOncePerThread().threadCount( 2 ).addGoal( "-DsleepLength=1200" ) );
105 assertDifferentPids( pids, 2 );
106 assertFalse( "pid 1 is not the same as the main process' pid", pids[0].equals( getMainPID() ) );
107 }
108
109 @Test
110 public void testForkCountZero()
111 {
112 String[] pids = doTest( unpack( getProject() ).forkCount( 0 ) );
113 assertSamePids( pids );
114 assertEndWith( pids, "_1_1", 3 );
115 assertEquals( "my pid is equal to pid 1 of the test", getMainPID(), pids[0] );
116 }
117
118 @Test
119 public void testForkCountOneNoReuse()
120 {
121 String[] pids = doTest( unpack( getProject() ).setForkJvm().forkCount( 1 ).reuseForks( false ) );
122 assertDifferentPids( pids );
123 assertEndWith( pids, "_1_1", 3 );
124 assertFalse( "pid 1 is not the same as the main process' pid", pids[0].equals( getMainPID() ) );
125 }
126
127 @Test
128 public void testForkCountOneReuse()
129 {
130 String[] pids = doTest( unpack( getProject() ).setForkJvm().forkCount( 1 ).reuseForks( true ) );
131 assertSamePids( pids );
132 assertEndWith( pids, "_1_1", 3 );
133 assertFalse( "pid 1 is not the same as the main process' pid", pids[0].equals( getMainPID() ) );
134 }
135
136 @Test
137 public void testForkCountTwoNoReuse()
138 {
139 String[] pids =
140 doTest( unpack( getProject() ).forkCount( 2 ).reuseForks( false ).addGoal( "-DsleepLength=1200" ) );
141 assertDifferentPids( pids );
142 assertFalse( "pid 1 is not the same as the main process' pid", pids[0].equals( getMainPID() ) );
143 }
144
145 @Test
146 public void testForkCountTwoReuse()
147 {
148 String[] pids =
149 doTest( unpack( getProject() ).forkCount( 2 ).reuseForks( true ).addGoal( "-DsleepLength=1200" ) );
150 assertDifferentPids( pids, 2 );
151 assertFalse( "pid 1 is not the same as the main process' pid", pids[0].equals( getMainPID() ) );
152 }
153
154 private void assertEndWith( String[] pids, String suffix, int expectedMatches )
155 {
156 int matches = 0;
157 for ( String pid : pids )
158 {
159 if ( pid.endsWith( suffix ) )
160 {
161 matches++;
162 }
163 }
164
165 assertEquals( "suffix " + suffix + " matched the correct number of pids", expectedMatches, matches );
166 }
167
168 private void assertDifferentPids( String[] pids, int numOfDifferentPids )
169 {
170 Set<String> pidSet = new HashSet<String>( Arrays.asList( pids ) );
171 assertEquals( "number of different pids is not as expected", numOfDifferentPids, pidSet.size() );
172 }
173
174 @Test
175 public void testForkModeOnce()
176 {
177 String[] pids = doTest( unpack( getProject() ).forkOnce() );
178 assertSamePids( pids );
179 assertFalse( "pid 1 is not the same as the main process' pid", pids[0].equals( getMainPID() ) );
180 }
181
182 private String getMainPID()
183 {
184 final TestFile targetFile = outputValidator.getTargetFile( "maven.pid" );
185 String pid = targetFile.slurpFile();
186 return pid + " testValue_1_1";
187 }
188
189 private void assertSamePids( String[] pids )
190 {
191 assertEquals( "pid 1 didn't match pid 2", pids[0], pids[1] );
192 assertEquals( "pid 1 didn't match pid 3", pids[0], pids[2] );
193 }
194
195 private void assertDifferentPids( String[] pids )
196 {
197 if ( pids[0].equals( pids[1] ) )
198 {
199 fail( "pid 1 matched pid 2: " + pids[0] );
200 }
201
202 if ( pids[0].equals( pids[2] ) )
203 {
204 fail( "pid 1 matched pid 3: " + pids[0] );
205 }
206
207 if ( pids[1].equals( pids[2] ) )
208 {
209 fail( "pid 2 matched pid 3: " + pids[0] );
210 }
211 }
212
213 private String[] doTest( SurefireLauncher forkMode )
214 {
215 forkMode.sysProp( "testProperty", "testValue_${surefire.threadNumber}_${surefire.forkNumber}" );
216 forkMode.addGoal( "org.apache.maven.plugins.surefire:maven-dump-pid-plugin:dump-pid" );
217 outputValidator = forkMode.executeTest();
218 outputValidator.verifyErrorFreeLog().assertTestSuiteResults( 3, 0, 0, 0 );
219 String[] pids = new String[3];
220 for ( int i = 1; i <= pids.length; i++ )
221 {
222 final TestFile targetFile = outputValidator.getTargetFile( "test" + i + "-pid" );
223 String pid = targetFile.slurpFile();
224 pids[i - 1] = pid;
225 }
226 return pids;
227 }
228
229 protected String getProject()
230 {
231 return "fork-mode";
232 }
233
234 }