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.junit.Test;
27  
28  import java.util.Iterator;
29  import java.util.Set;
30  import java.util.TreeSet;
31  
32  import static org.hamcrest.core.Is.is;
33  import static org.junit.Assert.assertThat;
34  import static org.junit.Assert.assertTrue;
35  
36  /**
37   * @author Kristian Rosenvold
38   */
39  public class Surefire747MethodParallelWithSuiteCountIT
40      extends SurefireJUnit4IntegrationTestCase
41  {
42  
43      private static Set<String> printTestLines( OutputValidator validator, String pattern )
44          throws VerificationException
45      {
46          Set<String> log = new TreeSet<String>( validator.loadLogLines() );
47          for ( Iterator<String> it = log.iterator(); it.hasNext(); )
48          {
49              String line = it.next();
50              if ( !line.contains( pattern ) )
51              {
52                  it.remove();
53              }
54          }
55          return log;
56      }
57  
58      private static long duration( String logLine )
59      {
60          return Integer.decode( logLine.split( "=" )[1] );
61      }
62  
63      @Test
64      public void testMethodsParallelWithSuite()
65          throws VerificationException
66      {
67          OutputValidator validator = unpack().executeTest().verifyErrorFree( 6 );
68          Set<String> testLines = printTestLines( validator, "test finished after duration=" );
69          assertThat( testLines.size(), is( 2 ) );
70          for ( String testLine : testLines )
71          {
72              long duration = duration( testLine );
73              long min = 250, max = 750;
74              assertTrue( String.format( "duration %d should be between %d and %d millis", duration, min, max ),
75                          duration > min && duration < max );
76          }
77          Set<String> suiteLines = printTestLines( validator, "suite finished after duration=" );
78          assertThat( suiteLines.size(), is( 1 ) );
79          long duration = duration( suiteLines.iterator().next() );
80          long min = 750, max = 1250;
81          assertTrue( String.format( "duration %d should be between %d and %d millis", duration, min, max ),
82                      duration > min && duration < max );
83      }
84  
85      @Test
86      public void testClassesParallelWithSuite()
87          throws VerificationException
88      {
89          OutputValidator validator = unpack().parallelClasses().executeTest().verifyErrorFree( 6 );
90          Set<String> testLines = printTestLines( validator, "test finished after duration=" );
91          assertThat( testLines.size(), is( 2 ) );
92          for ( String testLine : testLines )
93          {
94              long duration = duration( testLine );
95              long min = 1250, max = 1750;
96              assertTrue( String.format( "duration %d should be between %d and %d millis", duration, min, max ),
97                          duration > min && duration < max );
98          }
99          Set<String> suiteLines = printTestLines( validator, "suite finished after duration=" );
100         assertThat( suiteLines.size(), is( 1 ) );
101         long duration = duration( suiteLines.iterator().next() );
102         long min = 1250, max = 1750;
103         assertTrue( String.format( "duration %d should be between %d and %d millis", duration, min, max ),
104                     duration > min && duration < max );
105     }
106 
107     public SurefireLauncher unpack()
108     {
109         return unpack( "junit47-parallel-with-suite" );
110     }
111 }