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  
31  import static org.hamcrest.CoreMatchers.anyOf;
32  import static org.hamcrest.CoreMatchers.containsString;
33  import static org.hamcrest.CoreMatchers.is;
34  
35  public class Surefire1535TestNGParallelSuitesIT
36          extends SurefireJUnit4IntegrationTestCase
37  {
38      private static final Charset UTF8 = Charset.forName( "UTF-8" );
39      private static final String TEST_RESULT_1 = platformEncoding( "Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, " );
40      private static final String TEST_RESULT_2 = platformEncoding( "Tests run: 2, Failures: 0, Errors: 0, Skipped: 0" );
41      private static final String SUITE1 = platformEncoding( "Suite1.xml" );
42      private static final String SUITE2 = platformEncoding( "Suite2.xml" );
43      private static final String TEST1 = platformEncoding( "test 1" );
44      private static final String TEST2 = platformEncoding( "test 2" );
45      private static final String TEST_SUITE = platformEncoding( "Running TestSuite" );
46  
47      @Test
48      public void forks2() throws VerificationException
49      {
50          OutputValidator validator = unpack()
51                  .activateProfile( "forked-reports-directory" )
52                  .forkCount( 2 )
53                  .executeTest();
54  
55          TestFile testFile = validator.getSurefireReportsFile( "../surefire-reports-1/TEST-TestSuite.xml", UTF8 );
56          testFile.assertFileExists();
57          testFile.assertContainsText( "<testcase name=\"test\" classname=\"it.ParallelTest" );
58  
59          testFile = validator.getSurefireReportsFile( "../surefire-reports-2/TEST-TestSuite.xml", UTF8 );
60          testFile.assertFileExists();
61          testFile.assertContainsText( "<testcase name=\"test\" classname=\"it.ParallelTest" );
62  
63          validator.assertThatLogLine( containsString( TEST_RESULT_2 ), is( 1 ) )
64                  .assertThatLogLine( containsString( TEST_RESULT_1 ), is( 2 ) )
65                  .assertThatLogLine( containsString( SUITE1 ), is( 1 ) )
66                  .assertThatLogLine( containsString( SUITE2 ), is( 1 ) )
67                  .assertThatLogLine( containsString( TEST1 ), is( 1 ) )
68                  .assertThatLogLine( containsString( TEST2 ), is( 1 ) )
69                  .assertThatLogLine( containsString( TEST_SUITE ), is( 2 ) );
70      }
71  
72      @Test
73      public void forks2Redirected() throws VerificationException
74      {
75          OutputValidator validator = unpack()
76                  .activateProfile( "forked-reports-directory" )
77                  .forkCount( 2 )
78                  .redirectToFile( true )
79                  .executeTest();
80  
81          TestFile testFile = validator.getSurefireReportsFile( "../surefire-reports-1/TEST-TestSuite.xml", UTF8 );
82          testFile.assertFileExists();
83          testFile.assertContainsText( "<testcase name=\"test\" classname=\"it.ParallelTest" );
84  
85          testFile = validator.getSurefireReportsFile( "../surefire-reports-2/TEST-TestSuite.xml", UTF8 );
86          testFile.assertFileExists();
87          testFile.assertContainsText( "<testcase name=\"test\" classname=\"it.ParallelTest" );
88  
89          validator.assertThatLogLine( containsString( TEST_RESULT_2 ), is( 1 ) )
90                  .assertThatLogLine( containsString( TEST_RESULT_1 ), is( 2 ) )
91                  .assertThatLogLine( containsString( TEST_SUITE ), is( 2 ) );
92  
93          TestFile outFile = validator.getSurefireReportsFile( "../surefire-reports-1/TestSuite-output.txt" );
94          outFile.assertFileExists();
95          outFile.assertContainsText( anyOf( containsString( SUITE1 ), containsString( SUITE2 ) ) );
96          outFile.assertContainsText( anyOf( containsString( TEST1 ), containsString( TEST2 ) ) );
97  
98          outFile = validator.getSurefireReportsFile( "../surefire-reports-2/TestSuite-output.txt" );
99          outFile.assertFileExists();
100         outFile.assertContainsText( anyOf( containsString( SUITE1 ), containsString( SUITE2 ) ) );
101         outFile.assertContainsText( anyOf( containsString( TEST1 ), containsString( TEST2 ) ) );
102     }
103 
104     @Test
105     public void forks0() throws VerificationException
106     {
107         OutputValidator validator = unpack()
108                 .forkCount( 0 )
109                 .executeTest();
110 
111         TestFile testFile = validator.getSurefireReportsFile( "TEST-TestSuite.xml" );
112         testFile.assertFileExists();
113         testFile.assertContainsText( "<testcase name=\"test\" classname=\"it.ParallelTest1\"" );
114         testFile.assertContainsText( "<testcase name=\"test\" classname=\"it.ParallelTest2\"" );
115 
116         validator.assertThatLogLine( containsString( SUITE1 ), is( 1 ) )
117                 .assertThatLogLine( containsString( SUITE2 ), is( 1 ) )
118                 .assertThatLogLine( containsString( TEST1 ), is( 1 ) )
119                 .assertThatLogLine( containsString( TEST2 ), is( 1 ) )
120                 .assertThatLogLine( containsString( TEST_SUITE ), is( 1 ) );
121     }
122 
123     @Test
124     public void forks0Redirected() throws VerificationException
125     {
126         OutputValidator validator = unpack()
127                 .forkCount( 0 )
128                 .redirectToFile( true )
129                 .executeTest();
130 
131         TestFile testFile = validator.getSurefireReportsXmlFile( "TEST-TestSuite.xml" );
132         testFile.assertFileExists();
133         testFile.assertContainsText( "<testcase name=\"test\" classname=\"it.ParallelTest1\"" );
134         testFile.assertContainsText( "<testcase name=\"test\" classname=\"it.ParallelTest2\"" );
135 
136         validator.assertThatLogLine( containsString( TEST_SUITE ), is( 1 ) );
137 
138         TestFile outFile = validator.getSurefireReportsFile( "TestSuite-output.txt" );
139         outFile.assertFileExists();
140         outFile.assertContainsText( SUITE1 );
141         outFile.assertContainsText( SUITE2 );
142         outFile.assertContainsText( TEST1 );
143         outFile.assertContainsText( TEST2 );
144     }
145 
146     private SurefireLauncher unpack()
147     {
148         return unpack("/surefire-1535-parallel-testng");
149     }
150 
151     private static String platformEncoding( String text )
152     {
153         return new String( text.getBytes( UTF8 ) );
154     }
155 }