View Javadoc

1   package org.apache.maven.surefire.its;
2   /*
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   */
20  
21  import org.apache.maven.it.Verifier;
22  import org.apache.maven.it.util.ResourceExtractor;
23  import org.apache.maven.surefire.its.misc.HelperAssertions;
24  import org.apache.maven.surefire.its.misc.ReportTestSuite;
25  
26  import java.io.File;
27  import java.util.HashSet;
28  import java.util.List;
29  import java.util.Set;
30  
31  /**
32   * Test running two test cases; confirms reporting works correctly
33   *
34   * @author <a href="mailto:dfabulich@apache.org">Dan Fabulich</a>
35   */
36  public class TwoTestCasesIT
37      extends AbstractSurefireIntegrationTestClass
38  {
39      public void testTwoTestCases()
40          throws Exception
41      {
42          File testDir = ResourceExtractor.simpleExtractResources( getClass(), "/junit-twoTestCases" );
43  
44          Verifier verifier = new Verifier( testDir.getAbsolutePath() );
45          this.executeGoal( verifier, "test" );
46          verifier.verifyErrorFreeLog();
47          verifier.resetStreams();
48  
49          HelperAssertions.assertTestSuiteResults( 2, 0, 0, 0, testDir );
50      }
51  
52      /**
53       * Runs two tests encapsulated in a suite
54       */
55      public void testTwoTestCaseSuite()
56          throws Exception
57      {
58          File testDir = ResourceExtractor.simpleExtractResources( getClass(), "/junit-twoTestCaseSuite" );
59  
60          Verifier verifier = new Verifier( testDir.getAbsolutePath() );
61          this.executeGoal( verifier, "test" );
62          verifier.verifyErrorFreeLog();
63          verifier.resetStreams();
64          List<ReportTestSuite> reports = HelperAssertions.extractReports( ( new File[]{ testDir } ) );
65          Set<String> classNames = extractClassNames( reports );
66          assertContains( classNames, "junit.twoTestCaseSuite.BasicTest" );
67          assertContains( classNames, "junit.twoTestCaseSuite.TestTwo" );
68          assertEquals( "wrong number of classes", 2, classNames.size() );
69          IntegrationTestSuiteResults results = HelperAssertions.parseReportList( reports );
70          HelperAssertions.assertTestSuiteResults( 2, 0, 0, 0, results );
71      }
72  
73      private void assertContains( Set<String> set, String expected )
74      {
75          if ( set.contains( expected ) )
76          {
77              return;
78          }
79          fail( "Set didn't contain " + expected );
80      }
81  
82      private Set<String> extractClassNames( List<ReportTestSuite> reports )
83      {
84          HashSet<String> classNames = new HashSet<String>();
85          for ( ReportTestSuite suite : reports )
86          {
87              classNames.add( suite.getFullClassName() );
88          }
89          return classNames;
90      }
91  
92      public void testJunit4Suite()
93          throws Exception
94      {
95          File testDir = ResourceExtractor.simpleExtractResources( getClass(), "/junit4-twoTestCaseSuite" );
96  
97          Verifier verifier = new Verifier( testDir.getAbsolutePath() );
98          this.executeGoal( verifier, "test" );
99          verifier.verifyErrorFreeLog();
100         verifier.resetStreams();
101 
102         List<ReportTestSuite> reports = HelperAssertions.extractReports( ( new File[]{ testDir } ) );
103         Set<String> classNames = extractClassNames( reports );
104         assertContains( classNames, "twoTestCaseSuite.BasicTest" );
105         assertContains( classNames, "twoTestCaseSuite.Junit4TestTwo" );
106         assertEquals( "wrong number of classes", 2, classNames.size() );
107         IntegrationTestSuiteResults results = HelperAssertions.parseReportList( reports );
108         HelperAssertions.assertTestSuiteResults( 2, 0, 0, 0, results );
109     }
110 
111     public void testTestNGSuite()
112         throws Exception
113     {
114         File testDir = ResourceExtractor.simpleExtractResources( getClass(), "/testng-twoTestCaseSuite" );
115 
116         Verifier verifier = new Verifier( testDir.getAbsolutePath() );
117         this.executeGoal( verifier, "test" );
118         verifier.verifyErrorFreeLog();
119         verifier.resetStreams();
120 
121         List<ReportTestSuite> reports = HelperAssertions.extractReports( ( new File[]{ testDir } ) );
122         Set<String> classNames = extractClassNames( reports );
123         assertContains( classNames, "testng.two.TestNGTestTwo" );
124         assertContains( classNames, "testng.two.TestNGSuiteTest" );
125         assertEquals( "wrong number of classes", 2, classNames.size() );
126         IntegrationTestSuiteResults results = HelperAssertions.parseReportList( reports );
127         HelperAssertions.assertTestSuiteResults( 2, 0, 0, 0, results );
128     }
129 
130 }