1 package org.codehaus.plexus.util;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 import java.io.BufferedOutputStream;
20 import java.io.ByteArrayOutputStream;
21 import java.io.File;
22 import java.io.IOException;
23 import java.io.OutputStream;
24 import java.io.PrintStream;
25 import java.io.PrintWriter;
26 import java.io.Writer;
27 import java.lang.reflect.Method;
28 import java.nio.file.Files;
29
30 import org.junit.jupiter.api.BeforeEach;
31 import org.junit.jupiter.api.TestInfo;
32
33 import static org.junit.jupiter.api.Assertions.assertArrayEquals;
34 import static org.junit.jupiter.api.Assertions.assertTrue;
35 import static org.junit.jupiter.api.Assertions.fail;
36
37
38
39
40
41
42
43
44 public abstract class FileBasedTestCase {
45 private static File testDir;
46
47 private TestInfo testInfo;
48
49
50
51
52
53
54 public static File getTestDirectory() {
55 if (testDir == null) {
56 testDir = (new File("target/test/io/")).getAbsoluteFile();
57 }
58 return testDir;
59 }
60
61
62
63
64
65
66
67
68
69 protected byte[] createFile(final File file, final long size) throws IOException {
70 if (!file.getParentFile().exists()) {
71 throw new IOException("Cannot create file " + file + " as the parent directory does not exist");
72 }
73
74 byte[] data = generateTestData(size);
75
76 final BufferedOutputStream output = new BufferedOutputStream(Files.newOutputStream(file.toPath()));
77
78 try {
79 output.write(data);
80
81 return data;
82 } finally {
83 output.close();
84 }
85 }
86
87
88
89
90
91
92
93
94 protected boolean createSymlink(final File link, final File target) {
95 try {
96 String[] args = {"ln", "-s", target.getAbsolutePath(), link.getAbsolutePath()};
97 Process process = Runtime.getRuntime().exec(args);
98 process.waitFor();
99 if (0 != process.exitValue()) {
100 return false;
101 }
102 } catch (Exception e) {
103
104 return false;
105 }
106 return true;
107 }
108
109
110
111
112
113
114
115 protected byte[] generateTestData(final long size) {
116 try {
117 ByteArrayOutputStream baout = new ByteArrayOutputStream();
118 generateTestData(baout, size);
119 return baout.toByteArray();
120 } catch (IOException ioe) {
121 throw new RuntimeException("This should never happen: " + ioe.getMessage());
122 }
123 }
124
125
126
127
128
129
130
131
132 protected void generateTestData(final OutputStream out, final long size) throws IOException {
133 for (int i = 0; i < size; i++) {
134
135
136
137 out.write((byte) ((i % 127) + 1));
138 }
139 }
140
141
142
143
144
145
146
147
148 protected File newFile(String filename) throws IOException {
149 final File destination = new File(getTestDirectory(), filename);
150
151
152
153 if (destination.exists()) {
154 FileUtils.forceDelete(destination);
155 }
156 return destination;
157 }
158
159
160
161
162
163
164
165
166 protected void checkFile(final File file, final File referenceFile) throws Exception {
167 assertTrue(file.exists(), "Check existence of output file");
168 assertEqualContent(referenceFile, file);
169 }
170
171
172
173
174
175
176
177 protected void checkWrite(final OutputStream output) throws Exception {
178 try {
179 new PrintStream(output).write(0);
180 } catch (final Throwable t) {
181 fail("The copy() method closed the stream " + "when it shouldn't have. " + t.getMessage());
182 }
183 }
184
185
186
187
188
189
190
191 protected void checkWrite(final Writer output) throws Exception {
192 try {
193 new PrintWriter(output).write('a');
194 } catch (final Throwable t) {
195 fail("The copy() method closed the stream " + "when it shouldn't have. " + t.getMessage());
196 }
197 }
198
199
200
201
202
203
204
205 protected void deleteFile(final File file) throws Exception {
206 if (file.exists()) {
207 assertTrue(file.delete(), "Couldn't delete file: " + file);
208 }
209 }
210
211
212
213
214
215
216 private void assertEqualContent(final File f0, final File f1) throws IOException {
217
218
219
220
221
222 byte[] buf0 = Files.readAllBytes(f0.toPath());
223 byte[] buf1 = Files.readAllBytes(f0.toPath());
224 assertArrayEquals(buf0, buf1, "The files " + f0 + " and " + f1 + " have different content");
225 }
226
227
228
229
230
231
232
233
234 protected void assertEqualContent(final byte[] b0, final File file) throws IOException {
235 byte[] b1 = Files.readAllBytes(file.toPath());
236 assertArrayEquals(b0, b1, "Content differs");
237 }
238
239
240
241
242
243
244 protected void assertIsDirectory(File file) {
245 assertTrue(file.exists(), "The File doesn't exists: " + file.getAbsolutePath());
246
247 assertTrue(file.isDirectory(), "The File isn't a directory: " + file.getAbsolutePath());
248 }
249
250
251
252
253
254
255 protected void assertIsFile(File file) {
256 assertTrue(file.exists(), "The File doesn't exists: " + file.getAbsolutePath());
257
258 assertTrue(file.isFile(), "The File isn't a file: " + file.getAbsolutePath());
259 }
260
261 @BeforeEach
262 void init(TestInfo testInfo) {
263 this.testInfo = testInfo;
264 }
265
266 protected String getTestMethodName() {
267 return testInfo.getTestMethod().map(Method::getName).orElse(null);
268 }
269 }