1 package org.apache.maven.plugin.assembly.testutils;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.codehaus.plexus.util.FileUtils;
23 import org.codehaus.plexus.util.IOUtil;
24
25 import java.io.File;
26 import java.io.FileReader;
27 import java.io.FileWriter;
28 import java.io.IOException;
29 import java.util.ArrayList;
30 import java.util.Iterator;
31 import java.util.List;
32
33 public class TestFileManager
34 {
35
36 public static final String TEMP_DIR_PATH = System.getProperty( "java.io.tmpdir" );
37
38 private final List<File> filesToDelete = new ArrayList<File>();
39
40 private final String baseFilename;
41
42 private final String fileSuffix;
43
44 private StackTraceElement callerInfo;
45
46 private boolean warnAboutCleanup = false;
47
48 public TestFileManager( final String baseFilename, final String fileSuffix )
49 {
50 this.baseFilename = baseFilename;
51 this.fileSuffix = fileSuffix;
52
53 initializeCleanupMonitoring();
54 }
55
56 private void initializeCleanupMonitoring()
57 {
58 callerInfo = new NullPointerException().getStackTrace()[2];
59
60 final Runnable warning = new Runnable()
61 {
62
63 public void run()
64 {
65 maybeWarnAboutCleanUp();
66 }
67
68 };
69
70 Thread cleanupWarning = new Thread( warning );
71
72 Runtime.getRuntime()
73 .addShutdownHook( cleanupWarning );
74 }
75
76 private void maybeWarnAboutCleanUp()
77 {
78 if ( warnAboutCleanup )
79 {
80 System.out.println( "[WARNING] TestFileManager from: " + callerInfo.getClassName() + " not cleaned up!" );
81 }
82 }
83
84 public void markForDeletion( final File toDelete )
85 {
86 filesToDelete.add( toDelete );
87 warnAboutCleanup = true;
88 }
89
90 public synchronized File createTempDir()
91 {
92 try
93 {
94 Thread.sleep( 20 );
95 }
96 catch ( final InterruptedException ignore )
97 {
98 }
99
100 final File dir = new File( TEMP_DIR_PATH, baseFilename + System.currentTimeMillis() );
101
102
103 dir.mkdirs();
104 markForDeletion( dir );
105
106 return dir;
107 }
108
109 public synchronized File createTempFile() throws IOException
110 {
111 final File tempFile = File.createTempFile( baseFilename, fileSuffix );
112 tempFile.deleteOnExit();
113 markForDeletion( tempFile );
114
115 return tempFile;
116 }
117
118 public void cleanUp()
119 {
120 for ( final Iterator<File> it = filesToDelete.iterator(); it.hasNext(); )
121 {
122 final File file = it.next();
123
124 if ( file.exists() )
125 {
126 try
127 {
128 FileUtils.forceDelete( file );
129 }
130 catch ( final Exception e )
131 {
132 System.err.println( "Error while deleting test file/dir: " + file + "; ignoring." );
133 }
134 }
135
136 it.remove();
137 }
138
139 warnAboutCleanup = false;
140 }
141
142
143
144
145 @SuppressWarnings( "ResultOfMethodCallIgnored" )
146 public File createFile( final File dir, final String filename, final String contents ) throws IOException
147 {
148 final File file = new File( dir, filename );
149
150 file.getParentFile()
151 .mkdirs();
152
153 FileWriter writer = null;
154
155 try
156 {
157 writer = new FileWriter( file );
158
159 writer.write( contents );
160 }
161 finally
162 {
163 IOUtil.close( writer );
164 }
165
166 markForDeletion( file );
167
168 return file;
169 }
170
171
172
173
174 public String getFileContents( final File file ) throws IOException
175 {
176 String result = null;
177
178 FileReader reader = null;
179 try
180 {
181 reader = new FileReader( file );
182
183 result = IOUtil.toString( reader );
184 }
185 finally
186 {
187 IOUtil.close( reader );
188 }
189
190 return result;
191 }
192
193 @Override
194 protected void finalize() throws Throwable
195 {
196 maybeWarnAboutCleanUp();
197
198 super.finalize();
199 }
200
201 }