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