KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > ontology > impl > test > TestOntDocumentManager


1 /*****************************************************************************
2  * Source code information
3  * -----------------------
4  * Original author Ian Dickinson, HP Labs Bristol
5  * Author email Ian.Dickinson@hp.com
6  * Package Jena 2
7  * Web http://sourceforge.net/projects/jena/
8  * Created 4 Mar 2003
9  * Filename $RCSfile: TestOntDocumentManager.java,v $
10  * Revision $Revision: 1.16 $
11  * Release status $State: Exp $
12  *
13  * Last modified on $Date: 2005/03/04 12:51:03 $
14  * by $Author: ian_dickinson $
15  *
16  * (c) Copyright 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
17  * (see footer for full conditions)
18  *****************************************************************************/

19
20 // Package
21
///////////////
22
package com.hp.hpl.jena.ontology.impl.test;
23
24
25 // Imports
26
///////////////
27
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 /**
36  * <p>
37  * Unit tests for document manager
38  * </p>
39  *
40  * @author Ian Dickinson, HP Labs
41  * (<a HREF="mailto:Ian.Dickinson@hp.com" >email</a>)
42  * @version CVS $Id: TestOntDocumentManager.java,v 1.16 2005/03/04 12:51:03 ian_dickinson Exp $
43  */

44 public class TestOntDocumentManager
45     extends TestCase
46 {
47     // Constants
48
//////////////////////////////////
49

50     private static Boolean JavaDoc F = Boolean.FALSE;
51     private static Boolean JavaDoc T = Boolean.TRUE;
52
53     // Static variables
54
//////////////////////////////////
55

56     public static final Integer JavaDoc cnt( int x ) {return new Integer JavaDoc(x);}
57
58     /* Data for various combinations of test import conditions */
59     public static Object JavaDoc[][] s_testData = new Object JavaDoc[][] {
60         // directory to look in marker count imports path (null = default)
61
{ "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     // Instance variables
71
//////////////////////////////////
72

73
74     // Constructors
75
//////////////////////////////////
76

77     public TestOntDocumentManager( String JavaDoc s ) {
78         super( s );
79     }
80
81     public static TestSuite suite() {
82         TestSuite suite = new TestSuite( "TestOntDocumentManager" );
83
84         // add the fixed test cases
85
suite.addTestSuite( TestOntDocumentManager.class );
86
87         // add the data-driven test cases
88
for (int i = 0; i < s_testData.length; i++) {
89             suite.addTest( new DocManagerImportTest( (String JavaDoc) s_testData[i][0],
90                                                      ((Integer JavaDoc) s_testData[i][1]).intValue(),
91                                                      ((Boolean JavaDoc) s_testData[i][2]).booleanValue(),
92                                                      (String JavaDoc) s_testData[i][3]) );
93         }
94
95         return suite;
96     }
97
98
99     // External signature methods
100
//////////////////////////////////
101

102     public void setUp() {
103         // ensure the ont doc manager is in a consistent state
104
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         // make sure we don't fail on null
120
mgr = new OntDocumentManager( (String JavaDoc) null );
121         assertTrue( "Should be no specification loaded", !mgr.listDocuments().hasNext() );
122
123     }
124
125     public void testReset() {
126         OntDocumentManager mgr = new OntDocumentManager( (String JavaDoc) 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         // create a simple policy
155
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 JavaDoc) 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 JavaDoc) 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     /** Simple case: a imports b, b imports c, remove c */
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     /** case 2: a imports b, b imports c, remove b */
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     /** case 3: a imports b, b imports c, a imports d, d imports c, remove b */
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         // not dymamically imported by default
244
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         // dymamically imported
261
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 JavaDoc) 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 JavaDoc 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 JavaDoc uri = "http://example.com/test#m";
318         assertNull( odm.getModel( uri ));
319         odm.addModel( uri, m0 );
320
321         // add duplicate with no replace
322
odm.addModel( uri, m1 );
323         assertSame( m0, odm.getModel(uri));
324
325         // add duplicate with replace
326
odm.addModel( uri, m1, true );
327         assertSame( m1, odm.getModel(uri));
328     }
329
330
331     /* count the number of marker statements in the combined model */
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     // Internal implementation methods
345
//////////////////////////////////
346

347
348     //==============================================================================
349
// Inner class definitions
350
//==============================================================================
351

352     /**
353      * Document manager imports test case. Each test case starts with a root model (always a.owl in some
354      * directory), and loads the model. Depending on the model contents, and the settings of the doc
355      * manager, other models will be loaded. Each model is set to contain a fixed number of marker
356      * statements of the form:
357      * <code><pre>
358      * <Marker rdf:ID="a0" />
359      * </pre></code>
360      * the test for having correctly loaded the models is to count the markers and compare to the predicted
361      * total.
362      */

363     static class DocManagerImportTest
364         extends TestCase
365     {
366         String JavaDoc m_dir;
367         int m_count;
368         String JavaDoc m_path;
369         boolean m_processImports;
370
371         /* constuctor */
372         DocManagerImportTest( String JavaDoc dir, int count, boolean processImports, String JavaDoc path ) {
373             super( dir );
374             m_dir = dir;
375             m_count = count;
376             m_path = path;
377             m_processImports = processImports;
378         }
379
380         // external contract methods
381

382         public void runTest() {
383             OntDocumentManager dm = new OntDocumentManager();
384
385             // adjust the doc manager properties according to the test setup
386
dm.setProcessImports( m_processImports );
387             if (m_path != null) {
388                 dm.setMetadataSearchPath( m_path, true );
389             }
390
391             // now load the model - we always start from a.owl in the given directory
392
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 /*
405     (c) Copyright 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
406     All rights reserved.
407
408     Redistribution and use in source and binary forms, with or without
409     modification, are permitted provided that the following conditions
410     are met:
411
412     1. Redistributions of source code must retain the above copyright
413        notice, this list of conditions and the following disclaimer.
414
415     2. Redistributions in binary form must reproduce the above copyright
416        notice, this list of conditions and the following disclaimer in the
417        documentation and/or other materials provided with the distribution.
418
419     3. The name of the author may not be used to endorse or promote products
420        derived from this software without specific prior written permission.
421
422     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
423     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
424     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
425     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
426     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
427     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
428     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
429     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
430     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
431     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
432 */

433
434
Popular Tags