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