1 |
|
package org.europa.together.utils; |
2 |
|
|
3 |
|
import java.io.ByteArrayInputStream; |
4 |
|
import java.io.File; |
5 |
|
import java.io.IOException; |
6 |
|
import java.io.InputStream; |
7 |
|
import java.lang.reflect.Constructor; |
8 |
|
import java.nio.file.Files; |
9 |
|
import java.nio.file.Paths; |
10 |
|
import java.util.Collection; |
11 |
|
import org.europa.together.JUnit5Preperator; |
12 |
|
import org.europa.together.application.LogbackLogger; |
13 |
|
import org.europa.together.business.Logger; |
14 |
|
import org.europa.together.domain.ByteOrderMark; |
15 |
|
import static org.junit.jupiter.api.Assertions.*; |
16 |
|
import org.junit.jupiter.api.Test; |
17 |
|
import org.junit.jupiter.api.extension.ExtendWith; |
18 |
|
import org.springframework.test.context.ContextConfiguration; |
19 |
|
import org.springframework.test.context.junit.jupiter.SpringExtension; |
20 |
|
|
21 |
|
@SuppressWarnings("unchecked") |
22 |
|
@ExtendWith({JUnit5Preperator.class}) |
23 |
|
@ExtendWith(SpringExtension.class) |
24 |
|
@ContextConfiguration(locations = {"/applicationContext.xml"}) |
|
|
| 100% |
Uncovered Elements: 0 (85) |
Complexity: 20 |
Complexity Density: 0.32 |
|
25 |
|
public class FileUtilsTest { |
26 |
|
|
27 |
|
private static final Logger LOGGER = new LogbackLogger(FileUtilsTest.class); |
28 |
|
|
29 |
|
private static final String DIRECTORY |
30 |
|
= Constraints.SYSTEM_APP_DIR + "/target/test-classes/"; |
31 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
32 |
1 |
@Test... |
33 |
|
void privateConstructor() throws Exception { |
34 |
1 |
Constructor<FileUtils> clazz |
35 |
|
= FileUtils.class.getDeclaredConstructor(); |
36 |
1 |
clazz.setAccessible(true); |
37 |
1 |
assertThrows(Exception.class, () -> { |
38 |
1 |
FileUtils call = clazz.newInstance(); |
39 |
|
}); |
40 |
|
} |
41 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
1PASS
|
|
42 |
1 |
@Test... |
43 |
|
void getFileSize() { |
44 |
1 |
assertEquals(146329, FileUtils.getFileSize(DIRECTORY + "Attachment.pdf", null)); |
45 |
1 |
assertEquals(146329, FileUtils.getFileSize(DIRECTORY + "Attachment.pdf", "")); |
46 |
1 |
assertEquals(146329, FileUtils.getFileSize(DIRECTORY + "Attachment.pdf", " ")); |
47 |
1 |
assertEquals(142, FileUtils.getFileSize(DIRECTORY + "Attachment.pdf", "kilo")); |
48 |
|
|
49 |
1 |
assertEquals(0, FileUtils.getFileSize(DIRECTORY + "Attachment.pdf", "mega")); |
50 |
1 |
assertEquals(0, FileUtils.getFileSize(DIRECTORY + "Attachment.pdf", "giga")); |
51 |
1 |
assertEquals(0, FileUtils.getFileSize(DIRECTORY + "Attachment.pdf", "tera")); |
52 |
|
} |
53 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1PASS
|
|
54 |
1 |
@Test... |
55 |
|
void failGetFileSize() { |
56 |
1 |
assertEquals(0, FileUtils.getFileSize(DIRECTORY + "no_file", null)); |
57 |
|
} |
58 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
59 |
1 |
@Test... |
60 |
|
void writeEmptyFile() throws Exception { |
61 |
1 |
assertTrue(FileUtils.writeStringToFile("", DIRECTORY + "/empty.txt")); |
62 |
1 |
assertEquals(0, FileUtils.getFileSize(DIRECTORY + "/empty.txt", null)); |
63 |
1 |
Files.delete(Paths.get(DIRECTORY + "/empty.txt")); |
64 |
|
} |
65 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
66 |
1 |
@Test... |
67 |
|
void writeStringToFile() throws Exception { |
68 |
1 |
String fileContent = "Content of the written File"; |
69 |
1 |
assertTrue(FileUtils.writeStringToFile(fileContent, DIRECTORY + "/test.txt")); |
70 |
1 |
assertEquals(27, FileUtils.getFileSize(DIRECTORY + "/test.txt", null)); |
71 |
1 |
Files.delete(Paths.get(DIRECTORY + "/test.txt")); |
72 |
|
} |
73 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
74 |
1 |
@Test... |
75 |
|
void failWriteStringToFile() throws IOException { |
76 |
1 |
assertThrows(IOException.class, () -> { |
77 |
1 |
FileUtils.writeStringToFile("content", ""); |
78 |
|
}); |
79 |
|
} |
80 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
81 |
1 |
@Test... |
82 |
|
void readFileStream() throws Exception { |
83 |
1 |
String file = DIRECTORY + "TestFile"; |
84 |
1 |
assertEquals("Hello World!", FileUtils.readFileStream(new File(file))); |
85 |
1 |
assertEquals("Hello World!", FileUtils.readFileStream(new File(file), ByteOrderMark.NONE)); |
86 |
1 |
assertEquals("Hello World!", FileUtils.readFileStream(new File(file), ByteOrderMark.UTF_8)); |
87 |
|
} |
88 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
89 |
1 |
@Test... |
90 |
|
void failReadFileStream() throws Exception { |
91 |
1 |
assertThrows(Exception.class, () -> { |
92 |
1 |
FileUtils.readFileStream(new File("no_file")); |
93 |
|
}); |
94 |
|
} |
95 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
1PASS
|
|
96 |
1 |
@Test... |
97 |
|
void appendFile() throws Exception { |
98 |
1 |
String file = DIRECTORY + "AppendTestFile.txt"; |
99 |
1 |
FileUtils.writeStringToFile("Hello World!", file); |
100 |
|
|
101 |
1 |
String append = " more content."; |
102 |
1 |
FileUtils.appendFile(file, append); |
103 |
|
|
104 |
1 |
assertEquals("Hello World! more content.", FileUtils.readFileStream(new File(file))); |
105 |
1 |
Files.delete(Paths.get(file)); |
106 |
|
} |
107 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
108 |
1 |
@Test... |
109 |
|
void failAppendFile() throws Exception { |
110 |
1 |
assertThrows(Exception.class, () -> { |
111 |
1 |
FileUtils.appendFile("no_file_to_append", "appending string."); |
112 |
|
}); |
113 |
|
} |
114 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (16) |
Complexity: 4 |
Complexity Density: 0.4 |
1PASS
|
|
115 |
1 |
@Test... |
116 |
|
void listFileTree() { |
117 |
1 |
File file = new File(DIRECTORY + "dir-test"); |
118 |
1 |
Collection<File> filelist = FileUtils.listFileTree(file); |
119 |
|
|
120 |
1 |
assertEquals(3, filelist.size()); |
121 |
1 |
for (File entry : filelist) { |
122 |
3 |
if (entry.getName().equalsIgnoreCase("root")) { |
123 |
1 |
assertEquals(DIRECTORY + "dir-test/root", |
124 |
|
entry.getAbsolutePath()); |
125 |
|
} |
126 |
3 |
if (entry.getName().equalsIgnoreCase("file.sql")) { |
127 |
1 |
assertEquals(DIRECTORY + "dir-test/level_2.0/level_2.1/file.sql", |
128 |
|
entry.getAbsolutePath()); |
129 |
|
} |
130 |
3 |
if (entry.getName().equalsIgnoreCase("file_a.txt")) { |
131 |
1 |
assertEquals(DIRECTORY + "dir-test/level_1.0/file_a.txt", |
132 |
|
entry.getAbsolutePath()); |
133 |
|
} |
134 |
|
} |
135 |
|
} |
136 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
137 |
1 |
@Test... |
138 |
|
void listFileTreeWithEmptyDir() { |
139 |
1 |
File file = new File(DIRECTORY + "dir-test/empty"); |
140 |
1 |
assertTrue(file.mkdir()); |
141 |
1 |
assertEquals(0, FileUtils.listFileTree(file).size()); |
142 |
1 |
assertTrue(file.delete()); |
143 |
|
} |
144 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
145 |
1 |
@Test... |
146 |
|
void failListFileTree() { |
147 |
1 |
assertNotNull(FileUtils.listFileTree(new File(""))); |
148 |
1 |
assertNotNull(FileUtils.listFileTree(null)); |
149 |
|
} |
150 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
151 |
1 |
@Test... |
152 |
|
void copyFile() throws Exception { |
153 |
1 |
File source = new File(Constraints.SYSTEM_APP_DIR + "/README.md"); |
154 |
1 |
File destination = new File(Constraints.SYSTEM_APP_DIR + "/target/README-COPY.md"); |
155 |
1 |
FileUtils.copyFile(source, destination); |
156 |
1 |
assertTrue(destination.exists()); |
157 |
|
} |
158 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
159 |
1 |
@Test... |
160 |
|
void failCopyFile() throws Exception { |
161 |
1 |
assertThrows(Exception.class, () -> { |
162 |
1 |
FileUtils.copyFile(new File("empty/dontExist.txt"), new File("empty/dontExist-copy.txt")); |
163 |
|
}); |
164 |
|
} |
165 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
166 |
1 |
@Test... |
167 |
|
void inputStreamToByteArray() throws Exception { |
168 |
1 |
byte[] byteArray = {23, 12, 1, 0, 12, 34, 9}; |
169 |
1 |
InputStream input = new ByteArrayInputStream(byteArray); |
170 |
|
|
171 |
1 |
assertArrayEquals(byteArray, FileUtils.inputStreamToByteArray(input)); |
172 |
|
} |
173 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
174 |
1 |
@Test... |
175 |
|
void failInputStreamToByteArray() throws Exception { |
176 |
1 |
assertThrows(Exception.class, () -> { |
177 |
1 |
FileUtils.inputStreamToByteArray(null); |
178 |
|
}); |
179 |
|
} |
180 |
|
} |