001: /*
002: (c) Copyright 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: All rights reserved - see end of file.
004: $Id: TestAssemblerGroup.java,v 1.12 2008/01/02 12:05:56 andy_seaborne 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.*;
011: import com.hp.hpl.jena.assembler.assemblers.AssemblerGroup.ExpandingAssemblerGroup;
012: import com.hp.hpl.jena.assembler.exceptions.*;
013: import com.hp.hpl.jena.graph.Graph;
014: import com.hp.hpl.jena.mem.GraphMemBase;
015: import com.hp.hpl.jena.rdf.model.*;
016: import com.hp.hpl.jena.vocabulary.RDFS;
017:
018: public class TestAssemblerGroup extends AssemblerTestBase {
019: public TestAssemblerGroup(String name) {
020: super (name);
021: }
022:
023: protected Class getAssemblerClass() {
024: return AssemblerGroup.class;
025: }
026:
027: public void testEmptyAssemblerGroup() {
028: AssemblerGroup a = AssemblerGroup.create();
029: assertInstanceOf(AssemblerGroup.class, a);
030: assertEquals(null, a.assemblerFor(resource("ja:Anything")));
031: checkFailsType(a, "rdf:Resource");
032: }
033:
034: protected void checkFailsType(Assembler a, String type) {
035: try {
036: a.open(resourceInModel("x rdf:type " + type));
037: fail("should trap missing implementation");
038: } catch (NoSpecificTypeException e) {
039: assertEquals(resource("x"), e.getRoot());
040: }
041: // catch (NoImplementationException e)
042: // {
043: // assertEquals( resource( "x" ), e.getRoot() );
044: // assertEquals( JA.Object, e.getType() );
045: // assertNotNull( e.getAssembler() );
046: // }
047: }
048:
049: public static boolean loaded = false;
050:
051: public static class Trivial {
052: static {
053: loaded = true;
054: }
055: }
056:
057: public void testLoadsClasses() {
058: AssemblerGroup a = AssemblerGroup.create();
059: a.implementWith(resource("T"), new MockAssembler());
060: Resource root = resourceInModel("x rdf:type T; _c ja:loadClass '"
061: + TestAssemblerGroup.class.getName() + "$Trivial'");
062: assertFalse(
063: "something has pre-loaded Trivial, so we can't test if it gets loaded",
064: loaded);
065: assertEquals("mockmockmock", a.open(root));
066: assertTrue(
067: "the assembler group did not obey the ja:loadClass directive",
068: loaded);
069: }
070:
071: static class MockAssembler extends AssemblerBase {
072: public Object open(Assembler a, Resource root, Mode mode) {
073: return "mockmockmock";
074: }
075: }
076:
077: public void testSingletonAssemblerGroup() {
078: AssemblerGroup a = AssemblerGroup.create();
079: assertSame(a, a.implementWith(JA.InfModel, Assembler.infModel));
080: a.openModel(resourceInModel("x rdf:type ja:InfModel"));
081: checkFailsType(a, "js:DefaultModel");
082: }
083:
084: public void testMultipleAssemblerGroup() {
085: AssemblerGroup a = AssemblerGroup.create();
086: assertSame(a, a.implementWith(JA.InfModel, Assembler.infModel));
087: assertSame(a, a.implementWith(JA.MemoryModel,
088: Assembler.memoryModel));
089: assertInstanceOf(InfModel.class, a
090: .openModel(resourceInModel("x rdf:type ja:InfModel")));
091: assertFalse(a
092: .openModel(resourceInModel("y rdf:type ja:MemoryModel")) instanceof InfModel);
093: checkFailsType(a, "js:DefaultModel");
094: }
095:
096: public void testImpliedType() {
097: AssemblerGroup a = AssemblerGroup.create();
098: Resource root = resourceInModel("x ja:reasoner y");
099: Object expected = new Object();
100: a.implementWith(JA.InfModel, new NamedObjectAssembler(
101: resource("x"), expected));
102: assertSame(expected, a.open(root));
103: }
104:
105: public void testBuiltinGroup() {
106: AssemblerGroup g = Assembler.general;
107: assertInstanceOf(Model.class, g
108: .open(resourceInModel("x rdf:type ja:DefaultModel")));
109: assertInstanceOf(InfModel.class, g
110: .open(resourceInModel("x rdf:type ja:InfModel")));
111: assertMemoryModel(g
112: .open(resourceInModel("x rdf:type ja:MemoryModel")));
113: }
114:
115: private static Assembler mockAssembler = new AssemblerBase() {
116: public Object open(Assembler a, Resource root, Mode mode) {
117: return null;
118: }
119: };
120:
121: public void testAddingImplAddsSubclass() {
122: final Model[] fullModel = new Model[1];
123: AssemblerGroup g = new AssemblerGroup.ExpandingAssemblerGroup() {
124: public void loadClasses(Model full) {
125: fullModel[0] = full;
126: }
127: };
128: Resource root = resourceInModel("root rdf:type typeA");
129: Resource typeA = resource("typeA"), typeB = resource("typeB");
130: g.implementWith(typeA, mockAssembler);
131: g.implementWith(typeB, mockAssembler);
132: g.open(root);
133: assertTrue(fullModel[0].contains(typeA, RDFS.subClassOf,
134: JA.Object));
135: assertTrue(fullModel[0].contains(typeB, RDFS.subClassOf,
136: JA.Object));
137: }
138:
139: public static class ImplementsSPOO {
140: public static void whenRequiredByAssembler(AssemblerGroup g) {
141: g.implementWith(resource("SPOO"), mockAssembler);
142: }
143: }
144:
145: public void testClassesLoadedBeforeAddingTypes() {
146: String className = ImplementsSPOO.class.getName();
147: Resource root = resourceInModel("_root rdf:type ja:MemoryModel; _x ja:loadClass '"
148: + className + "'");
149: ExpandingAssemblerGroup g = new AssemblerGroup.ExpandingAssemblerGroup();
150: g.implementWith(resource("ja:MemoryModel"), mockAssembler);
151: g.open(root);
152: assertEquals(resourceSet("SPOO ja:MemoryModel"), g
153: .implements Types());
154: }
155:
156: protected void assertMemoryModel(Object object) {
157: if (object instanceof Model) {
158: Graph g = ((Model) object).getGraph();
159: assertInstanceOf(GraphMemBase.class, g);
160: } else
161: fail("expected a Model, but got a " + object.getClass());
162: }
163:
164: public void testPassesSelfIn() {
165: final AssemblerGroup group = AssemblerGroup.create();
166: final Object result = new Object();
167: Assembler fake = new AssemblerBase() {
168: public Object open(Assembler a, Resource root,
169: Mode irrelevant) {
170: assertSame(
171: "nested call should pass in assembler group:",
172: group, a);
173: return result;
174: }
175: };
176: group.implementWith(JA.Object, fake);
177: assertSame(result, group
178: .open(resourceInModel("x rdf:type ja:Object")));
179: }
180:
181: public void testCopyPreservesMapping() {
182: AssemblerGroup initial = AssemblerGroup.create().implementWith(
183: JA.InfModel, new InfModelAssembler());
184: AssemblerGroup copy = initial.copy();
185: assertSame(initial.assemblerFor(JA.InfModel), copy
186: .assemblerFor(JA.InfModel));
187: assertNull(copy.assemblerFor(JA.Connection));
188: }
189:
190: public void testCopyHasOwnMapping() {
191: AssemblerGroup initial = AssemblerGroup.create().implementWith(
192: JA.InfModel, new InfModelAssembler());
193: AssemblerGroup copy = initial.copy();
194: copy.implementWith(JA.Connection, new ConnectionAssembler());
195: assertNull(initial.assemblerFor(JA.Connection));
196: }
197: }
198:
199: /*
200: * (c) Copyright 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
201: * All rights reserved.
202: *
203: * Redistribution and use in source and binary forms, with or without
204: * modification, are permitted provided that the following conditions
205: * are met:
206: * 1. Redistributions of source code must retain the above copyright
207: * notice, this list of conditions and the following disclaimer.
208: * 2. Redistributions in binary form must reproduce the above copyright
209: * notice, this list of conditions and the following disclaimer in the
210: * documentation and/or other materials provided with the distribution.
211: * 3. The name of the author may not be used to endorse or promote products
212: * derived from this software without specific prior written permission.
213: *
214: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
215: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
216: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
217: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
218: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
219: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
220: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
221: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
222: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
223: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
224: */
|