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.PrintWriter;
23  import java.util.ArrayList;
24  import java.util.Iterator;
25  import java.util.List;
26  
27  /**
28   * Text based reporter.
29   *
30   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
31   */
32  public abstract class AbstractTextReporter
33      extends AbstractReporter
34  {
35      protected static final String BRIEF = "brief";
36  
37      protected static final String PLAIN = "plain";
38  
39      protected static final String SUMMARY = "summary";
40  
41      protected PrintWriter writer;
42  
43      private static final String TEST_SET_COMPLETED_PREFIX = "Tests run: ";
44  
45      private final String format;
46  
47      private List testResults;
48  
49      protected AbstractTextReporter( ReporterConfiguration reporterConfiguration, String format )
50      {
51          super( reporterConfiguration );
52  
53          this.format = format;
54      }
55  
56  
57      protected AbstractTextReporter( PrintWriter writer, String format, ReporterConfiguration reporterConfiguration )
58      {
59          super( reporterConfiguration );
60  
61          this.writer = writer;
62  
63          this.format = format;
64      }
65  
66      public void setWriter( PrintWriter writer )
67      {
68          this.writer = writer;
69      }
70  
71      public void writeMessage( String message )
72      {
73          if ( writer != null )
74          {
75              writer.println( message );
76  
77              writer.flush();
78          }
79      }
80  
81      public void writeDetailMessage( String message )
82      {
83          writeMessage( message );
84      }
85  
86      public void testSucceeded( ReportEntry report )
87      {
88          super.testSucceeded( report );
89  
90          if ( PLAIN.equals( format ) )
91          {
92              testResults.add( getElapsedTimeSummary( report ) );
93          }
94      }
95  
96      public void testSkipped( ReportEntry report )
97      {
98          super.testSkipped( report );
99  
100         if ( PLAIN.equals( format ) )
101         {
102             testResults.add( report.getName() + " skipped" );
103         }
104     }
105 
106     public void testError( ReportEntry report, String stdOut, String stdErr )
107     {
108         super.testError( report, stdOut, stdErr );
109 
110         testResults.add( getOutput( report, "ERROR" ) );
111     }
112 
113     public void testFailed( ReportEntry report, String stdOut, String stdErr )
114     {
115         super.testFailed( report, stdOut, stdErr );
116 
117         testResults.add( getOutput( report, "FAILURE" ) );
118     }
119 
120     public void testSetStarting( ReportEntry report )
121         throws ReporterException
122     {
123         super.testSetStarting( report );
124 
125         testResults = new ArrayList();
126     }
127 
128     public void testSetCompleted( ReportEntry report )
129         throws ReporterException
130     {
131         super.testSetCompleted( report );
132 
133         writeMessage( getTestSetSummary( report ) );
134 
135         if ( format.equals( BRIEF ) || format.equals( PLAIN ) )
136         {
137             for ( Iterator i = testResults.iterator(); i.hasNext(); )
138             {
139                 writeMessage( (String) i.next() );
140             }
141         }
142     }
143 
144     protected String getTestSetSummary( ReportEntry report )
145     {
146         StringBuffer buf = new StringBuffer();
147 
148         buf.append( TEST_SET_COMPLETED_PREFIX );
149         buf.append( completedCount );
150         buf.append( ", Failures: " );
151         buf.append( failures );
152         buf.append( ", Errors: " );
153         buf.append( errors );
154         buf.append( ", Skipped: " );
155         buf.append( skipped );
156         buf.append( ", Time elapsed: " );
157         int elapsed = report.getElapsed() != null
158             ? report.getElapsed().intValue()
159             : (int) ( System.currentTimeMillis() - testSetStartTime );
160         buf.append( elapsedTimeAsString( elapsed ) );
161         buf.append( " sec" );
162 
163         if ( failures > 0 || errors > 0 )
164         {
165             buf.append( " <<< FAILURE!" );
166         }
167 
168         return buf.toString();
169     }
170 
171     protected String getElapsedTimeSummary( ReportEntry report )
172     {
173         StringBuffer reportContent = new StringBuffer();
174         long runTime = getActualRunTime( report );
175 
176         reportContent.append( report.getName() );
177         reportContent.append( "  Time elapsed: " );
178         reportContent.append( elapsedTimeAsString( runTime ) );
179         reportContent.append( " sec" );
180 
181         return reportContent.toString();
182     }
183 
184     protected String getOutput( ReportEntry report, String msg )
185     {
186         StringBuffer buf = new StringBuffer();
187 
188         buf.append( getElapsedTimeSummary( report ) );
189 
190         buf.append( "  <<< " ).append( msg ).append( "!" ).append( NL );
191 
192         buf.append( getStackTrace( report ) );
193 
194         return buf.toString();
195     }
196 
197     /**
198      * Check if the String passed as argument is a "test set completed" message.
199      *
200      * @param message the message to check
201      * @return true if it is a "test set completed" message
202      */
203     public static boolean isTestSetCompletedMessage( String message )
204     {
205         return message.startsWith( TEST_SET_COMPLETED_PREFIX );
206     }
207 
208     public void reset()
209     {
210         super.reset();
211         if ( writer != null )
212         {
213             writer.flush();
214         }
215     }
216 }