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      static final String BRIEF = "brief";
36  
37      static final String PLAIN = "plain";
38  
39      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  
50      protected AbstractTextReporter( boolean trimStackTrace, String format )
51      {
52          super( trimStackTrace );
53          this.format = format;
54      }
55  
56      protected AbstractTextReporter( PrintWriter writer, boolean trimStackTrace, String format )
57      {
58          this( trimStackTrace, format);
59          this.writer = writer;
60      }
61  
62  
63      public void setWriter( PrintWriter writer )
64      {
65          this.writer = writer;
66      }
67  
68      public void writeMessage( String message )
69      {
70          if ( writer != null )
71          {
72              writer.print( message );
73  
74              writer.flush();
75          }
76      }
77  
78      public void testSucceeded( ReportEntry report )
79      {
80          super.testSucceeded( report );
81  
82          if ( PLAIN.equals( format ) )
83          {
84              testResults.add( getElapsedTimeSummary( report ) );
85          }
86      }
87  
88      public void testSkipped( ReportEntry report )
89      {
90          super.testSkipped( report );
91  
92          if ( PLAIN.equals( format ) )
93          {
94              testResults.add( report.getName() + " skipped" );
95          }
96      }
97  
98      public void testError( ReportEntry report, String stdOut, String stdErr )
99      {
100         super.testError( report, stdOut, stdErr );
101 
102         testResults.add( getOutput( report, "ERROR" ) );
103     }
104 
105     public void testFailed( ReportEntry report, String stdOut, String stdErr )
106     {
107         super.testFailed( report, stdOut, stdErr );
108 
109         testResults.add( getOutput( report, "FAILURE" ) );
110     }
111 
112     public void testSetStarting( ReportEntry report )
113         throws ReporterException
114     {
115         super.testSetStarting( report );
116 
117         testResults = new ArrayList();
118     }
119 
120     public void testSetCompleted( ReportEntry report )
121         throws ReporterException
122     {
123         super.testSetCompleted( report );
124 
125         writeMessage( getTestSetSummary( report ) );
126 
127         if ( format.equals( BRIEF ) || format.equals( PLAIN ) )
128         {
129             for ( Iterator i = testResults.iterator(); i.hasNext(); )
130             {
131                 writeMessage( (String) i.next() );
132             }
133         }
134     }
135 
136     protected String getTestSetSummary( ReportEntry report )
137     {
138         StringBuffer buf = new StringBuffer();
139 
140         buf.append( TEST_SET_COMPLETED_PREFIX );
141         buf.append( completedCount );
142         buf.append( ", Failures: " );
143         buf.append( failures );
144         buf.append( ", Errors: " );
145         buf.append( errors );
146         buf.append( ", Skipped: " );
147         buf.append( skipped );
148         buf.append( ", Time elapsed: " );
149         int elapsed = report.getElapsed() != null
150             ? report.getElapsed().intValue()
151             : (int) ( System.currentTimeMillis() - testSetStartTime );
152         buf.append( elapsedTimeAsString( elapsed ) );
153         buf.append( " sec" );
154 
155         if ( failures > 0 || errors > 0 )
156         {
157             buf.append( " <<< FAILURE!" );
158         }
159 
160         buf.append( "\n" );
161 
162         return buf.toString();
163     }
164 
165     protected String getElapsedTimeSummary( ReportEntry report )
166     {
167         StringBuffer reportContent = new StringBuffer();
168         long runTime = getActualRunTime( report );
169 
170         reportContent.append( report.getName() );
171         reportContent.append( "  Time elapsed: " );
172         reportContent.append( elapsedTimeAsString( runTime ) );
173         reportContent.append( " sec" );
174 
175         return reportContent.toString();
176     }
177 
178     protected String getOutput( ReportEntry report, String msg )
179     {
180         StringBuffer buf = new StringBuffer();
181 
182         buf.append( getElapsedTimeSummary( report ) );
183 
184         buf.append( "  <<< " ).append( msg ).append( "!" ).append( NL );
185 
186         buf.append( getStackTrace( report ) );
187 
188         return buf.toString();
189     }
190 
191     public void reset()
192     {
193         super.reset();
194         if ( writer != null )
195         {
196             writer.flush();
197         }
198     }
199 }