001: /*
002: (c) Copyright 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: All rights reserved - see end of file.
004: $Id: TestAssemblerHelp.java,v 1.14 2008/01/02 12:05:57 andy_seaborne Exp $
005: */
006:
007: package com.hp.hpl.jena.assembler.test;
008:
009: import java.util.*;
010:
011: import com.hp.hpl.jena.assembler.*;
012: import com.hp.hpl.jena.assembler.assemblers.*;
013: import com.hp.hpl.jena.assembler.exceptions.*;
014: import com.hp.hpl.jena.rdf.model.*;
015: import com.hp.hpl.jena.shared.*;
016: import com.hp.hpl.jena.vocabulary.RDF;
017:
018: public class TestAssemblerHelp extends AssemblerTestBase {
019: public TestAssemblerHelp(String name) {
020: super (name);
021: }
022:
023: protected Class getAssemblerClass() {
024: throw new BrokenException(
025: "TestAssemblers does not need this method");
026: }
027:
028: public void testClosureFootprint() {
029: Resource root = resourceInModel("x ja:reasoner y");
030: Statement footprint = root.getModel().createStatement(JA.This,
031: RDF.type, JA.Expanded);
032: assertFalse(root.getModel().contains(footprint));
033: Resource expanded = AssemblerHelp.withFullModel(root);
034: assertTrue(expanded.getModel().contains(footprint));
035: }
036:
037: public void testFootprintPreventsClosure() {
038: Resource root = resourceInModel("x ja:reasoner y; ja:this rdf:type ja:Expanded");
039: Model original = model("").add(root.getModel());
040: Resource expanded = AssemblerHelp.withFullModel(root);
041: assertSame(root, expanded);
042: assertIsoModels(original, expanded.getModel());
043: }
044:
045: public void testSpecificType() {
046: testSpecificType("ja:Connectable", "x ja:connection _C");
047: testSpecificType("ja:NamedModel", "x ja:modelName 'name'");
048: testSpecificType("ja:NamedModel",
049: "x ja:modelName 'name'; x rdf:type irrelevant");
050: testSpecificType("ja:RDBModel",
051: "x rdf:type ja:RDBModel; x rdf:type ja:Model");
052: }
053:
054: public void testFindSpecificTypes() {
055: testFindSpecificTypes("", "x rdf:type A", "Top");
056: testFindSpecificTypes("", "x rdf:type A; x rdf:type B", "Top");
057: testFindSpecificTypes("A",
058: "x rdf:type A; A rdfs:subClassOf Top", "Top");
059: testFindSpecificTypes("A",
060: "x rdf:type A; x rdf:type B; A rdfs:subClassOf Top",
061: "Top");
062: testFindSpecificTypes(
063: "A B",
064: "x rdf:type A; x rdf:type B; A rdfs:subClassOf Top; B rdfs:subClassOf Top",
065: "Top");
066: testFindSpecificTypes(
067: "B",
068: "x rdf:type A; x rdf:type B; A rdfs:subClassOf Top; B rdfs:subClassOf Top; B rdfs:subClassOf A",
069: "Top");
070: }
071:
072: private void testFindSpecificTypes(String expectedString,
073: String model, String baseString) {
074: Resource root = resourceInModel(model);
075: Resource baseType = resource(baseString);
076: Set expected = resourceSet(expectedString);
077: Set answer = AssemblerHelp.findSpecificTypes(root, baseType);
078: assertEquals(expected, answer);
079: }
080:
081: public void testFindRootByExplicitType() {
082: Model model = model("x rdf:type ja:Object; y rdf:type Irrelevant");
083: Set roots = AssemblerHelp.findAssemblerRoots(model);
084: assertEquals(resourceSet("x"), roots);
085: }
086:
087: public void testFindRootByImplicitType() {
088: Model model = model("x ja:reificationMode ja:Standard");
089: Set roots = AssemblerHelp.findAssemblerRoots(model);
090: assertEquals(resourceSet("x"), roots);
091: }
092:
093: public void testFindMultipleRoots() {
094: Model model = model("x rdf:type ja:Object; y ja:reificationMode ja:Minimal");
095: Set roots = AssemblerHelp.findAssemblerRoots(model);
096: assertEquals(resourceSet("y x"), roots);
097: }
098:
099: public void testFindRootsWithSpecifiedType() {
100: Model model = model("x rdf:type ja:Model; y rdf:type ja:Object");
101: Set roots = AssemblerHelp.findAssemblerRoots(model, JA.Model);
102: assertEquals(resourceSet("x"), roots);
103: }
104:
105: public void testThrowsIfNoRoots() {
106: try {
107: AssemblerHelp.singleModelRoot(model(""));
108: fail("should trap if no roots");
109: } catch (BadDescriptionNoRootException e) {
110: pass();
111: }
112: }
113:
114: public void testThrowsIfManyRoots() {
115: try {
116: AssemblerHelp
117: .singleModelRoot(model("a rdf:type ja:Model; b rdf:type ja:Model"));
118: fail("should trap if many roots");
119: } catch (BadDescriptionMultipleRootsException e) {
120: pass();
121: }
122: }
123:
124: public void testExtractsSingleRoot() {
125: Resource it = AssemblerHelp
126: .singleModelRoot(model("a rdf:type ja:Model"));
127: assertEquals(resource("a"), it);
128: }
129:
130: public void testSpecificTypeFails() {
131: try {
132: testSpecificType("xxx",
133: "x rdf:type ja:Model; x rdf:type ja:PrefixMapping");
134: fail("should trap multiple types");
135: } catch (AmbiguousSpecificTypeException e) {
136: assertEquals(resource("x"), e.getRoot());
137: assertEquals(resources(e.getRoot(),
138: "ja:Model ja:PrefixMapping"), new HashSet(e
139: .getTypes()));
140: }
141: }
142:
143: private Set resources(Resource root, String items) {
144: List L = listOfStrings(items);
145: Set result = new HashSet();
146: for (int i = 0; i < L.size(); i += 1)
147: result.add(resource(root.getModel(), (String) L.get(i)));
148: return result;
149: }
150:
151: private void testSpecificType(String expected, String specification) { // TODO relies on fullModel, would be nice to remove this dependency
152: Resource root = resourceInModel(specification);
153: Resource rooted = (Resource) root.inModel(AssemblerHelp
154: .fullModel(root.getModel()));
155: Resource mst = AssemblerHelp.findSpecificType(rooted);
156: assertEquals(resource(root.getModel(), expected), mst);
157: }
158:
159: public static boolean impIsLoaded = false;
160: public static boolean impIsConstructed = false;
161:
162: public static class Imp extends AssemblerBase {
163: static {
164: impIsLoaded = true;
165: }
166:
167: public Imp() {
168: impIsConstructed = true;
169: }
170:
171: public Object open(Assembler a, Resource root, Mode irrelevant) {
172: return null;
173: }
174: }
175:
176: static Model gremlinModel = modelWithStatements("eh:Wossname ja:assembler 'com.hp.hpl.jena.assembler.test.TestAssemblerHelp$Gremlin'");
177:
178: static boolean gremlinInvoked = false;
179:
180: public static class Gremlin extends AssemblerBase {
181: public Gremlin() {
182: fail("Gremlin no-argument constructor should not be called");
183: }
184:
185: public Gremlin(Resource root) {
186: assertEquals(resource("eh:Wossname"), root);
187: assertIsoModels(gremlinModel, root.getModel());
188: gremlinInvoked = true;
189: }
190:
191: public Object open(Assembler a, Resource root, Mode irrelevant) {
192: return null;
193: }
194: }
195:
196: public void testClassAssociation() {
197: String className = "com.hp.hpl.jena.assembler.test.TestAssemblerHelp$Imp";
198: AssemblerGroup group = AssemblerGroup.create();
199: Model m = model("eh:Wossname ja:assembler '" + className + "'");
200: assertEquals(false, impIsLoaded);
201: AssemblerHelp.loadAssemblerClasses(group, m);
202: assertEquals(true, impIsLoaded);
203: assertEquals(true, impIsConstructed);
204: assertEquals(className, group.assemblerFor(
205: resource("eh:Wossname")).getClass().getName());
206: }
207:
208: public void testClassResourceConstructor() {
209: AssemblerGroup group = AssemblerGroup.create();
210: Model m = model("eh:Wossname ja:assembler 'com.hp.hpl.jena.assembler.test.TestAssemblerHelp$Gremlin'");
211: assertEquals(false, gremlinInvoked);
212: AssemblerHelp.loadAssemblerClasses(group, m);
213: assertEquals(true, gremlinInvoked);
214: }
215: }
216:
217: /*
218: * (c) Copyright 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
219: * All rights reserved.
220: *
221: * Redistribution and use in source and binary forms, with or without
222: * modification, are permitted provided that the following conditions
223: * are met:
224: * 1. Redistributions of source code must retain the above copyright
225: * notice, this list of conditions and the following disclaimer.
226: * 2. Redistributions in binary form must reproduce the above copyright
227: * notice, this list of conditions and the following disclaimer in the
228: * documentation and/or other materials provided with the distribution.
229: * 3. The name of the author may not be used to endorse or promote products
230: * derived from this software without specific prior written permission.
231: *
232: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
233: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
234: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
235: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
236: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
237: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
238: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
239: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
240: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
241: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
242: */
|