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