1 19 20 package com.hp.hpl.jena.ontology.impl.test; 23 24 25 import junit.framework.*; 28 29 import com.hp.hpl.jena.ontology.*; 30 import com.hp.hpl.jena.rdf.model.*; 31 import com.hp.hpl.jena.vocabulary.*; 32 33 34 35 44 public class TestOntDocumentManager 45 extends TestCase 46 { 47 50 private static Boolean F = Boolean.FALSE; 51 private static Boolean T = Boolean.TRUE; 52 53 56 public static final Integer cnt( int x ) {return new Integer (x);} 57 58 59 public static Object [][] s_testData = new Object [][] { 60 { "testing/ontology/testImport1", cnt(1), T, null }, 62 { "testing/ontology/testImport2", cnt(2), T, null }, 63 { "testing/ontology/testImport2", cnt(1), F, null }, 64 { "testing/ontology/testImport3", cnt(3), T, null }, 65 { "testing/ontology/testImport4", cnt(2), T, null }, 66 { "testing/ontology/testImport5", cnt(2), T, "file:testing/ontology/testImport5/ont-policy.rdf" } 67 }; 68 69 70 73 74 77 public TestOntDocumentManager( String s ) { 78 super( s ); 79 } 80 81 public static TestSuite suite() { 82 TestSuite suite = new TestSuite( "TestOntDocumentManager" ); 83 84 suite.addTestSuite( TestOntDocumentManager.class ); 86 87 for (int i = 0; i < s_testData.length; i++) { 89 suite.addTest( new DocManagerImportTest( (String ) s_testData[i][0], 90 ((Integer ) s_testData[i][1]).intValue(), 91 ((Boolean ) s_testData[i][2]).booleanValue(), 92 (String ) s_testData[i][3]) ); 93 } 94 95 return suite; 96 } 97 98 99 102 public void setUp() { 103 OntDocumentManager.getInstance().reset( true ); 105 } 106 107 108 public void testInitialisation() { 109 OntDocumentManager mgr = new OntDocumentManager( "file:etc/ont-policy-test.rdf" ); 110 111 assertTrue( "Should be at least one specification loaded", mgr.listDocuments().hasNext() ); 112 assertNotNull( "cache URL for owl should not be null", mgr.doAltURLMapping( "http://www.w3.org/2002/07/owl" )); 113 assertEquals( "cache URL for owl not correct", "file:vocabularies/owl.owl", mgr.doAltURLMapping( "http://www.w3.org/2002/07/owl" )); 114 assertEquals( "prefix for owl not correct", "owl", mgr.getPrefixForURI( "http://www.w3.org/2002/07/owl#" )); 115 116 mgr = new OntDocumentManager( "" ); 117 assertTrue( "Should be no specification loaded", !mgr.listDocuments().hasNext() ); 118 119 mgr = new OntDocumentManager( (String ) null ); 121 assertTrue( "Should be no specification loaded", !mgr.listDocuments().hasNext() ); 122 123 } 124 125 public void testReset() { 126 OntDocumentManager mgr = new OntDocumentManager( (String ) null ); 127 128 assertTrue( mgr.getProcessImports() ); 129 mgr.setProcessImports( false ); 130 assertFalse( mgr.getProcessImports() ); 131 mgr.reset(); 132 assertTrue( mgr.getProcessImports() ); 133 134 assertEquals( OntDocumentManager.DEFAULT_METADATA_PATH, mgr.getMetadataSearchPath() ); 135 mgr.setMetadataSearchPath( "file:foo.xml", true ); 136 assertEquals( "file:foo.xml", mgr.getMetadataSearchPath() ); 137 mgr.reset(); 138 assertEquals( OntDocumentManager.DEFAULT_METADATA_PATH, mgr.getMetadataSearchPath() ); 139 140 assertTrue( mgr.getCacheModels() ); 141 mgr.setCacheModels(false ); 142 assertFalse( mgr.getCacheModels() ); 143 mgr.reset(); 144 assertTrue( mgr.getCacheModels() ); 145 146 assertTrue( mgr.useDeclaredPrefixes() ); 147 mgr.setUseDeclaredPrefixes( false ); 148 assertFalse( mgr.useDeclaredPrefixes() ); 149 mgr.reset(); 150 assertTrue( mgr.useDeclaredPrefixes() ); 151 } 152 153 public void testConfigure() { 154 Model m = ModelFactory.createDefaultModel(); 156 Resource policy = m.createResource(); 157 m.add( policy, RDF.type, OntDocManagerVocab.DocumentManagerPolicy ); 158 m.add( policy, OntDocManagerVocab.cacheModels, false ); 159 160 OntDocumentManager mgr = new OntDocumentManager( (String ) null ); 161 assertTrue( mgr.getCacheModels() ); 162 mgr.configure( m ); 163 assertFalse( "Docmgr configure() should have updated cache models flag", mgr.getCacheModels() ); 164 } 165 166 167 public void testManualAssociation() { 168 OntDocumentManager mgr = new OntDocumentManager( (String ) null ); 169 170 mgr.addPrefixMapping( "http://www.w3.org/2002/07/owl#", "owl" ); 171 assertEquals( "prefix for owl not correct", "owl", mgr.getPrefixForURI( "http://www.w3.org/2002/07/owl#" )); 172 assertEquals( "URI for owl not correct", "http://www.w3.org/2002/07/owl#", mgr.getURIForPrefix( "owl" )); 173 174 mgr.addAltEntry( "http://www.w3.org/2002/07/owl", "file:foo.bar" ); 175 assertEquals( "Failed to retrieve cache location", "file:foo.bar", mgr.doAltURLMapping( "http://www.w3.org/2002/07/owl" ) ); 176 177 mgr.addLanguageEntry( "http://www.w3.org/2002/07/owl", "http://www.w3.org/2002/07/owl" ); 178 assertEquals( "Failed to retrieve language", "http://www.w3.org/2002/07/owl", mgr.getLanguage( "http://www.w3.org/2002/07/owl" ) ); 179 } 180 181 182 public void testIgnoreImport() { 183 OntDocumentManager dm = new OntDocumentManager(); 184 185 dm.addIgnoreImport( "file:testing/ontology/testImport3/c.owl" ); 186 187 OntModelSpec spec = new OntModelSpec( null, dm, null, ProfileRegistry.OWL_LANG ); 188 OntModel m = ModelFactory.createOntologyModel( spec, null ); 189 assertNotNull( "Ontology model should not be null", m ); 190 191 m.read( "file:testing/ontology/testImport3/a.owl" ); 192 assertEquals( "Marker count not correct", 2, countMarkers( m )); 193 } 194 195 196 public void testRemoveImport1() { 197 OntModel m = ModelFactory.createOntologyModel(); 198 m.read( "file:testing/ontology/testImport3/a.owl" ); 199 assertEquals( "Marker count not correct", 3, countMarkers( m ) ); 200 201 assertTrue( "c should be imported", m.hasLoadedImport( "file:testing/ontology/testImport3/c.owl" ) ); 202 m.getDocumentManager().unloadImport( m, "file:testing/ontology/testImport3/c.owl" ); 203 assertEquals( "Marker count not correct", 2, countMarkers( m ) ); 204 assertFalse( "c should not be imported", m.hasLoadedImport( "file:testing/ontology/testImport3/c.owl" ) ); 205 } 206 207 208 public void testRemoveImport2() { 209 OntModel m = ModelFactory.createOntologyModel(); 210 m.read( "file:testing/ontology/testImport3/a.owl" ); 211 assertEquals( "Marker count not correct", 3, countMarkers( m ) ); 212 213 assertTrue( "c should be imported", m.hasLoadedImport( "file:testing/ontology/testImport3/c.owl" ) ); 214 assertTrue( "b should be imported", m.hasLoadedImport( "file:testing/ontology/testImport3/b.owl" ) ); 215 m.getDocumentManager().unloadImport( m, "file:testing/ontology/testImport3/b.owl" ); 216 assertEquals( "Marker count not correct", 1, countMarkers( m ) ); 217 assertFalse( "c should not be imported", m.hasLoadedImport( "file:testing/ontology/testImport3/c.owl" ) ); 218 assertFalse( "b should not be imported", m.hasLoadedImport( "file:testing/ontology/testImport3/b.owl" ) ); 219 } 220 221 222 public void testRemoveImport3() { 223 OntModel m = ModelFactory.createOntologyModel(); 224 m.read( "file:testing/ontology/testImport6/a.owl" ); 225 assertEquals( "Marker count not correct", 4, countMarkers( m ) ); 226 227 assertTrue( "c should be imported", m.hasLoadedImport( "file:testing/ontology/testImport6/c.owl" ) ); 228 assertTrue( "b should be imported", m.hasLoadedImport( "file:testing/ontology/testImport6/b.owl" ) ); 229 assertTrue( "d should be imported", m.hasLoadedImport( "file:testing/ontology/testImport6/d.owl" ) ); 230 m.getDocumentManager().unloadImport( m, "file:testing/ontology/testImport6/b.owl" ); 231 assertEquals( "Marker count not correct", 3, countMarkers( m ) ); 232 assertTrue( "c should be imported", m.hasLoadedImport( "file:testing/ontology/testImport6/c.owl" ) ); 233 assertTrue( "d should be imported", m.hasLoadedImport( "file:testing/ontology/testImport6/d.owl" ) ); 234 assertFalse( "b should not be imported", m.hasLoadedImport( "file:testing/ontology/testImport6/b.owl" ) ); 235 } 236 237 public void testDynamicImports1() { 238 OntModel m = ModelFactory.createOntologyModel(); 239 Resource a = m.getResource( "file:testing/ontology/testImport3/a.owl" ); 240 Resource b = m.getResource( "file:testing/ontology/testImport3/b.owl" ); 241 m.add( a, m.getProfile().IMPORTS(), b ); 242 243 assertEquals( "Marker count not correct", 0, countMarkers( m ) ); 245 246 assertFalse( "c should not be imported", m.hasLoadedImport( "file:testing/ontology/testImport3/c.owl" ) ); 247 assertFalse( "b should not be imported", m.hasLoadedImport( "file:testing/ontology/testImport3/b.owl" ) ); 248 } 249 250 251 public void testDynamicImports2() { 252 OntModel m = ModelFactory.createOntologyModel(); 253 Resource a = m.getResource( "file:testing/ontology/testImport3/a.owl" ); 254 Resource b = m.getResource( "file:testing/ontology/testImport3/b.owl" ); 255 256 m.setDynamicImports( true ); 257 258 m.add( a, m.getProfile().IMPORTS(), b ); 259 260 assertEquals( "Marker count not correct", 2, countMarkers( m ) ); 262 263 assertTrue( "c should be imported", m.hasLoadedImport( "file:testing/ontology/testImport3/c.owl" ) ); 264 assertTrue( "b should be imported", m.hasLoadedImport( "file:testing/ontology/testImport3/b.owl" ) ); 265 } 266 267 268 public void testDynamicImports3() { 269 OntModel m = ModelFactory.createOntologyModel(); 270 m.read( "file:testing/ontology/testImport3/a.owl" ); 271 assertEquals( "Marker count not correct", 3, countMarkers( m ) ); 272 273 assertTrue( "c should be imported", m.hasLoadedImport( "file:testing/ontology/testImport3/c.owl" ) ); 274 assertTrue( "b should be imported", m.hasLoadedImport( "file:testing/ontology/testImport3/b.owl" ) ); 275 276 m.setDynamicImports( true ); 277 278 Resource a = m.getResource( "file:testing/ontology/testImport3/a.owl" ); 279 Resource b = m.getResource( "file:testing/ontology/testImport3/b.owl" ); 280 m.remove( m.createStatement( a, m.getProfile().IMPORTS(), b ) ); 281 282 assertEquals( "Marker count not correct", 1, countMarkers( m ) ); 283 assertFalse( "c should not be imported", m.hasLoadedImport( "file:testing/ontology/testImport3/c.owl" ) ); 284 assertFalse( "b should not be imported", m.hasLoadedImport( "file:testing/ontology/testImport3/b.owl" ) ); 285 } 286 287 public void testSearchPath() { 288 OntDocumentManager o1 = new OntDocumentManager( "file:etc/ont-policy-test.rdf" ); 289 assertEquals( "Did not return correct loaded search path", "file:etc/ont-policy-test.rdf", o1.getLoadedPolicyURL() ); 290 291 OntDocumentManager o2 = new OntDocumentManager( "file:etc/ont-policy-test.notexist.rdf;file:etc/ont-policy-test.rdf" ); 292 assertEquals( "Did not return correct loaded search path", "file:etc/ont-policy-test.rdf", o2.getLoadedPolicyURL() ); 293 294 OntDocumentManager o3 = new OntDocumentManager( (String ) null ); 295 assertNull( "Most recent policy should be null", o3.getLoadedPolicyURL() ); 296 297 o3.setMetadataSearchPath( "file:etc/ont-policy-test.rdf", true ); 298 assertEquals( "Did not return correct loaded search path", "file:etc/ont-policy-test.rdf", o2.getLoadedPolicyURL() ); 299 300 o3.setMetadataSearchPath( "file:etc/ont-policy-test.notexist.rdf", true ); 301 assertNull( "Most recent policy should be null", o3.getLoadedPolicyURL() ); 302 } 303 304 public void testAddModel0() { 305 OntDocumentManager odm = OntDocumentManager.getInstance(); 306 Model m = ModelFactory.createDefaultModel(); 307 String uri = "http://example.com/test#m"; 308 assertNull( odm.getModel( uri )); 309 odm.addModel( uri, m ); 310 assertSame( m, odm.getModel(uri)); 311 } 312 313 public void testAddModel1() { 314 OntDocumentManager odm = OntDocumentManager.getInstance(); 315 Model m0 = ModelFactory.createDefaultModel(); 316 Model m1 = ModelFactory.createDefaultModel(); 317 String uri = "http://example.com/test#m"; 318 assertNull( odm.getModel( uri )); 319 odm.addModel( uri, m0 ); 320 321 odm.addModel( uri, m1 ); 323 assertSame( m0, odm.getModel(uri)); 324 325 odm.addModel( uri, m1, true ); 327 assertSame( m1, odm.getModel(uri)); 328 } 329 330 331 332 public static int countMarkers( Model m ) { 333 int count = 0; 334 335 Resource marker = m.getResource( "http://jena.hpl.hp.com/2003/03/testont#Marker" ); 336 for (StmtIterator i = m.listStatements( null, RDF.type, marker ); i.hasNext(); ) { 337 count++; 338 i.next(); 339 } 340 341 return count; 342 } 343 344 347 348 352 363 static class DocManagerImportTest 364 extends TestCase 365 { 366 String m_dir; 367 int m_count; 368 String m_path; 369 boolean m_processImports; 370 371 372 DocManagerImportTest( String dir, int count, boolean processImports, String path ) { 373 super( dir ); 374 m_dir = dir; 375 m_count = count; 376 m_path = path; 377 m_processImports = processImports; 378 } 379 380 382 public void runTest() { 383 OntDocumentManager dm = new OntDocumentManager(); 384 385 dm.setProcessImports( m_processImports ); 387 if (m_path != null) { 388 dm.setMetadataSearchPath( m_path, true ); 389 } 390 391 OntModelSpec spec = new OntModelSpec( null, dm, null, ProfileRegistry.OWL_LANG ); 393 OntModel m = ModelFactory.createOntologyModel( spec, null ); 394 assertNotNull( "Ontology model should not be null", m ); 395 396 m.read( "file:" + m_dir + "/a.owl" ); 397 assertEquals( "Marker count not correct", m_count, countMarkers( m )); 398 } 399 } 400 401 } 402 403 404 433 434 | Popular Tags |