View Javadoc

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