|
4 | 4 | import static org.biojava.nbio.core.util.FileDownloadUtils.getFilePrefix; |
5 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; |
6 | 6 | import static org.junit.jupiter.api.Assertions.assertFalse; |
| 7 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
7 | 8 | import static org.junit.jupiter.api.Assertions.assertTrue; |
8 | 9 |
|
9 | 10 | import java.io.File; |
10 | 11 | import java.io.FileInputStream; |
11 | 12 | import java.io.FileOutputStream; |
12 | 13 | import java.io.IOException; |
| 14 | +import java.io.PrintStream; |
| 15 | +import java.net.URL; |
13 | 16 | import java.nio.file.Files; |
14 | 17 |
|
15 | 18 | import org.junit.jupiter.api.Nested; |
@@ -190,4 +193,50 @@ void deleteFolderTree() throws IOException{ |
190 | 193 | assertFalse(toDelete.exists()); |
191 | 194 | } |
192 | 195 | } |
| 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 | + } |
193 | 242 | } |
0 commit comments