001: /*
002: (c) Copyright 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: All rights reserved - see end of file.
004: $Id: TestImportManager.java,v 1.9 2008/01/02 12:05:55 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.graph.compose.MultiUnion;
013: import com.hp.hpl.jena.rdf.model.Model;
014: import com.hp.hpl.jena.util.FileManager;
015:
016: public class TestImportManager extends AssemblerTestBase {
017: public TestImportManager(String name) {
018: super (name);
019: }
020:
021: static class FixedFileManager extends FileManager {
022: Map map = new HashMap();
023:
024: public Model loadModel(String URL) {
025: Model result = (Model) map.get(URL);
026: if (result == null)
027: fail("no model for " + URL);
028: return result;
029: }
030:
031: public FixedFileManager add(String URL, Model m) {
032: map.put(URL, m);
033: return this ;
034: }
035: }
036:
037: public void testFollowOwlImports() {
038: final Model modelToLoad = model("this hasMarker B5");
039: Model m = model("x ja:reasoner y; _x owl:imports eh:/loadMe");
040: FileManager fm = new FixedFileManager().add("eh:/loadMe",
041: modelToLoad);
042: Model m2 = new ImportManager().withImports(fm, m);
043: assertInstanceOf(MultiUnion.class, m2.getGraph());
044: assertIsoModels(modelToLoad.union(m), m2);
045: }
046:
047: public void testFollowJAImports() {
048: final Model modelToLoad = model("this hasMarker B5");
049: Model m = model("x ja:reasoner y; _x ja:imports eh:/loadMe");
050: FileManager fm = new FixedFileManager().add("eh:/loadMe",
051: modelToLoad);
052: Model m2 = new ImportManager().withImports(fm, m);
053: assertInstanceOf(MultiUnion.class, m2.getGraph());
054: assertIsoModels(modelToLoad.union(m), m2);
055: }
056:
057: public void testImportMayBeLiteral() {
058: final Model modelToLoad = model("this hasMarker B5");
059: Model m = model("x ja:reasoner y; _x ja:imports 'eh:/loadMe'");
060: FileManager fm = new FixedFileManager().add("eh:/loadMe",
061: modelToLoad);
062: Model m2 = new ImportManager().withImports(fm, m);
063: assertInstanceOf(MultiUnion.class, m2.getGraph());
064: assertIsoModels(modelToLoad.union(m), m2);
065: }
066:
067: public void testBadImportObjectFails() {
068: testBadImportObjectFails("_bnode");
069: testBadImportObjectFails("17");
070: testBadImportObjectFails("'chat'fr");
071: testBadImportObjectFails("'chat'xsd:wrong");
072: }
073:
074: private void testBadImportObjectFails(String object) {
075: String string = "x ja:imports " + object;
076: Model m = model(string);
077: try {
078: new ImportManager().withImports(m);
079: fail("should trap bad import specification " + string);
080: } catch (BadObjectException e) {
081: assertEquals(resource("x"), e.getRoot());
082: assertEquals(rdfNode(m, object), e.getObject());
083: }
084: }
085:
086: public void testFollowOwlImportsDeeply() {
087: final Model m1 = model("this hasMarker M1; _x owl:imports M2"), m2 = model("this hasMarker M2");
088: Model m = model("x ja:reasoner y; _x owl:imports M1");
089: FileManager fm = new FixedFileManager().add("eh:/M1", m1).add(
090: "eh:/M2", m2);
091: Model result = new ImportManager().withImports(fm, m);
092: assertInstanceOf(MultiUnion.class, result.getGraph());
093: assertIsoModels(m1.union(m2).union(m), result);
094: }
095:
096: public void testCatchesCircularity() {
097: final Model m1 = model("this hasMarker Mx; _x owl:imports My"), m2 = model("this hasMarker My; _x owl:imports Mx");
098: FileManager fm = new FixedFileManager().add("eh:/Mx", m1).add(
099: "eh:/My", m2);
100: Model result = new ImportManager().withImports(fm, m1);
101: assertIsoModels(m1.union(m2), result);
102: }
103:
104: public void testCacheModels() {
105: ImportManager im = new ImportManager();
106: Model spec = model("_x owl:imports M1");
107: Model m1 = model("this isModel M1");
108: FileManager withM1 = new FixedFileManager().add("eh:/M1", m1);
109: Model A = im.withImports(withM1, spec);
110: FileManager none = new FixedFileManager();
111: Model B = im.withImports(none, spec);
112: assertIsoModels(A, B);
113: }
114: }
115:
116: /*
117: * (c) Copyright 2006, 2007, 2008 Hewlett-Packard Development Company, LP
118: * All rights reserved.
119: *
120: * Redistribution and use in source and binary forms, with or without
121: * modification, are permitted provided that the following conditions
122: * are met:
123: * 1. Redistributions of source code must retain the above copyright
124: * notice, this list of conditions and the following disclaimer.
125: * 2. Redistributions in binary form must reproduce the above copyright
126: * notice, this list of conditions and the following disclaimer in the
127: * documentation and/or other materials provided with the distribution.
128: * 3. The name of the author may not be used to endorse or promote products
129: * derived from this software without specific prior written permission.
130: *
131: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
132: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
133: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
134: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
135: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
136: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
137: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
138: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
139: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
140: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
141: */
|