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