1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.shared.utils.testhelpers;
20
21 import java.io.File;
22 import java.io.FileOutputStream;
23 import java.io.IOException;
24 import java.io.OutputStream;
25 import java.io.OutputStreamWriter;
26 import java.io.Writer;
27
28 import org.apache.commons.io.FileUtils;
29 import org.junit.rules.TemporaryFolder;
30
31
32
33
34 public final class FileTestHelper {
35
36 private FileTestHelper() {
37
38 }
39
40 public static void generateTestData(OutputStream out, long size) throws IOException {
41 for (int i = 0; i < size; i++) {
42
43 out.write((byte) ((i % 127) + 1));
44 }
45 }
46
47 public static void generateTestFile(File testfile, int size) throws IOException {
48 if (testfile.exists()) {
49
50 testfile.delete();
51 }
52
53 try (OutputStream os = new FileOutputStream(testfile)) {
54 generateTestData(os, size);
55 os.flush();
56 }
57 }
58
59 public static void createLineBasedFile(File file, String[] data) throws IOException {
60 if (file.getParentFile() != null && !file.getParentFile().exists()) {
61 throw new IOException("Cannot create file " + file + " as the parent directory does not exist");
62 }
63
64 try (Writer out = new OutputStreamWriter(new FileOutputStream(file), "UTF-8")) {
65 for (String aData : data) {
66 out.write(aData);
67 out.write(System.lineSeparator());
68 }
69 }
70 }
71
72
73
74
75
76
77
78 public static File newFile(TemporaryFolder folder, String filename) throws IOException {
79 File destination = folder.newFile(filename);
80
81 if (destination.exists()) {
82 FileUtils.deleteQuietly(destination);
83 }
84 return destination;
85 }
86 }