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.io.BufferedOutputStream;
23  import java.io.OutputStreamWriter;
24  import java.io.PrintStream;
25  import java.io.PrintWriter;
26  import java.util.List;
27  import org.apache.maven.surefire.report.ReportEntry;
28  
29  /**
30   * Base class for console reporters.
31   *
32   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
33   * @author Kristian Rosenvold
34   */
35  public class ConsoleReporter
36  {
37      public static final String BRIEF = "brief";
38  
39      public static final String PLAIN = "plain";
40  
41      private static final String TEST_SET_STARTING_PREFIX = "Running ";
42  
43      private static final int BUFFER_SIZE = 4096;
44  
45      private final PrintWriter writer;
46  
47  
48      public ConsoleReporter( PrintStream originalSystemOut )
49      {
50          OutputStreamWriter out = new OutputStreamWriter( new BufferedOutputStream( originalSystemOut, BUFFER_SIZE ) );
51          this.writer = new PrintWriter( out );
52      }
53  
54      public void testSetStarting( ReportEntry report )
55      {
56          writeMessage( getTestSetStartingMessage( report ) );
57      }
58  
59      public void writeMessage( String message )
60      {
61          if ( writer != null )
62          {
63              writer.print( message );
64  
65              writer.flush();
66          }
67      }
68  
69      public void writeLnMessage( String message )
70      {
71          if ( writer != null )
72          {
73              writer.println( message );
74  
75              writer.flush();
76          }
77      }
78  
79      public void testSetCompleted( WrappedReportEntry report, TestSetStats testSetStats, List<String> testResults )
80      {
81          writeMessage( testSetStats.getTestSetSummary( report ) );
82  
83          if ( testResults != null )
84          {
85              for ( String testResult : testResults )
86              {
87                  writeLnMessage( testResult );
88              }
89          }
90      }
91  
92  
93      public void reset()
94      {
95          if ( writer != null )
96          {
97              writer.flush();
98          }
99      }
100 
101     /**
102      * Get the test set starting message for a report.
103      * eg. "Running org.foo.BarTest ( of group )"
104      *
105      * @param report report whose test set is starting
106      * @return the message
107      */
108     static String getTestSetStartingMessage( ReportEntry report )
109     {
110         StringBuilder message = new StringBuilder();
111         message.append( TEST_SET_STARTING_PREFIX );
112         message.append( report.getNameWithGroup() );
113 
114         message.append( "\n" );
115         return message.toString();
116     }
117 
118 
119 }