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.File;
23  import java.io.FileWriter;
24  import java.io.IOException;
25  import java.io.PrintWriter;
26  
27  /**
28   * Base class for file reporters.
29   *
30   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
31   */
32  public abstract class AbstractFileReporter
33      extends AbstractTextReporter
34  {
35      private final File reportsDirectory;
36  
37      private final boolean deleteOnStarting;
38  
39      private final String reportNameSuffix;
40  
41      AbstractFileReporter( boolean trimStackTrace, String format, File reportsDirectory )
42      {
43          this( trimStackTrace, format, reportsDirectory, null );
44      }
45      
46      AbstractFileReporter( boolean trimStackTrace, String format, File reportsDirectory, String reportNameSuffix )
47      {
48          super( trimStackTrace, format );
49          this.reportsDirectory = reportsDirectory;
50          this.deleteOnStarting = false;
51          this.reportNameSuffix = reportNameSuffix;
52      }
53  
54      public void testSetStarting( ReportEntry report )
55          throws ReporterException
56      {
57          super.testSetStarting( report );
58  
59          File reportFile = getReportFile( reportsDirectory, report.getName(), reportNameSuffix, ".txt" );
60  
61          File reportDir = reportFile.getParentFile();
62  
63          //noinspection ResultOfMethodCallIgnored
64          reportDir.mkdirs();
65  
66          if ( deleteOnStarting && reportFile.exists() )
67          {
68              //noinspection ResultOfMethodCallIgnored
69              reportFile.delete();
70          }
71  
72          try
73          {
74              PrintWriter writer = new PrintWriter( new FileWriter( reportFile ) );
75  
76              writer.println( "-------------------------------------------------------------------------------" );
77  
78              writer.println( "Test set: " + report.getName() );
79  
80              writer.println( "-------------------------------------------------------------------------------" );
81  
82              setWriter( writer );
83          }
84          catch ( IOException e )
85          {
86              throw new ReporterException( "Unable to create file for report: " + e.getMessage(), e );
87          }
88      }
89  
90      public static File getReportFile( File reportsDirectory, String reportEntryName, 
91              String reportNameSuffix, String fileExtension )
92      {
93          File reportFile;
94  
95          if ( reportNameSuffix != null && reportNameSuffix.length() > 0)
96          {
97              reportFile = new File( reportsDirectory, reportEntryName + "-" + reportNameSuffix + fileExtension );
98          }
99          else
100         {
101             reportFile = new File( reportsDirectory, reportEntryName + fileExtension );
102         }
103         return reportFile;
104     }
105 
106     public void testSetCompleted( ReportEntry report )
107         throws ReporterException
108     {
109         super.testSetCompleted( report );
110 
111         writer.flush();
112 
113         writer.close();
114 
115         writer = null;
116     }
117 }