1 package org.apache.maven.surefire.util.internal;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
34
35
36
37
38
39
40 public final class DumpFileUtils
41 {
42 private DumpFileUtils()
43 {
44 throw new IllegalStateException( "no instantiable constructor" );
45 }
46
47
48
49
50 public static synchronized File newDumpFile( String dumpFileName, ReporterConfiguration configuration )
51 {
52 return new File( configuration.getReportsDirectory(), dumpFileName );
53 }
54
55 public static void dumpException( Throwable t, File dumpFile )
56 {
57 dumpException( t, null, dumpFile );
58 }
59
60 public static void dumpException( Throwable t, String msg, File dumpFile )
61 {
62 try
63 {
64 if ( t != null && dumpFile != null
65 && ( dumpFile.exists() || mkdirs( dumpFile ) && dumpFile.createNewFile() ) )
66 {
67 Writer fw = createWriter( dumpFile );
68 if ( msg != null )
69 {
70 fw.append( msg )
71 .append( StringUtils.NL );
72 }
73 PrintWriter pw = new PrintWriter( fw );
74 t.printStackTrace( pw );
75 pw.flush();
76 fw.append( StringUtils.NL )
77 .append( StringUtils.NL )
78 .close();
79 }
80 }
81 catch ( Exception e )
82 {
83
84 }
85 }
86
87 public static void dumpText( String msg, File dumpFile )
88 {
89 try
90 {
91 if ( msg != null && dumpFile != null
92 && ( dumpFile.exists() || mkdirs( dumpFile ) && dumpFile.createNewFile() ) )
93 {
94 createWriter( dumpFile )
95 .append( msg )
96 .append( StringUtils.NL )
97 .append( StringUtils.NL )
98 .close();
99 }
100 }
101 catch ( Exception e )
102 {
103
104 }
105 }
106
107 public static String newFormattedDateFileName()
108 {
109 return new SimpleDateFormat( "yyyy-MM-dd'T'HH-mm-ss_SSS" ).format( new Date() );
110 }
111
112 private static Writer createWriter( File dumpFile ) throws IOException
113 {
114 return new OutputStreamWriter( new FileOutputStream( dumpFile, true ), "UTF-8" )
115 .append( "# Created on " )
116 .append( new SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ss.SSS" ).format( new Date() ) )
117 .append( StringUtils.NL );
118 }
119
120 private static boolean mkdirs( File dumpFile )
121 {
122 File dir = dumpFile.getParentFile();
123 return dir.exists() || dir.mkdirs();
124 }
125 }