1 | |
package org.apache.maven.shared.tools.easymock; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import java.io.File; |
23 | |
import java.io.FileReader; |
24 | |
import java.io.FileWriter; |
25 | |
import java.io.IOException; |
26 | |
import java.io.StringReader; |
27 | |
import java.io.StringWriter; |
28 | |
import java.util.ArrayList; |
29 | |
import java.util.Iterator; |
30 | |
import java.util.List; |
31 | |
|
32 | |
import junit.framework.Assert; |
33 | |
|
34 | |
import org.codehaus.plexus.util.FileUtils; |
35 | |
import org.codehaus.plexus.util.IOUtil; |
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
public class TestFileManager |
41 | |
{ |
42 | |
|
43 | 0 | public static final String TEMP_DIR_PATH = System.getProperty( "java.io.tmpdir" ); |
44 | |
|
45 | 0 | private List filesToDelete = new ArrayList(); |
46 | |
|
47 | |
private final String baseFilename; |
48 | |
|
49 | |
private final String fileSuffix; |
50 | |
|
51 | |
private StackTraceElement callerInfo; |
52 | |
|
53 | |
private Thread cleanupWarning; |
54 | |
|
55 | 0 | private boolean warnAboutCleanup = false; |
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
public TestFileManager( String baseFilename, String fileSuffix ) |
64 | 0 | { |
65 | 0 | this.baseFilename = baseFilename; |
66 | 0 | this.fileSuffix = fileSuffix; |
67 | |
|
68 | 0 | initializeCleanupMonitoring(); |
69 | 0 | } |
70 | |
|
71 | |
private void initializeCleanupMonitoring() |
72 | |
{ |
73 | 0 | callerInfo = new NullPointerException().getStackTrace()[2]; |
74 | |
|
75 | 0 | Runnable warning = new Runnable() |
76 | |
{ |
77 | |
|
78 | 0 | public void run() |
79 | |
{ |
80 | 0 | maybeWarnAboutCleanUp(); |
81 | 0 | } |
82 | |
|
83 | |
}; |
84 | |
|
85 | 0 | cleanupWarning = new Thread( warning ); |
86 | |
|
87 | 0 | Runtime.getRuntime().addShutdownHook( cleanupWarning ); |
88 | 0 | } |
89 | |
|
90 | |
protected void maybeWarnAboutCleanUp() |
91 | |
{ |
92 | 0 | if ( warnAboutCleanup ) |
93 | |
{ |
94 | 0 | System.out.println( "[WARNING] TestFileManager from: " + callerInfo.getClassName() + " not cleaned up!" ); |
95 | |
} |
96 | 0 | } |
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
public void markForDeletion( File toDelete ) |
102 | |
{ |
103 | 0 | filesToDelete.add( toDelete ); |
104 | 0 | warnAboutCleanup = true; |
105 | 0 | } |
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
public synchronized File createTempDir() |
111 | |
{ |
112 | |
try |
113 | |
{ |
114 | 0 | Thread.sleep( 20 ); |
115 | |
} |
116 | 0 | catch ( InterruptedException e ) |
117 | |
{ |
118 | |
|
119 | 0 | } |
120 | |
|
121 | 0 | File dir = new File( TEMP_DIR_PATH, baseFilename + System.currentTimeMillis() ); |
122 | |
|
123 | 0 | dir.mkdirs(); |
124 | 0 | markForDeletion( dir ); |
125 | |
|
126 | 0 | return dir; |
127 | |
} |
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
public synchronized File createTempFile() |
135 | |
throws IOException |
136 | |
{ |
137 | 0 | File tempFile = File.createTempFile( baseFilename, fileSuffix ); |
138 | 0 | tempFile.deleteOnExit(); |
139 | 0 | markForDeletion( tempFile ); |
140 | |
|
141 | 0 | return tempFile; |
142 | |
} |
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
public void cleanUp() |
148 | |
throws IOException |
149 | |
{ |
150 | 0 | for ( Iterator it = filesToDelete.iterator(); it.hasNext(); ) |
151 | |
{ |
152 | 0 | File file = (File) it.next(); |
153 | |
|
154 | 0 | if ( file.exists() ) |
155 | |
{ |
156 | 0 | if ( file.isDirectory() ) |
157 | |
{ |
158 | 0 | FileUtils.deleteDirectory( file ); |
159 | |
} |
160 | |
else |
161 | |
{ |
162 | 0 | file.delete(); |
163 | |
} |
164 | |
} |
165 | |
|
166 | 0 | it.remove(); |
167 | 0 | } |
168 | |
|
169 | 0 | warnAboutCleanup = false; |
170 | 0 | } |
171 | |
|
172 | |
|
173 | |
|
174 | |
|
175 | |
|
176 | |
|
177 | |
public void assertFileExistence( File dir, String filename, boolean shouldExist ) |
178 | |
{ |
179 | 0 | File file = new File( dir, filename ); |
180 | |
|
181 | 0 | if ( shouldExist ) |
182 | |
{ |
183 | 0 | Assert.assertTrue( file.exists() ); |
184 | |
} |
185 | |
else |
186 | |
{ |
187 | 0 | Assert.assertFalse( file.exists() ); |
188 | |
} |
189 | 0 | } |
190 | |
|
191 | |
|
192 | |
|
193 | |
|
194 | |
|
195 | |
|
196 | |
|
197 | |
public void assertFileContents( File dir, String filename, String contentsTest ) |
198 | |
throws IOException |
199 | |
{ |
200 | 0 | assertFileExistence( dir, filename, true ); |
201 | |
|
202 | 0 | File file = new File( dir, filename ); |
203 | |
|
204 | 0 | FileReader reader = null; |
205 | 0 | StringWriter writer = new StringWriter(); |
206 | |
|
207 | |
try |
208 | |
{ |
209 | 0 | reader = new FileReader( file ); |
210 | |
|
211 | 0 | IOUtil.copy( reader, writer ); |
212 | |
} |
213 | |
finally |
214 | |
{ |
215 | 0 | IOUtil.close( reader ); |
216 | 0 | } |
217 | |
|
218 | 0 | Assert.assertEquals( contentsTest, writer.toString() ); |
219 | 0 | } |
220 | |
|
221 | |
|
222 | |
|
223 | |
|
224 | |
|
225 | |
|
226 | |
|
227 | |
|
228 | |
public File createFile( File dir, String filename, String contents ) |
229 | |
throws IOException |
230 | |
{ |
231 | 0 | File file = new File( dir, filename ); |
232 | |
|
233 | 0 | file.getParentFile().mkdirs(); |
234 | |
|
235 | 0 | FileWriter writer = null; |
236 | |
|
237 | |
try |
238 | |
{ |
239 | 0 | writer = new FileWriter( file ); |
240 | |
|
241 | 0 | IOUtil.copy( new StringReader( contents ), writer ); |
242 | |
} |
243 | |
finally |
244 | |
{ |
245 | 0 | IOUtil.close( writer ); |
246 | 0 | } |
247 | |
|
248 | 0 | markForDeletion( file ); |
249 | |
|
250 | 0 | return file; |
251 | |
} |
252 | |
|
253 | |
|
254 | |
|
255 | |
|
256 | |
|
257 | |
|
258 | |
public String getFileContents( File file ) |
259 | |
throws IOException |
260 | |
{ |
261 | 0 | String result = null; |
262 | |
|
263 | 0 | FileReader reader = null; |
264 | |
try |
265 | |
{ |
266 | 0 | reader = new FileReader( file ); |
267 | |
|
268 | 0 | StringWriter writer = new StringWriter(); |
269 | |
|
270 | 0 | IOUtil.copy( reader, writer ); |
271 | |
|
272 | 0 | result = writer.toString(); |
273 | |
} |
274 | |
finally |
275 | |
{ |
276 | 0 | IOUtil.close( reader ); |
277 | 0 | } |
278 | |
|
279 | 0 | return result; |
280 | |
} |
281 | |
|
282 | |
|
283 | |
protected void finalize() |
284 | |
throws Throwable |
285 | |
{ |
286 | 0 | maybeWarnAboutCleanUp(); |
287 | |
|
288 | 0 | super.finalize(); |
289 | 0 | } |
290 | |
|
291 | |
|
292 | |
|
293 | |
|
294 | |
|
295 | |
|
296 | |
|
297 | |
public File createFile( String filename, String content ) |
298 | |
throws IOException |
299 | |
{ |
300 | 0 | File dir = createTempDir(); |
301 | 0 | return createFile( dir, filename, content ); |
302 | |
} |
303 | |
} |