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() )
95 .setForkJvm()
96 .forkPerThread()
97 .reuseForks( true )
98 .threadCount( 1 ) );
99 assertSamePids( pids );
100 assertEndWith( pids, "_1_1", 3 );
101 assertFalse( "pid 1 is not the same as the main process' pid", pids[0].equals( getMainPID() ) );
102 }
103
104 @Test
105 public void testForkModeOncePerThreadTwoThreads()
106 {
107 String[] pids = doTest( unpack( getProject() )
108 .forkPerThread()
109 .reuseForks( true )
110 .threadCount( 2 )
111 .addGoal( "-DsleepLength=7200" ) );
112 assertDifferentPids( pids, 2 );
113 assertFalse( "pid 1 is not the same as the main process' pid", pids[0].equals( getMainPID() ) );
114 }
115
116 @Test
117 public void testForkCountZero()
118 {
119 String[] pids = doTest( unpack( getProject() ).forkCount( 0 ) );
120 assertSamePids( pids );
121 assertEndWith( pids, "_1_1", 3 );
122 assertEquals( "my pid is equal to pid 1 of the test", getMainPID(), pids[0] );
123 }
124
125 @Test
126 public void testForkCountOneNoReuse()
127 {
128 String[] pids = doTest( unpack( getProject() ).setForkJvm().forkCount( 1 ).reuseForks( false ) );
129 assertDifferentPids( pids );
130 assertEndWith( pids, "_1_1", 3 );
131 assertFalse( "pid 1 is not the same as the main process' pid", pids[0].equals( getMainPID() ) );
132 }
133
134 @Test
135 public void testForkCountOneReuse()
136 {
137 String[] pids = doTest( unpack( getProject() ).setForkJvm().forkCount( 1 ).reuseForks( true ) );
138 assertSamePids( pids );
139 assertEndWith( pids, "_1_1", 3 );
140 assertFalse( "pid 1 is not the same as the main process' pid", pids[0].equals( getMainPID() ) );
141 }
142
143 @Test
144 public void testForkCountTwoNoReuse()
145 {
146 String[] pids =
147 doTest( unpack( getProject() ).setForkJvm().forkCount( 2 ).reuseForks( false ).addGoal( "-DsleepLength=7200" ) );
148 assertDifferentPids( pids );
149 assertFalse( "pid 1 is not the same as the main process' pid", pids[0].equals( getMainPID() ) );
150 }
151
152 @Test
153 public void testForkCountTwoReuse()
154 {
155 String[] pids =
156 doTest( unpack( getProject() ).forkCount( 2 ).reuseForks( true ).addGoal( "-DsleepLength=7200" ) );
157 assertDifferentPids( pids, 2 );
158 assertFalse( "pid 1 is not the same as the main process' pid", pids[0].equals( getMainPID() ) );
159 }
160
161 private void assertEndWith( String[] pids, String suffix, int expectedMatches )
162 {
163 int matches = 0;
164 for ( String pid : pids )
165 {
166 if ( pid.endsWith( suffix ) )
167 {
168 matches++;
169 }
170 }
171
172 assertEquals( "suffix " + suffix + " matched the correct number of pids", expectedMatches, matches );
173 }
174
175 private void assertDifferentPids( String[] pids, int numOfDifferentPids )
176 {
177 Set<String> pidSet = new HashSet<>( Arrays.asList( pids ) );
178 assertEquals( "number of different pids is not as expected", numOfDifferentPids, pidSet.size() );
179 }
180
181 @Test
182 public void testForkModeOnce()
183 {
184 String[] pids = doTest( unpack( getProject() ).forkOnce() );
185 assertSamePids( pids );
186 assertFalse( "pid 1 is not the same as the main process' pid", pids[0].equals( getMainPID() ) );
187 }
188
189 private String getMainPID()
190 {
191 final TestFile targetFile = outputValidator.getTargetFile( "maven.pid" );
192 String pid = targetFile.slurpFile();
193 return pid + " testValue_1_1";
194 }
195
196 private void assertSamePids( String[] pids )
197 {
198 assertEquals( "pid 1 didn't match pid 2", pids[0], pids[1] );
199 assertEquals( "pid 1 didn't match pid 3", pids[0], pids[2] );
200 }
201
202 private void assertDifferentPids( String[] pids )
203 {
204 if ( pids[0].equals( pids[1] ) )
205 {
206 fail( "pid 1 matched pid 2: " + pids[0] );
207 }
208
209 if ( pids[0].equals( pids[2] ) )
210 {
211 fail( "pid 1 matched pid 3: " + pids[0] );
212 }
213
214 if ( pids[1].equals( pids[2] ) )
215 {
216 fail( "pid 2 matched pid 3: " + pids[0] );
217 }
218 }
219
220 private String[] doTest( SurefireLauncher forkMode )
221 {
222 forkMode.sysProp( "testProperty", "testValue_${surefire.threadNumber}_${surefire.forkNumber}" );
223 forkMode.addGoal( "org.apache.maven.plugins.surefire:maven-dump-pid-plugin:dump-pid" );
224 outputValidator = forkMode.executeTest();
225 outputValidator.verifyErrorFreeLog().assertTestSuiteResults( 3, 0, 0, 0 );
226 String[] pids = new String[3];
227 for ( int i = 1; i <= pids.length; i++ )
228 {
229 final TestFile targetFile = outputValidator.getTargetFile( "test" + i + "-pid" );
230 String pid = targetFile.slurpFile();
231 pids[i - 1] = pid;
232 }
233 return pids;
234 }
235
236 protected String getProject()
237 {
238 return "fork-mode";
239 }
240
241 }