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