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.lang.reflect.Method;
25 import java.nio.file.Files;
26
27 import org.junit.jupiter.api.BeforeEach;
28 import org.junit.jupiter.api.TestInfo;
29
30 import static org.junit.jupiter.api.Assertions.assertArrayEquals;
31 import static org.junit.jupiter.api.Assertions.assertTrue;
32
33
34
35
36
37
38
39 public abstract class FileBasedTestCase {
40 private static File testDir;
41
42 private TestInfo testInfo;
43
44 public static File getTestDirectory() {
45 if (testDir == null) {
46 testDir = (new File("target/test/io/")).getAbsoluteFile();
47 }
48 return testDir;
49 }
50
51
52
53
54
55
56
57
58 protected void createFile(final File file, final long size) throws IOException {
59 if (!file.getParentFile().exists()) {
60 throw new IOException("Cannot create file " + file + " as the parent directory does not exist");
61 }
62
63 byte[] data = generateTestData(size);
64 try (BufferedOutputStream output = new BufferedOutputStream(Files.newOutputStream(file.toPath()))) {
65 output.write(data);
66 }
67 }
68
69
70
71
72
73
74
75 protected void createSymlink(final File link, final File target) {
76 try {
77 String[] args = {"ln", "-s", target.getAbsolutePath(), link.getAbsolutePath()};
78 Process process = Runtime.getRuntime().exec(args);
79 process.waitFor();
80 } catch (Exception e) {
81
82 }
83 }
84
85 protected byte[] generateTestData(final long size) {
86 try {
87 ByteArrayOutputStream baout = new ByteArrayOutputStream();
88 generateTestData(baout, size);
89 return baout.toByteArray();
90 } catch (IOException ioe) {
91 throw new RuntimeException("This should never happen: " + ioe.getMessage());
92 }
93 }
94
95 protected void generateTestData(final OutputStream out, final long size) throws IOException {
96 for (int i = 0; i < size; i++) {
97
98
99
100 out.write((byte) ((i % 127) + 1));
101 }
102 }
103
104 protected void checkFile(final File file, final File referenceFile) throws Exception {
105 assertTrue(file.exists(), "Check existence of output file");
106 assertEqualContent(referenceFile, file);
107 }
108
109
110 private void assertEqualContent(final File f0, final File f1) throws IOException {
111
112
113
114
115
116 byte[] buf0 = Files.readAllBytes(f0.toPath());
117 byte[] buf1 = Files.readAllBytes(f0.toPath());
118 assertArrayEquals(buf0, buf1, "The files " + f0 + " and " + f1 + " have different content");
119 }
120
121
122
123
124
125
126
127
128 protected void assertEqualContent(final byte[] b0, final File file) throws IOException {
129 byte[] b1 = Files.readAllBytes(file.toPath());
130 assertArrayEquals(b0, b1, "Content differs");
131 }
132
133 protected void assertIsDirectory(File file) {
134 assertTrue(file.exists(), "The File doesn't exists: " + file.getAbsolutePath());
135
136 assertTrue(file.isDirectory(), "The File isn't a directory: " + file.getAbsolutePath());
137 }
138
139 @BeforeEach
140 void init(TestInfo testInfo) {
141 this.testInfo = testInfo;
142 }
143
144 protected String getTestMethodName() {
145 return testInfo.getTestMethod().map(Method::getName).orElse(null);
146 }
147 }