View Javadoc

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