View Javadoc
1   package org.apache.maven.surefire.its.jiras;
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.it.VerificationException;
23  import org.apache.maven.surefire.its.fixture.OutputValidator;
24  import org.apache.maven.surefire.its.fixture.SurefireJUnit4IntegrationTestCase;
25  import org.apache.maven.surefire.its.fixture.SurefireLauncher;
26  import org.apache.maven.surefire.its.fixture.TestFile;
27  import org.junit.Test;
28  
29  import java.nio.charset.Charset;
30  import java.util.Collection;
31  import java.util.Set;
32  import java.util.TreeSet;
33  
34  import static org.apache.maven.surefire.its.fixture.IsRegex.regex;
35  import static org.hamcrest.CoreMatchers.startsWith;
36  import static org.hamcrest.core.AnyOf.anyOf;
37  import static org.hamcrest.core.Is.is;
38  import static org.hamcrest.core.StringContains.containsString;
39  import static org.junit.Assert.assertThat;
40  
41  /**
42   * @author <a href="mailto:tibordigana@apache.org">Tibor Digana (tibor17)</a>
43   * @see <a href="https://issues.apache.org/jira/browse/SUREFIRE-1082">SUREFIRE-1082</a>
44   * @since 2.18
45   */
46  public class Surefire1082ParallelJUnitParameterizedIT
47      extends SurefireJUnit4IntegrationTestCase
48  {
49      private static Set<String> printOnlyTestLinesFromOutFile( OutputValidator validator )
50      {
51          TestFile report = validator.getSurefireReportsFile( "jiras.surefire1082.Jira1082Test-output.txt" );
52          report.assertFileExists();
53          return printOnlyTestLines( validator.loadFile( report.getFile(), Charset.forName( "UTF-8" ) ) );
54      }
55  
56      private static Set<String> printOnlyTestLines( Collection<String> logs )
57      {
58          Set<String> log = new TreeSet<>();
59          for ( String line : logs )
60          {
61              if ( line.startsWith( "class jiras.surefire1082." ) )
62              {
63                  log.add( line );
64              }
65          }
66          return log;
67      }
68  
69      private static void assertParallelRun( Set<String> log )
70      {
71          assertThat( log.size(), is( 4 ) );
72  
73          Set<String> expectedLogs1 = new TreeSet<>();
74          expectedLogs1.add( "class jiras.surefire1082.Jira1082Test a 0 pool-[\\d]+-thread-1" );
75          expectedLogs1.add( "class jiras.surefire1082.Jira1082Test b 0 pool-[\\d]+-thread-1" );
76          expectedLogs1.add( "class jiras.surefire1082.Jira1082Test a 1 pool-[\\d]+-thread-2" );
77          expectedLogs1.add( "class jiras.surefire1082.Jira1082Test b 1 pool-[\\d]+-thread-2" );
78  
79          Set<String> expectedLogs2 = new TreeSet<>();
80          expectedLogs2.add( "class jiras.surefire1082.Jira1082Test a 1 pool-[\\d]+-thread-1" );
81          expectedLogs2.add( "class jiras.surefire1082.Jira1082Test b 1 pool-[\\d]+-thread-1" );
82          expectedLogs2.add( "class jiras.surefire1082.Jira1082Test a 0 pool-[\\d]+-thread-2" );
83          expectedLogs2.add( "class jiras.surefire1082.Jira1082Test b 0 pool-[\\d]+-thread-2" );
84  
85          assertThat( log, anyOf( regex( expectedLogs1 ), regex( expectedLogs2 ) ) );
86      }
87  
88      @Test
89      public void checkClassesRunParallel()
90          throws VerificationException
91      {
92          OutputValidator validator = unpack().setTestToRun( "Jira1082Test" )
93                                              .parallelClasses()
94                                              .useUnlimitedThreads()
95                                              .executeTest()
96                                              .verifyErrorFree( 4 );
97  
98          validator.getSurefireReportsXmlFile( "TEST-jiras.surefire1082.Jira1082Test.xml" )
99                  .assertFileExists();
100 
101         validator.assertThatLogLine( containsString( "Running jiras.surefire1082.Jira1082Test" ), is( 1 ) );
102 
103         Set<String> log = new TreeSet<>( validator.loadLogLines( startsWith( "class jiras.surefire1082." ) ) );
104         assertParallelRun( log );
105     }
106 
107     @Test
108     public void checkOutFileClassesRunParallel()
109             throws VerificationException
110     {
111         OutputValidator validator = unpack().redirectToFile( true )
112                                             .setTestToRun( "Jira1082Test" )
113                                             .parallelClasses()
114                                             .useUnlimitedThreads()
115                                             .executeTest()
116                                             .verifyErrorFree( 4 );
117 
118         validator.getSurefireReportsXmlFile( "TEST-jiras.surefire1082.Jira1082Test.xml" )
119                 .assertFileExists();
120 
121         validator.assertThatLogLine( containsString( "Running jiras.surefire1082.Jira1082Test" ), is( 1 ) );
122 
123         Set<String> log = printOnlyTestLinesFromOutFile( validator );
124         assertParallelRun( log );
125     }
126 
127     @Test
128     public void shouldRunTwo() throws VerificationException
129     {
130         OutputValidator validator = unpack().redirectToFile( true )
131                                             .parallelClasses()
132                                             .useUnlimitedThreads()
133                                             .executeTest()
134                                             .verifyErrorFree( 8 );
135 
136         validator.getSurefireReportsXmlFile( "TEST-jiras.surefire1082.Jira1082Test.xml" )
137                 .assertFileExists();
138 
139         validator.getSurefireReportsXmlFile( "TEST-jiras.surefire1082.Jira1264Test.xml" )
140                 .assertFileExists();
141 
142         validator.getSurefireReportsFile( "jiras.surefire1082.Jira1082Test-output.txt" )
143                 .assertFileExists();
144 
145         validator.getSurefireReportsFile( "jiras.surefire1082.Jira1264Test-output.txt" )
146                 .assertFileExists();
147 
148         validator.assertThatLogLine( containsString( "Running jiras.surefire1082.Jira1082Test" ), is( 1 ) );
149 
150         validator.assertThatLogLine( containsString( "Running jiras.surefire1082.Jira1264Test" ), is( 1 ) );
151     }
152 
153     private SurefireLauncher unpack()
154     {
155         return unpack( "surefire-1082-parallel-junit-parameterized" );
156     }
157 }