Skip to content

Commit ddbad5d

Browse files
committed
Ensure that all ontology tests use a temporary directory for caching and indexing
1 parent 704c101 commit ddbad5d

5 files changed

Lines changed: 113 additions & 90 deletions

File tree

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package ubic.basecode.ontology;
2+
3+
import org.apache.commons.io.file.PathUtils;
4+
import org.junit.AfterClass;
5+
import org.junit.BeforeClass;
6+
import ubic.basecode.util.Configuration;
7+
8+
import java.io.IOException;
9+
import java.nio.file.Files;
10+
import java.nio.file.Path;
11+
12+
/**
13+
* Base class for ontology-based tests.
14+
* <p>
15+
* This class ensures that the ontology are stored and indexed in a temporary directory.
16+
*/
17+
public class AbstractOntologyTest {
18+
19+
private static Path tempDir;
20+
private static String prevCacheDir, prevIndexDir;
21+
22+
@BeforeClass
23+
public static void setUpOntologyCacheDir() throws IOException {
24+
prevCacheDir = Configuration.getString( "ontology.cache.dir" );
25+
prevIndexDir = Configuration.getString( "ontology.index.dir" );
26+
tempDir = Files.createTempDirectory( "ontologyCache" );
27+
Configuration.setString( "ontology.cache.dir", tempDir.toAbsolutePath().toString() );
28+
Configuration.setString( "ontology.index.dir", tempDir.resolve( "indices" ).toAbsolutePath().toString() );
29+
}
30+
31+
@AfterClass
32+
public static void clearOntologyCacheDir() throws IOException {
33+
try {
34+
PathUtils.deleteDirectory( tempDir );
35+
} finally {
36+
Configuration.setString( "ontology.cache.dir", prevCacheDir );
37+
Configuration.setString( "ontology.index.dir", prevIndexDir );
38+
}
39+
}
40+
}
Lines changed: 45 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,68 @@
11
/*
22
* The baseCode project
3-
*
3+
*
44
* Copyright (c) 2013 University of British Columbia
5-
*
5+
*
66
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
77
* the License. You may obtain a copy of the License at
8-
*
8+
*
99
* http://www.apache.org/licenses/LICENSE-2.0
10-
*
10+
*
1111
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
1212
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
1313
* specific language governing permissions and limitations under the License.
1414
*/
1515
package ubic.basecode.ontology;
1616

17-
import org.junit.AfterClass;
18-
import org.junit.BeforeClass;
19-
import ubic.basecode.util.Configuration;
17+
import org.apache.commons.io.FileUtils;
18+
import org.junit.Test;
19+
import ubic.basecode.ontology.jena.OntologyLoader;
20+
21+
import java.io.File;
22+
import java.io.InputStream;
23+
import java.net.URL;
24+
import java.nio.file.Files;
25+
import java.nio.file.StandardCopyOption;
26+
27+
import static org.junit.Assert.*;
2028

