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 junit.framework.Assert;
22  import org.apache.maven.plugins.surefire.report.ReportTestSuite;
23  import org.apache.maven.plugins.surefire.report.SurefireReportParser;
24  import org.apache.maven.reporting.MavenReportException;
25  
26  import java.io.File;
27  import java.util.List;
28  
29  public class HelperAssertions
30  {
31      /**
32       * assert that the reports in the specified testDir have the right summary statistics
33       */
34      public static void assertTestSuiteResults( int total, int errors, int failures, int skipped, File testDir )
35          throws MavenReportException
36      {
37          IntegrationTestSuiteResults suite = parseTestResults( new File[]{ testDir } );
38          assertTestSuiteResults( total, errors, failures, skipped, suite );
39      }
40  
41      protected static void assertTestSuiteResults( int total, int errors, int failures, int skipped,
42                                                    IntegrationTestSuiteResults actualSuite )
43      {
44          Assert.assertEquals( "wrong number of tests", total, actualSuite.getTotal() );
45          Assert.assertEquals( "wrong number of errors", errors, actualSuite.getErrors() );
46          Assert.assertEquals( "wrong number of failures", failures, actualSuite.getFailures() );
47          Assert.assertEquals( "wrong number of skipped", skipped, actualSuite.getSkipped() );
48      }
49  
50      protected static IntegrationTestSuiteResults parseTestResults( File testDir )
51          throws MavenReportException
52      {
53          return parseTestResults( new File[]{ testDir } );
54      }
55  
56      protected static IntegrationTestSuiteResults parseTestResults( File[] testDirs )
57          throws MavenReportException
58      {
59          List reports = extractReports( testDirs );
60          IntegrationTestSuiteResults results = parseReportList( reports );
61          return results;
62      }
63  
64      /**
65       * Converts a list of ReportTestSuites into an IntegrationTestSuiteResults object, suitable for summary assertions
66       */
67      protected static IntegrationTestSuiteResults parseReportList( List reports )
68      {
69          Assert.assertTrue( "No reports!", reports.size() > 0 );
70          int total = 0, errors = 0, failures = 0, skipped = 0;
71          for ( int i = 0; i < reports.size(); i++ )
72          {
73              ReportTestSuite suite = (ReportTestSuite) reports.get( i );
74              total += suite.getNumberOfTests();
75              errors += suite.getNumberOfErrors();
76              failures += suite.getNumberOfFailures();
77              skipped += suite.getNumberOfSkipped();
78          }
79          IntegrationTestSuiteResults results = new IntegrationTestSuiteResults( total, errors, failures, skipped );
80          return results;
81      }
82  
83      protected static List extractReports( File testDir )
84      {
85          return extractReports( new File[]{ testDir } );
86      }
87  
88      /**
89       * Extracts a list of ReportTestSuites from the specified testDirs
90       */
91      protected static List extractReports( File[] testDirs )
92      {
93          SurefireReportParser parser = new SurefireReportParser();
94          File[] reportsDirs = new File[testDirs.length];
95          for ( int i = 0; i < testDirs.length; i++ )
96          {
97              File testDir = testDirs[i];
98              File reportsDir = new File( testDir, "target/surefire-reports" );
99              Assert.assertTrue( "Reports directory is missing: " + reportsDir.getAbsolutePath(), reportsDir.exists() );
100             reportsDirs[i] = reportsDir;
101         }
102         parser.setReportsDirectories( reportsDirs );
103         List reports;
104         try
105         {
106             reports = parser.parseXMLReportFiles();
107         }
108         catch ( Exception e )
109         {
110             throw new RuntimeException( "Couldn't parse XML reports", e );
111         }
112         return reports;
113     }
114 }