001: /*
002: (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: [See end of file]
004: $Id: AbstractTestModel.java,v 1.17 2008/01/02 12:04:41 andy_seaborne Exp $
005: */
006:
007: package com.hp.hpl.jena.rdf.model.test;
008:
009: import com.hp.hpl.jena.rdf.model.*;
010: import com.hp.hpl.jena.rdf.model.impl.ModelCom;
011: import com.hp.hpl.jena.shared.*;
012: import com.hp.hpl.jena.graph.*;
013: import com.hp.hpl.jena.graph.test.NodeCreateUtils;
014:
015: /**
016: @author kers
017: */
018: public abstract class AbstractTestModel extends ModelTestBase {
019: public AbstractTestModel(String name) {
020: super (name);
021: }
022:
023: public abstract Model getModel();
024:
025: private Model model;
026:
027: public void setUp() {
028: model = getModel();
029: }
030:
031: public void tearDown() {
032: model.close();
033: }
034:
035: public void testTransactions() {
036: Command cmd = new Command() {
037: public Object execute() {
038: return null;
039: }
040: };
041: if (model.supportsTransactions())
042: model.executeInTransaction(cmd);
043: }
044:
045: public void testCreateResourceFromNode() {
046: RDFNode S = model.getRDFNode(NodeCreateUtils.create("spoo:S"));
047: assertInstanceOf(Resource.class, S);
048: assertEquals("spoo:S", ((Resource) S).getURI());
049: }
050:
051: public void testCreateLiteralFromNode() {
052: RDFNode S = model.getRDFNode(NodeCreateUtils.create("42"));
053: assertInstanceOf(Literal.class, S);
054: assertEquals("42", ((Literal) S).getLexicalForm());
055: }
056:
057: public void testCreateBlankFromNode() {
058: RDFNode S = model.getRDFNode(NodeCreateUtils.create("_Blank"));
059: assertInstanceOf(Resource.class, S);
060: assertEquals(new AnonId("_Blank"), ((Resource) S).getId());
061: }
062:
063: public void testIsEmpty() {
064: Statement S1 = statement(model, "model rdf:type nonEmpty");
065: Statement S2 = statement(model, "pinky rdf:type Pig");
066: assertTrue(model.isEmpty());
067: model.add(S1);
068: assertFalse(model.isEmpty());
069: model.add(S2);
070: assertFalse(model.isEmpty());
071: model.remove(S1);
072: assertFalse(model.isEmpty());
073: model.remove(S2);
074: assertTrue(model.isEmpty());
075: }
076:
077: public void testContainsResource() {
078: modelAdd(model, "x R y; _a P _b");
079: assertTrue(model.containsResource(resource(model, "x")));
080: assertTrue(model.containsResource(resource(model, "R")));
081: assertTrue(model.containsResource(resource(model, "y")));
082: assertTrue(model.containsResource(resource(model, "_a")));
083: assertTrue(model.containsResource(resource(model, "P")));
084: assertTrue(model.containsResource(resource(model, "_b")));
085: assertFalse(model.containsResource(resource(model, "i")));
086: assertFalse(model.containsResource(resource(model, "_j")));
087: }
088:
089: /**
090: Test the new version of getProperty(), which delivers null for not-found
091: properties.
092: */
093: public void testGetProperty() {
094: modelAdd(model, "x P a; x P b; x R c");
095: Resource x = resource(model, "x");
096: assertEquals(resource(model, "c"), x.getProperty(
097: property(model, "R")).getObject());
098: RDFNode ob = x.getProperty(property(model, "P")).getObject();
099: assertTrue(ob.equals(resource(model, "a"))
100: || ob.equals(resource(model, "b")));
101: assertNull(x.getProperty(property(model, "noSuchPropertyHere")));
102: }
103:
104: public void testToStatement() {
105: Triple t = triple("a P b");
106: Statement s = model.asStatement(t);
107: assertEquals(node("a"), s.getSubject().asNode());
108: assertEquals(node("P"), s.getPredicate().asNode());
109: assertEquals(node("b"), s.getObject().asNode());
110: }
111:
112: public void testAsRDF() {
113: RDFNode r = model.asRDFNode(node("a"));
114: }
115:
116: public void testRemoveAll() {
117: testRemoveAll("");
118: testRemoveAll("a RR b");
119: testRemoveAll("x P y; a Q b; c R 17; _d S 'e'");
120: testRemoveAll("subject Predicate 'object'; http://nowhere/x scheme:cunning not:plan");
121: }
122:
123: protected void testRemoveAll(String statements) {
124: modelAdd(model, statements);
125: assertSame(model, model.removeAll());
126: assertEquals(
127: "model should have size 0 following removeAll(): ", 0,
128: model.size());
129: }
130:
131: /**
132: Test cases for RemoveSPO(); each entry is a triple (add, remove, result).
133: <ul>
134: <li>add - the triples to add to the graph to start with
135: <li>remove - the pattern to use in the removal
136: <li>result - the triples that should remain in the graph
137: </ul>
138: */
139: protected String[][] cases = { { "x R y", "x R y", "" },
140: { "x R y; a P b", "x R y", "a P b" },
141: { "x R y; a P b", "?? R y", "a P b" },
142: { "x R y; a P b", "x R ??", "a P b" },
143: { "x R y; a P b", "x ?? y", "a P b" },
144: { "x R y; a P b", "?? ?? ??", "" },
145: { "x R y; a P b; c P d", "?? P ??", "x R y" },
146: { "x R y; a P b; x S y", "x ?? ??", "a P b" }, };
147:
148: /**
149: Test that remove(s, p, o) works, in the presence of inferencing graphs that
150: mean emptyness isn't available. This is why we go round the houses and
151: test that expected ~= initialContent + addedStuff - removed - initialContent.
152: */
153: public void testRemoveSPO() {
154: ModelCom mc = (ModelCom) ModelFactory.createDefaultModel();
155: for (int i = 0; i < cases.length; i += 1)
156: for (int j = 0; j < 3; j += 1) {
157: Model content = getModel();
158: Model baseContent = copy(content);
159: modelAdd(content, cases[i][0]);
160: Triple remove = triple(cases[i][1]);
161: Node s = remove.getSubject(), p = remove.getPredicate(), o = remove
162: .getObject();
163: Resource S = (Resource) (s.equals(Node.ANY) ? null : mc
164: .getRDFNode(s));
165: Property P = (Property) ((p.equals(Node.ANY) ? null
166: : mc.getRDFNode(p).as(Property.class)));
167: RDFNode O = o.equals(Node.ANY) ? null : mc
168: .getRDFNode(o);
169: Model expected = modelWithStatements(cases[i][2]);
170: content.removeAll(S, P, O);
171: Model finalContent = copy(content).remove(baseContent);
172: assertIsoModels(cases[i][1], expected, finalContent);
173: }
174: }
175:
176: public void testIsClosedDelegatedToGraph() {
177: Model m = getModel();
178: assertFalse(m.isClosed());
179: m.close();
180: assertTrue(m.isClosed());
181: }
182:
183: protected Model copy(Model m) {
184: return ModelFactory.createDefaultModel().add(m);
185: }
186: }
187:
188: /*
189: (c) Copyright 2002, 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
190: All rights reserved.
191:
192: Redistribution and use in source and binary forms, with or without
193: modification, are permitted provided that the following conditions
194: are met:
195:
196: 1. Redistributions of source code must retain the above copyright
197: notice, this list of conditions and the following disclaimer.
198:
199: 2. Redistributions in binary form must reproduce the above copyright
200: notice, this list of conditions and the following disclaimer in the
201: documentation and/or other materials provided with the distribution.
202:
203: 3. The name of the author may not be used to endorse or promote products
204: derived from this software without specific prior written permission.
205:
206: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
207: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
208: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
209: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
210: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
211: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
212: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
213: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
214: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
215: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
216: */
|