View Javadoc
1   package org.apache.maven.surefire.util.internal;
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 org.apache.maven.surefire.report.ReporterConfiguration;
23  
24  import java.io.File;
25  import java.io.FileOutputStream;
26  import java.io.IOException;
27  import java.io.OutputStreamWriter;
28  import java.io.PrintWriter;
29  import java.io.Writer;
30  import java.text.SimpleDateFormat;
31  import java.util.Date;
32  
33  import static org.apache.maven.surefire.util.internal.StringUtils.UTF_8;
34  
35  /**
36   * Dumps a text or exception in dump file.
37   * Each call logs a date when it was written to the dump file.
38   *
39   * @author <a href="mailto:tibordigana@apache.org">Tibor Digana (tibor17)</a>
40   * @since 2.20
41   */
42  public final class DumpFileUtils
43  {
44      private DumpFileUtils()
45      {
46          throw new IllegalStateException( "no instantiable constructor" );
47      }
48  
49      /**
50       * New dump file. Synchronized object appears in main memory and perfectly visible in other threads.
51       *
52       * @param dumpFileName    dump file name
53       * @param configuration    only report directory
54       */
55      public static synchronized File newDumpFile( String dumpFileName, ReporterConfiguration configuration )
56      {
57          return new File( configuration.getReportsDirectory(), dumpFileName );
58      }
59  
60      public static void dumpException( Throwable t, File dumpFile )
61      {
62          dumpException( t, null, dumpFile );
63      }
64  
65      public static void dumpException( Throwable t, String msg, File dumpFile )
66      {
67          try
68          {
69              if ( t != null && dumpFile != null
70                           && ( dumpFile.exists() || mkdirs( dumpFile ) && dumpFile.createNewFile() ) )
71              {
72                  Writer fw = createWriter( dumpFile );
73                  if ( msg != null )
74                  {
75                      fw.append( msg )
76                              .append( StringUtils.NL );
77                  }
78                  PrintWriter pw = new PrintWriter( fw );
79                  t.printStackTrace( pw );
80                  pw.flush();
81                  fw.append( StringUtils.NL )
82                    .append( StringUtils.NL )
83                    .close();
84              }
85          }
86          catch ( Exception e )
87          {
88              // do nothing
89          }
90      }
91  
92      public static void dumpText( String msg, File dumpFile )
93      {
94          try
95          {
96              if ( msg != null && dumpFile != null
97                           && ( dumpFile.exists() || mkdirs( dumpFile ) && dumpFile.createNewFile() ) )
98              {
99                  createWriter( dumpFile )
100                         .append( msg )
101                         .append( StringUtils.NL )
102                         .append( StringUtils.NL )
103                         .close();
104             }
105         }
106         catch ( Exception e )
107         {
108             // do nothing
109         }
110     }
111 
112     public static String newFormattedDateFileName()
113     {
114         return new SimpleDateFormat( "yyyy-MM-dd'T'HH-mm-ss_SSS" ).format( new Date() );
115     }
116 
117     private static Writer createWriter( File dumpFile ) throws IOException
118     {
119         return new OutputStreamWriter( new FileOutputStream( dumpFile, true ), UTF_8 )
120                        .append( "# Created on " )
121                        .append( new SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ss.SSS" ).format( new Date() ) )
122                        .append( StringUtils.NL );
123     }
124 
125     private static boolean mkdirs( File dumpFile )
126     {
127         File dir = dumpFile.getParentFile();
128         return dir.exists() || dir.mkdirs();
129     }
130 }