-
Notifications
You must be signed in to change notification settings - Fork 379
/
FileDownloadUtilsTest.java
203 lines (174 loc) · 6.77 KB
/
FileDownloadUtilsTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package org.biojava.nbio.core.util;
import static org.biojava.nbio.core.util.FileDownloadUtils.getFileExtension;
import static org.biojava.nbio.core.util.FileDownloadUtils.getFilePrefix;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.net.URL;
import java.nio.file.Files;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
class FileDownloadUtilsTest {
@Nested
class FileExtension {
@Test
void getExtensionHappyCase(){
File someFile = new File("sequence.fasta");
assertEquals("fasta", getFileExtension(someFile));
}
@Test
void lastSuffixOnlyReturned(){
File someFile = new File("sequence.1.a.fasta");
assertEquals("fasta", getFileExtension(someFile));
}
@Test
void fileNameEndingInDotReturnsEmptyString(){
File someFile = new File("noExtension.");
assertEquals("", getFileExtension(someFile));
}
@Test
void hiddenFile(){
File someFile = new File(".m2");
assertEquals("m2", getFileExtension(someFile));
}
@Test
void noExtension(){
File someFile = new File("nameOnly");
assertEquals("nameOnly", getFileExtension(someFile));
}
}
@Nested
class GetFilePrefix{
@Test
void standardFileName(){
File someFile = new File("sequence.fasta");
assertEquals("sequence", getFilePrefix(someFile));
}
@Test
void prefixIsUpToFirstDot(){
File someFile = new File("sequence.1.2.fasta");
assertEquals("sequence", getFilePrefix(someFile));
}
@Test
void noExtension(){
File someFile = new File("nameOnly");
assertEquals("nameOnly", getFilePrefix(someFile));
}
@Test
void hiddenFile(){
File someFile = new File(".m2");
assertEquals("", getFilePrefix(someFile));
}
}
@Nested
class ToUnixPath {
@Test
void windowsToUnixAddsTrailingSlash(){
String winPath = "C:\\a\\b\\c";
assertEquals("C:/a/b/c/", FileDownloadUtils.toUnixPath(winPath));
}
@Test
void unixPathReturnedUnchanged(){
String path = "/a/b/c/";
assertEquals(path, FileDownloadUtils.toUnixPath(path));
}
}
@Nested
class ExpandUserHome {
String currUserHome = System.getProperty("user.home");
@Test
void minimalPath (){
String path="~";
assertEquals(currUserHome, FileDownloadUtils.expandUserHome(path));
}
@Test
void simplePath (){
String path="~/sequence.gb";
assertEquals(currUserHome+File.separator+"sequence.gb", FileDownloadUtils.expandUserHome(path));
}
@Test
void nestedPath (){
String path="~/a/b/c/sequence.gb";
assertEquals(currUserHome+File.separator
+ "a" + File.separator
+ "b" + File.separator
+ "c" + File.separator
+ "sequence.gb",
FileDownloadUtils.expandUserHome(path));
}
}
@Nested
class URLMethods {
final String availableUrl = "https://www.google.com";
@Test
void pingGoogleOK(){
assertTrue(FileDownloadUtils.ping(availableUrl, 1000));
}
@Test
void pingNonExistentFalse(){
assertFalse(FileDownloadUtils.ping("https://non-existent.biojava", 1));
}
}
@Nested
class DeleteDirectory {
private File createDirectoryTree () throws IOException {
File tmpdir = Files.createTempDirectory("tmpDirPrefix").toFile();
File child1 = new File(tmpdir, "a");
File child2 = new File(child1, "b");
File child3 = new File(child2, "c");
File f = new File(child3, "seq.fa");
child3.mkdirs();
f.createNewFile();
return tmpdir;
}
@Test
void deleteFolderTree() throws IOException{
File toDelete = createDirectoryTree();
assertTrue(toDelete.exists());
FileDownloadUtils.deleteDirectory(toDelete.getAbsolutePath());
assertFalse(toDelete.exists());
}
}
@Nested
class CreateValidationFiles{
@Test
void testValidationFiles() throws IOException{
URL sourceUrl = new URL("https://files.wwpdb.org/pub/pdb/data/structures/divided/mmCIF/45/145d.cif.gz");
File destFile = new File(System.getProperty("java.io.tmpdir"), "145d.cif.gz");
File sizeFile = new File(destFile.getParentFile(), destFile.getName()+".size");
File hashFile = new File(destFile.getParentFile(), destFile.getName()+".hash_MD5");
System.out.println(destFile.getAbsolutePath());
destFile.delete();
sizeFile.delete();
hashFile.delete();
assertFalse(destFile.exists(), "couldn't delete dest file");
assertFalse(sizeFile.exists(), "couldn't delete size file");
assertFalse(hashFile.exists(), "couldn't delete hash file");
FileDownloadUtils.downloadFile(sourceUrl, destFile);
assertTrue(destFile.exists(), "couldn't create dest file");
assertTrue(FileDownloadUtils.validateFile(destFile), "file detected to be invalid although there are no validation files");
PrintStream temp1 = new PrintStream(sizeFile);
temp1.print(15); // some wrong size value
temp1.close();
assertFalse(FileDownloadUtils.validateFile(destFile), "file not detected to be invalid although size value is wrong.");
System.out.println("Just ignore the previous warning. It is expected.");
FileDownloadUtils.createValidationFiles(sourceUrl, destFile, null, FileDownloadUtils.Hash.UNKNOWN);
assertTrue(sizeFile.exists(), "couldn't create size file");
assertTrue(FileDownloadUtils.validateFile(destFile), "file not detected to be invalid although there is correct size validation file");
PrintStream temp2 = new PrintStream(hashFile);
temp2.print("ABCD"); // some wrong hash value
temp2.close();
//This is not yet implemented. I am using this test for documentation purpose.
assertThrows(UnsupportedOperationException.class,
() -> FileDownloadUtils.validateFile(destFile),
"file not detected to be invalid although hash value is wrong.");
destFile.delete();
sizeFile.delete();
hashFile.delete();
}
}
}