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  import org.junit.experimental.categories.Category;
25  
26  import java.io.File;
27  import java.util.ArrayList;
28  import java.util.HashSet;
29  import java.util.List;
30  import java.util.Set;
31  
32  import static org.junit.Assert.assertEquals;
33  import static org.junit.Assert.assertTrue;
34  
35  /**
36   * Test forkMode in a multi module project with parallel maven builds
37   * 
38   * @author Andreas Gudian
39   */
40  @Category( Not2xCompatible.class )
41  public class ForkModeMultiModuleIT
42      extends SurefireJUnit4IntegrationTestCase
43  {
44      @Test
45      public void testForkCountOneNoReuse()
46      {
47          List<String> pids = doTest( unpack( getProject() ).forkCount( 1 ).reuseForks( false ) );
48          assertAllDifferentPids( pids );
49          int matchesOne = countSuffixMatches( pids, "_1_1");
50          int matchesTwo = countSuffixMatches( pids, "_2_2" );
51          assertTrue( "At least one fork had forkNumber 1", matchesOne >= 1 );
52          assertTrue( "At least one fork had forkNumber 2", matchesTwo >= 1 );
53          assertEquals( "No other forkNumbers than 1 and 2 have been used", 6, matchesOne + matchesTwo);
54      }
55  
56  
57      @Test
58      public void testForkCountOneReuse()
59      {
60          List<String> pids = doTest( unpack( getProject() ).forkCount( 1 ).reuseForks( true ) );
61          assertDifferentPids( pids, 2 );
62          assertEndWith( pids, "_1_1", 3 );
63          assertEndWith( pids, "_2_2", 3 );
64      }
65  
66      @Test
67      public void testForkCountTwoNoReuse()
68      {
69          List<String> pids = doTest( unpack( getProject() ).forkCount( 2 ).reuseForks( false ) );
70          assertAllDifferentPids( pids );
71          int matchesOne = countSuffixMatches( pids, "_1_1");
72          int matchesTwo = countSuffixMatches( pids, "_2_2" );
73          int matchesThree = countSuffixMatches( pids, "_3_3");
74          int matchesFour = countSuffixMatches( pids, "_4_4" );
75          assertTrue( "At least one fork had forkNumber 1", matchesOne >= 1 );
76          assertTrue( "At least one fork had forkNumber 2", matchesTwo >= 1 );
77          assertTrue( "At least one fork had forkNumber 3", matchesThree >= 1 );
78          assertTrue( "At least one fork had forkNumber 4", matchesFour >= 1 );
79          assertEquals( "No other forkNumbers than 1, 2, 3, or 4 have been used", 6, matchesOne + matchesTwo + matchesThree + matchesFour );
80      }
81  
82      @Test
83      public void testForkCountTwoReuse()
84      {
85          List<String> pids =
86              doTest( unpack( getProject() ).forkCount( 2 ).reuseForks( true ) );
87          assertDifferentPids( pids, 4 );
88          
89          int matchesOne = countSuffixMatches( pids, "_1_1");
90          int matchesTwo = countSuffixMatches( pids, "_2_2" );
91          int matchesThree = countSuffixMatches( pids, "_3_3");
92          int matchesFour = countSuffixMatches( pids, "_4_4" );
93          assertTrue( "At least one fork had forkNumber 1", matchesOne >= 1 );
94          assertTrue( "At least one fork had forkNumber 2", matchesTwo >= 1 );
95          assertTrue( "At least one fork had forkNumber 3", matchesThree >= 1 );
96          assertTrue( "At least one fork had forkNumber 4", matchesFour >= 1 );
97          assertEquals( "No other forkNumbers than 1, 2, 3, or 4 have been used", 6, matchesOne + matchesTwo + matchesThree + matchesFour );
98      }
99  
100     private void assertEndWith( List<String> pids, String suffix, int expectedMatches )
101     {
102         int matches = countSuffixMatches( pids, suffix );
103 
104         assertEquals( "suffix " + suffix + " matched the correct number of pids", expectedMatches, matches );
105     }
106 
107     private int countSuffixMatches( List<String> pids, String suffix )
108     {
109         int matches = 0;
110         for ( String pid : pids )
111         {
112             if ( pid.endsWith( suffix ) )
113             {
114                 matches++;
115             }
116         }
117         return matches;
118     }
119 
120     private void assertDifferentPids( List<String> pids, int numOfDifferentPids )
121     {
122         Set<String> pidSet = new HashSet<String>( pids );
123         assertEquals( "number of different pids is not as expected", numOfDifferentPids, pidSet.size() );
124     }
125 
126     private void assertAllDifferentPids( List<String> pids )
127     {
128         assertDifferentPids( pids, pids.size() );
129     }
130 
131     private List<String> doTest( SurefireLauncher forkMode )
132     {
133         forkMode.addGoal( "-T 2" );
134         forkMode.sysProp( "testProperty", "testValue_${surefire.threadNumber}_${surefire.forkNumber}" );
135         final OutputValidator outputValidator = forkMode.setForkJvm().executeTest();
136         List<String> pids = new ArrayList<String>( 6 );
137         pids.addAll( validateModule( outputValidator, "module-a" ) );
138         pids.addAll( validateModule( outputValidator, "module-b" ) );
139 
140         return pids;
141     }
142 
143     private List<String> validateModule( OutputValidator outputValidator, String module )
144     {
145         HelperAssertions.assertTestSuiteResults( 3, 0, 0, 0, new File( outputValidator.getBaseDir(), module ) );
146 
147         List<String> pids = new ArrayList<String>( 3 );
148         for ( int i = 1; i <= 3; i++ )
149         {
150             final TestFile targetFile = outputValidator.getTargetFile( module, "test" + i + "-pid" );
151             String pid = targetFile.slurpFile();
152             pids.add( pid );
153         }
154         
155         return pids;
156     }
157 
158     protected String getProject()
159     {
160         return "fork-mode-multimodule";
161     }
162 
163 
164 }