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.File;
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.io.OutputStream;
24 import java.io.PrintStream;
25 import java.io.PrintWriter;
26 import java.io.Reader;
27 import java.io.Writer;
28 import java.nio.file.Files;
29 import java.util.Arrays;
30
31 import org.junit.jupiter.api.BeforeEach;
32 import org.junit.jupiter.api.Test;
33
34 import static org.junit.jupiter.api.Assertions.assertArrayEquals;
35 import static org.junit.jupiter.api.Assertions.assertNotNull;
36 import static org.junit.jupiter.api.Assertions.assertTrue;
37 import static org.junit.jupiter.api.Assertions.fail;
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54 public final class IOUtilTest {
55
56
57
58
59
60
61 private int FILE_SIZE = 1024 * 4 + 1;
62
63 private File testDirectory;
64
65 private File testFile;
66
67
68
69
70 @BeforeEach
71 public void setUp() {
72 try {
73 testDirectory = (new File("target/test/io/")).getAbsoluteFile();
74 if (!testDirectory.exists()) {
75 testDirectory.mkdirs();
76 }
77
78 testFile = new File(testDirectory, "file2-test.txt");
79
80 createFile(testFile, FILE_SIZE);
81 } catch (IOException ioe) {
82 throw new RuntimeException("Can't run this test because environment could not be built");
83 }
84 }
85
86
87
88
89 public void tearDown() {
90 testFile.delete();
91 testDirectory.delete();
92 }
93
94 private void createFile(File file, long size) throws IOException {
95 BufferedOutputStream output = new BufferedOutputStream(Files.newOutputStream(file.toPath()));
96
97 for (int i = 0; i < size; i++) {
98 output.write((byte) (i % 128));
99 }
100
101 output.close();
102 }
103
104
105 private void assertEqualContent(byte[] b0, byte[] b1) {
106 assertTrue(Arrays.equals(b0, b1), "Content not equal according to java.util.Arrays#equals()");
107 }
108
109
110 private void assertEqualContent(File f0, File f1) throws IOException {
111 byte[] buf0 = Files.readAllBytes(f0.toPath());
112 byte[] buf1 = Files.readAllBytes(f1.toPath());
113 assertArrayEquals(buf0, buf1, "The files " + f0 + " and " + f1 + " have different content");
114 }
115
116
117 private void assertEqualContent(byte[] b0, File file) throws IOException {
118 byte[] b1 = Files.readAllBytes(file.toPath());
119 assertArrayEquals(b0, b1, "Content differs");
120 }
121
122
123
124
125
126
127 @Test
128 public void testInputStreamToOutputStream() throws Exception {
129 File destination = newFile("copy1.txt");
130 InputStream fin = Files.newInputStream(testFile.toPath());
131 OutputStream fout = Files.newOutputStream(destination.toPath());
132
133 IOUtil.copy(fin, fout);
134 assertTrue(fin.available() == 0, "Not all bytes were read");
135 fout.flush();
136
137 checkFile(destination);
138 checkWrite(fout);
139 fout.close();
140 fin.close();
141 deleteFile(destination);
142 }
143
144
145
146
147
148
149 @org.junit.jupiter.api.Test
150 public void testInputStreamToWriter() throws Exception {
151 File destination = newFile("copy2.txt");
152 InputStream fin = Files.newInputStream(testFile.toPath());
153 Writer fout = Files.newBufferedWriter(destination.toPath());
154
155 IOUtil.copy(fin, fout);
156
157 assertTrue(fin.available() == 0, "Not all bytes were read");
158 fout.flush();
159
160 checkFile(destination);
161 checkWrite(fout);
162 fout.close();
163 fin.close();
164 deleteFile(destination);
165 }
166
167
168
169
170
171
172 @Test
173 public void testInputStreamToString() throws Exception {
174 InputStream fin = Files.newInputStream(testFile.toPath());
175 String out = IOUtil.toString(fin);
176 assertNotNull(out);
177 assertTrue(fin.available() == 0, "Not all bytes were read");
178 assertTrue(out.length() == FILE_SIZE, "Wrong output size: out.length()=" + out.length() + "!=" + FILE_SIZE);
179 fin.close();
180 }
181
182
183
184
185
186
187 @Test
188 public void testReaderToOutputStream() throws Exception {
189 File destination = newFile("copy3.txt");
190 Reader fin = Files.newBufferedReader(testFile.toPath());
191 OutputStream fout = Files.newOutputStream(destination.toPath());
192 IOUtil.copy(fin, fout);
193
194
195
196
197
198
199
200 checkFile(destination);
201 checkWrite(fout);
202 fout.close();
203 fin.close();
204 deleteFile(destination);
205 }
206
207
208
209
210
211
212 @org.junit.jupiter.api.Test
213 public void testReaderToWriter() throws Exception {
214 File destination = newFile("copy4.txt");
215 Reader fin = Files.newBufferedReader(testFile.toPath());
216 Writer fout = Files.newBufferedWriter(destination.toPath());
217 IOUtil.copy(fin, fout);
218
219 fout.flush();
220 checkFile(destination);
221 checkWrite(fout);
222 fout.close();
223 fin.close();
224 deleteFile(destination);
225 }
226
227
228
229
230
231
232 @org.junit.jupiter.api.Test
233 public void testReaderToString() throws Exception {
234 Reader fin = Files.newBufferedReader(testFile.toPath());
235 String out = IOUtil.toString(fin);
236 assertNotNull(out);
237 assertTrue(out.length() == FILE_SIZE, "Wrong output size: out.length()=" + out.length() + "!=" + FILE_SIZE);
238 fin.close();
239 }
240
241
242
243
244
245
246 @org.junit.jupiter.api.Test
247 public void testStringToOutputStream() throws Exception {
248 File destination = newFile("copy5.txt");
249 Reader fin = Files.newBufferedReader(testFile.toPath());
250
251 String str = IOUtil.toString(fin);
252 OutputStream fout = Files.newOutputStream(destination.toPath());
253 IOUtil.copy(str, fout);
254
255
256
257
258
259
260
261 checkFile(destination);
262 checkWrite(fout);
263 fout.close();
264 fin.close();
265 deleteFile(destination);
266 }
267
268
269
270
271
272
273 @org.junit.jupiter.api.Test
274 public void testStringToWriter() throws Exception {
275 File destination = newFile("copy6.txt");
276 Reader fin = Files.newBufferedReader(testFile.toPath());
277
278 String str = IOUtil.toString(fin);
279 Writer fout = Files.newBufferedWriter(destination.toPath());
280 IOUtil.copy(str, fout);
281 fout.flush();
282
283 checkFile(destination);
284 checkWrite(fout);
285 fout.close();
286 fin.close();
287
288 deleteFile(destination);
289 }
290
291
292
293
294
295
296 @Test
297 public void testInputStreamToByteArray() throws Exception {
298 InputStream fin = Files.newInputStream(testFile.toPath());
299 byte[] out = IOUtil.toByteArray(fin);
300 assertNotNull(out);
301 assertTrue(fin.available() == 0, "Not all bytes were read");
302 assertTrue(out.length == FILE_SIZE, "Wrong output size: out.length=" + out.length + "!=" + FILE_SIZE);
303 assertEqualContent(out, testFile);
304 fin.close();
305 }
306
307
308
309
310
311
312 @Test
313 public void testStringToByteArray() throws Exception {
314 Reader fin = Files.newBufferedReader(testFile.toPath());
315
316
317 String str = IOUtil.toString(fin);
318
319 byte[] out = IOUtil.toByteArray(str);
320 assertEqualContent(str.getBytes(), out);
321 fin.close();
322 }
323
324
325
326
327
328
329 @Test
330 public void testByteArrayToWriter() throws Exception {
331 File destination = newFile("copy7.txt");
332 Writer fout = Files.newBufferedWriter(destination.toPath());
333 InputStream fin = Files.newInputStream(testFile.toPath());
334
335
336 byte[] in = IOUtil.toByteArray(fin);
337 IOUtil.copy(in, fout);
338 fout.flush();
339 checkFile(destination);
340 checkWrite(fout);
341 fout.close();
342 fin.close();
343 deleteFile(destination);
344 }
345
346
347
348
349
350
351 @org.junit.jupiter.api.Test
352 public void testByteArrayToString() throws Exception {
353 InputStream fin = Files.newInputStream(testFile.toPath());
354 byte[] in = IOUtil.toByteArray(fin);
355
356 String str = IOUtil.toString(in);
357 assertEqualContent(in, str.getBytes());
358 fin.close();
359 }
360
361
362
363
364
365
366 @Test
367 public void testByteArrayToOutputStream() throws Exception {
368 File destination = newFile("copy8.txt");
369 OutputStream fout = Files.newOutputStream(destination.toPath());
370 InputStream fin = Files.newInputStream(testFile.toPath());
371
372
373 byte[] in = IOUtil.toByteArray(fin);
374
375 IOUtil.copy(in, fout);
376
377 fout.flush();
378
379 checkFile(destination);
380 checkWrite(fout);
381 fout.close();
382 fin.close();
383 deleteFile(destination);
384 }
385
386
387
388
389
390
391
392
393
394
395 @org.junit.jupiter.api.Test
396 public void testCloseInputStream() throws Exception {
397 IOUtil.close((InputStream) null);
398
399 TestInputStream inputStream = new TestInputStream();
400
401 IOUtil.close(inputStream);
402
403 assertTrue(inputStream.closed);
404 }
405
406
407
408
409
410
411 @org.junit.jupiter.api.Test
412 public void testCloseOutputStream() throws Exception {
413 IOUtil.close((OutputStream) null);
414
415 TestOutputStream outputStream = new TestOutputStream();
416
417 IOUtil.close(outputStream);
418
419 assertTrue(outputStream.closed);
420 }
421
422
423
424
425
426
427 @Test
428 public void testCloseReader() throws Exception {
429 IOUtil.close((Reader) null);
430
431 TestReader reader = new TestReader();
432
433 IOUtil.close(reader);
434
435 assertTrue(reader.closed);
436 }
437
438
439
440
441
442
443 @Test
444 public void testCloseWriter() throws Exception {
445 IOUtil.close((Writer) null);
446
447 TestWriter writer = new TestWriter();
448
449 IOUtil.close(writer);
450
451 assertTrue(writer.closed);
452 }
453
454 private class TestInputStream extends InputStream {
455 boolean closed;
456
457 public void close() {
458 closed = true;
459 }
460
461 public int read() {
462 fail("This method shouldn't be called");
463
464 return 0;
465 }
466 }
467
468 private class TestOutputStream extends OutputStream {
469 boolean closed;
470
471 public void close() {
472 closed = true;
473 }
474
475 public void write(int value) {
476 fail("This method shouldn't be called");
477 }
478 }
479
480 private class TestReader extends Reader {
481 boolean closed;
482
483 public void close() {
484 closed = true;
485 }
486
487 public int read(char cbuf[], int off, int len) {
488 fail("This method shouldn't be called");
489
490 return 0;
491 }
492 }
493
494 private class TestWriter extends Writer {
495 boolean closed;
496
497 public void close() {
498 closed = true;
499 }
500
501 public void write(char cbuf[], int off, int len) {
502 fail("This method shouldn't be called");
503 }
504
505 public void flush() {
506 fail("This method shouldn't be called");
507 }
508 }
509
510
511
512
513
514 private File newFile(String filename) throws Exception {
515 File destination = new File(testDirectory, filename);
516 assertTrue(!destination.exists(), filename + "Test output data file shouldn't previously exist");
517
518 return destination;
519 }
520
521 private void checkFile(File file) throws Exception {
522 assertTrue(file.exists(), "Check existence of output file");
523 assertEqualContent(testFile, file);
524 }
525
526 private void checkWrite(OutputStream output) throws Exception {
527 try {
528 new PrintStream(output).write(0);
529 } catch (Throwable t) {
530 throw new Exception("The copy() method closed the stream " + "when it shouldn't have. " + t.getMessage());
531 }
532 }
533
534 private void checkWrite(Writer output) throws Exception {
535 try {
536 new PrintWriter(output).write('a');
537 } catch (Throwable t) {
538 throw new Exception("The copy() method closed the stream " + "when it shouldn't have. " + t.getMessage());
539 }
540 }
541
542 private void deleteFile(File file) throws Exception {
543 assertTrue(
544 file.length() == FILE_SIZE + 1,
545 "Wrong output size: file.length()=" + file.length() + "!=" + FILE_SIZE + 1);
546
547 assertTrue((file.delete() || (!file.exists())), "File would not delete");
548 }
549 }