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