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.hamcrest.BaseMatcher;
28  import org.hamcrest.Description;
29  import org.hamcrest.Matcher;
30  import org.junit.Test;
31  
32  import java.nio.charset.Charset;
33  import java.util.Collection;
34  import java.util.Iterator;
35  import java.util.Set;
36  import java.util.TreeSet;
37  
38  import static org.hamcrest.core.AnyOf.anyOf;
39  import static org.hamcrest.core.Is.is;
40  import static org.hamcrest.core.StringContains.containsString;
41  import static org.junit.Assert.assertThat;
42  
43  /**
44   * @author <a href="mailto:tibordigana@apache.org">Tibor Digana (tibor17)</a>
45   * @see <a href="https://issues.apache.org/jira/browse/SUREFIRE-1082">SUREFIRE-1082</a>
46   * @since 2.18
47   */
48  public class Surefire1082ParallelJUnitParameterizedIT
49      extends SurefireJUnit4IntegrationTestCase
50  {
51      private static Set<String> printOnlyTestLinesFromConsole( OutputValidator validator )
52              throws VerificationException
53      {
54          return printOnlyTestLines( validator.loadLogLines() );
55      }
56  
57      private static Set<String> printOnlyTestLinesFromOutFile( OutputValidator validator )
58              throws VerificationException
59      {
60          TestFile report = validator.getSurefireReportsFile( "jiras.surefire1082.Jira1082Test-output.txt" );
61          report.assertFileExists();
62          return printOnlyTestLines( validator.loadFile( report.getFile(), Charset.forName( "UTF-8" ) ) );
63      }
64  
65      private static Set<String> printOnlyTestLines( Collection<String> logs )
66          throws VerificationException
67      {
68          Set<String> log = new TreeSet<String>();
69          for ( String line : logs )
70          {
71              if ( line.startsWith( "class jiras.surefire1082." ) )
72              {
73                  log.add( line );
74              }
75          }
76          return log;
77      }
78  
79      private static Matcher<Set<String>> regex( Set<String> r )
80      {
81          return new IsRegex( r );
82      }
83  
84      private static void assertParallelRun( Set<String> log )
85      {
86          assertThat( log.size(), is( 4 ) );
87  
88          Set<String> expectedLogs1 = new TreeSet<String>();
89          expectedLogs1.add( "class jiras.surefire1082.Jira1082Test a 0 pool-[\\d]+-thread-1" );
90          expectedLogs1.add( "class jiras.surefire1082.Jira1082Test b 0 pool-[\\d]+-thread-1" );
91          expectedLogs1.add( "class jiras.surefire1082.Jira1082Test a 1 pool-[\\d]+-thread-2" );
92          expectedLogs1.add( "class jiras.surefire1082.Jira1082Test b 1 pool-[\\d]+-thread-2" );
93  
94          Set<String> expectedLogs2 = new TreeSet<String>();
95          expectedLogs2.add( "class jiras.surefire1082.Jira1082Test a 1 pool-[\\d]+-thread-1" );
96          expectedLogs2.add( "class jiras.surefire1082.Jira1082Test b 1 pool-[\\d]+-thread-1" );
97          expectedLogs2.add( "class jiras.surefire1082.Jira1082Test a 0 pool-[\\d]+-thread-2" );
98          expectedLogs2.add( "class jiras.surefire1082.Jira1082Test b 0 pool-[\\d]+-thread-2" );
99  
100         assertThat( log, anyOf( regex( expectedLogs1 ), regex( expectedLogs2 ) ) );
101     }
102 
103     @Test
104     public void checkClassesRunParallel()
105         throws VerificationException
106     {
107         OutputValidator validator = unpack().setTestToRun( "Jira1082Test" )
108                                             .parallelClasses()
109                                             .useUnlimitedThreads()
110                                             .executeTest()
111                                             .verifyErrorFree( 4 );
112 
113         validator.getSurefireReportsXmlFile( "TEST-jiras.surefire1082.Jira1082Test.xml" )
114                 .assertFileExists();
115 
116         validator.assertThatLogLine( containsString( "Running jiras.surefire1082.Jira1082Test" ), is( 1 ) );
117 
118         Set<String> log = printOnlyTestLinesFromConsole( validator );
119         assertParallelRun( log );
120     }
121 
122     @Test
123     public void checkOutFileClassesRunParallel()
124             throws VerificationException
125     {
126         OutputValidator validator = unpack().redirectToFile( true )
127                                             .setTestToRun( "Jira1082Test" )
128                                             .parallelClasses()
129                                             .useUnlimitedThreads()
130                                             .executeTest()
131                                             .verifyErrorFree( 4 );
132 
133         validator.getSurefireReportsXmlFile( "TEST-jiras.surefire1082.Jira1082Test.xml" )
134                 .assertFileExists();
135 
136         validator.assertThatLogLine( containsString( "Running jiras.surefire1082.Jira1082Test" ), is( 1 ) );
137 
138         Set<String> log = printOnlyTestLinesFromOutFile( validator );
139         assertParallelRun( log );
140     }
141 
142     @Test
143     public void shouldRunTwo() throws VerificationException
144     {
145         OutputValidator validator = unpack().redirectToFile( true )
146                                             .parallelClasses()
147                                             .useUnlimitedThreads()
148                                             .executeTest()
149                                             .verifyErrorFree( 8 );
150 
151         validator.getSurefireReportsXmlFile( "TEST-jiras.surefire1082.Jira1082Test.xml" )
152                 .assertFileExists();
153 
154         validator.getSurefireReportsXmlFile( "TEST-jiras.surefire1082.Jira1264Test.xml" )
155                 .assertFileExists();
156 
157         validator.getSurefireReportsFile( "jiras.surefire1082.Jira1082Test-output.txt" )
158                 .assertFileExists();
159 
160         validator.getSurefireReportsFile( "jiras.surefire1082.Jira1264Test-output.txt" )
161                 .assertFileExists();
162 
163         validator.assertThatLogLine( containsString( "Running jiras.surefire1082.Jira1082Test" ), is( 1 ) );
164 
165         validator.assertThatLogLine( containsString( "Running jiras.surefire1082.Jira1264Test" ), is( 1 ) );
166     }
167 
168     private SurefireLauncher unpack()
169     {
170         return unpack( "surefire-1082-parallel-junit-parameterized" );
171     }
172 
173     private static class IsRegex
174         extends BaseMatcher<Set<String>>
175     {
176         private final Set<String> expectedRegex;
177 
178         IsRegex( Set<String> expectedRegex )
179         {
180             this.expectedRegex = expectedRegex;
181         }
182 
183         @Override
184         public boolean matches( Object o )
185         {
186             if ( o != null && o instanceof Set )
187             {
188                 Set<String> actual = (Set<String>) o;
189                 boolean matches = actual.size() == expectedRegex.size();
190                 Iterator<String> regex = expectedRegex.iterator();
191                 for ( String s : actual )
192                 {
193                     if ( s == null || !regex.hasNext() || !s.matches( regex.next() ) )
194                     {
195                         matches = false;
196                     }
197                 }
198                 return matches;
199             }
200             else
201             {
202                 return false;
203             }
204         }
205 
206         @Override
207         public void describeTo( Description description )
208         {
209             description.appendValue( expectedRegex );
210         }
211     }
212 }