| 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.net.URI; |
| 9 |
|
import java.net.URL; |
| 10 |
|
import java.net.URLConnection; |
| 11 |
|
import java.net.URLStreamHandler; |
| 12 |
|
import java.nio.file.Files; |
| 13 |
|
import java.nio.file.Paths; |
| 14 |
|
import java.util.Collection; |
| 15 |
|
import org.europa.together.JUnit5Preperator; |
| 16 |
|
import org.europa.together.application.LogbackLogger; |
| 17 |
|
import org.europa.together.business.Logger; |
| 18 |
|
import org.europa.together.domain.ByteOrderMark; |
| 19 |
|
import org.europa.together.domain.LogLevel; |
| 20 |
|
import static org.hamcrest.Matchers.equalTo; |
| 21 |
|
import static org.hamcrest.MatcherAssert.assertThat; |
| 22 |
|
import static org.junit.jupiter.api.Assertions.*; |
| 23 |
|
import org.junit.jupiter.api.Test; |
| 24 |
|
import org.junit.jupiter.api.extension.ExtendWith; |
| 25 |
|
import static org.mockito.BDDMockito.given; |
| 26 |
|
import static org.mockito.Mockito.mock; |
| 27 |
|
import org.springframework.test.context.ContextConfiguration; |
| 28 |
|
import org.springframework.test.context.junit.jupiter.SpringExtension; |
| 29 |
|
|
| 30 |
|
@SuppressWarnings("unchecked") |
| 31 |
|
@ExtendWith({JUnit5Preperator.class}) |
| 32 |
|
@ExtendWith(SpringExtension.class) |
| 33 |
|
@ContextConfiguration(locations = {"/applicationContext.xml"}) |
| |
|
| 100% |
Uncovered Elements: 0 (105) |
Complexity: 25 |
Complexity Density: 0.32 |
|
| 34 |
|
public class FileUtilsTest { |
| 35 |
|
|
| 36 |
|
private static final Logger LOGGER = new LogbackLogger(FileUtilsTest.class); |
| 37 |
|
|
| 38 |
|
private static final String DIRECTORY |
| 39 |
|
= Constraints.SYSTEM_APP_DIR + "/target/test-classes/"; |
| 40 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
| 41 |
1 |
@Test... |
| 42 |
|
void privateConstructor() throws Exception { |
| 43 |
1 |
Constructor<FileUtils> clazz |
| 44 |
|
= FileUtils.class.getDeclaredConstructor(); |
| 45 |
1 |
clazz.setAccessible(true); |
| 46 |
1 |
assertThrows(Exception.class, () -> { |
| 47 |
1 |
FileUtils call = clazz.newInstance(); |
| 48 |
|
}); |
| 49 |
|
} |
| 50 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
1PASS
|
|
| 51 |
1 |
@Test... |
| 52 |
|
void getFileSize() { |
| 53 |
1 |
assertEquals(146329, FileUtils.getFileSize(DIRECTORY + "Attachment.pdf", null)); |
| 54 |
1 |
assertEquals(146329, FileUtils.getFileSize(DIRECTORY + "Attachment.pdf", "")); |
| 55 |
1 |
assertEquals(146329, FileUtils.getFileSize(DIRECTORY + "Attachment.pdf", " ")); |
| 56 |
1 |
assertEquals(142, FileUtils.getFileSize(DIRECTORY + "Attachment.pdf", "kilo")); |
| 57 |
|
|
| 58 |
1 |
assertEquals(0, FileUtils.getFileSize(DIRECTORY + "Attachment.pdf", "mega")); |
| 59 |
1 |
assertEquals(0, FileUtils.getFileSize(DIRECTORY + "Attachment.pdf", "giga")); |
| 60 |
1 |
assertEquals(0, FileUtils.getFileSize(DIRECTORY + "Attachment.pdf", "tera")); |
| 61 |
|
} |
| 62 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1PASS
|
|
| 63 |
1 |
@Test... |
| 64 |
|
void failGetFileSize() { |
| 65 |
1 |
assertEquals(0, FileUtils.getFileSize(DIRECTORY + "no_file", null)); |
| 66 |
|
} |
| 67 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
| 68 |
1 |
@Test... |
| 69 |
|
void writeEmptyFile() throws Exception { |
| 70 |
1 |
assertTrue(FileUtils.writeStringToFile("", DIRECTORY + "/empty.txt")); |
| 71 |
1 |
assertEquals(0, FileUtils.getFileSize(DIRECTORY + "/empty.txt", null)); |
| 72 |
1 |
Files.delete(Paths.get(DIRECTORY + "/empty.txt")); |
| 73 |
|
} |
| 74 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
| 75 |
1 |
@Test... |
| 76 |
|
void writeStringToFile() throws Exception { |
| 77 |
1 |
String fileContent = "Content of the written File"; |
| 78 |
1 |
assertTrue(FileUtils.writeStringToFile(fileContent, DIRECTORY + "/test.txt")); |
| 79 |
1 |
assertEquals(27, FileUtils.getFileSize(DIRECTORY + "/test.txt", null)); |
| 80 |
1 |
Files.delete(Paths.get(DIRECTORY + "/test.txt")); |
| 81 |
|
} |
| 82 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
| 83 |
1 |
@Test... |
| 84 |
|
void failWriteStringToFile() throws IOException { |
| 85 |
1 |
assertThrows(IOException.class, () -> { |
| 86 |
1 |
FileUtils.writeStringToFile("content", ""); |
| 87 |
|
}); |
| 88 |
|
} |
| 89 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
| 90 |
1 |
@Test... |
| 91 |
|
void readFileStream() throws Exception { |
| 92 |
1 |
String file = DIRECTORY + "TestFile"; |
| 93 |
1 |
assertEquals("Hello World!", FileUtils.readFileStream(new File(file))); |
| 94 |
1 |
assertEquals("Hello World!", FileUtils.readFileStream(new File(file), ByteOrderMark.NONE)); |
| 95 |
|
} |
| 96 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
| 97 |
1 |
@Test... |
| 98 |
|
void failReadFileStream() throws Exception { |
| 99 |
1 |
assertThrows(Exception.class, () -> { |
| 100 |
1 |
FileUtils.readFileStream(new File("no_file")); |
| 101 |
|
}); |
| 102 |
|
} |
| 103 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
| 104 |
1 |
@Test... |
| 105 |
|
void readFileFromClasspath() throws Exception { |
| 106 |
1 |
String content = FileUtils.readFileFromClasspath("/TestFile"); |
| 107 |
1 |
assertEquals("Hello World! \n", content); |
| 108 |
|
} |
| 109 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
| 110 |
1 |
@Test... |
| 111 |
|
void failReadFileFromClasspath() throws Exception { |
| 112 |
1 |
assertThrows(Exception.class, () -> { |
| 113 |
1 |
FileUtils.readFileStream(new File("no_file")); |
| 114 |
|
}); |
| 115 |
|
} |
| 116 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
1PASS
|
|
| 117 |
1 |
@Test... |
| 118 |
|
void appendFile() throws Exception { |
| 119 |
1 |
String file = DIRECTORY + "AppendTestFile.txt"; |
| 120 |
1 |
FileUtils.writeStringToFile("Hello World!", file); |
| 121 |
|
|
| 122 |
1 |
String append = " more content."; |
| 123 |
1 |
FileUtils.appendFile(file, append); |
| 124 |
|
|
| 125 |
1 |
assertEquals("Hello World! more content.", FileUtils.readFileStream(new File(file))); |
| 126 |
1 |
Files.delete(Paths.get(file)); |
| 127 |
|
} |
| 128 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
| 129 |
1 |
@Test... |
| 130 |
|
void failAppendFile() throws Exception { |
| 131 |
1 |
assertThrows(Exception.class, () -> { |
| 132 |
1 |
FileUtils.appendFile("no_file_to_append", "appending string."); |
| 133 |
|
}); |
| 134 |
|
} |
| 135 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (16) |
Complexity: 4 |
Complexity Density: 0.4 |
1PASS
|
|
| 136 |
1 |
@Test... |
| 137 |
|
void listFileTree() { |
| 138 |
1 |
File file = new File(DIRECTORY + "dir-test"); |
| 139 |
1 |
Collection<File> filelist = FileUtils.listFileTree(file); |
| 140 |
|
|
| 141 |
1 |
assertEquals(3, filelist.size()); |
| 142 |
1 |
for (File entry : filelist) { |
| 143 |
3 |
if (entry.getName().equalsIgnoreCase("root")) { |
| 144 |
1 |
assertEquals(DIRECTORY + "dir-test/root", |
| 145 |
|
entry.getAbsolutePath()); |
| 146 |
|
} |
| 147 |
3 |
if (entry.getName().equalsIgnoreCase("file.sql")) { |
| 148 |
1 |
assertEquals(DIRECTORY + "dir-test/level_2.0/level_2.1/file.sql", |
| 149 |
|
entry.getAbsolutePath()); |
| 150 |
|
} |
| 151 |
3 |
if (entry.getName().equalsIgnoreCase("file_a.txt")) { |
| 152 |
1 |
assertEquals(DIRECTORY + "dir-test/level_1.0/file_a.txt", |
| 153 |
|
entry.getAbsolutePath()); |
| 154 |
|
} |
| 155 |
|
} |
| 156 |
|
} |
| 157 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
| 158 |
1 |
@Test... |
| 159 |
|
void listFileTreeWithEmptyDir() { |
| 160 |
1 |
File file = new File(DIRECTORY + "dir-test/empty"); |
| 161 |
1 |
assertTrue(file.mkdir()); |
| 162 |
1 |
assertEquals(0, FileUtils.listFileTree(file).size()); |
| 163 |
1 |
assertTrue(file.delete()); |
| 164 |
|
} |
| 165 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
| 166 |
1 |
@Test... |
| 167 |
|
void failListFileTree() { |
| 168 |
1 |
assertEquals(0, FileUtils.listFileTree(null).size()); |
| 169 |
1 |
assertEquals(0, FileUtils.listFileTree(new File("")).size()); |
| 170 |
1 |
assertEquals(0, FileUtils.listFileTree( |
| 171 |
|
new File(DIRECTORY + "dir-test/root")).size() |
| 172 |
|
); |
| 173 |
|
} |
| 174 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
| 175 |
1 |
@Test... |
| 176 |
|
void copyFile() throws Exception { |
| 177 |
1 |
File source = new File(Constraints.SYSTEM_APP_DIR + "/README.md"); |
| 178 |
1 |
File destination = new File(Constraints.SYSTEM_APP_DIR + "/target/README-COPY.md"); |
| 179 |
1 |
FileUtils.copyFile(source, destination); |
| 180 |
1 |
assertTrue(destination.exists()); |
| 181 |
|
} |
| 182 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
| 183 |
1 |
@Test... |
| 184 |
|
void failCopyFile() throws Exception { |
| 185 |
1 |
assertThrows(Exception.class, () -> { |
| 186 |
1 |
FileUtils.copyFile(new File("empty/dontExist.txt"), new File("empty/dontExist-copy.txt")); |
| 187 |
|
}); |
| 188 |
|
} |
| 189 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
| 190 |
1 |
@Test... |
| 191 |
|
void inputStreamToByteArray() throws Exception { |
| 192 |
1 |
byte[] byteArray = {23, 12, 1, 0, 12, 34, 9}; |
| 193 |
1 |
InputStream input = new ByteArrayInputStream(byteArray); |
| 194 |
|
|
| 195 |
1 |
assertArrayEquals(byteArray, FileUtils.inputStreamToByteArray(input)); |
| 196 |
|
} |
| 197 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
| 198 |
1 |
@Test... |
| 199 |
|
void failInputStreamToByteArray() throws Exception { |
| 200 |
1 |
assertThrows(Exception.class, () -> { |
| 201 |
1 |
FileUtils.inputStreamToByteArray(null); |
| 202 |
|
}); |
| 203 |
|
} |
| 204 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 1 |
Complexity Density: 0.12 |
1PASS
|
|
| 205 |
1 |
@Test... |
| 206 |
|
void readRemoteResource() throws Exception { |
| 207 |
|
|
| 208 |
1 |
byte[] expectedImageBytes = FileUtils.toBytes( |
| 209 |
|
0x47, 0x49, 0x46, 0x38, 0x39, 0x61, 0x0A, 0x00, 0x0A, 0x00, 0x91, 0x00, 0x00, |
| 210 |
|
0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x00, 0x00, 0x21, |
| 211 |
|
0xF9, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2C, 0x00, 0x00, 0x00, 0x00, 0x0A, |
| 212 |
|
0x00, 0x0A, 0x00, 0x00, 0x02, 0x16, 0x8C, 0x2D, 0x99, 0x87, 0x2A, 0x1C, 0xDC, |
| 213 |
|
0x33, 0xA0, 0x02, 0x75, 0xEC, 0x95, 0xFA, 0xA8, 0xDE, 0x60, 0x8C, 0x04, 0x91, |
| 214 |
|
0x4C, 0x01, 0x00, 0x3B); |
| 215 |
1 |
InputStream imageInputStream = new ByteArrayInputStream(expectedImageBytes); |
| 216 |
|
|
| 217 |
1 |
URLConnection mockConnection = mock(URLConnection.class); |
| 218 |
1 |
URLStreamHandler handler = new URLStreamHandler() { |
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 219 |
1 |
@Override... |
| 220 |
|
protected URLConnection openConnection(final URL arg0) |
| 221 |
|
throws IOException { |
| 222 |
1 |
return mockConnection; |
| 223 |
|
} |
| 224 |
|
}; |
| 225 |
1 |
given(mockConnection.getInputStream()).willReturn(imageInputStream); |
| 226 |
|
|
| 227 |
1 |
@SuppressWarnings("deprecation") |
| 228 |
|
URL url = new URL("http", "sample.org", 80, "image.gif", handler); |
| 229 |
|
|
| 230 |
|
|
| 231 |
|
|
| 232 |
1 |
LOGGER.log("URL: " + url.toString(), LogLevel.DEBUG); |
| 233 |
1 |
assertThat(FileUtils.readRemoteResource(url), equalTo(expectedImageBytes)); |
| 234 |
|
} |
| 235 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
| 236 |
1 |
@Test... |
| 237 |
|
void failReadRemoteResource() throws Exception { |
| 238 |
1 |
assertThrows(Exception.class, () -> { |
| 239 |
1 |
FileUtils.readRemoteResource(null); |
| 240 |
|
}); |
| 241 |
|
} |
| 242 |
|
} |