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