View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.surefire.its.fixture;
20  
21  import java.io.File;
22  import java.util.ArrayList;
23  import java.util.Comparator;
24  import java.util.List;
25  
26  import org.apache.maven.plugin.surefire.log.api.ConsoleLogger;
27  import org.apache.maven.plugin.surefire.log.api.PrintStreamLogger;
28  import org.apache.maven.plugins.surefire.report.ReportTestSuite;
29  import org.apache.maven.plugins.surefire.report.SurefireReportParser;
30  
31  import static java.lang.Double.parseDouble;
32  import static java.nio.charset.StandardCharsets.UTF_8;
33  import static junit.framework.Assert.assertEquals;
34  import static junit.framework.Assert.assertFalse;
35  import static junit.framework.Assert.assertTrue;
36  import static org.junit.Assume.assumeTrue;
37  
38  /**
39   *
40   */
41  @SuppressWarnings({"JavaDoc"})
42  public final class HelperAssertions {
43      private HelperAssertions() {
44          throw new IllegalStateException("no instantiable constructor");
45      }
46  
47      /**
48       * assert that the reports in the specified testDir have the right summary statistics
49       */
50      public static void assertTestSuiteResults(int total, int errors, int failures, int skipped, File testDir) {
51          IntegrationTestSuiteResults suite = parseTestResults(testDir);
52          assertTestSuiteResults(total, errors, failures, skipped, suite);
53      }
54  
55      public static void assertTestSuiteResults(
56              int total, int errors, int failures, int skipped, int flakes, File testDir) {
57          IntegrationTestSuiteResults suite = parseTestResults(testDir);
58          assertTestSuiteResults(total, errors, failures, skipped, flakes, suite);
59      }
60  
61      public static void assertTestSuiteResults(int total, File testDir) {
62          IntegrationTestSuiteResults suite = parseTestResults(testDir);
63          assertTestSuiteResults(total, suite);
64      }
65  
66      /**
67       * assert that the reports in the specified testDir have the right summary statistics
68       */
69      public static void assertIntegrationTestSuiteResults(
70              int total, int errors, int failures, int skipped, File testDir) {
71          IntegrationTestSuiteResults suite = parseIntegrationTestResults(testDir);
72          assertTestSuiteResults(total, errors, failures, skipped, suite);
73      }
74  
75      public static void assertIntegrationTestSuiteResults(int total, File testDir) {
76          IntegrationTestSuiteResults suite = parseIntegrationTestResults(testDir);
77          assertTestSuiteResults(total, suite);
78      }
79  
80      public static void assertTestSuiteResults(
81              int total, int errors, int failures, int skipped, IntegrationTestSuiteResults actualSuite) {
82          assertEquals("wrong number of tests", total, actualSuite.getTotal());
83          assertEquals("wrong number of errors", errors, actualSuite.getErrors());
84          assertEquals("wrong number of failures", failures, actualSuite.getFailures());
85          assertEquals("wrong number of skipped", skipped, actualSuite.getSkipped());
86      }
87  
88      public static void assertTestSuiteResults(int total, IntegrationTestSuiteResults actualSuite) {
89          assertEquals("wrong number of tests", total, actualSuite.getTotal());
90      }
91  
92      public static void assertTestSuiteResults(
93              int total, int errors, int failures, int skipped, int flakes, IntegrationTestSuiteResults actualSuite) {
94          assertTestSuiteResults(total, errors, failures, skipped, actualSuite);
95          assertEquals("wrong number of flaky tests", flakes, actualSuite.getFlakes());
96      }
97  
98      public static IntegrationTestSuiteResults parseTestResults(File... testDirs) {
99          List<ReportTestSuite> reports = extractReports(testDirs);
100         return parseReportList(reports);
101     }
102 
103     private static IntegrationTestSuiteResults parseIntegrationTestResults(File... testDirs) {
104         List<ReportTestSuite> reports = extractITReports(testDirs);
105         return parseReportList(reports);
106     }
107 
108     /**
109      * Converts a list of ReportTestSuites into an IntegrationTestSuiteResults object, suitable for summary assertions
110      */
111     public static IntegrationTestSuiteResults parseReportList(List<ReportTestSuite> reports) {
112         assertFalse("No reports!", reports.isEmpty());
113         int total = 0, errors = 0, failures = 0, skipped = 0, flakes = 0;
114         for (ReportTestSuite report : reports) {
115             total += report.getNumberOfTests();
116             errors += report.getNumberOfErrors();
117             failures += report.getNumberOfFailures();
118             skipped += report.getNumberOfSkipped();
119             flakes += report.getNumberOfFlakes();
120         }
121         return new IntegrationTestSuiteResults(total, errors, failures, skipped, flakes);
122     }
123 
124     public static List<ReportTestSuite> extractReports(File... testDirs) {
125         List<File> reportsDirs = new ArrayList<>();
126         for (File testDir : testDirs) {
127             File reportsDir = new File(testDir, "target/surefire-reports");
128             assertTrue("Reports directory is missing: " + reportsDir.getAbsolutePath(), reportsDir.exists());
129             reportsDirs.add(reportsDir);
130         }
131         ConsoleLogger logger = new PrintStreamLogger(System.out);
132         SurefireReportParser parser = new SurefireReportParser(reportsDirs, logger);
133         try {
134             List<ReportTestSuite> suites = parser.parseXMLReportFiles();
135             suites.sort(Comparator.comparing(ReportTestSuite::getLastModified));
136             return suites;
137         } catch (Exception e) {
138             throw new RuntimeException("Couldn't parse XML reports", e);
139         }
140     }
141 
142     private static List<ReportTestSuite> extractITReports(File... testDirs) {
143         List<File> reportsDirs = new ArrayList<>();
144         for (File testDir : testDirs) {
145             File reportsDir = new File(testDir, "target/failsafe-reports");
146             assertTrue("Reports directory is missing: " + reportsDir.getAbsolutePath(), reportsDir.exists());
147             reportsDirs.add(reportsDir);
148         }
149         ConsoleLogger logger = new PrintStreamLogger(System.out);
150         SurefireReportParser parser = new SurefireReportParser(reportsDirs, logger);
151         try {
152             return parser.parseXMLReportFiles();
153         } catch (Exception e) {
154             throw new RuntimeException("Couldn't parse XML reports", e);
155         }
156     }
157 
158     public static void assumeJavaVersion(double expectedVersion) {
159         String thisVersion = System.getProperty("java.specification.version");
160         assumeTrue("java.specification.version: " + thisVersion, parseDouble(thisVersion) >= expectedVersion);
161     }
162 
163     public static void assumeJavaMaxVersion(double expectedMaxVersion) {
164         String thisVersion = System.getProperty("java.specification.version");
165         assumeTrue("java.specification.version: " + thisVersion, parseDouble(thisVersion) <= expectedMaxVersion);
166     }
167 
168     public static void assumeJavaVersionExcluded(double excludedVersion) {
169         String thisVersion = System.getProperty("java.specification.version");
170         assumeTrue("java.specification.version: " + thisVersion, parseDouble(thisVersion) != excludedVersion);
171     }
172 
173     public static String convertUnicodeToUTF8(String unicode) {
174         return new String(unicode.getBytes(UTF_8), UTF_8);
175     }
176 }