Skip to content

Commit a26adde

Browse files
committed
Unit testing
1 parent f59de95 commit a26adde

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

biojava-core/src/test/java/org/biojava/nbio/core/util/FileDownloadUtilsTest.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@
44
import static org.biojava.nbio.core.util.FileDownloadUtils.getFilePrefix;
55
import static org.junit.jupiter.api.Assertions.assertEquals;
66
import static org.junit.jupiter.api.Assertions.assertFalse;
7+
import static org.junit.jupiter.api.Assertions.assertThrows;
78
import static org.junit.jupiter.api.Assertions.assertTrue;
89

910
import java.io.File;
1011
import java.io.FileInputStream;
1112
import java.io.FileOutputStream;
1213
import java.io.IOException;
14+
import java.io.PrintStream;
15+
import java.net.URL;
1316
import java.nio.file.Files;
1417

1518
import org.junit.jupiter.api.Nested;
@@ -190,4 +193,50 @@ void deleteFolderTree() throws IOException{
190193
assertFalse(toDelete.exists());
191194
}
192195
}
196+
197+
@Nested
198+
class CreateValidationFiles{
199+
200+
@Test
201+
void testValidationFiles() throws IOException{
202+
URL sourceUrl = new URL("https://ftp.wwpdb.org/pub/pdb/data/structures/divided/mmCIF/45/145d.cif.gz");
203+
File destFile = new File(System.getProperty("java.io.tmpdir"), "145d.cif.gz");
204+
File sizeFile = new File(destFile.getParentFile(), destFile.getName()+".size");
205+
File hashFile = new File(destFile.getParentFile(), destFile.getName()+".hash");
206+
System.out.println(destFile.getAbsolutePath());
207+
destFile.delete();
208+
sizeFile.delete();
209+
hashFile.delete();
210+
assertFalse(destFile.exists(), "couldn't delete dest file");
211+
assertFalse(sizeFile.exists(), "couldn't delete size file");
212+
assertFalse(hashFile.exists(), "couldn't delete hash file");
213+
214+
FileDownloadUtils.downloadFile(sourceUrl, destFile);
215+
assertTrue(destFile.exists(), "couldn't create dest file");
216+
217+
assertTrue(FileDownloadUtils.validateFile(destFile), "file not detected to be invalid although there are no validation files");
218+
219+
PrintStream temp1 = new PrintStream(sizeFile);
220+
temp1.print(15); // some wrong size value
221+
temp1.close();
222+
assertFalse(FileDownloadUtils.validateFile(destFile), "file not detected to be invalid although size value is wrong.");
223+
System.out.println("Just ignore the previous warning. It is expected.");
224+
225+
FileDownloadUtils.createValidationFiles(sourceUrl, destFile, null);
226+
assertTrue(sizeFile.exists(), "couldn't create size file");
227+
assertTrue(FileDownloadUtils.validateFile(destFile), "file not detected to be invalid although there is correct size validation file");
228+
229+
PrintStream temp2 = new PrintStream(hashFile);
230+
temp2.print("ABCD"); // some wrong hash value
231+
temp2.close();
232+
//This is not yet implemented. I am using this test for documentation purpose.
233+
assertThrows(UnsupportedOperationException.class,
234+
() -> FileDownloadUtils.validateFile(destFile),
235+
"file not detected to be invalid although size value is wrong.");
236+
237+
destFile.delete();
238+
sizeFile.delete();
239+
hashFile.delete();
240+
}
241+
}
193242
}

0 commit comments

Comments
 (0)