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.CoreMatchers.anyOf;
33  import static org.hamcrest.CoreMatchers.containsString;
34  import static org.hamcrest.CoreMatchers.endsWith;
35  import static org.hamcrest.core.Is.is;
36  import static org.junit.Assert.assertThat;
37  import static org.junit.Assert.assertTrue;
38  
39  /**
40   * @author Kristian Rosenvold
41   */
42  public class Surefire747MethodParallelWithSuiteCountIT
43      extends SurefireJUnit4IntegrationTestCase
44  {
45  
46      private static Set<String> printTestLines( OutputValidator validator, String pattern )
47          throws VerificationException
48      {
49          Set<String> log = new TreeSet<String>( validator.loadLogLines() );
50          for ( Iterator<String> it = log.iterator(); it.hasNext(); )
51          {
52              String line = it.next();
53              if ( !line.contains( pattern ) )
54              {
55                  it.remove();
56              }
57          }
58          return log;
59      }
60  
61      private static long duration( String logLine )
62      {
63          return Integer.decode( logLine.split( "=" )[1] );
64      }
65  
66      @Test
67      public void testMethodsParallelWithSuite()
68          throws VerificationException
69      {
70          OutputValidator validator = unpack().executeTest().verifyErrorFree( 6 );
71          Set<String> testLines = printTestLines( validator, "test finished after duration=" );
72          assertThat( testLines.size(), is( 2 ) );
73          for ( String testLine : testLines )
74          {
75              long duration = duration( testLine );
76              long min = 250, max = 750;
77              assertTrue( String.format( "duration %d should be between %d and %d millis", duration, min, max ),
78                          duration > min && duration < max );
79          }
80          Set<String> suiteLines = printTestLines( validator, "suite finished after duration=" );
81          assertThat( suiteLines.size(), is( 1 ) );
82          long duration = duration( suiteLines.iterator().next() );
83          long min = 750, max = 1250;
84          assertTrue( String.format( "duration %d should be between %d and %d millis", duration, min, max ),
85                      duration > min && duration < max );
86  
87          for ( String line : validator.loadLogLines() )
88          {
89              if ( line.startsWith( "Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed:" ) )
90              {
91                  assertThat( line, anyOf( // 0.5 sec, the delta -1.0/+0.3 can be varying depending on CI jobs
92                          containsString( "Time elapsed: 0.4" ),
93                          containsString( "Time elapsed: 0.5" ),
94                          containsString( "Time elapsed: 0.6" ),
95                          containsString( "Time elapsed: 0.7" ),
96                          containsString( "Time elapsed: 0.8" ) ) );
97                  assertThat( line, anyOf(
98                          endsWith(" sec - in surefire747.SuiteTest1" ),
99                          endsWith(" sec - in surefire747.SuiteTest2" ) ) );
100             }
101         }
102     }
103 
104     @Test
105     public void testClassesParallelWithSuite()
106         throws VerificationException
107     {
108         OutputValidator validator = unpack().parallelClasses().executeTest().verifyErrorFree( 6 );
109         Set<String> testLines = printTestLines( validator, "test finished after duration=" );
110         assertThat( testLines.size(), is( 2 ) );
111         for ( String testLine : testLines )
112         {
113             long duration = duration( testLine );
114             long min = 1250, max = 1750;
115             assertTrue( String.format( "duration %d should be between %d and %d millis", duration, min, max ),
116                         duration > min && duration < max );
117         }
118         Set<String> suiteLines = printTestLines( validator, "suite finished after duration=" );
119         assertThat( suiteLines.size(), is( 1 ) );
120         long duration = duration( suiteLines.iterator().next() );
121         long min = 1250, max = 1750;
122         assertTrue( String.format( "duration %d should be between %d and %d millis", duration, min, max ),
123                     duration > min && duration < max );
124     }
125 
126     public SurefireLauncher unpack()
127     {
128         return unpack( "junit47-parallel-with-suite" );
129     }
130 }