001: /*
002: (c) Copyright 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: All rights reserved - see end of file.
004: $Id: AssemblerTestBase.java,v 1.11 2008/01/03 15:18:54 chris-dollin Exp $
005: */
006:
007: package com.hp.hpl.jena.assembler.test;
008:
009: import com.hp.hpl.jena.assembler.*;
010: import com.hp.hpl.jena.assembler.assemblers.AssemblerBase;
011: import com.hp.hpl.jena.assembler.exceptions.CannotConstructException;
012: import com.hp.hpl.jena.rdf.model.*;
013: import com.hp.hpl.jena.rdf.model.test.ModelTestBase;
014: import com.hp.hpl.jena.shared.*;
015: import com.hp.hpl.jena.vocabulary.*;
016:
017: /**
018: A convenient base class for Assembler tests. The instance methods
019: <code>modelAddFacts</code> and <code>setRequiredPrefixes</code>
020: may be over-ridden in subclasses to control the parser that is used to
021: construct models and the prefixes added to the model (these features
022: added for Eyeball).
023:
024: @author kers
025: */
026: public abstract class AssemblerTestBase extends ModelTestBase {
027: protected Class getAssemblerClass() {
028: throw new BrokenException(
029: "this class must define getAssemblerClass");
030: }
031:
032: /**
033: An assembler that always returns the same fixed object.
034: @author kers
035: */
036: protected static final class FixedObjectAssembler extends
037: AssemblerBase {
038: private final Object x;
039:
040: protected FixedObjectAssembler(Object x) {
041: this .x = x;
042: }
043:
044: public Object open(Assembler a, Resource root, Mode irrelevant) {
045: return x;
046: }
047: }
048:
049: /**
050: An assembler that insists on being called on a given name, and always
051: returns the same fixed object.
052: @author kers
053: */
054: protected static class NamedObjectAssembler extends AssemblerBase {
055: final Resource name;
056: final Object result;
057:
058: public NamedObjectAssembler(Resource name, Object result) {
059: this .name = name;
060: this .result = result;
061: }
062:
063: public Model openModel(Resource root, Mode mode) {
064: return (Model) open(this , root, mode);
065: }
066:
067: public Object open(Assembler a, Resource root, Mode irrelevant) {
068: assertEquals(name, root);
069: return result;
070: }
071: }
072:
073: protected static final Model schema = JA.getSchema();
074:
075: public AssemblerTestBase(String name) {
076: super (name);
077: }
078:
079: protected Model model(String string) {
080: Model result = createModel(ReificationStyle.Standard);
081: setRequiredPrefixes(result);
082: return modelAddFacts(result, string);
083: }
084:
085: protected Model model() {
086: return model("");
087: }
088:
089: /**
090: Subclasses may override to use their choice of string-to-model
091: parsers.
092: */
093: protected Model modelAddFacts(Model result, String string) {
094: return modelAdd(result, string);
095: }
096:
097: /**
098: Subclasses may extend to get their choice of defined prefixes.
099: */
100: protected Model setRequiredPrefixes(Model m) {
101: m.setNsPrefix("ja", JA.getURI());
102: m.setNsPrefix("lm", LocationMappingVocab.getURI());
103: return m;
104: }
105:
106: protected Resource resourceInModel(String string) {
107: Model m = model(string);
108: Resource r = resource(string.substring(0, string.indexOf(' ')));
109: return (Resource) r.inModel(m);
110: }
111:
112: protected void testDemandsMinimalType(Assembler a, Resource type) {
113: try {
114: a.open(resourceInModel("x rdf:type rdf:Resource"));
115: fail("should trap insufficient type");
116: } catch (CannotConstructException e) {
117: assertEquals(getAssemblerClass(), e.getAssemblerClass());
118: assertEquals(type, e.getType());
119: assertEquals(resource("x"), e.getRoot());
120: }
121: }
122:
123: protected void assertSamePrefixMapping(PrefixMapping wanted,
124: PrefixMapping got) {
125: if (!wanted.samePrefixMappingAs(got))
126: fail("wanted: " + wanted + " but got: " + got);
127: }
128:
129: /**
130: assert that the property <code>p</code> has <code>domain</code> as
131: its rdfs:domain.
132: */
133: protected void assertDomain(Resource domain, Property p) {
134: if (!schema.contains(p, RDFS.domain, domain))
135: fail(p + " was expected to have domain " + domain);
136: }
137:
138: /**
139: assert that the property <code>p</code> has <code>range</code> as
140: its rdfs:range.
141: */
142: protected void assertRange(Resource range, Property p) {
143: if (!schema.contains(p, RDFS.range, range))
144: fail(p + " was expected to have range " + range);
145: }
146:
147: /**
148: assert that <code>expectedSub</code> is an rdfs:subClassOf
149: <code>expectedSuper</code>.
150: */
151: protected void assertSubclassOf(Resource expectedSub,
152: Resource expectedSuper) {
153: if (!schema.contains(expectedSub, RDFS.subClassOf,
154: expectedSuper))
155: fail(expectedSub + " should be a subclass of "
156: + expectedSuper);
157: }
158:
159: /**
160: assert that <code>instance</code> has rdf:type <code>type</code>.
161: */
162: protected void assertType(Resource type, Resource instance) {
163: if (!schema.contains(instance, RDF.type, type))
164: fail(instance + " should have rdf:type " + type);
165: }
166: }
167:
168: /*
169: * (c) Copyright 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
170: * All rights reserved.
171: *
172: * Redistribution and use in source and binary forms, with or without
173: * modification, are permitted provided that the following conditions
174: * are met:
175: * 1. Redistributions of source code must retain the above copyright
176: * notice, this list of conditions and the following disclaimer.
177: * 2. Redistributions in binary form must reproduce the above copyright
178: * notice, this list of conditions and the following disclaimer in the
179: * documentation and/or other materials provided with the distribution.
180: * 3. The name of the author may not be used to endorse or promote products
181: * derived from this software without specific prior written permission.
182: *
183: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
184: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
185: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
186: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
187: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
188: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
189: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
190: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
191: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
192: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
193: */
|