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