001: /*
002: (c) Copyright 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: All rights reserved - see end of file.
004: $Id: TestOntModelSpecAssembler.java,v 1.10 2008/01/31 12:30:51 chris-dollin Exp $
005: */
006:
007: package com.hp.hpl.jena.assembler.test;
008:
009: import java.lang.reflect.Field;
010:
011: import com.hp.hpl.jena.assembler.*;
012: import com.hp.hpl.jena.assembler.assemblers.*;
013: import com.hp.hpl.jena.assembler.exceptions.ReasonerClashException;
014: import com.hp.hpl.jena.ontology.*;
015: import com.hp.hpl.jena.rdf.model.*;
016: import com.hp.hpl.jena.reasoner.*;
017: import com.hp.hpl.jena.reasoner.rulesys.*;
018: import com.hp.hpl.jena.shared.CannotCreateException;
019:
020: import junit.framework.*;
021:
022: public class TestOntModelSpecAssembler extends AssemblerTestBase {
023: public TestOntModelSpecAssembler(String name) {
024: super (name);
025: }
026:
027: protected Class getAssemblerClass() {
028: return OntModelSpecAssembler.class;
029: }
030:
031: public void testOntModelSpecAssemblerType() {
032: testDemandsMinimalType(new OntModelSpecAssembler(),
033: JA.OntModelSpec);
034: }
035:
036: public static TestSuite suite() {
037: TestSuite result = new TestSuite();
038: result.addTestSuite(TestOntModelSpecAssembler.class);
039: addParameterisedTests(result);
040: return result;
041: }
042:
043: protected static void addParameterisedTests(TestSuite result) {
044: Field[] fields = OntModelSpec.class.getFields();
045: for (int i = 0; i < fields.length; i += 1) {
046: Field f = fields[i];
047: String name = f.getName();
048: if (f.getType() == OntModelSpec.class)
049: try {
050: result.addTest(createTest((OntModelSpec) f
051: .get(null), name));
052: } catch (Exception e) {
053: System.err
054: .println("WARNING: failed to create test for OntModelSpec "
055: + name);
056: }
057: }
058: }
059:
060: protected void testBuiltinSpec(OntModelSpec ontModelSpec,
061: String specName) {
062: testBuiltinSpecAsRootName(ontModelSpec, specName);
063: testBuiltinSpecAsLikeTarget(ontModelSpec, specName);
064: }
065:
066: private void testBuiltinSpecAsLikeTarget(OntModelSpec ontModelSpec,
067: String specName) {
068: Resource rr = resourceInModel("_x rdf:type ja:OntModelSpec; _x ja:likeBuiltinSpec <OM>"
069: .replaceAll("<OM>", JA.getURI() + specName));
070: assertEquals(ontModelSpec, new OntModelSpecAssembler().open(rr));
071: }
072:
073: private void testBuiltinSpecAsRootName(OntModelSpec ontModelSpec,
074: String specName) {
075: Resource root = resourceInModel(JA.getURI() + specName
076: + " rdf:type ja:OntModelSpec");
077: assertEquals(ontModelSpec, new OntModelSpecAssembler()
078: .open(root));
079: }
080:
081: protected static Test createTest(final OntModelSpec spec,
082: final String name) {
083: return new TestOntModelSpecAssembler(name) {
084: public void runBare() {
085: testBuiltinSpec(spec, name);
086: }
087: };
088: }
089:
090: public void testOntModelSpecVocabulary() {
091: assertDomain(JA.OntModelSpec, JA.ontLanguage);
092: assertDomain(JA.OntModelSpec, JA.documentManager);
093: assertDomain(JA.OntModelSpec, JA.likeBuiltinSpec);
094: }
095:
096: public void testCreateFreshDocumentManager() {
097: Assembler a = new OntModelSpecAssembler();
098: Resource root = resourceInModel("x rdf:type ja:OntModelSpec; x ja:documentManager y");
099: OntDocumentManager dm = new OntDocumentManager();
100: NamedObjectAssembler mock = new NamedObjectAssembler(
101: resource("y"), dm);
102: OntModelSpec om = (OntModelSpec) a.open(mock, root);
103: assertSame(dm, om.getDocumentManager());
104: }
105:
106: public void testUseSpecifiedReasoner() {
107: Assembler a = new OntModelSpecAssembler();
108: Resource root = resourceInModel("x rdf:type ja:OntModelSpec; x ja:reasonerFactory R");
109: ReasonerFactory rf = new FakeReasonerFactory();
110: NamedObjectAssembler mock = new NamedObjectAssembler(
111: resource("R"), rf);
112: OntModelSpec om = (OntModelSpec) a.open(mock, root);
113: assertSame(rf, om.getReasonerFactory());
114: }
115:
116: public void testUseSpecifiedImpliedReasoner() {
117: testUsedSpecifiedImpliedReasoner(OWLFBRuleReasonerFactory.URI);
118: testUsedSpecifiedImpliedReasoner(RDFSRuleReasonerFactory.URI);
119: }
120:
121: private void testUsedSpecifiedImpliedReasoner(String R) {
122: ReasonerFactory rf = ReasonerFactoryAssembler
123: .getReasonerFactoryByURL(resource("x"), resource(R));
124: Assembler a = new OntModelSpecAssembler();
125: Resource root = resourceInModel("x rdf:type ja:OntModelSpec; x ja:reasonerURL "
126: + R);
127: Assembler mock = new FixedObjectAssembler(
128: "(should not be this string)");
129: OntModelSpec om = (OntModelSpec) a.open(mock, root);
130: assertSame(rf, om.getReasonerFactory());
131: }
132:
133: public void testDetectsClashingImpliedAndExplicitReasoners() {
134: Assembler a = new OntModelSpecAssembler();
135: Resource root = resourceInModel("x rdf:type ja:OntModelSpec; x ja:reasonerURL R; x ja:reasonerFactory F");
136: Assembler mock = new FixedObjectAssembler(
137: "(should not be this string)");
138: try {
139: a.open(mock, root);
140: fail("should detect reasoner clash");
141: } catch (ReasonerClashException e) {
142: pass();
143: }
144: }
145:
146: public void testUseSpecifiedLanguage() {
147: testSpecifiedLanguage(ProfileRegistry.DAML_LANG);
148: testSpecifiedLanguage(ProfileRegistry.OWL_DL_LANG);
149: testSpecifiedLanguage(ProfileRegistry.OWL_LANG);
150: testSpecifiedLanguage(ProfileRegistry.OWL_LITE_LANG);
151: testSpecifiedLanguage(ProfileRegistry.RDFS_LANG);
152: }
153:
154: private void testSpecifiedLanguage(String lang) {
155: Assembler a = new OntModelSpecAssembler();
156: Resource root = resourceInModel("x rdf:type ja:OntModelSpec; x ja:ontLanguage "
157: + lang);
158: OntModelSpec om = (OntModelSpec) a.open(root);
159: assertEquals(lang, om.getLanguage());
160: }
161:
162: public void testSpecifiedModelGetter() {
163: Assembler a = new OntModelSpecAssembler();
164: ModelGetter getter = new ModelGetter() {
165: public Model getModel(String URL) {
166: return null;
167: }
168:
169: public Model getModel(String URL, ModelReader loadIfAbsent) {
170: throw new CannotCreateException(URL);
171: }
172: };
173: NamedObjectAssembler mock = new NamedObjectAssembler(
174: resource("source"), getter);
175: Resource root = resourceInModel("x rdf:type ja:OntModelSpec; x ja:importSource source");
176: OntModelSpec om = (OntModelSpec) a.open(mock, root);
177: assertSame(getter, om.getImportModelGetter());
178: }
179:
180: private static final class FakeReasonerFactory implements
181: ReasonerFactory {
182: public Reasoner create(Resource configuration) {
183: return null;
184: }
185:
186: public Model getCapabilities() {
187: return null;
188: }
189:
190: public String getURI() {
191: return null;
192: }
193: }
194: }
195:
196: /*
197: * (c) Copyright 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
198: * All rights reserved.
199: *
200: * Redistribution and use in source and binary forms, with or without
201: * modification, are permitted provided that the following conditions
202: * are met:
203: * 1. Redistributions of source code must retain the above copyright
204: * notice, this list of conditions and the following disclaimer.
205: * 2. Redistributions in binary form must reproduce the above copyright
206: * notice, this list of conditions and the following disclaimer in the
207: * documentation and/or other materials provided with the distribution.
208: * 3. The name of the author may not be used to endorse or promote products
209: * derived from this software without specific prior written permission.
210: *
211: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
212: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
213: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
214: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
215: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
216: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
217: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
218: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
219: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
220: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
221: */
|