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