001: /*****************************************************************************
002: * Source code information
003: * -----------------------
004: * Original author Ian Dickinson, HP Labs Bristol
005: * Author email Ian.Dickinson@hp.com
006: * Package Jena 2
007: * Web http://sourceforge.net/projects/jena/
008: * Created 4 Mar 2003
009: * Filename $RCSfile: TestOntDocumentManager.java,v $
010: * Revision $Revision: 1.29 $
011: * Release status $State: Exp $
012: *
013: * Last modified on $Date: 2008/01/14 16:31:23 $
014: * by $Author: ian_dickinson $
015: *
016: * (c) Copyright 2002, 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
017: * (see footer for full conditions)
018: *****************************************************************************/package com.hp.hpl.jena.ontology.impl.test;
019:
020: // Imports
021: ///////////////
022: import java.io.StringReader;
023: import java.util.*;
024:
025: import org.apache.commons.logging.Log;
026: import org.apache.commons.logging.LogFactory;
027:
028: import junit.framework.*;
029:
030: import com.hp.hpl.jena.ontology.*;
031: import com.hp.hpl.jena.ontology.OntDocumentManager.ReadFailureHandler;
032: import com.hp.hpl.jena.rdf.model.*;
033: import com.hp.hpl.jena.reasoner.test.TestUtil;
034: import com.hp.hpl.jena.vocabulary.*;
035:
036: /**
037: * <p>
038: * Unit tests for document manager
039: * </p>
040: *
041: * @author Ian Dickinson, HP Labs
042: * (<a href="mailto:Ian.Dickinson@hp.com" >email</a>)
043: * @version CVS $Id: TestOntDocumentManager.java,v 1.29 2008/01/14 16:31:23 ian_dickinson Exp $
044: */
045: public class TestOntDocumentManager extends TestCase {
046: // Constants
047: //////////////////////////////////
048:
049: private static Boolean F = Boolean.FALSE;
050: private static Boolean T = Boolean.TRUE;
051:
052: // Static variables
053: //////////////////////////////////
054:
055: /** Log for this class */
056: private static Log log = LogFactory
057: .getLog(TestOntDocumentManager.class);
058:
059: public static final Integer cnt(int x) {
060: return new Integer(x);
061: }
062:
063: /* Data for various combinations of test import conditions */
064: public static Object[][] s_testData = new Object[][] {
065: // directory to look in marker count imports path (null = default)
066: { "testing/ontology/testImport1", cnt(1), T, null },
067: { "testing/ontology/testImport2", cnt(2), T, null },
068: { "testing/ontology/testImport2", cnt(1), F, null },
069: { "testing/ontology/testImport3", cnt(3), T, null },
070: { "testing/ontology/testImport4", cnt(2), T, null },
071: { "testing/ontology/testImport5", cnt(2), T,
072: "file:testing/ontology/testImport5/ont-policy.rdf" } };
073:
074: // Instance variables
075: //////////////////////////////////
076:
077: // Constructors
078: //////////////////////////////////
079:
080: public TestOntDocumentManager(String s) {
081: super (s);
082: }
083:
084: public static TestSuite suite() {
085: TestSuite suite = new TestSuite("TestOntDocumentManager");
086:
087: // add the fixed test cases
088: suite.addTestSuite(TestOntDocumentManager.class);
089:
090: // add the data-driven test cases
091: for (int i = 0; i < s_testData.length; i++) {
092: suite.addTest(new DocManagerImportTest(
093: (String) s_testData[i][0],
094: ((Integer) s_testData[i][1]).intValue(),
095: ((Boolean) s_testData[i][2]).booleanValue(),
096: (String) s_testData[i][3]));
097: }
098: return suite;
099: }
100:
101: // External signature methods
102: //////////////////////////////////
103:
104: public void setUp() {
105: // ensure the ont doc manager is in a consistent state
106: OntDocumentManager.getInstance().reset(true);
107:
108: // forget any cached models in the model spec
109: // TODO remove this once we rationalise modelmakers in the OntModel code
110: Set modelNames = new HashSet();
111: ModelMaker memMaker = OntModelSpec.OWL_MEM
112: .getImportModelMaker();
113: for (Iterator i = memMaker.listModels(); i.hasNext();) {
114: modelNames.add(i.next());
115: }
116: for (Iterator i = modelNames.iterator(); i.hasNext();) {
117:
118: String mn = (String) i.next();
119: memMaker.removeModel(mn);
120: }
121: }
122:
123: public void testConstruct0() {
124: OntDocumentManager m = new OntDocumentManager();
125: assertNotNull(m);
126: assertEquals(m.getMetadataSearchPath(),
127: OntDocumentManager.DEFAULT_METADATA_PATH);
128: }
129:
130: public void testConstruct1() {
131: OntDocumentManager mgr = new OntDocumentManager("");
132: assertTrue("Should be no specification loaded", !mgr
133: .listDocuments().hasNext());
134: }
135:
136: public void testConstruct2() {
137: // make sure we don't fail on null
138: OntDocumentManager mgr = new OntDocumentManager((String) null);
139: assertTrue("Should be no specification loaded", !mgr
140: .listDocuments().hasNext());
141: }
142:
143: public void testConstruct3() {
144: Model m = ModelFactory.createDefaultModel();
145: Resource r = m.createResource();
146: r.addProperty(RDF.type, OntDocManagerVocab.OntologySpec);
147: r.addProperty(OntDocManagerVocab.publicURI, m
148: .createResource("http://example.com/foo"));
149: r.addProperty(OntDocManagerVocab.altURL, m
150: .createResource("file:local.rdf"));
151:
152: OntDocumentManager mgr = new OntDocumentManager(m);
153: assertEquals("cache URL not correct", "file:local.rdf", mgr
154: .doAltURLMapping("http://example.com/foo"));
155: }
156:
157: public void testInitialisation() {
158: OntDocumentManager mgr = new OntDocumentManager(
159: "file:etc/ont-policy-test.rdf");
160:
161: assertTrue("Should be at least one specification loaded", mgr
162: .listDocuments().hasNext());
163: assertNotNull("cache URL for owl should not be null", mgr
164: .doAltURLMapping("http://www.w3.org/2002/07/owl"));
165: assertEquals("cache URL for owl not correct",
166: "file:vocabularies/owl.owl",
167: mgr.doAltURLMapping("http://www.w3.org/2002/07/owl"));
168: assertEquals("prefix for owl not correct", "owl", mgr
169: .getPrefixForURI("http://www.w3.org/2002/07/owl#"));
170: }
171:
172: public void testGetInstance() {
173: OntDocumentManager odm = OntDocumentManager.getInstance();
174: assertNotNull(odm);
175:
176: OntDocumentManager odm2 = OntDocumentManager.getInstance();
177: assertSame(odm, odm2);
178: }
179:
180: public void testSetMetadataSearchPath() {
181: OntDocumentManager odm = new OntDocumentManager(
182: "file:etc/ont-policy-test.rdf");
183: assertEquals("file:etc/ont-policy-test.rdf", odm
184: .getMetadataSearchPath());
185: assertTrue(odm.listDocuments().hasNext());
186: assertEquals("file:etc/ont-policy-test.rdf", odm
187: .getLoadedPolicyURL());
188:
189: odm.setMetadataSearchPath("file:notexist.rdf", false);
190: assertTrue(odm.listDocuments().hasNext());
191: assertNull(odm.getLoadedPolicyURL());
192:
193: odm.setMetadataSearchPath("file:notexist.rdf", true);
194: assertFalse(odm.listDocuments().hasNext());
195: assertNull(odm.getLoadedPolicyURL());
196:
197: odm
198: .setMetadataSearchPath("file:etc/ont-policy-test.rdf",
199: false);
200: assertTrue(odm.listDocuments().hasNext());
201: assertEquals("file:etc/ont-policy-test.rdf", odm
202: .getLoadedPolicyURL());
203: }
204:
205: public void testConfigure0() {
206: Model m = ModelFactory.createDefaultModel();
207: Resource r = m.createResource();
208: r.addProperty(RDF.type, OntDocManagerVocab.OntologySpec);
209: r.addProperty(OntDocManagerVocab.publicURI, m
210: .createResource("http://example.com/foo"));
211: r.addProperty(OntDocManagerVocab.altURL, m
212: .createResource("file:local.rdf"));
213:
214: OntDocumentManager odm = new OntDocumentManager(
215: "file:etc/ont-policy-test.rdf");
216: TestUtil.assertIteratorLength(odm.listDocuments(), 3);
217:
218: odm.configure(m, false);
219: TestUtil.assertIteratorLength(odm.listDocuments(), 4);
220: }
221:
222: public void testConfigure1() {
223: Model m = ModelFactory.createDefaultModel();
224: Resource r = m.createResource();
225: r.addProperty(RDF.type, OntDocManagerVocab.OntologySpec);
226: r.addProperty(OntDocManagerVocab.publicURI, m
227: .createResource("http://example.com/foo"));
228: r.addProperty(OntDocManagerVocab.altURL, m
229: .createResource("file:local.rdf"));
230:
231: OntDocumentManager odm = new OntDocumentManager(
232: "file:etc/ont-policy-test.rdf");
233: TestUtil.assertIteratorLength(odm.listDocuments(), 3);
234:
235: odm.configure(m);
236: TestUtil.assertIteratorLength(odm.listDocuments(), 1);
237: }
238:
239: public void testConfigure2() {
240: // create a simple policy
241: Model m = ModelFactory.createDefaultModel();
242: Resource policy = m.createResource();
243: m.add(policy, RDF.type,
244: OntDocManagerVocab.DocumentManagerPolicy);
245: m.addLiteral(policy, OntDocManagerVocab.cacheModels, false);
246:
247: OntDocumentManager mgr = new OntDocumentManager((String) null);
248: assertTrue(mgr.getCacheModels());
249: mgr.configure(m);
250: assertFalse(
251: "Docmgr configure() should have updated cache models flag",
252: mgr.getCacheModels());
253: }
254:
255: public void testReset() {
256: OntDocumentManager mgr = new OntDocumentManager((String) null);
257:
258: assertTrue(mgr.getProcessImports());
259: mgr.setProcessImports(false);
260: assertFalse(mgr.getProcessImports());
261: mgr.reset();
262: assertTrue(mgr.getProcessImports());
263:
264: mgr.setMetadataSearchPath("file:foo.xml", true);
265: assertEquals("file:foo.xml", mgr.getMetadataSearchPath());
266: mgr.reset();
267: assertEquals("file:foo.xml", mgr.getMetadataSearchPath());
268:
269: assertTrue(mgr.getCacheModels());
270: mgr.setCacheModels(false);
271: assertFalse(mgr.getCacheModels());
272: mgr.reset();
273: assertTrue(mgr.getCacheModels());
274:
275: assertTrue(mgr.useDeclaredPrefixes());
276: mgr.setUseDeclaredPrefixes(false);
277: assertFalse(mgr.useDeclaredPrefixes());
278: mgr.reset();
279: assertTrue(mgr.useDeclaredPrefixes());
280: }
281:
282: public void testDoAltMapping() {
283: OntDocumentManager odm = new OntDocumentManager(
284: "file:etc/ont-policy-test.rdf");
285: assertEquals("file:vocabularies/owl.owl", odm
286: .doAltURLMapping("http://www.w3.org/2002/07/owl"));
287: assertEquals("http://example.com/nocache", odm
288: .doAltURLMapping("http://example.com/nocache"));
289: }
290:
291: public void testGetLanguage() {
292: OntDocumentManager odm = new OntDocumentManager(
293: "file:etc/ont-policy-test.rdf");
294: assertEquals(ProfileRegistry.OWL_LANG, odm
295: .getLanguage("http://www.w3.org/2002/07/owl"));
296: assertNull(odm.getLanguage("http://example.com/notthere"));
297: }
298:
299: public void testGetPrefixForURI() {
300: OntDocumentManager odm = new OntDocumentManager(
301: "file:etc/ont-policy-test.rdf");
302: assertEquals("owl", odm
303: .getPrefixForURI("http://www.w3.org/2002/07/owl#"));
304: assertNull(odm.getPrefixForURI("http://example.com/notthere"));
305: }
306:
307: public void testGetURIForPrefix() {
308: OntDocumentManager odm = new OntDocumentManager(
309: "file:etc/ont-policy-test.rdf");
310: assertEquals("http://www.w3.org/2002/07/owl#", odm
311: .getURIForPrefix("owl"));
312: assertNull(odm.getURIForPrefix("http://example.com/notthere"));
313: }
314:
315: public void testAddModel0() {
316: OntDocumentManager odm = OntDocumentManager.getInstance();
317: Model m = ModelFactory.createDefaultModel();
318: String uri = "http://example.com/test#m";
319: assertNull(odm.getModel(uri));
320: odm.addModel(uri, m);
321: assertSame(m, odm.getModel(uri));
322: }
323:
324: public void testAddModel1() {
325: OntDocumentManager odm = OntDocumentManager.getInstance();
326: Model m0 = ModelFactory.createDefaultModel();
327: Model m1 = ModelFactory.createDefaultModel();
328: String uri = "http://example.com/test#m";
329: assertNull(odm.getModel(uri));
330: odm.addModel(uri, m0);
331:
332: // add duplicate with no replace
333: odm.addModel(uri, m1);
334: assertSame(m0, odm.getModel(uri));
335:
336: // add duplicate with replace
337: odm.addModel(uri, m1, true);
338: assertSame(m1, odm.getModel(uri));
339: }
340:
341: public void testClearCache() {
342: OntDocumentManager odm = OntDocumentManager.getInstance();
343: Model m = ModelFactory.createDefaultModel();
344: String uri = "http://example.com/test#m";
345: assertNull(odm.getModel(uri));
346: odm.addModel(uri, m);
347: odm.clearCache();
348: assertSame(null, odm.getModel(uri));
349: }
350:
351: public void testForget() {
352: OntDocumentManager odm = new OntDocumentManager(
353: "file:etc/ont-policy-test.rdf");
354: assertEquals("file:vocabularies/owl.owl", odm
355: .doAltURLMapping("http://www.w3.org/2002/07/owl"));
356: assertEquals("http://www.w3.org/2002/07/owl#", odm
357: .getLanguage("http://www.w3.org/2002/07/owl"));
358: OntModel m = ModelFactory.createOntologyModel();
359: odm.addModel("http://www.w3.org/2002/07/owl#", m);
360: assertNotNull(odm.getModel("http://www.w3.org/2002/07/owl#"));
361:
362: odm.forget("http://www.w3.org/2002/07/owl#");
363: odm.forget("http://www.w3.org/2002/07/owl");
364:
365: assertEquals("http://www.w3.org/2002/07/owl", odm
366: .doAltURLMapping("http://www.w3.org/2002/07/owl"));
367: assertNull(odm.getLanguage("http://www.w3.org/2002/07/owl"));
368: assertNull(odm.getModel("http://www.w3.org/2002/07/owl#"));
369: }
370:
371: public void testGetOntology() {
372: OntDocumentManager odm = new OntDocumentManager(
373: "file:etc/ont-policy-test.rdf");
374: OntModel m = odm.getOntology("http://www.w3.org/2002/07/owl",
375: OntModelSpec.OWL_MEM);
376: assertNotNull(m);
377: assertSame(odm, m.getDocumentManager());
378: OntModel m1 = odm.getOntology("http://www.w3.org/2002/07/owl",
379: OntModelSpec.OWL_MEM);
380: assertSame(m, m1);
381: }
382:
383: public void testProcessImports() {
384: OntDocumentManager odm = new OntDocumentManager(
385: "file:etc/ont-policy-test.rdf");
386: assertTrue(odm.getProcessImports());
387: odm.setProcessImports(false);
388: assertFalse(odm.getProcessImports());
389: }
390:
391: public void testCacheModels() {
392: OntDocumentManager odm = new OntDocumentManager(
393: "file:etc/ont-policy-test.rdf");
394: assertTrue(odm.getCacheModels());
395: odm.setCacheModels(false);
396: assertFalse(odm.getCacheModels());
397: }
398:
399: public void testManualAssociation() {
400: OntDocumentManager odm = new OntDocumentManager((String) null);
401:
402: odm.addPrefixMapping("http://www.w3.org/2002/07/owl#", "owl");
403: assertEquals("prefix for owl not correct", "owl", odm
404: .getPrefixForURI("http://www.w3.org/2002/07/owl#"));
405: assertEquals("URI for owl not correct",
406: "http://www.w3.org/2002/07/owl#", odm
407: .getURIForPrefix("owl"));
408:
409: odm
410: .addAltEntry("http://www.w3.org/2002/07/owl",
411: "file:foo.bar");
412: assertEquals("Failed to retrieve cache location",
413: "file:foo.bar",
414: odm.doAltURLMapping("http://www.w3.org/2002/07/owl"));
415:
416: odm.addLanguageEntry("http://www.w3.org/2002/07/owl",
417: "http://www.w3.org/2002/07/owl");
418: assertEquals("Failed to retrieve language",
419: "http://www.w3.org/2002/07/owl", odm
420: .getLanguage("http://www.w3.org/2002/07/owl"));
421: }
422:
423: public void testIgnoreImport() {
424: OntDocumentManager odm = new OntDocumentManager();
425: TestUtil.assertIteratorLength(odm.listIgnoredImports(), 0);
426:
427: odm.addIgnoreImport("file:testing/ontology/testImport3/c.owl");
428: TestUtil.assertIteratorLength(odm.listIgnoredImports(), 1);
429: assertTrue(odm
430: .ignoringImport("file:testing/ontology/testImport3/c.owl"));
431: assertFalse(odm.ignoringImport("file:testing/ontology/foo.owl"));
432:
433: OntModelSpec spec = new OntModelSpec(null, odm, null,
434: ProfileRegistry.OWL_LANG);
435: OntModel m = ModelFactory.createOntologyModel(spec, null);
436: assertNotNull("Ontology model should not be null", m);
437:
438: m.read("file:testing/ontology/testImport3/a.owl");
439: assertEquals("Marker count not correct", 2, countMarkers(m));
440:
441: odm
442: .removeIgnoreImport("file:testing/ontology/testImport3/c.owl");
443: TestUtil.assertIteratorLength(odm.listIgnoredImports(), 0);
444: assertFalse(odm
445: .ignoringImport("file:testing/ontology/testImport3/c.owl"));
446: }
447:
448: /** Simple case: a imports b, b imports c, remove c */
449: public void testUnloadImport1() {
450: OntModel m = ModelFactory.createOntologyModel();
451: m.read("file:testing/ontology/testImport3/a.owl");
452: assertEquals("Marker count not correct", 3, countMarkers(m));
453:
454: assertTrue(
455: "c should be imported",
456: m
457: .hasLoadedImport("file:testing/ontology/testImport3/c.owl"));
458: m.getDocumentManager().unloadImport(m,
459: "file:testing/ontology/testImport3/c.owl");
460: assertEquals("Marker count not correct", 2, countMarkers(m));
461: assertFalse(
462: "c should not be imported",
463: m
464: .hasLoadedImport("file:testing/ontology/testImport3/c.owl"));
465: }
466:
467: /** case 2: a imports b, b imports c, remove b */
468: public void testUnloadImport2() {
469: OntModel m = ModelFactory.createOntologyModel();
470: m.read("file:testing/ontology/testImport3/a.owl");
471: assertEquals("Marker count not correct", 3, countMarkers(m));
472:
473: assertTrue(
474: "c should be imported",
475: m
476: .hasLoadedImport("file:testing/ontology/testImport3/c.owl"));
477: assertTrue(
478: "b should be imported",
479: m
480: .hasLoadedImport("file:testing/ontology/testImport3/b.owl"));
481: m.getDocumentManager().unloadImport(m,
482: "file:testing/ontology/testImport3/b.owl");
483: assertEquals("Marker count not correct", 1, countMarkers(m));
484: assertFalse(
485: "c should not be imported",
486: m
487: .hasLoadedImport("file:testing/ontology/testImport3/c.owl"));
488: assertFalse(
489: "b should not be imported",
490: m
491: .hasLoadedImport("file:testing/ontology/testImport3/b.owl"));
492: }
493:
494: /** case 3: a imports b, b imports c, a imports d, d imports c, remove b */
495: public void testUnloadImport3() {
496: OntModel m = ModelFactory.createOntologyModel();
497: m.read("file:testing/ontology/testImport6/a.owl");
498: assertEquals("Marker count not correct", 4, countMarkers(m));
499:
500: assertTrue(
501: "c should be imported",
502: m
503: .hasLoadedImport("file:testing/ontology/testImport6/c.owl"));
504: assertTrue(
505: "b should be imported",
506: m
507: .hasLoadedImport("file:testing/ontology/testImport6/b.owl"));
508: assertTrue(
509: "d should be imported",
510: m
511: .hasLoadedImport("file:testing/ontology/testImport6/d.owl"));
512: m.getDocumentManager().unloadImport(m,
513: "file:testing/ontology/testImport6/b.owl");
514: assertEquals("Marker count not correct", 3, countMarkers(m));
515: assertTrue(
516: "c should be imported",
517: m
518: .hasLoadedImport("file:testing/ontology/testImport6/c.owl"));
519: assertTrue(
520: "d should be imported",
521: m
522: .hasLoadedImport("file:testing/ontology/testImport6/d.owl"));
523: assertFalse(
524: "b should not be imported",
525: m
526: .hasLoadedImport("file:testing/ontology/testImport6/b.owl"));
527: }
528:
529: public void testDynamicImports1() {
530: OntModel m = ModelFactory.createOntologyModel();
531: Resource a = m
532: .getResource("file:testing/ontology/testImport3/a.owl");
533: Resource b = m
534: .getResource("file:testing/ontology/testImport3/b.owl");
535: m.add(a, m.getProfile().IMPORTS(), b);
536:
537: // not dymamically imported by default
538: assertEquals("Marker count not correct", 0, countMarkers(m));
539:
540: assertFalse(
541: "c should not be imported",
542: m
543: .hasLoadedImport("file:testing/ontology/testImport3/c.owl"));
544: assertFalse(
545: "b should not be imported",
546: m
547: .hasLoadedImport("file:testing/ontology/testImport3/b.owl"));
548: }
549:
550: public void testDynamicImports2() {
551: OntModel m = ModelFactory.createOntologyModel();
552: Resource a = m
553: .getResource("file:testing/ontology/testImport3/a.owl");
554: Resource b = m
555: .getResource("file:testing/ontology/testImport3/b.owl");
556:
557: m.setDynamicImports(true);
558:
559: m.add(a, m.getProfile().IMPORTS(), b);
560:
561: // dymamically imported
562: assertEquals("Marker count not correct", 2, countMarkers(m));
563:
564: assertTrue(
565: "c should be imported",
566: m
567: .hasLoadedImport("file:testing/ontology/testImport3/c.owl"));
568: assertTrue(
569: "b should be imported",
570: m
571: .hasLoadedImport("file:testing/ontology/testImport3/b.owl"));
572: }
573:
574: public void testDynamicImports3() {
575: OntModel m = ModelFactory.createOntologyModel();
576: m.read("file:testing/ontology/testImport3/a.owl");
577: assertEquals("Marker count not correct", 3, countMarkers(m));
578:
579: assertTrue(
580: "c should be imported",
581: m
582: .hasLoadedImport("file:testing/ontology/testImport3/c.owl"));
583: assertTrue(
584: "b should be imported",
585: m
586: .hasLoadedImport("file:testing/ontology/testImport3/b.owl"));
587:
588: m.setDynamicImports(true);
589:
590: Resource a = m
591: .getResource("file:testing/ontology/testImport3/a.owl");
592: Resource b = m
593: .getResource("file:testing/ontology/testImport3/b.owl");
594: m.remove(m.createStatement(a, m.getProfile().IMPORTS(), b));
595:
596: assertEquals("Marker count not correct", 1, countMarkers(m));
597: assertFalse(
598: "c should not be imported",
599: m
600: .hasLoadedImport("file:testing/ontology/testImport3/c.owl"));
601: assertFalse(
602: "b should not be imported",
603: m
604: .hasLoadedImport("file:testing/ontology/testImport3/b.owl"));
605: }
606:
607: public void testSearchPath() {
608: OntDocumentManager o1 = new OntDocumentManager(
609: "file:etc/ont-policy-test.rdf");
610: assertEquals("Did not return correct loaded search path",
611: "file:etc/ont-policy-test.rdf", o1.getLoadedPolicyURL());
612:
613: OntDocumentManager o2 = new OntDocumentManager(
614: "file:etc/ont-policy-test.notexist.rdf;file:etc/ont-policy-test.rdf");
615: assertEquals("Did not return correct loaded search path",
616: "file:etc/ont-policy-test.rdf", o2.getLoadedPolicyURL());
617:
618: OntDocumentManager o3 = new OntDocumentManager((String) null);
619: assertNull("Most recent policy should be null", o3
620: .getLoadedPolicyURL());
621:
622: o3.setMetadataSearchPath("file:etc/ont-policy-test.rdf", true);
623: assertEquals("Did not return correct loaded search path",
624: "file:etc/ont-policy-test.rdf", o2.getLoadedPolicyURL());
625:
626: o3.setMetadataSearchPath(
627: "file:etc/ont-policy-test.notexist.rdf", true);
628: assertNull("Most recent policy should be null", o3
629: .getLoadedPolicyURL());
630: }
631:
632: public void testReadFailHandler0() {
633: OntDocumentManager o1 = new OntDocumentManager(
634: "file:etc/ont-policy-test.rdf");
635: assertNull(o1.getReadFailureHandler());
636:
637: OntDocumentManager.ReadFailureHandler rfh = new OntDocumentManager.ReadFailureHandler() {
638: public void handleFailedRead(String url, Model model,
639: Exception e) {
640: }
641: };
642:
643: o1.setReadFailureHandler(rfh);
644: assertSame(rfh, o1.getReadFailureHandler());
645: }
646:
647: public void testReadFailHandler1() {
648: OntDocumentManager o1 = new OntDocumentManager(
649: "file:etc/ont-policy-test.rdf");
650:
651: TestFailHandler rfh = new TestFailHandler();
652: o1.setReadFailureHandler(rfh);
653:
654: // trigger the odm to read a non-existant source
655: String source = "@prefix owl: <http://www.w3.org/2002/07/owl#> . <> a owl:Ontology ; owl:imports <http://example.com/not/exist>. ";
656: OntModelSpec spec = new OntModelSpec(OntModelSpec.OWL_MEM);
657: spec.setDocumentManager(o1);
658: OntModel m = ModelFactory.createOntologyModel(spec);
659: m.read(new StringReader(source), "http://example.com/foo#",
660: "N3");
661:
662: assertTrue(rfh.m_seen);
663: }
664:
665: public void testReadHook0() {
666: TestReadHook rh = new TestReadHook(false);
667: OntDocumentManager o1 = new OntDocumentManager(
668: "file:etc/ont-policy-test.rdf");
669: o1.setReadHook(rh);
670: o1.reset();
671:
672: String source = "@prefix owl: <http://www.w3.org/2002/07/owl#> . <> a owl:Ontology ; owl:imports <file:testing/ontology/testImport3/a.owl>. ";
673:
674: OntModelSpec spec = new OntModelSpec(OntModelSpec.OWL_MEM);
675: spec.setDocumentManager(o1);
676: OntModel m = ModelFactory.createOntologyModel(spec);
677: m.read(new StringReader(source), "http://example.com/foo#",
678: "N3");
679:
680: assertEquals("Wrong number of calls to before load hook", 3,
681: rh.m_before);
682: assertEquals("Wrong number of calls to before load hook", 3,
683: rh.m_after);
684: }
685:
686: public void testReadHook1() {
687: TestReadHook rh = new TestReadHook(true);
688: OntDocumentManager o1 = new OntDocumentManager(
689: "file:etc/ont-policy-test.rdf");
690: o1.setReadHook(rh);
691: o1.reset();
692:
693: String source = "@prefix owl: <http://www.w3.org/2002/07/owl#> . <> a owl:Ontology ; owl:imports <file:testing/ontology/testImport3/a.owl>. ";
694:
695: OntModelSpec spec = new OntModelSpec(OntModelSpec.OWL_MEM);
696: spec.setDocumentManager(o1);
697: OntModel m = ModelFactory.createOntologyModel(spec);
698: m.read(new StringReader(source), "http://example.com/foo#",
699: "N3");
700:
701: assertEquals("Wrong number of calls to before load hook", 1,
702: rh.m_before);
703: assertEquals("Wrong number of calls to after load hook", 1,
704: rh.m_after);
705: }
706:
707: /* count the number of marker statements in the combined model */
708: public static int countMarkers(Model m) {
709: int count = 0;
710:
711: Resource marker = m
712: .getResource("http://jena.hpl.hp.com/2003/03/testont#Marker");
713: for (StmtIterator i = m.listStatements(null, RDF.type, marker); i
714: .hasNext();) {
715: count++;
716: i.next();
717: }
718:
719: return count;
720: }
721:
722: // Internal implementation methods
723: //////////////////////////////////
724:
725: //==============================================================================
726: // Inner class definitions
727: //==============================================================================
728:
729: /**
730: * Document manager imports test case. Each test case starts with a root model (always a.owl in some
731: * directory), and loads the model. Depending on the model contents, and the settings of the doc
732: * manager, other models will be loaded. Each model is set to contain a fixed number of marker
733: * statements of the form:
734: * <code><pre>
735: * <Marker rdf:ID="a0" />
736: * </pre></code>
737: * the test for having correctly loaded the models is to count the markers and compare to the predicted
738: * total.
739: */
740: static class DocManagerImportTest extends TestCase {
741: String m_dir;
742: int m_count;
743: String m_path;
744: boolean m_processImports;
745:
746: /* constuctor */
747: DocManagerImportTest(String dir, int count,
748: boolean processImports, String path) {
749: super (dir);
750: m_dir = dir;
751: m_count = count;
752: m_path = path;
753: m_processImports = processImports;
754: }
755:
756: // external contract methods
757:
758: public void runTest() {
759: OntDocumentManager dm = new OntDocumentManager();
760:
761: // adjust the doc manager properties according to the test setup
762: dm.setProcessImports(m_processImports);
763: if (m_path != null) {
764: dm.setMetadataSearchPath(m_path, true);
765: }
766:
767: // now load the model - we always start from a.owl in the given directory
768: OntModelSpec spec = new OntModelSpec(null, dm, null,
769: ProfileRegistry.OWL_LANG);
770: OntModel m = ModelFactory.createOntologyModel(spec, null);
771: assertNotNull("Ontology model should not be null", m);
772:
773: m.read("file:" + m_dir + "/a.owl");
774: assertEquals("Marker count not correct", m_count,
775: countMarkers(m));
776: }
777: }
778:
779: static class TestFailHandler implements ReadFailureHandler {
780: public boolean m_seen = false;
781:
782: public void handleFailedRead(String url, Model model,
783: Exception e) {
784: m_seen = true;
785: log.debug("Seeing failed read of " + url, e);
786: }
787:
788: }
789:
790: static class TestReadHook implements OntDocumentManager.ReadHook {
791: private int m_before = 0;
792: private int m_after = 0;
793: private boolean m_renaming = false;
794:
795: TestReadHook(boolean renaming) {
796: m_renaming = renaming;
797: }
798:
799: public void afterRead(Model model, String source,
800: OntDocumentManager odm) {
801: m_after++;
802: }
803:
804: public String beforeRead(Model model, String source,
805: OntDocumentManager odm) {
806: if (m_renaming) {
807: // local rewrite of the source file, which could be used e.g. to
808: // get the source from a .jar file
809: m_before++;
810: return "file:testing/ontology/testImport3/c.owl";
811: } else {
812: m_before++;
813: return source;
814: }
815: }
816:
817: }
818: }
819:
820: /*
821: (c) Copyright 2002, 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
822: All rights reserved.
823:
824: Redistribution and use in source and binary forms, with or without
825: modification, are permitted provided that the following conditions
826: are met:
827:
828: 1. Redistributions of source code must retain the above copyright
829: notice, this list of conditions and the following disclaimer.
830:
831: 2. Redistributions in binary form must reproduce the above copyright
832: notice, this list of conditions and the following disclaimer in the
833: documentation and/or other materials provided with the distribution.
834:
835: 3. The name of the author may not be used to endorse or promote products
836: derived from this software without specific prior written permission.
837:
838: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
839: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
840: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
841: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
842: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
843: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
844: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
845: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
846: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
847: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
848: */
|