2129
/**
2230
* Test loading a database-backed ontology
23-
*
31+
*
2432
* @author Paul
25-
*
2633
*/
27-
public class OntologyLoaderTest {
34+
public class OntologyLoaderTest extends AbstractOntologyTest {
2835

29-
private static String prevDir = null;
30-
private static String dataResource = "/data/nif.organism.test.owl.xml";
36+
@Test
37+
public void testHasChanged() throws Exception {
38+
String name = "fooTEST1234";
3139

32-
@BeforeClass
33-
public static void setup() throws Exception {
34-
prevDir = Configuration.getString( "ontology.cache.dir" );
35-
Configuration.setString( "ontology.cache.dir", System.getProperty( "java.io.tmpdir" ) );
36-
}
40+
File f = OntologyLoader.getDiskCachePath( name );
41+
File oldFile = new File( f.getAbsolutePath() + ".old" );
3742

38-
@AfterClass
39-
public static void tearDown() throws Exception {
40-
Configuration.setString( "ontology.cache.dir", prevDir );
41-
}
43+
assertFalse( f.exists() );
44+
assertFalse( oldFile.exists() );
4245

43-
// @Test
44-
// public void testHasChanged() throws Exception {
45-
// String name = "fooTEST1234";
46-
//
47-
// File f = OntologyLoader.getDiskCachePath( name );
48-
// if ( f.exists() ) {
49-
// f.delete();
50-
// }
51-
//
52-
// File oldFile = OntologyLoader.getOldDiskCachePath( name );
53-
// if ( oldFile.exists() ) {
54-
// oldFile.delete();
55-
// }
56-
//
57-
// assertTrue( !f.exists() );
58-
// assertTrue( !oldFile.exists() );
59-
//
60-
// URL resource = this.getClass().getResource( dataResource );
61-
// try (InputStream in = resource.openStream();) {
62-
// Files.copy( in, f.toPath(), StandardCopyOption.REPLACE_EXISTING );
63-
// }
64-
//
65-
// try (InputStream in = resource.openStream();) {
66-
// Files.copy( in, oldFile.toPath(), StandardCopyOption.REPLACE_EXISTING );
67-
// }
68-
//
69-
// assertTrue( !OntologyLoader.hasChanged( name ) );
70-
//
71-
// // Now if the dataResource has changed
72-
// resource = this.getClass().getResource( "/data/nif.organism.test.owl-off-by-one.xml" );
73-
// try (InputStream in = resource.openStream();) {
74-
// Files.copy( in, f.toPath(), StandardCopyOption.REPLACE_EXISTING );
75-
// }
76-
// assertTrue( OntologyLoader.hasChanged( name ) );
77-
//
78-
// }
46+
FileUtils.forceMkdirParent( f );
7947

48+
URL resource = this.getClass().getResource( "/data/nif.organism.test.owl.xml" );
49+
assertNotNull( resource );
50+
try ( InputStream in = resource.openStream(); ) {
51+
Files.copy( in, f.toPath(), StandardCopyOption.REPLACE_EXISTING );
52+
}
53+
54+
try ( InputStream in = resource.openStream(); ) {
55+
Files.copy( in, oldFile.toPath(), StandardCopyOption.REPLACE_EXISTING );
56+
}
57+
58+
assertFalse( OntologyLoader.hasChanged( name ) );
59+
60+
// Now if the dataResource has changed
61+
resource = this.getClass().getResource( "/data/nif.organism.test.owl-off-by-one.xml" );
62+
assertNotNull( resource );
63+
try ( InputStream in = resource.openStream(); ) {
64+
Files.copy( in, f.toPath(), StandardCopyOption.REPLACE_EXISTING );
65+
}
66+
assertTrue( OntologyLoader.hasChanged( name ) );
67+
}
8068
}

test/ubic/basecode/ontology/OntologyTermTest.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
/**
3737
* @author Paul
3838
*/
39-
public class OntologyTermTest {
39+
public class OntologyTermTest extends AbstractOntologyTest {
4040

4141
private static final Logger log = LoggerFactory.getLogger( OntologyTermTest.class );
4242

@@ -64,6 +64,7 @@ public void testGetChildren() throws Exception {
6464
s.initialize( is, false );
6565

6666
OntologyTerm t = s.getTerm( "http://purl.obolibrary.org/obo/DOID_4159" );
67+
assertNotNull( t );
6768

6869
// test of getting basic properties thrown in here
6970
assertEquals( "skin cancer", t.getLabel() );
@@ -146,6 +147,7 @@ public void testGetParents() throws Exception {
146147
*/
147148

148149
OntologyTerm t = s.getTerm( "http://purl.obolibrary.org/obo/DOID_10040" );
150+
assertNotNull( t );
149151
Collection<OntologyTerm> parents = t.getParents( true );
150152
assertEquals( 1, parents.size() );
151153
OntologyTerm p = parents.iterator().next();
@@ -175,14 +177,17 @@ public void testGetParents() throws Exception {
175177
@Test
176178
public void testUberon() {
177179
OntologyTerm t = uberon.getTerm( "http://purl.obolibrary.org/obo/BFO_0000001" );
180+
assertNotNull( t );
178181
assertTrue( t.isRoot() );
179182
assertFalse( t.isObsolete() );
180183

181184
OntologyTerm t2 = uberon.getTerm( "http://purl.obolibrary.org/obo/BFO_0000002" );
185+
assertNotNull( t2 );
182186
assertFalse( t2.isRoot() );
183187
assertFalse( t2.isObsolete() );
184188

185189
OntologyTerm t3 = uberon.getTerm( "http://purl.obolibrary.org/obo/UBERON_0007234" );
190+
assertNotNull( t3 );
186191
assertTrue( t3.isObsolete() );
187192
}
188193

@@ -248,6 +253,7 @@ public void testRejectNonEnglish() throws Exception {
248253
s.initialize( is, false );
249254

250255
OntologyTerm t = s.getTerm( "http://purl.obolibrary.org/obo/CLO_0000292" );
256+
assertNotNull( t );
251257
assertEquals( "immortal larynx-derived cell line cell", t.getLabel() );
252258

253259
}

test/ubic/basecode/ontology/providers/AbstractOntologyServiceTest.java

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,10 @@
1919

2020
package ubic.basecode.ontology.providers;
2121

22-
import org.junit.AfterClass;
23-
import org.junit.BeforeClass;
2422
import org.junit.Test;
23+
import ubic.basecode.ontology.AbstractOntologyTest;
2524
import ubic.basecode.ontology.jena.OntologyLoader;
2625
import ubic.basecode.ontology.model.OntologyTerm;
27-
import ubic.basecode.util.Configuration;
2826

2927
import java.io.File;
3028
import java.net.URL;
@@ -36,22 +34,10 @@
3634
/**
3735
* @author mjacobson
3836
*/
39-
public class AbstractOntologyServiceTest {
37+
public class AbstractOntologyServiceTest extends AbstractOntologyTest {
4038

41-
private static String prevDir = null;
4239
private static final String dataResource = "/data/nif.organism.test.owl.xml";
4340

44-
@BeforeClass
45-
public static void setup() throws Exception {
46-
prevDir = Configuration.getString( "ontology.cache.dir" );
47-
Configuration.setString( "ontology.cache.dir", System.getProperty( "java.io.tmpdir" ) );
48-
}
49-
50-
@AfterClass
51-
public static void tearDown() throws Exception {
52-
Configuration.setString( "ontology.cache.dir", prevDir );
53-
}
54-
5541
@Test
5642
public void testCacheOntologyToDisk() throws Exception {
5743
String name = "fooTEST1234";
@@ -89,11 +75,10 @@ public void testCacheOntologyToDisk() throws Exception {
8975
public void testGenericOntologyServiceMem() throws Exception {
9076
URL resource = this.getClass().getResource( dataResource );
9177
assertNotNull( resource );
92-
GenericOntologyService s = createService( "foo", resource.toString(), false );
78+
GenericOntologyService s = createService( "foo", resource.toString(), true );
9379

9480
Collection<OntologyTerm> r = s.findTerm( "Mouse" );
9581
assertFalse( r.isEmpty() );
96-
9782
}
9883

9984
@Test

test/ubic/basecode/ontology/providers/ObiServiceTest.java

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,21 @@
1414
*/
1515
package ubic.basecode.ontology.providers;
1616

17-
import static org.junit.Assert.assertTrue;
18-
import static org.junit.Assert.fail;
19-
20-
import java.util.Collection;
21-
2217
import org.junit.Test;
23-
import org.slf4j.Logger;
24-
import org.slf4j.LoggerFactory;
25-
18+
import ubic.basecode.ontology.AbstractOntologyTest;
2619
import ubic.basecode.ontology.model.OntologyIndividual;
2720
import ubic.basecode.ontology.model.OntologyResource;
2821
import ubic.basecode.ontology.model.OntologyTerm;
22+
import ubic.basecode.util.Configuration;
23+
24+
import java.util.Collection;
25+
26+
import static org.junit.Assert.*;
2927

3028
/**
3129
* @author paul
3230
*/
33-
public class ObiServiceTest {
34-
35-
private static Logger log = LoggerFactory.getLogger( ObiServiceTest.class );
31+
public class ObiServiceTest extends AbstractOntologyTest {
3632

3733
@Test
3834
public void testLoadAndSearch() throws Exception {
@@ -42,12 +38,20 @@ public void testLoadAndSearch() throws Exception {
4238
assertTrue( m.isOntologyLoaded() );
4339

4440
Collection<OntologyTerm> hits = m.findTerm( "batch" );
45-
assertTrue( !hits.isEmpty() );
41+
assertFalse( hits.isEmpty() );
4642

4743
Collection<OntologyIndividual> ihits = m.findIndividuals( "batch" );
48-
assertTrue( !ihits.isEmpty() );
44+
assertFalse( ihits.isEmpty() );
4945

5046
Collection<OntologyResource> rhits = m.findResources( "batch" );
51-
assertTrue( !rhits.isEmpty() );
47+
assertFalse( rhits.isEmpty() );
48+
}
49+
50+
@Test
51+
public void testWithoutCache() {
52+
Configuration.setString( "ontology.cache.dir", "" );
53+
assertThrows( IllegalArgumentException.class, () -> {
54+
new ObiService().initialize( true, true );
55+
} );
5256
}
5357
}

0 commit comments

Comments
 (0)