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