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.plugin.surefire.report;
20  
21  import java.util.List;
22  
23  import org.apache.maven.plugin.surefire.log.api.ConsoleLogger;
24  import org.apache.maven.plugin.surefire.log.api.Level;
25  import org.apache.maven.surefire.api.report.TestSetReportEntry;
26  import org.apache.maven.surefire.extensions.StatelessTestsetInfoConsoleReportEventListener;
27  import org.apache.maven.surefire.shared.utils.logging.MessageBuilder;
28  
29  import static org.apache.maven.plugin.surefire.log.api.Level.resolveLevel;
30  import static org.apache.maven.plugin.surefire.report.TestSetStats.concatenateWithTestGroup;
31  import static org.apache.maven.surefire.shared.utils.logging.MessageUtils.buffer;
32  
33  /**
34   * Base class for console reporters.
35   *
36   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
37   * @author Kristian Rosenvold
38   */
39  public class ConsoleReporter extends StatelessTestsetInfoConsoleReportEventListener<WrappedReportEntry, TestSetStats> {
40      public static final String BRIEF = "brief";
41      public static final String PLAIN = "plain";
42      private static final String TEST_SET_STARTING_PREFIX = "Running ";
43  
44      private final boolean usePhrasedClassNameInRunning;
45      private final boolean usePhrasedClassNameInTestCaseSummary;
46  
47      public ConsoleReporter(
48              ConsoleLogger logger, boolean usePhrasedClassNameInRunning, boolean usePhrasedClassNameInTestCaseSummary) {
49          super(logger);
50          this.usePhrasedClassNameInRunning = usePhrasedClassNameInRunning;
51          this.usePhrasedClassNameInTestCaseSummary = usePhrasedClassNameInTestCaseSummary;
52      }
53  
54      @Override
55      public void testSetStarting(TestSetReportEntry report) {
56          MessageBuilder builder = buffer().a(TEST_SET_STARTING_PREFIX);
57          String runningTestCase = concatenateWithTestGroup(builder, report, usePhrasedClassNameInRunning);
58          getConsoleLogger().info(runningTestCase);
59      }
60  
61      @Override
62      public void testSetCompleted(WrappedReportEntry report, TestSetStats testSetStats, List<String> testResults) {
63          boolean success = testSetStats.getCompletedCount() > 0;
64          boolean failures = testSetStats.getFailures() > 0;
65          boolean errors = testSetStats.getErrors() > 0;
66          boolean skipped = testSetStats.getSkipped() > 0;
67          boolean flakes = testSetStats.getSkipped() > 0;
68          Level level = resolveLevel(success, failures, errors, skipped, flakes);
69  
70          println(testSetStats.getColoredTestSetSummary(report, usePhrasedClassNameInTestCaseSummary), level);
71          for (String testResult : testResults) {
72              println(testResult, level);
73          }
74      }
75  
76      @Override
77      public void reset() {}
78  
79      private void println(String message, Level level) {
80          switch (level) {
81              case FAILURE:
82                  getConsoleLogger().error(message);
83                  break;
84              case UNSTABLE:
85                  getConsoleLogger().warning(message);
86                  break;
87              default:
88                  getConsoleLogger().info(message);
89          }
90      }
91  }