View Javadoc

1   package org.apache.maven.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  
27  /**
28   * Base class for console reporters.
29   *
30   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
31   */
32  public abstract class AbstractConsoleReporter
33      extends AbstractTextReporter
34      implements RunReporter
35  {
36      private static final String TEST_SET_STARTING_PREFIX = "Running ";
37  
38      private static final String TEST_SET_STARTING_GROUP_PREFIX = " (of ";
39  
40      private static final String TEST_SET_STARTING_GROUP_SUFIX = ")";
41  
42      private static final int BUFFER_SIZE = 4096;
43  
44      private static final PrintStream ORIGINAL_SYSTEM_OUT = System.out;
45  
46      AbstractConsoleReporter( String format, ReporterConfiguration reporterConfiguration )
47      {
48          // TODO: use logger
49          super(
50              new PrintWriter( new OutputStreamWriter( new BufferedOutputStream( ORIGINAL_SYSTEM_OUT, BUFFER_SIZE ) ) ),
51              format, reporterConfiguration );
52      }
53  
54      public void testSetStarting( ReportEntry report )
55          throws ReporterException
56      {
57          super.testSetStarting( report );
58  
59          writeMessage( getTestSetStartingMessage( report ) );
60      }
61  
62      public void runStarting()
63      {
64          writeHeading( "" );
65          writeHeading( "-------------------------------------------------------" );
66          writeHeading( " T E S T S" );
67          writeHeading( "-------------------------------------------------------" );
68      }
69  
70      void writeHeading( String message )
71      {
72          writer.println( message );
73          writer.flush();
74      }
75  
76      /**
77       * Get the test set starting message for a report.
78       * eg. "Running org.foo.BarTest ( of group )"
79       *
80       * @param report report whose test set is starting
81       * @return the message
82       */
83      public static String getTestSetStartingMessage( ReportEntry report )
84      {
85          StringBuffer message = new StringBuffer();
86          message.append( TEST_SET_STARTING_PREFIX );
87          message.append( report.getName() );
88  
89          if ( report.getGroup() != null && !report.getName().equals( report.getGroup() ) )
90          {
91              message.append( TEST_SET_STARTING_GROUP_PREFIX );
92              message.append( report.getGroup() );
93              message.append( TEST_SET_STARTING_GROUP_SUFIX );
94          }
95          return message.toString();
96      }
97  
98      /**
99       * Parses a Surefire test set starting message into a {@link ReportEntry} object.
100      * Only name and group will be set if applicable.
101      *
102      * @param message The test starting message
103      * @return the parsed {@link ReportEntry}
104      */
105     public static ReportEntry parseTestSetStartingMessage( String message )
106     {
107         String name;
108         String group = null;
109         int i = message.indexOf( TEST_SET_STARTING_GROUP_PREFIX );
110         int j;
111         if ( i >= 0 )
112         {
113             j = message.indexOf( TEST_SET_STARTING_GROUP_SUFIX );
114             if ( j <= 0 )
115             {
116                 throw new RuntimeException( "Message provided can not be parsed" );
117             }
118             group = message.substring( i + TEST_SET_STARTING_GROUP_PREFIX.length(), j );
119         }
120         else
121         {
122             i = message.length();
123             if ( i <= 0 )
124             {
125                 throw new RuntimeException( "Message provided can not be parsed" );
126             }
127         }
128         name = message.substring( TEST_SET_STARTING_PREFIX.length(), i );
129         return CategorizedReportEntry.nameGroup( name, group );
130     }
131 
132     /**
133      * Check if the String passed as argument is a "test starting" message.
134      * If so it can be passed to {@link #parseTestSetStartingMessage(String)}
135      *
136      * @param message the message to check
137      * @return true if it is a "test starting" message
138      */
139     public static boolean isTestSetStartingMessage( String message )
140     {
141         return message.startsWith( TEST_SET_STARTING_PREFIX );
142     }
143 
144 }