View Javadoc
1   package org.apache.maven.plugin.surefire.report;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.List;
23  
24  import org.apache.maven.plugin.surefire.log.api.ConsoleLogger;
25  import org.apache.maven.surefire.shared.utils.logging.MessageBuilder;
26  import org.apache.maven.plugin.surefire.log.api.Level;
27  import org.apache.maven.surefire.extensions.StatelessTestsetInfoConsoleReportEventListener;
28  import org.apache.maven.surefire.api.report.TestSetReportEntry;
29  
30  import static org.apache.maven.plugin.surefire.log.api.Level.resolveLevel;
31  import static org.apache.maven.plugin.surefire.report.TestSetStats.concatenateWithTestGroup;
32  import static org.apache.maven.surefire.shared.utils.logging.MessageUtils.buffer;
33  
34  /**
35   * Base class for console reporters.
36   *
37   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
38   * @author Kristian Rosenvold
39   */
40  public class ConsoleReporter
41          extends StatelessTestsetInfoConsoleReportEventListener<WrappedReportEntry, TestSetStats>
42  {
43      public static final String BRIEF = "brief";
44      public static final String PLAIN = "plain";
45      private static final String TEST_SET_STARTING_PREFIX = "Running ";
46  
47      private final boolean usePhrasedClassNameInRunning;
48      private final boolean usePhrasedClassNameInTestCaseSummary;
49  
50      public ConsoleReporter( ConsoleLogger logger,
51                              boolean usePhrasedClassNameInRunning, boolean usePhrasedClassNameInTestCaseSummary )
52      {
53          super( logger );
54          this.usePhrasedClassNameInRunning = usePhrasedClassNameInRunning;
55          this.usePhrasedClassNameInTestCaseSummary = usePhrasedClassNameInTestCaseSummary;
56      }
57  
58      @Override
59      public void testSetStarting( TestSetReportEntry report )
60      {
61          MessageBuilder builder = buffer().a( TEST_SET_STARTING_PREFIX );
62          String runningTestCase = concatenateWithTestGroup( builder, report, usePhrasedClassNameInRunning );
63          getConsoleLogger()
64                  .info( runningTestCase );
65      }
66  
67      @Override
68      public void testSetCompleted( WrappedReportEntry report, TestSetStats testSetStats, List<String> testResults )
69      {
70          boolean success = testSetStats.getCompletedCount() > 0;
71          boolean failures = testSetStats.getFailures() > 0;
72          boolean errors = testSetStats.getErrors() > 0;
73          boolean skipped = testSetStats.getSkipped() > 0;
74          boolean flakes = testSetStats.getSkipped() > 0;
75          Level level = resolveLevel( success, failures, errors, skipped, flakes );
76  
77          println( testSetStats.getColoredTestSetSummary( report, usePhrasedClassNameInTestCaseSummary ), level );
78          for ( String testResult : testResults )
79          {
80              println( testResult, level );
81          }
82      }
83  
84      @Override
85      public void reset()
86      {
87      }
88  
89      private void println( String message, Level level )
90      {
91          switch ( level )
92          {
93              case FAILURE:
94                  getConsoleLogger()
95                          .error( message );
96                  break;
97              case UNSTABLE:
98                  getConsoleLogger()
99                          .warning( message );
100                 break;
101             default:
102                 getConsoleLogger()
103                         .info( message );
104         }
105     }
106 }