Skip to content
Merged
Prev Previous commit
Next Next commit
Add test for #703
  • Loading branch information
sbliven committed Jun 4, 2018
commit 4785d57be7839ace552328c1097fa4595f41a3da
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@
*/
package org.biojava.nbio.structure.io.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
Expand All @@ -36,6 +33,15 @@
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.channels.ReadableByteChannel;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class FileDownloadUtils {

Expand Down Expand Up @@ -240,6 +246,41 @@ public static URLConnection prepareURLConnection(String url, int timeout) throws
connection.setConnectTimeout(timeout);
return connection;
}

/**
* Recursively delete a folder & contents
*
* @param dir directory to delete
*/
public static void deleteDirectory(Path dir) throws IOException {
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Surprisingly this isn't an nio method and I couldn't find one in BioJava

if(dir == null || !Files.exists(dir))
return;
Files.walkFileTree(dir, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
Files.delete(file);
return FileVisitResult.CONTINUE;
}

@Override
public FileVisitResult postVisitDirectory(Path dir, IOException e) throws IOException {
if (e != null) {
throw e;
}
Files.delete(dir);
return FileVisitResult.CONTINUE;
}
});
}
/**
* Recursively delete a folder & contents
*
* @param dir directory to delete
*/
public static void deleteDirectory(String dir) throws IOException {
deleteDirectory(Paths.get(dir));
}


public static void main(String[] args) {
String url;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,48 @@
*/
package org.biojava.nbio.structure.align.util;

import org.biojava.nbio.structure.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.FileAttribute;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.zip.GZIPOutputStream;

import org.biojava.nbio.structure.AtomPositionMap;
import org.biojava.nbio.structure.Chain;
import org.biojava.nbio.structure.Group;
import org.biojava.nbio.structure.ResidueRangeAndLength;
import org.biojava.nbio.structure.Structure;
import org.biojava.nbio.structure.StructureException;
import org.biojava.nbio.structure.StructureIO;
import org.biojava.nbio.structure.StructureIdentifier;
import org.biojava.nbio.structure.StructureTools;
import org.biojava.nbio.structure.SubstructureIdentifier;
import org.biojava.nbio.structure.io.LocalPDBDirectory;
import org.biojava.nbio.structure.io.LocalPDBDirectory.FetchBehavior;
import org.biojava.nbio.structure.io.LocalPDBDirectory.ObsoleteBehavior;
import org.biojava.nbio.structure.io.util.FileDownloadUtils;
import org.biojava.nbio.structure.io.MMCIFFileReader;
import org.biojava.nbio.structure.scop.ScopDatabase;
import org.biojava.nbio.structure.scop.ScopFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import static org.junit.Assert.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
Expand All @@ -49,22 +71,30 @@
*/
public class AtomCacheTest {

private static Logger logger = LoggerFactory.getLogger(AtomCacheTest.class);
private AtomCache cache;
private String previousPDB_DIR;
private String previousPDB_CACHE_DIR;
private AtomCache cleanCache = new AtomCache();

@Before
public void setUp() {
previousPDB_DIR = System.getProperty(UserConfiguration.PDB_DIR, null);
previousPDB_CACHE_DIR = System.getProperty(UserConfiguration.PDB_CACHE_DIR, null);
cache = new AtomCache();
cache.setObsoleteBehavior(ObsoleteBehavior.FETCH_OBSOLETE);
StructureIO.setAtomCache(cache);
// Use a fixed SCOP version for stability
ScopFactory.setScopDatabase(ScopFactory.VERSION_1_75B);
}

@After
public void tearDown() {
if (previousPDB_DIR != null)
if (previousPDB_DIR != null) {
System.setProperty(UserConfiguration.PDB_DIR, previousPDB_DIR);
System.setProperty(UserConfiguration.PDB_CACHE_DIR, previousPDB_CACHE_DIR);
}
StructureIO.setAtomCache(cleanCache);
}

/**
Expand Down Expand Up @@ -312,4 +342,80 @@ public void testSeqRes() throws StructureException, IOException {

}

/**
* Test for #703 - Chemical component cache poisoning
*
* Handle empty CIF files
* @throws IOException
* @throws StructureException
*/
@Test
public void testEmptyChemComp() throws IOException, StructureException {
Path tmpCache = Paths.get(System.getProperty("java.io.tmpdir"),"BIOJAVA_TEST_CACHE");
logger.info("Testing AtomCache at {}", tmpCache.toString());
System.setProperty(UserConfiguration.PDB_DIR, tmpCache.toString());
System.setProperty(UserConfiguration.PDB_CACHE_DIR, tmpCache.toString());

FileDownloadUtils.deleteDirectory(tmpCache);
Files.createDirectory(tmpCache);
try {
cache.setPath(tmpCache.toString());
cache.setCachePath(tmpCache.toString());
cache.setUseMmCif(true);

// Create an empty chemcomp
Path sub = tmpCache.resolve(Paths.get("chemcomp", "ALA.cif.gz"));
Files.createDirectories(sub.getParent());
Files.createFile(sub);
assertTrue(Files.exists(sub));
assertEquals(0, Files.size(sub));

Structure s = cache.getStructure("1A4W");

assertNotNull(s);
} finally {
FileDownloadUtils.deleteDirectory(tmpCache);
}
}

/**
* Test for #703 - Chemical component cache poisoning
*
* Handle empty CIF files
* @throws IOException
* @throws StructureException
*/
@Test
public void testEmptyGZChemComp() throws IOException, StructureException {
Path tmpCache = Paths.get(System.getProperty("java.io.tmpdir"),"BIOJAVA_TEST_CACHE");
logger.info("Testing AtomCache at {}", tmpCache.toString());
System.setProperty(UserConfiguration.PDB_DIR, tmpCache.toString());
System.setProperty(UserConfiguration.PDB_CACHE_DIR, tmpCache.toString());

FileDownloadUtils.deleteDirectory(tmpCache);
Files.createDirectory(tmpCache);
try {
cache.setPath(tmpCache.toString());
cache.setCachePath(tmpCache.toString());
cache.setUseMmCif(true);

// Create an empty chemcomp
Path sub = tmpCache.resolve(Paths.get("chemcomp", "ALA.cif.gz"));
Files.createDirectories(sub.getParent());
try(GZIPOutputStream out = new GZIPOutputStream(new FileOutputStream(sub.toFile()))) {
// don't write anything
out.flush();
}
assertTrue(Files.exists(sub));
assertTrue(0 < Files.size(sub));

Structure s = cache.getStructure("1A4W");

assertNotNull(s);

} finally {
FileDownloadUtils.deleteDirectory(tmpCache);
}
}

}