001: /*
002: (c) Copyright 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: All rights reserved - see end of file.
004: $Id: TestUnionModelAssembler.java,v 1.6 2008/01/02 12:05:56 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.graph.compose.*;
014: import com.hp.hpl.jena.rdf.model.*;
015: import com.hp.hpl.jena.shared.AddDeniedException;
016:
017: public class TestUnionModelAssembler extends AssemblerTestBase {
018: public TestUnionModelAssembler(String name) {
019: super (name);
020: }
021:
022: protected Class getAssemblerClass() {
023: return UnionModelAssembler.class;
024: }
025:
026: public void testUnionModelAssemblerType() {
027: testDemandsMinimalType(new UnionModelAssembler(), JA.UnionModel);
028: }
029:
030: public void testUnionVocabulary() {
031: assertSubclassOf(JA.UnionModel, JA.Model);
032: assertDomain(JA.UnionModel, JA.subModel);
033: assertRange(JA.Model, JA.subModel);
034: assertDomain(JA.UnionModel, JA.rootModel);
035: assertRange(JA.Model, JA.rootModel);
036: }
037:
038: public void testCreatesMultiUnion() {
039: Resource root = resourceInModel("x rdf:type ja:UnionModel");
040: Assembler a = new UnionModelAssembler();
041: Model m = a.openModel(root);
042: assertInstanceOf(MultiUnion.class, m.getGraph());
043: checkImmutable(m);
044: }
045:
046: private void checkImmutable(Model m) {
047: try {
048: m.add(statement("S P O"));
049: fail("should be immutable");
050: } catch (AddDeniedException e) {
051: pass();
052: }
053: }
054:
055: static class SmudgeAssembler extends AssemblerBase {
056: Map map = new HashMap();
057:
058: public SmudgeAssembler add(String name, Model m) {
059: map.put(resource(name), m);
060: return this ;
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: return (Model) map.get(root);
069: }
070: }
071:
072: public void testCreatesUnionWithSubModels() {
073: Resource root = resourceInModel("x rdf:type ja:UnionModel; x ja:subModel A; x ja:subModel B");
074: Assembler a = new UnionModelAssembler();
075: Model modelA = model(""), modelB = model("");
076: Set expected = new HashSet();
077: expected.add(modelA.getGraph());
078: expected.add(modelB.getGraph());
079: Assembler mock = new SmudgeAssembler().add("A", modelA).add(
080: "B", modelB);
081: Model m = (Model) a.open(mock, root);
082: assertInstanceOf(MultiUnion.class, m.getGraph());
083: MultiUnion mu = (MultiUnion) m.getGraph();
084: List L = mu.getSubGraphs();
085: assertEquals(expected, new HashSet(L));
086: checkImmutable(m);
087: }
088:
089: public void testSubModelsCheckObject() {
090: Resource root = resourceInModel("x rdf:type ja:UnionModel; x ja:subModel 'A'");
091: Assembler a = new UnionModelAssembler();
092: try {
093: a.open(root);
094: fail("should trap unsuitable object");
095: } catch (BadObjectException e) {
096: assertEquals(resource("x"), e.getRoot());
097: assertEquals(rdfNode(empty, "'A'"), e.getObject());
098: }
099: }
100:
101: public void testCreatesUnionWithBaseModel() {
102: Resource root = resourceInModel("x rdf:type ja:UnionModel; x ja:subModel A; x ja:rootModel B");
103: Assembler a = new UnionModelAssembler();
104: Model modelA = model(""), modelB = model("");
105: Set expected = new HashSet();
106: expected.add(modelA.getGraph());
107: Assembler mock = new SmudgeAssembler().add("A", modelA).add(
108: "B", modelB);
109: Model m = (Model) a.open(mock, root);
110: assertInstanceOf(MultiUnion.class, m.getGraph());
111: //
112: MultiUnion mu = (MultiUnion) m.getGraph();
113: assertSame(modelB.getGraph(), mu.getBaseGraph());
114: assertEquals(listOfOne(modelA.getGraph()), mu.getSubGraphs());
115: m.add(statement("a P b"));
116: assertIsoModels(model("a P b"), modelB);
117: }
118: }
119:
120: /*
121: * (c) Copyright 2006, 2007, 2008 Hewlett-Packard Development Company, LP
122: * All rights reserved.
123: *
124: * Redistribution and use in source and binary forms, with or without
125: * modification, are permitted provided that the following conditions
126: * are met:
127: * 1. Redistributions of source code must retain the above copyright
128: * notice, this list of conditions and the following disclaimer.
129: * 2. Redistributions in binary form must reproduce the above copyright
130: * notice, this list of conditions and the following disclaimer in the
131: * documentation and/or other materials provided with the distribution.
132: * 3. The name of the author may not be used to endorse or promote products
133: * derived from this software without specific prior written permission.
134: *
135: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
136: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
137: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
138: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
139: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
140: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
141: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
142: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
143: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
144: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
145: */
|