2020 */
2121package org .biojava .nbio .structure ;
2222
23- import junit . framework . TestCase ;
23+ import org . biojava . nbio . core . util . FlatFileCache ;
2424import org .biojava .nbio .structure .io .mmcif .DownloadChemCompProvider ;
2525import org .biojava .nbio .structure .io .mmcif .model .ChemComp ;
26+ import org .junit .Test ;
27+ import static org .junit .Assert .*;
2628
27- public class TestDownloadChemCompProvider extends TestCase {
29+ import java .io .File ;
30+ import java .io .FileOutputStream ;
31+ import java .io .IOException ;
32+ import java .io .PrintWriter ;
33+ import java .util .zip .GZIPOutputStream ;
2834
35+ public class TestDownloadChemCompProvider {
36+
37+ @ Test
2938 public void testProtectedIDs (){
3039
3140 String id = "CON" ;
@@ -36,5 +45,98 @@ public void testProtectedIDs(){
3645
3746 assertEquals (cc .getId (), id );
3847 }
48+
49+ @ Test
50+ public void testRedirectWorks () {
51+ // since August 2017, RCSB is redirecting:
52+ // http://rcsb.org/pdb/files/ligand/HEM.cif ----> http://files.org/ligands/HEM.cif
53+ // see #703
54+
55+ File file = new File (DownloadChemCompProvider .getLocalFileName ("HEM" ));
56+ file .delete ();
57+
58+ DownloadChemCompProvider prov = new DownloadChemCompProvider ();
59+
60+ DownloadChemCompProvider .serverBaseUrl = "http://www.rcsb.org/pdb/files/ligand/" ;
61+
62+ ChemComp cc = prov .getChemComp ("HEM" );
63+
64+ //System.out.println(file.toString());
65+
66+ assertTrue (file .exists ());
67+
68+ // just in case the we did get garbage, let's clean up
69+ file .delete ();
70+
71+ // very important: we have a memory cache of files, we need to reset it not to pollute the cache for later tests
72+ FlatFileCache .clear ();
73+
74+ assertNotNull (cc );
75+
76+ assertNotNull (cc .getName ());
77+
78+ // reset to default URL or otherwise we could affect other tests
79+ DownloadChemCompProvider .serverBaseUrl = DownloadChemCompProvider .DEFAULT_SERVER_URL ;
80+ }
81+
82+ @ Test
83+ public void testWeDontCacheGarbage () {
84+ // see #703
85+
86+ File file = new File (DownloadChemCompProvider .getLocalFileName ("HEM" ));
87+
88+ file .delete ();
89+
90+ DownloadChemCompProvider prov = new DownloadChemCompProvider ();
91+
92+ // a fake URL that should give a 404
93+ DownloadChemCompProvider .serverBaseUrl = "http://www.rcsb.org/non-existent-ligand-url/" ;
94+
95+ ChemComp cc = prov .getChemComp ("HEM" );
96+
97+ // we got a 404 back from server so we shouldn't have cached a file
98+ assertTrue (!file .exists ());
99+
100+ file .delete ();
101+
102+ // very important: we have a memory cache of files, we need to reset it not to pollute the cache for later tests
103+ FlatFileCache .clear ();
104+
105+ // we couldn't parse, thus there should be no content
106+ assertNull (cc .getName ());
107+
108+ // reset to default URL or otherwise we could affect other tests
109+ DownloadChemCompProvider .serverBaseUrl = DownloadChemCompProvider .DEFAULT_SERVER_URL ;
110+
111+
112+ }
113+
114+ @ Test
115+ public void testIfWeCachedGarbageWeCanDetectIt () throws IOException {
116+ // see #703
117+ // TODO this test for the moment only asserts that we get an empty chemcomp, since we can't detect bad cached files yet
118+
119+ File file = new File (DownloadChemCompProvider .getLocalFileName ("HEM" ));
120+
121+ PrintWriter pw = new PrintWriter (new GZIPOutputStream (new FileOutputStream (file )));
122+ pw .println ("A lot of garbage" );
123+ pw .close ();
124+
125+ DownloadChemCompProvider prov = new DownloadChemCompProvider ();
126+
127+ ChemComp cc = prov .getChemComp ("HEM" );
128+
129+ assertTrue (file .exists ());
130+
131+ file .delete ();
132+
133+ // very important: we have a memory cache of files, we need to reset it not to pollute the cache for later tests
134+ FlatFileCache .clear ();
135+
136+ assertNull (cc .getName ());
137+
138+
139+
140+ }
39141
40142}
0 commit comments