1   package org.apache.maven.surefire.its;
2   
3   import java.io.File;
4   import java.util.HashSet;
5   import java.util.List;
6   import java.util.Set;
7   
8   import junit.framework.TestCase;
9   
10  import org.apache.maven.it.Verifier;
11  import org.apache.maven.it.util.ResourceExtractor;
12  import org.apache.maven.plugins.surefire.report.ReportTestSuite;
13  
14  /**
15   * Test running two test cases; confirms reporting works correctly
16   * 
17   * @author <a href="mailto:dfabulich@apache.org">Dan Fabulich</a>
18   */
19  public class TwoTestCasesTest
20      extends TestCase
21  {
22      public void testTwoTestCases()
23          throws Exception
24      {
25          File testDir = ResourceExtractor.simpleExtractResources( getClass(), "/junit-twoTestCases" );
26  
27          Verifier verifier = new Verifier( testDir.getAbsolutePath() );
28          verifier.executeGoal( "test" );
29          verifier.verifyErrorFreeLog();
30          verifier.resetStreams();
31  
32          HelperAssertions.assertTestSuiteResults( 2, 0, 0, 0, testDir );
33      }
34  
35      /** Runs two tests encapsulated in a suite */
36      public void testTwoTestCaseSuite()
37          throws Exception
38      {
39          File testDir = ResourceExtractor.simpleExtractResources( getClass(), "/junit-twoTestCaseSuite" );
40  
41          Verifier verifier = new Verifier( testDir.getAbsolutePath() );
42          verifier.executeGoal( "test" );
43          verifier.verifyErrorFreeLog();
44          verifier.resetStreams();
45          List reports = HelperAssertions.extractReports( (new File[] { testDir }) );
46          Set classNames = extractClassNames( reports );
47          assertContains( classNames, "junit.twoTestCaseSuite.BasicTest" );
48          assertContains( classNames, "junit.twoTestCaseSuite.TestTwo" );
49          assertEquals( "wrong number of classes", 2, classNames.size() );
50          ITSuiteResults results = HelperAssertions.parseReportList( reports );
51          HelperAssertions.assertTestSuiteResults( 2, 0, 0, 0, results );
52      }
53      
54      private void assertContains( Set set, String expected )
55      {
56          if ( set.contains( expected ) ) return;
57          fail( "Set didn't contain " + expected );
58      }
59      
60      private Set extractClassNames( List reports )
61      {
62          ReportTestSuite suite;
63          HashSet classNames = new HashSet();
64          for ( int i = 0; i < reports.size(); i++ )
65          {
66              suite = (ReportTestSuite) reports.get( i );
67              classNames.add( suite.getFullClassName() );
68          }
69          return classNames;
70      }
71  
72      public void testJunit4Suite()
73          throws Exception
74      {
75          File testDir = ResourceExtractor.simpleExtractResources( getClass(), "/junit4-twoTestCaseSuite" );
76  
77          Verifier verifier = new Verifier( testDir.getAbsolutePath() );
78          verifier.executeGoal( "test" );
79          verifier.verifyErrorFreeLog();
80          verifier.resetStreams();
81  
82          List reports = HelperAssertions.extractReports( (new File[] { testDir }) );
83          Set classNames = extractClassNames( reports );
84          assertContains( classNames, "twoTestCaseSuite.BasicTest" );
85          assertContains( classNames, "twoTestCaseSuite.Junit4TestTwo" );
86          assertEquals( "wrong number of classes", 2, classNames.size() );
87          ITSuiteResults results = HelperAssertions.parseReportList( reports );
88          HelperAssertions.assertTestSuiteResults( 2, 0, 0, 0, results );
89      }
90  
91      public void testTestNGSuite()
92          throws Exception
93      {
94          File testDir = ResourceExtractor.simpleExtractResources( getClass(), "/testng-twoTestCaseSuite" );
95  
96          Verifier verifier = new Verifier( testDir.getAbsolutePath() );
97          verifier.executeGoal( "test" );
98          verifier.verifyErrorFreeLog();
99          verifier.resetStreams();
100 
101         List reports = HelperAssertions.extractReports( (new File[] { testDir }) );
102         Set classNames = extractClassNames( reports );
103         assertContains( classNames, "testng.two.TestNGTestTwo" );
104         assertContains( classNames, "testng.two.TestNGSuiteTest" );
105         assertEquals( "wrong number of classes", 2, classNames.size() );
106         ITSuiteResults results = HelperAssertions.parseReportList( reports );
107         HelperAssertions.assertTestSuiteResults( 2, 0, 0, 0, results );
108     }
109 
110 }