View Javadoc
1   package org.apache.maven.surefire.its;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.surefire.its.fixture.*;
23  import org.junit.Test;
24  
25  import java.io.File;
26  import java.util.ArrayList;
27  import java.util.HashSet;
28  import java.util.List;
29  import java.util.Set;
30  
31  import static org.junit.Assert.assertEquals;
32  import static org.junit.Assert.assertTrue;
33  
34  /**
35   * Test forkMode in a multi module project with parallel maven builds
36   * 
37   * @author Andreas Gudian
38   */
39  public class ForkModeMultiModuleIT
40      extends SurefireJUnit4IntegrationTestCase
41  {
42      @Test
43      public void testForkCountOneNoReuse()
44      {
45          List<String> pids = doTest( unpack( getProject() ).forkCount( 1 ).reuseForks( false ) );
46          assertAllDifferentPids( pids );
47          int matchesOne = countSuffixMatches( pids, "_1_1");
48          int matchesTwo = countSuffixMatches( pids, "_2_2" );
49          assertTrue( "At least one fork had forkNumber 1", matchesOne >= 1 );
50          assertTrue( "At least one fork had forkNumber 2", matchesTwo >= 1 );
51          assertEquals( "No other forkNumbers than 1 and 2 have been used", 6, matchesOne + matchesTwo);
52      }
53  
54  
55      @Test
56      public void testForkCountOneReuse()
57      {
58          List<String> pids = doTest( unpack( getProject() ).forkCount( 1 ).reuseForks( true ) );
59          assertDifferentPids( pids, 2 );
60          assertEndWith( pids, "_1_1", 3 );
61          assertEndWith( pids, "_2_2", 3 );
62      }
63  
64      @Test
65      public void testForkCountTwoNoReuse()
66      {
67          List<String> pids = doTest( unpack( getProject() ).forkCount( 2 ).reuseForks( false ) );
68          assertAllDifferentPids( pids );
69          int matchesOne = countSuffixMatches( pids, "_1_1");
70          int matchesTwo = countSuffixMatches( pids, "_2_2" );
71          int matchesThree = countSuffixMatches( pids, "_3_3");
72          int matchesFour = countSuffixMatches( pids, "_4_4" );
73          assertTrue( "At least one fork had forkNumber 1", matchesOne >= 1 );
74          assertTrue( "At least one fork had forkNumber 2", matchesTwo >= 1 );
75          assertTrue( "At least one fork had forkNumber 3", matchesThree >= 1 );
76          assertTrue( "At least one fork had forkNumber 4", matchesFour >= 1 );
77          assertEquals( "No other forkNumbers than 1, 2, 3, or 4 have been used", 6, matchesOne + matchesTwo + matchesThree + matchesFour );
78      }
79  
80      @Test
81      public void testForkCountTwoReuse()
82      {
83          List<String> pids =
84              doTest( unpack( getProject() ).forkCount( 2 ).reuseForks( true ) );
85          assertDifferentPids( pids, 4 );
86          
87          int matchesOne = countSuffixMatches( pids, "_1_1");
88          int matchesTwo = countSuffixMatches( pids, "_2_2" );
89          int matchesThree = countSuffixMatches( pids, "_3_3");
90          int matchesFour = countSuffixMatches( pids, "_4_4" );
91          assertTrue( "At least one fork had forkNumber 1", matchesOne >= 1 );
92          assertTrue( "At least one fork had forkNumber 2", matchesTwo >= 1 );
93          assertTrue( "At least one fork had forkNumber 3", matchesThree >= 1 );
94          assertTrue( "At least one fork had forkNumber 4", matchesFour >= 1 );
95          assertEquals( "No other forkNumbers than 1, 2, 3, or 4 have been used", 6, matchesOne + matchesTwo + matchesThree + matchesFour );
96      }
97  
98      private void assertEndWith( List<String> pids, String suffix, int expectedMatches )
99      {
100         int matches = countSuffixMatches( pids, suffix );
101 
102         assertEquals( "suffix " + suffix + " matched the correct number of pids", expectedMatches, matches );
103     }
104 
105     private int countSuffixMatches( List<String> pids, String suffix )
106     {
107         int matches = 0;
108         for ( String pid : pids )
109         {
110             if ( pid.endsWith( suffix ) )
111             {
112                 matches++;
113             }
114         }
115         return matches;
116     }
117 
118     private void assertDifferentPids( List<String> pids, int numOfDifferentPids )
119     {
120         Set<String> pidSet = new HashSet<String>( pids );
121         assertEquals( "number of different pids is not as expected", numOfDifferentPids, pidSet.size() );
122     }
123 
124     private void assertAllDifferentPids( List<String> pids )
125     {
126         assertDifferentPids( pids, pids.size() );
127     }
128 
129     private List<String> doTest( SurefireLauncher forkMode )
130     {
131         forkMode.addGoal( "-T2" );
132         forkMode.sysProp( "testProperty", "testValue_${surefire.threadNumber}_${surefire.forkNumber}" );
133         final OutputValidator outputValidator = forkMode.setForkJvm().executeTest();
134         List<String> pids = new ArrayList<String>( 6 );
135         pids.addAll( validateModule( outputValidator, "module-a" ) );
136         pids.addAll( validateModule( outputValidator, "module-b" ) );
137 
138         return pids;
139     }
140 
141     private List<String> validateModule( OutputValidator outputValidator, String module )
142     {
143         HelperAssertions.assertTestSuiteResults( 3, 0, 0, 0, new File( outputValidator.getBaseDir(), module ) );
144 
145         List<String> pids = new ArrayList<String>( 3 );
146         for ( int i = 1; i <= 3; i++ )
147         {
148             final TestFile targetFile = outputValidator.getTargetFile( module, "test" + i + "-pid" );
149             String pid = targetFile.slurpFile();
150             pids.add( pid );
151         }
152         
153         return pids;
154     }
155 
156     protected String getProject()
157     {
158         return "fork-mode-multimodule";
159     }
160 
161 
162 }