Added: incubator/stanbol/trunk/ontologymanager/ontonet/src/test/java/org/apache/stanbol/ontologymanager/ontonet/Locations.java URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/ontologymanager/ontonet/src/test/java/org/apache/stanbol/ontologymanager/ontonet/Locations.java?rev=1147236&view=auto ============================================================================== --- incubator/stanbol/trunk/ontologymanager/ontonet/src/test/java/org/apache/stanbol/ontologymanager/ontonet/Locations.java (added) +++ incubator/stanbol/trunk/ontologymanager/ontonet/src/test/java/org/apache/stanbol/ontologymanager/ontonet/Locations.java Fri Jul 15 16:56:12 2011 @@ -0,0 +1,62 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.stanbol.ontologymanager.ontonet; + +import org.semanticweb.owlapi.model.IRI; + +/** + * Physical and logical IRIs for unit tests. + */ +public class Locations { + + /** + * Default physical location of the ontology registry for testing. + */ + public static final IRI _CP_NAMESPACE = IRI.create("http://www.ontologydesignpatterns.org/cp/owl/"); + + /** + * Default physical location of the ontology registry for testing. + */ + public static final IRI _REGISTRY_TEST = IRI + .create("http://www.ontologydesignpatterns.org/registry/krestest.owl"); + + /** + * Identifier of test ontology library 1. + */ + public static final IRI LIBRARY_TEST1 = IRI.create(_REGISTRY_TEST + "#TestRegistry"); + + /** + * Identifier of test ontology library 2. + */ + public static final IRI LIBRARY_TEST2 = IRI.create(_REGISTRY_TEST + "#TestLibrary2"); + + /** + * An ontology in test libraries 1 and 2. + */ + public static final IRI ODP_OBJECTROLE = IRI.create(_CP_NAMESPACE + "objectrole.owl"); + + /** + * An ontology in test library 2 but not in test library 1. + */ + public static final IRI ODP_SITUATION = IRI.create(_CP_NAMESPACE + "situation.owl"); + + /** + * An ontology in test library 1 but not in test library 2. + */ + public static final IRI ODP_TYPESOFENTITIES = IRI.create(_CP_NAMESPACE + "typesofentities.owl"); + +}
Added: incubator/stanbol/trunk/ontologymanager/ontonet/src/test/java/org/apache/stanbol/ontologymanager/ontonet/registry/TestOntologyLibrary.java URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/ontologymanager/ontonet/src/test/java/org/apache/stanbol/ontologymanager/ontonet/registry/TestOntologyLibrary.java?rev=1147236&view=auto ============================================================================== --- incubator/stanbol/trunk/ontologymanager/ontonet/src/test/java/org/apache/stanbol/ontologymanager/ontonet/registry/TestOntologyLibrary.java (added) +++ incubator/stanbol/trunk/ontologymanager/ontonet/src/test/java/org/apache/stanbol/ontologymanager/ontonet/registry/TestOntologyLibrary.java Fri Jul 15 16:56:12 2011 @@ -0,0 +1,130 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.stanbol.ontologymanager.ontonet.registry; + +import static org.junit.Assert.*; + +import java.io.File; +import java.util.Hashtable; + +import org.apache.stanbol.ontologymanager.ontonet.Locations; +import org.apache.stanbol.ontologymanager.ontonet.api.ONManager; +import org.apache.stanbol.ontologymanager.ontonet.api.ONManagerConfiguration; +import org.apache.stanbol.ontologymanager.ontonet.api.io.OntologyInputSource; +import org.apache.stanbol.ontologymanager.ontonet.api.registry.RegistryLoader; +import org.apache.stanbol.ontologymanager.ontonet.api.registry.io.LibrarySource; +import org.apache.stanbol.ontologymanager.ontonet.api.registry.models.Registry; +import org.apache.stanbol.ontologymanager.ontonet.api.registry.models.RegistryItem; +import org.apache.stanbol.ontologymanager.ontonet.api.registry.models.RegistryLibrary; +import org.apache.stanbol.ontologymanager.ontonet.impl.ONManagerConfigurationImpl; +import org.apache.stanbol.ontologymanager.ontonet.impl.ONManagerImpl; +import org.apache.stanbol.ontologymanager.ontonet.impl.registry.model.impl.RegistryLoaderImpl; +import org.junit.Before; +import org.junit.Test; +import org.semanticweb.owlapi.model.IRI; +import org.semanticweb.owlapi.model.OWLImportsDeclaration; +import org.semanticweb.owlapi.model.OWLOntology; +import org.semanticweb.owlapi.util.AutoIRIMapper; + +public class TestOntologyLibrary { + + private String registryResource = "/ontologies/registry/krestest.owl"; + + private ONManager onm; + + private RegistryLoader loader; + + /** + * Reset the ontology network manager and registry loader. + */ + @Before + public void setupTest() throws Exception { + // An ONManagerImpl with no store and default settings + ONManagerConfiguration configuration = new ONManagerConfigurationImpl(new Hashtable<String,Object>()); + onm = new ONManagerImpl(null, null, configuration, new Hashtable<String,Object>()); + onm.getOwlCacheManager().addIRIMapper( + new AutoIRIMapper(new File(getClass().getResource("/ontologies/odp").toURI()), true)); + loader = new RegistryLoaderImpl(onm); + } + + /** + * Utility method to recurse into registry items. + * + * TODO: move this to main? + * + * @param item + * @param ontologyId + * @return + */ + private boolean containsOntologyRecursive(RegistryItem item, IRI ontologyId) { + + boolean result = false; + if (item.isOntology()) { + // An Ontology MUST have a non-null URI. + try { + IRI iri = IRI.create(item.getURL()); + result |= iri.equals(ontologyId); + } catch (Exception e) { + return false; + } + } else if (item.isLibrary() || item instanceof Registry) + // Inspect children + for (RegistryItem child : ((RegistryLibrary) item).getChildren()) { + result |= containsOntologyRecursive(child, ontologyId); + if (result) break; + } + return result; + + } + + /** + * Uses a plain {@link RegistryLoader} to load a single ontology library and checks for its expected hits + * and misses. + * + * @throws Exception + */ + @Test + public void testLibraryLoad() throws Exception { + IRI localTestRegistry = IRI.create(getClass().getResource(registryResource)); + RegistryLibrary lib = loader.loadLibraryEager(localTestRegistry, Locations.LIBRARY_TEST2); + assertTrue(lib.hasChildren()); + // Should be in the library. + boolean hasSituation = containsOntologyRecursive(lib, Locations.ODP_SITUATION); + // Should NOT be in the library (belongs to another library in the same registry). + boolean hasTypes = containsOntologyRecursive(lib, Locations.ODP_TYPESOFENTITIES); + assertTrue(hasSituation && !hasTypes); + } + + @Test + public void testLibrarySourceCreation() throws Exception { + IRI localTestRegistry = IRI.create(getClass().getResource(registryResource)); + OntologyInputSource src = new LibrarySource(Locations.LIBRARY_TEST1, localTestRegistry, + onm.getOwlCacheManager(), loader); + OWLOntology o = src.getRootOntology(); + boolean hasTypes = false, hasObjectRole = false; + for (OWLImportsDeclaration ax : o.getImportsDeclarations()) { + // Since we added a local IRI mapping, import statements might be using file: IRIs instead of + // HTTP, in which case IRI equality would fail. + String tmpstr = ax.getIRI().toString(); + if (!hasTypes && tmpstr.endsWith("typesofentities.owl")) hasTypes = true; + else if (!hasObjectRole && tmpstr.endsWith("objectrole.owl")) hasObjectRole = true; + if (hasTypes && hasObjectRole) break; + } + assertTrue(hasTypes && hasObjectRole); + } + +} Modified: incubator/stanbol/trunk/ontologymanager/ontonet/src/test/java/org/apache/stanbol/ontologymanager/ontonet/registry/TestRegistry.java URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/ontologymanager/ontonet/src/test/java/org/apache/stanbol/ontologymanager/ontonet/registry/TestRegistry.java?rev=1147236&r1=1147235&r2=1147236&view=diff ============================================================================== --- incubator/stanbol/trunk/ontologymanager/ontonet/src/test/java/org/apache/stanbol/ontologymanager/ontonet/registry/TestRegistry.java (original) +++ incubator/stanbol/trunk/ontologymanager/ontonet/src/test/java/org/apache/stanbol/ontologymanager/ontonet/registry/TestRegistry.java Fri Jul 15 16:56:12 2011 @@ -1,35 +1,28 @@ /* -* Licensed to the Apache Software Foundation (ASF) under one or more -* contributor license agreements. See the NOTICE file distributed with -* this work for additional information regarding copyright ownership. -* The ASF licenses this file to You under the Apache License, Version 2.0 -* (the "License"); you may not use this file except in compliance with -* the License. You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package org.apache.stanbol.ontologymanager.ontonet.registry; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; - -import java.io.File; -import java.net.URL; -import java.util.Dictionary; +import static org.junit.Assert.*; + import java.util.Hashtable; import org.apache.stanbol.ontologymanager.ontonet.api.DuplicateIDException; import org.apache.stanbol.ontologymanager.ontonet.api.ONManager; import org.apache.stanbol.ontologymanager.ontonet.api.ONManagerConfiguration; -import org.apache.stanbol.ontologymanager.ontonet.api.io.OntologyInputSource; -import org.apache.stanbol.ontologymanager.ontonet.api.io.ParentPathInputSource; import org.apache.stanbol.ontologymanager.ontonet.api.ontology.CoreOntologySpace; import org.apache.stanbol.ontologymanager.ontonet.api.ontology.OntologyScope; import org.apache.stanbol.ontologymanager.ontonet.api.ontology.SessionOntologySpace; @@ -41,99 +34,88 @@ import org.apache.stanbol.ontologymanage import org.junit.BeforeClass; import org.junit.Test; import org.semanticweb.owlapi.model.IRI; -import org.semanticweb.owlapi.model.OWLOntologyIRIMapper; import org.semanticweb.owlapi.model.OWLOntologyManager; -import org.semanticweb.owlapi.util.AutoIRIMapper; public class TestRegistry { - private static OWLOntologyManager ontologyManager; - private static RegistryLoader loader; - private static OntologyRegistryIRISource ontologySource; - private static ONManagerConfiguration configuration; - private static ONManager onm; - - private static IRI testRegistryIri = IRI - .create("http://www.ontologydesignpatterns.org/registry/krestest.owl"); - - @SuppressWarnings("unused") - private static IRI submissionsIri = IRI - .create("http://www.ontologydesignpatterns.org/registry/submissions.owl"); - - @BeforeClass - public static void setup() { - // An ONManagerImpl with no store and default settings - configuration = new ONManagerConfigurationImpl( - new Hashtable<String, Object>()); - onm = new ONManagerImpl(null, null, configuration, - new Hashtable<String, Object>()); - ontologyManager = onm.getOwlCacheManager(); - loader = onm.getRegistryLoader(); - - - } - - private static boolean mapperIsSet = false; - - public void setupOfflineMapper() { - if (mapperIsSet) { - } else { - ontologySource = new OntologyRegistryIRISource(testRegistryIri, - ontologyManager, loader); - mapperIsSet = true; - } - } - - @Test - public void testAddRegistryToSessionSpace() { - setupOfflineMapper(); - IRI scopeIri = IRI.create("http://fise.iks-project.eu/scopone"); - SessionOntologySpace space = null; - space = onm.getOntologySpaceFactory().createSessionOntologySpace( - scopeIri); - space.setUp(); - try { - // space.addOntology(new - // OntologyRegistryIRISource(testRegistryIri,onm.getOwlCacheManager(),onm.getRegistryLoader())); - space.addOntology(ontologySource); - } catch (UnmodifiableOntologySpaceException e) { - fail("Adding libraries to session space failed. " - + "This should not happen for active session spaces."); - } - - assertTrue(space.getTopOntology() != null); - assertTrue(space.getOntologies().contains(space.getTopOntology())); - } - - @Test - public void testScopeCreationWithRegistry() { - setupOfflineMapper(); - IRI scopeIri = IRI.create("http://fise.iks-project.eu/scopone"); - OntologyScope scope = null; - // The factory call also invokes loadRegistriesEager() and - // gatherOntologies() so no need to test them individually. - try { - scope = onm.getOntologyScopeFactory().createOntologyScope(scopeIri, - ontologySource); - } catch (DuplicateIDException e) { - fail("DuplicateID exception caught when creating test scope."); - } - - assertTrue(scope != null - && scope.getCoreSpace().getTopOntology() != null); - // OntologyUtils.printOntology(scope.getCoreSpace().getTopOntology(), - // System.err); - } - - @Test - public void testSpaceCreationWithRegistry() { - setupOfflineMapper(); - IRI scopeIri = IRI.create("http://fise.iks-project.eu/scopone"); - CoreOntologySpace space = null; - // The factory call also invokes loadRegistriesEager() and - // gatherOntologies() so no need to test them individually. - space = onm.getOntologySpaceFactory().createCoreOntologySpace(scopeIri, - ontologySource); - assertTrue(space != null && space.getTopOntology() != null); - } + private static OWLOntologyManager ontologyManager; + private static RegistryLoader loader; + private static OntologyRegistryIRISource ontologySource; + private static ONManagerConfiguration configuration; + private static ONManager onm; + + private static IRI testRegistryIri = IRI + .create("http://www.ontologydesignpatterns.org/registry/krestest.owl"); + + @SuppressWarnings("unused") + private static IRI submissionsIri = IRI + .create("http://www.ontologydesignpatterns.org/registry/submissions.owl"); + + @BeforeClass + public static void setup() { + // An ONManagerImpl with no store and default settings + configuration = new ONManagerConfigurationImpl(new Hashtable<String,Object>()); + onm = new ONManagerImpl(null, null, configuration, new Hashtable<String,Object>()); + ontologyManager = onm.getOwlCacheManager(); + loader = onm.getRegistryLoader(); + + } + + private static boolean mapperIsSet = false; + + public void setupOfflineMapper() { + if (mapperIsSet) {} else { + ontologySource = new OntologyRegistryIRISource(testRegistryIri, ontologyManager, loader); + mapperIsSet = true; + } + } + + @Test + public void testAddRegistryToSessionSpace() { + setupOfflineMapper(); + IRI scopeIri = IRI.create("http://fise.iks-project.eu/scopone"); + SessionOntologySpace space = null; + space = onm.getOntologySpaceFactory().createSessionOntologySpace(scopeIri); + space.setUp(); + try { + // space.addOntology(new + // OntologyRegistryIRISource(testRegistryIri,onm.getOwlCacheManager(),onm.getRegistryLoader())); + space.addOntology(ontologySource); + } catch (UnmodifiableOntologySpaceException e) { + fail("Adding libraries to session space failed. " + + "This should not happen for active session spaces."); + } + + assertTrue(space.getTopOntology() != null); + assertTrue(space.getOntologies().contains(space.getTopOntology())); + } + + @Test + public void testScopeCreationWithRegistry() { + setupOfflineMapper(); + IRI scopeIri = IRI.create("http://fise.iks-project.eu/scopone"); + OntologyScope scope = null; + // The factory call also invokes loadRegistriesEager() and + // gatherOntologies() so no need to test them individually. + try { + scope = onm.getOntologyScopeFactory().createOntologyScope(scopeIri, ontologySource); + } catch (DuplicateIDException e) { + fail("DuplicateID exception caught when creating test scope."); + } + + assertTrue(scope != null && scope.getCoreSpace().getTopOntology() != null); + // OntologyUtils.printOntology(scope.getCoreSpace().getTopOntology(), + // System.err); + } + + @Test + public void testSpaceCreationWithRegistry() { + setupOfflineMapper(); + IRI scopeIri = IRI.create("http://fise.iks-project.eu/scopone"); + CoreOntologySpace space = null; + // The factory call also invokes loadRegistriesEager() and + // gatherOntologies() so no need to test them individually. + space = onm.getOntologySpaceFactory().createCoreOntologySpace(scopeIri, ontologySource); + assertTrue(space != null && space.getTopOntology() != null); + } } Modified: incubator/stanbol/trunk/ontologymanager/ontonet/src/test/resources/ontologies/registry/krestest.owl URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/ontologymanager/ontonet/src/test/resources/ontologies/registry/krestest.owl?rev=1147236&r1=1147235&r2=1147236&view=diff ============================================================================== --- incubator/stanbol/trunk/ontologymanager/ontonet/src/test/resources/ontologies/registry/krestest.owl (original) +++ incubator/stanbol/trunk/ontologymanager/ontonet/src/test/resources/ontologies/registry/krestest.owl Fri Jul 15 16:56:12 2011 @@ -14,7 +14,13 @@ </owl:Ontology> <rdf:Description rdf:ID="TestRegistry"> - <rdfs:label rdf:datatype="http://www.w3.org/2001/XMLSchema#string">KReS Test Registry</rdfs:label> + <rdfs:label rdf:datatype="http://www.w3.org/2001/XMLSchema#string">KReS Test Library</rdfs:label> + <rdf:type rdf:resource="http://www.ontologydesignpatterns.org/cpont/codo/coddata.owl#OntologyLibrary"/> + <owl:versionInfo rdf:datatype="http://www.w3.org/2001/XMLSchema#string">0.1</owl:versionInfo> + </rdf:Description> + + <rdf:Description rdf:ID="TestLibrary2"> + <rdfs:label rdf:datatype="http://www.w3.org/2001/XMLSchema#string">KReS Test Library 2</rdfs:label> <rdf:type rdf:resource="http://www.ontologydesignpatterns.org/cpont/codo/coddata.owl#OntologyLibrary"/> <owl:versionInfo rdf:datatype="http://www.w3.org/2001/XMLSchema#string">0.1</owl:versionInfo> </rdf:Description> @@ -24,32 +30,39 @@ <meta:isOntologyOf rdf:resource="#TestRegistry"/> <rdf:type rdf:resource="http://www.ontologydesignpatterns.org/cpont/codo/codkernel.owl#Ontology"/> </rdf:Description> + + <rdf:Description rdf:about="http://www.ontologydesignpatterns.org/cp/owl/situation.owl"> + <rdfs:label rdf:datatype="http://www.w3.org/2001/XMLSchema#string">Situation</rdfs:label> + <meta:isOntologyOf rdf:resource="#TestLibrary2"/> + <rdf:type rdf:resource="http://www.ontologydesignpatterns.org/cpont/codo/codkernel.owl#Ontology"/> + </rdf:Description> <!-- For testing ontologies that do not exist --> - <!--rdf:Description rdf:about="http://www.ontologydesignpatterns.org/cp/owl/doesnotexist.owl"> + <rdf:Description rdf:about="http://www.ontologydesignpatterns.org/cp/owl/doesnotexist.owl"> <rdfs:label rdf:datatype="http://www.w3.org/2001/XMLSchema#string">Does not exist</rdfs:label> - <meta:isOntologyOf rdf:resource="#TestRegistry"/> + <!-- <meta:isOntologyOf rdf:resource="#TestRegistry"/> --> <rdf:type rdf:resource="http://www.ontologydesignpatterns.org/cpont/codo/codkernel.owl#Ontology"/> - </rdf:Description--> + </rdf:Description> <!-- For testing already loaded ontologies --> <rdf:Description rdf:about="http://www.ontologydesignpatterns.org/cp/owl/objectrole.owl"> <rdfs:label rdf:datatype="http://www.w3.org/2001/XMLSchema#string">Object-Role</rdfs:label> <meta:isOntologyOf rdf:resource="#TestRegistry"/> + <meta:isOntologyOf rdf:resource="#TestLibrary2"/> <rdf:type rdf:resource="http://www.ontologydesignpatterns.org/cpont/codo/codkernel.owl#Ontology"/> </rdf:Description> <!-- For testing anonymous ontologies --> - <!--rdf:Description rdf:about="http://www.ontologydesignpatterns.org/ont/iks/nameless_ontology.owl"> + <rdf:Description rdf:about="http://www.ontologydesignpatterns.org/ont/iks/nameless_ontology.owl"> <rdfs:label rdf:datatype="http://www.w3.org/2001/XMLSchema#string">Nameless</rdfs:label> - <meta:isOntologyOf rdf:resource="#TestRegistry"/> + <!-- <meta:isOntologyOf rdf:resource="#TestRegistry"/> --> <rdf:type rdf:resource="http://www.ontologydesignpatterns.org/cpont/codo/codkernel.owl#Ontology"/> - </rdf:Description--> + </rdf:Description> <!-- For testing undefined registries --> <rdf:Description rdf:about="http://www.ontologydesignpatterns.org/cp/owl/classification.owl"> <rdfs:label rdf:datatype="http://www.w3.org/2001/XMLSchema#string">Classification</rdfs:label> - <meta:isOntologyOf rdf:resource="#NonExistentRegistry"/> + <meta:isOntologyOf rdf:resource="#NonExistentLibrary"/> <rdf:type rdf:resource="http://www.ontologydesignpatterns.org/cpont/codo/codkernel.owl#Ontology"/> </rdf:Description>
