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