You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jena.apache.org by cl...@apache.org on 2013/09/14 00:32:06 UTC
svn commit: r1523135 [3/6] - in
/jena/Experimental/new-test/src/test/java/com/hp/hpl/jena: graph/
graph/compose/ graph/impl/ mem/ rdf/ rdf/model/ rdf/model/impl/
rdf/model/temp/ shared/ testing_framework/
Added: jena/Experimental/new-test/src/test/java/com/hp/hpl/jena/rdf/model/AbstractModelTest.java
URL: http://svn.apache.org/viewvc/jena/Experimental/new-test/src/test/java/com/hp/hpl/jena/rdf/model/AbstractModelTest.java?rev=1523135&view=auto
==============================================================================
--- jena/Experimental/new-test/src/test/java/com/hp/hpl/jena/rdf/model/AbstractModelTest.java (added)
+++ jena/Experimental/new-test/src/test/java/com/hp/hpl/jena/rdf/model/AbstractModelTest.java Fri Sep 13 22:32:05 2013
@@ -0,0 +1,3490 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.hp.hpl.jena.rdf.model;
+
+import static com.hp.hpl.jena.testing_framework.ModelHelper.*;
+import static org.junit.Assert.*;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.StringReader;
+import java.net.ConnectException;
+import java.net.NoRouteToHostException;
+import java.net.UnknownHostException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.StringTokenizer;
+
+import org.apache.jena.iri.IRIException;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.hp.hpl.jena.datatypes.xsd.XSDDatatype;
+import com.hp.hpl.jena.graph.Factory;
+import com.hp.hpl.jena.graph.Graph;
+import com.hp.hpl.jena.graph.GraphEvents;
+import com.hp.hpl.jena.graph.Node;
+import com.hp.hpl.jena.graph.RecordingListener;
+import com.hp.hpl.jena.graph.Triple;
+import com.hp.hpl.jena.n3.IRIResolver;
+import com.hp.hpl.jena.rdf.listeners.ChangedListener;
+import com.hp.hpl.jena.rdf.listeners.NullListener;
+import com.hp.hpl.jena.rdf.listeners.ObjectListener;
+import com.hp.hpl.jena.rdf.listeners.StatementListener;
+import com.hp.hpl.jena.rdf.model.impl.ModelCom;
+import com.hp.hpl.jena.rdf.model.impl.StmtIteratorImpl;
+import com.hp.hpl.jena.reasoner.InfGraph;
+import com.hp.hpl.jena.reasoner.Reasoner;
+import com.hp.hpl.jena.reasoner.rulesys.GenericRuleReasoner;
+import com.hp.hpl.jena.reasoner.rulesys.Rule;
+import com.hp.hpl.jena.shared.AbstractPrefixMappingTest;
+import com.hp.hpl.jena.shared.Command;
+import com.hp.hpl.jena.shared.ConfigException;
+import com.hp.hpl.jena.shared.InvalidPropertyURIException;
+import com.hp.hpl.jena.shared.JenaException;
+import com.hp.hpl.jena.shared.Lock;
+import com.hp.hpl.jena.shared.NoReaderForLangException;
+import com.hp.hpl.jena.shared.PrefixMapping;
+import com.hp.hpl.jena.shared.PropertyNotFoundException;
+import com.hp.hpl.jena.testing_framework.AbstractModelProducerUser;
+import com.hp.hpl.jena.testing_framework.NodeCreateUtils;
+import com.hp.hpl.jena.testing_framework.ModelHelper.LitTestObj;
+import com.hp.hpl.jena.util.CollectionFactory;
+import com.hp.hpl.jena.vocabulary.RDF;
+import com.hp.hpl.jena.vocabulary.RDFS;
+
+public abstract class AbstractModelTest extends AbstractModelProducerUser {
+ private Model model;
+
+ static boolean booleanValue = true;
+
+ static char charValue = 'c';
+ static long longValue = 456;
+ static float floatValue = 5.67F;
+ static double doubleValue = 6.78;
+ static String stringValue = "stringValue";
+ static String langValue = "en";
+
+ private static Logger log = LoggerFactory.getLogger(AbstractModelTest.class);
+
+ @Before
+ public void setupAbstractModelTest() {
+ model = getModelProducer().newModel();
+ SL = new RecordingModelListener();
+ }
+
+ @Test
+ public void testTransactions() {
+
+ Command cmd = new Command() {
+ @Override
+ public Object execute() {
+ return null;
+ }
+ };
+ if (model.supportsTransactions())
+ model.executeInTransaction(cmd);
+ }
+
+ @Test
+ public void testCreateResourceFromNode() {
+ RDFNode S = model.getRDFNode(NodeCreateUtils.create("spoo:S"));
+ assertInstanceOf(Resource.class, S);
+ assertEquals("spoo:S", ((Resource) S).getURI());
+ }
+
+ @Test
+ public void testCreateLiteralFromNode() {
+ RDFNode S = model.getRDFNode(NodeCreateUtils.create("42"));
+ assertInstanceOf(Literal.class, S);
+ assertEquals("42", ((Literal) S).getLexicalForm());
+ }
+
+ @Test
+ public void testCreateBlankFromNode() {
+ RDFNode S = model.getRDFNode(NodeCreateUtils.create("_Blank"));
+ assertInstanceOf(Resource.class, S);
+ assertEquals(new AnonId("_Blank"), ((Resource) S).getId());
+ }
+
+ @Test
+ public void testIsEmpty() {
+ Statement S1 = statement( "model rdf:type nonEmpty");
+ Statement S2 = statement( "pinky rdf:type Pig");
+ assertTrue(model.isEmpty());
+ model.add(S1);
+ assertFalse(model.isEmpty());
+ model.add(S2);
+ assertFalse(model.isEmpty());
+ model.remove(S1);
+ assertFalse(model.isEmpty());
+ model.remove(S2);
+ assertTrue(model.isEmpty());
+ }
+
+ @Test
+ public void testContainsResource() {
+ modelAdd(model, "x R y; _a P _b");
+ assertTrue(model.containsResource(resource( "x")));
+ assertTrue(model.containsResource(resource( "R")));
+ assertTrue(model.containsResource(resource( "y")));
+ assertTrue(model.containsResource(resource( "_a")));
+ assertTrue(model.containsResource(resource( "P")));
+ assertTrue(model.containsResource(resource( "_b")));
+ assertFalse(model.containsResource(resource( "i")));
+ assertFalse(model.containsResource(resource( "_j")));
+ }
+
+ /**
+ * Always retuns a value even if the property is no in the model.
+ */
+ @Test
+ public void testGetPropertyURI() {
+ modelAdd(model, "x P a; x P b; x R c");
+ Assert.assertNotNull(model.getProperty(resource( "P").getURI()));
+ Assert.assertNotNull(model.getProperty(resource( "R").getURI()));
+ Assert.assertNotNull(model.getProperty(resource( "x").getURI()));
+ }
+
+ @Test
+ public void testGetPropertySP() {
+ modelAdd(model, "x P a; x P b; x R c; z P d");
+ Resource S = resource("x");
+ Property P = property("R");
+ Statement s = model.getProperty(S, P);
+ Assert.assertNotNull( s );
+ Assert.assertEquals( S, s.getSubject());
+ Assert.assertEquals( P, s.getPredicate());
+ Assert.assertEquals( rdfNode("c"), s.getObject());
+
+ s = model.getProperty(resource("y"), P);
+ Assert.assertNull( s );
+
+ s = model.getProperty(null, P);
+ Assert.assertNotNull( s );
+ Assert.assertEquals( S, s.getSubject());
+ Assert.assertEquals( P, s.getPredicate());
+ Assert.assertEquals( rdfNode("c"), s.getObject());
+
+ s = model.getProperty(resource("z"), null);
+ Assert.assertNotNull( s );
+ Assert.assertEquals( resource("z"), s.getSubject());
+ Assert.assertEquals( property("P"), s.getPredicate());
+ Assert.assertEquals( rdfNode("d"), s.getObject());
+
+ s = model.getProperty(null, (Property)null);
+ Assert.assertNotNull( s );
+ }
+
+
+ @Test
+ public void testToStatement() {
+ Triple t = triple("a P b");
+ Statement s = model.asStatement(t);
+ assertEquals(node("a"), s.getSubject().asNode());
+ assertEquals(node("P"), s.getPredicate().asNode());
+ assertEquals(node("b"), s.getObject().asNode());
+ }
+
+ @Test
+ public void testAsRDF() {
+ verifyPresentAsRDFNode(node("a"), Resource.class);
+ verifyPresentAsRDFNode(node("17"), Literal.class);
+ verifyPresentAsRDFNode(node("_b"), Resource.class);
+ }
+
+ private void verifyPresentAsRDFNode(Node n, Class<? extends RDFNode> nodeClass) {
+ RDFNode r = model.asRDFNode(n);
+ assertSame(n, r.asNode());
+ assertInstanceOf(nodeClass, r);
+ }
+
+ @Test
+ public void testURINodeAsResource() {
+ Node n = node("a");
+ Resource r = model.wrapAsResource(n);
+ assertSame(n, r.asNode());
+ }
+
+ @Test
+ public void testLiteralNodeAsResourceFails() {
+ try {
+ model.wrapAsResource(node("17"));
+ fail("should fail to convert literal to Resource");
+ } catch (UnsupportedOperationException e) {
+ // expected
+ }
+ }
+
+ @Test
+ public void testRemoveAll() {
+ testRemoveAll("");
+ testRemoveAll("a RR b");
+ testRemoveAll("x P y; a Q b; c R 17; _d S 'e'");
+ testRemoveAll("subject Predicate 'object'; http://nowhere/x scheme:cunning not:plan");
+ }
+
+ protected void testRemoveAll(String statements) {
+ modelAdd(model, statements);
+ assertSame(model, model.removeAll());
+ assertEquals("model should have size 0 following removeAll(): ", 0,
+ model.size());
+ }
+
+ /**
+ * Test cases for RemoveSPO(); each entry is a triple (add, remove, result).
+ * <ul>
+ * <li>add - the triples to add to the graph to start with
+ * <li>remove - the pattern to use in the removal
+ * <li>result - the triples that should remain in the graph
+ * </ul>
+ */
+ protected String[][] cases = { { "x R y", "x R y", "" },
+ { "x R y; a P b", "x R y", "a P b" },
+ { "x R y; a P b", "?? R y", "a P b" },
+ { "x R y; a P b", "x R ??", "a P b" },
+ { "x R y; a P b", "x ?? y", "a P b" },
+ { "x R y; a P b", "?? ?? ??", "" },
+ { "x R y; a P b; c P d", "?? P ??", "x R y" },
+ { "x R y; a P b; x S y", "x ?? ??", "a P b" }, };
+
+ /**
+ * Test that remove(s, p, o) works, in the presence of inferencing graphs
+ * that mean emptyness isn't available. This is why we go round the houses
+ * and test that expected ~= initialContent + addedStuff - removed -
+ * initialContent.
+ */
+ @Test
+ public void testRemoveSPO() {
+ ModelCom mc = (ModelCom) ModelFactory.createDefaultModel();
+ for (int i = 0; i < cases.length; i += 1)
+ for (int j = 0; j < 3; j += 1) {
+ Model content = getModelProducer().newModel();
+ Model baseContent = copy(content);
+ modelAdd(content, cases[i][0]);
+ Triple remove = triple(cases[i][1]);
+ Node s = remove.getSubject(), p = remove.getPredicate(), o = remove
+ .getObject();
+ Resource S = (Resource) (s.equals(Node.ANY) ? null : mc
+ .getRDFNode(s));
+ Property P = ((p.equals(Node.ANY) ? null : mc.getRDFNode(p).as(
+ Property.class)));
+ RDFNode O = o.equals(Node.ANY) ? null : mc.getRDFNode(o);
+ Model expected = modelWithStatements(getModelProducer(),
+ cases[i][2]);
+ content.removeAll(S, P, O);
+ Model finalContent = copy(content).remove(baseContent);
+ assertIsoModels(cases[i][1], expected, finalContent);
+ }
+ }
+
+ @Test
+ public void testIsClosedDelegatedToGraph() {
+ assertFalse(model.isClosed());
+ assertFalse( model.getGraph().isClosed());
+ model.close();
+ assertTrue(model.isClosed());
+ assertTrue(model.getGraph().isClosed());
+ }
+
+ protected Model copy(Model m) {
+ return getModelProducer().newModel().add(m);
+ }
+
+ @Test
+ public void testAddLiteralByStatement() {
+ final Literal L = model.createTypedLiteral(210);
+ final Resource S = model.createResource("http://nowhere.man/subject");
+ final Statement s = model.createStatement(S, RDF.value, L);
+ Assert.assertTrue(model.add(s).contains(s));
+ Assert.assertTrue(model.contains(S, RDF.value));
+ }
+
+ @Test
+ public void testAddLiteralBoolean() {
+ final Resource S = model.createResource("http://nowhere.man/subject");
+ final Property P = model.createProperty("http://nowhere.man/predicate");
+ model.addLiteral(S, P, tvBoolean);
+ Assert.assertTrue(model.containsLiteral(S, P, tvBoolean));
+ }
+
+ @Test
+ public void testAddLiteralByte() {
+ final Resource S = model.createResource("http://nowhere.man/subject");
+ final Property P = model.createProperty("http://nowhere.man/predicate");
+ model.addLiteral(S, P, tvByte);
+ Assert.assertTrue(model.containsLiteral(S, P, tvByte));
+ }
+
+ @Test
+ public void testAddLiteralChar() {
+ final Resource S = model.createResource("http://nowhere.man/subject");
+ final Property P = model.createProperty("http://nowhere.man/predicate");
+ model.addLiteral(S, P, tvChar);
+ Assert.assertTrue(model.containsLiteral(S, P, tvChar));
+ }
+
+ @Test
+ public void testAddLiteralDouble() {
+ final Resource S = model.createResource("http://nowhere.man/subject");
+ final Property P = model.createProperty("http://nowhere.man/predicate");
+ model.addLiteral(S, P, tvDouble);
+ Assert.assertTrue(model.containsLiteral(S, P, tvDouble));
+ }
+
+ @Test
+ public void testAddLiteralFloat() {
+ final Resource S = model.createResource("http://nowhere.man/subject");
+ final Property P = model.createProperty("http://nowhere.man/predicate");
+ model.addLiteral(S, P, tvFloat);
+ Assert.assertTrue(model.containsLiteral(S, P, tvFloat));
+ }
+
+ @Test
+ public void testAddLiteralInt() {
+ final Resource S = model.createResource("http://nowhere.man/subject");
+ final Property P = model.createProperty("http://nowhere.man/predicate");
+ model.addLiteral(S, P, tvInt);
+ Assert.assertTrue(model.containsLiteral(S, P, tvInt));
+ }
+
+ @Test
+ public void testAddLiteralStringWithLanguage() {
+ final Resource S = model.createResource("http://nowhere.man/subject");
+ final Property P = model.createProperty("http://nowhere.man/predicate");
+ model.add(S, P, "test string", "en");
+ Assert.assertFalse(model.contains(S, P, "test string"));
+ Assert.assertTrue(model.contains(S, P, "test string", "en"));
+ }
+
+ @Test
+ public void testAddLiteralLong() {
+ final Resource S = model.createResource("http://nowhere.man/subject");
+ final Property P = model.createProperty("http://nowhere.man/predicate");
+ model.addLiteral(S, P, tvLong);
+ Assert.assertTrue(model.containsLiteral(S, P, tvLong));
+ }
+
+ @Test
+ public void testAddLiteralString() {
+ final Resource S = model.createResource("http://nowhere.man/subject");
+ final Property P = model.createProperty("http://nowhere.man/predicate");
+ model.add(S, P, "test string");
+ Assert.assertTrue(model.contains(S, P, "test string"));
+ Assert.assertFalse(model.contains(S, P, "test string", "en"));
+ }
+
+ // public void testAddContainsObject()
+ // {
+ // LitTestObj O = new LitTestObj( 12345 );
+ // model.addLiteral( S, P, O );
+ // assertTrue( model.containsLiteral( S, P, O ) );
+ // }
+
+ @Test
+ public void testAddResource() {
+ final Resource S = model.createResource("http://nowhere.man/subject");
+ final Property P = model.createProperty("http://nowhere.man/predicate");
+ final Resource r = model.createResource();
+ model.add(S, P, r);
+ Assert.assertTrue(model.contains(S, P, r));
+ }
+
+ @Test
+ public void testAddLiteralShort() {
+ final Resource S = model.createResource("http://nowhere.man/subject");
+ final Property P = model.createProperty("http://nowhere.man/predicate");
+ model.addLiteral(S, P, tvShort);
+ Assert.assertTrue(model.containsLiteral(S, P, tvShort));
+ }
+
+ @Test
+ public void testAddDuplicateStatementLeavesSizeSame() {
+ final Resource S = model.createResource("http://nowhere.man/subject");
+ final Property P = model.createProperty("http://nowhere.man/predicate");
+ final Statement s = model.createStatement(S, RDF.value, "something");
+ model.add(s);
+ final long size = model.size();
+ model.add(s);
+ Assert.assertEquals(size, model.size());
+ }
+
+ @Test
+ public void testEmpty() {
+ final Resource S = model.createResource("http://nowhere.man/subject");
+ final Property P = model.createProperty("http://nowhere.man/predicate");
+ Assert.assertTrue( "Model should be empty", model.isEmpty() );
+ Assert.assertFalse(model.containsLiteral(S, P, tvBoolean));
+ Assert.assertFalse(model.contains(S, P, model.createResource()));
+ Assert.assertFalse(model.containsLiteral(S, P, tvByte));
+ Assert.assertFalse(model.containsLiteral(S, P, tvShort));
+ Assert.assertFalse(model.containsLiteral(S, P, tvInt));
+ Assert.assertFalse(model.containsLiteral(S, P, tvLong));
+ Assert.assertFalse(model.containsLiteral(S, P, tvChar));
+ Assert.assertFalse(model.containsLiteral(S, P, tvFloat));
+ Assert.assertFalse(model.containsLiteral(S, P, tvDouble));
+ Assert.assertFalse(model.containsLiteral(S, P, new LitTestObj(12345)));
+ Assert.assertFalse(model.contains(S, P, "test string"));
+ Assert.assertFalse(model.contains(S, P, "test string", "en"));
+ }
+
+ @Test
+ public void testAddStatementByIterator() {
+ Statement[] stmts = statements( "moon orbits earth; earth orbits sun");
+ StmtIterator iter = new StmtIteratorImpl(Arrays.asList(stmts).iterator());
+ model.add( iter );
+ for (Statement s : stmts )
+ {
+ assertTrue( String.format( "%s is missing", s), model.contains(s));
+ }
+ }
+
+ @Test
+ public void testRemoveStatementByIterator() {
+ Statement[] stmts = statements( "moon orbits earth; earth orbits sun");
+ StmtIterator iter = new StmtIteratorImpl(Arrays.asList(stmts).iterator());
+ model.remove( iter );
+ for (Statement s : stmts )
+ {
+ assertFalse( String.format( "%s is still in model", s), model.contains(s));
+ }
+ assertEquals( 0, model.size() );
+ }
+
+ @Test
+ public void testAddModel() {
+ model.register(SL);
+ final Model model2 = modelAdd(getModelProducer().newModel(), "a P b; c P d; x Q 1; y Q 2");
+ model.add(model2);
+ SL.assertHas(new Object[] { "addModel", model2 });
+ Assert.assertEquals(model.size(), model2.size());
+ assertSameStatements(model, model2);
+ }
+
+ @Test
+ public void testRemoveModel() {
+ final Model model2 = getModelProducer().newModel();
+ modelAdd(model, "a P b; c P d; x Q 1; y Q 2");
+ model2.add(model).remove(model);
+ Assert.assertEquals(0, model2.size());
+ Assert.assertFalse(model2.listStatements().hasNext());
+ }
+
+ @Test
+ public void testRemoveModelPartial()
+ {
+ Assert.assertEquals("precondition: model must be empty", 0,
+ model.size());
+ final Model A = modelWithStatements( getModelProducer(),
+ "clouds offer rain; trees offer shelter");
+ final Model B = modelWithStatements(getModelProducer(),
+ "x R y; y Q z; z P x");
+ model.add(A);
+ assertIsoModels(A, model);
+ model.add(B);
+ model.remove(A);
+ assertIsoModels(B, model);
+ model.remove(B);
+ assertEquals(0, model.size());
+ }
+
+ @Test
+ public void testContains() {
+ testContains(false, "", "x");
+ testContains(false, "a R b", "x");
+ testContains(false, "a R b; c P d", "x");
+ /* */
+ testContains(false, "a R b", "z");
+ /* */
+ testContains(true, "x R y", "x");
+ testContains(true, "a P b", "P");
+ testContains(true, "i Q j", "j");
+ testContains(true, "x R y; a P b; i Q j", "y");
+ /* */
+ testContains(true, "x R y; a P b; i Q j", "y");
+ testContains(true, "x R y; a P b; i Q j", "R");
+ testContains(true, "x R y; a P b; i Q j", "a");
+ }
+
+ private void testContains(final boolean yes, final String facts,
+ final String resource) {
+ final Model m = modelWithStatements(getModelProducer(), facts);
+ final RDFNode r = rdfNode(resource);
+ if (m.containsResource(r) != yes) {
+ Assert.fail(String.format("[%s] should%s contain %s", facts,
+ (yes ? "" : " not"), resource));
+ }
+ }
+
+ @Test
+ public void testContainsWithNull() {
+ testCWN(false, "", null, null, null);
+ testCWN(true, "x R y", null, null, null);
+ testCWN(false, "x R y", null, null, res("z"));
+ testCWN(true, "x RR y", res("x"), prop("RR"), null);
+ testCWN(true, "a BB c", null, prop("BB"), res("c"));
+ testCWN(false, "a BB c", null, prop("ZZ"), res("c"));
+ }
+
+ private void testCWN(final boolean yes, final String facts,
+ final Resource S, final Property P, final RDFNode O) {
+ Model m = modelWithStatements(getModelProducer(), facts);
+ if (m.contains(S, P, O) != yes) {
+ Assert.fail(String.format("[%s] should%s contain [%s, %s, %s]",
+ facts, (yes ? "" : " not"), S, P, O));
+ }
+ Assert.assertEquals(yes, modelWithStatements(getModelProducer(), facts)
+ .contains(S, P, O));
+ }
+
+ // FIXME move this to modelCon specific test
+ @Test
+ public void testModelComContainsSPcallsContainsSPO() {
+ final Graph g = Factory.createDefaultGraph();
+ final boolean[] wasCalled = { false };
+ // FIXME change to dynamic proxy
+ final Model m = new ModelCom(g) {
+ @Override
+ public boolean contains(final Resource s, final Property p,
+ final RDFNode o) {
+ wasCalled[0] = true;
+ return super.contains(s, p, o);
+ }
+ };
+ Assert.assertFalse(m.contains(resource("r"), property("p")));
+ Assert.assertTrue("contains(S,P) should call contains(S,P,O)",
+ wasCalled[0]);
+ }
+
+ /**
+ * test moving things between models
+ */
+ @Test
+ public void testAddingStatementsChangesModel() {
+ final Resource S = ResourceFactory.createResource();
+ final Property P = ResourceFactory
+ .createProperty("http://example.com/property");
+ final Resource O = ResourceFactory.createResource();
+ final Model model2 = getModelProducer().newModel();
+ final Statement stmt = model.createStatement(S, P, O);
+ Assert.assertEquals(model, stmt.getModel());
+ Assert.assertEquals(0, model.size());
+ Assert.assertEquals(model, stmt.getSubject().getModel());
+ Assert.assertEquals(model, stmt.getPredicate().getModel());
+ Assert.assertEquals(model, stmt.getObject().getModel());
+ model.add(stmt);
+ Assert.assertEquals(1, model.size());
+ Assert.assertEquals(model, stmt.getSubject().getModel());
+ Assert.assertEquals(model, stmt.getPredicate().getModel());
+ Assert.assertEquals(model, stmt.getObject().getModel());
+ model2.add(stmt);
+ Assert.assertEquals(1, model.size());
+ Assert.assertEquals(model, stmt.getSubject().getModel());
+ Assert.assertEquals(model, stmt.getPredicate().getModel());
+ Assert.assertEquals(model, stmt.getObject().getModel());
+ Assert.assertEquals(1, model2.size());
+ final Statement stmt2 = model2.listStatements().next();
+ Assert.assertEquals(model2, stmt2.getSubject().getModel());
+ Assert.assertEquals(model2, stmt2.getPredicate().getModel());
+ Assert.assertEquals(model2, stmt2.getObject().getModel());
+ }
+
+ /*
+ * try { Statement stmt; StmtIterator sIter; //
+ * System.out.println("Beginning " + test);
+ *
+ * try { n=100; Resource r11 = m1.createResource(); // Resource r12 =
+ * m2.createResource(new ResTestObjF()); long size1 = m1.size(); long size2
+ * = m2.size();
+ *
+ * r11.addLiteral(RDF.value, 1); n++; if (! (m1.size() == ++size1))
+ * error(test, n); n++; if (! (m2.size() == size2)) error(test,n);
+ *
+ * // stmt = m2.createStatement(r11, RDF.value, r12); // n++; if (!
+ * (stmt.getSubject().getModel() == m2)) error(test,n); // n++; if (!
+ * (stmt.getResource().getModel() == m2)) error(test,n); // // m1.add(stmt);
+ * // n++; if (! (m1.size() == ++size1)) error(test, n); // n++; if (!
+ * (m2.size() == size2)) error(test,n); // // sIter = m1.listStatements( new
+ * SimpleSelector( r11, RDF.value, r12 ) ); // n++; if (! sIter.hasNext())
+ * error(test, n); // n++; stmt = sIter.nextStatement(); // n++; if (!
+ * (stmt.getSubject().getModel() == m1)) error(test,n); // n++; if (!
+ * (stmt.getResource().getModel() == m1)) error(test,n); // sIter.close();
+ *
+ *
+ * } catch (Exception e) { error(test, n, e); } } catch (Exception e) {
+ * logger.error( "test " + test + "[" + n + "]", e ); errors = true; } //
+ * System.out.println("End of " + test); }
+ */
+
+ @Test
+ public void testGetAlt() {
+ final String uri = "http://aldabaran.hpl.hp.com/rdf/test4/" + 160;
+ model.createAlt(uri);
+ final Alt a = model.getAlt(uri);
+ Assert.assertEquals(uri, a.getURI());
+ Assert.assertTrue(model.contains(a, RDF.type, RDF.Alt));
+ }
+
+ // FIXME move to resourec factory test
+ // public void testGetResourceFactory()
+ // {
+ // String uri = "http://aldabaran.hpl.hp.com/rdf/test4/a" + 120;
+ // Resource r = model.getResource( uri, new ResTestObjF() );
+ // assertEquals( uri, r.getURI() );
+ // }
+
+ @Test
+ public void testGetBag() {
+ final String uri = "http://aldabaran.hpl.hp.com/rdf/test4/" + 150;
+ model.createBag(uri);
+ final Bag b = model.getBag(uri);
+ Assert.assertEquals(uri, b.getURI());
+ Assert.assertTrue(model.contains(b, RDF.type, RDF.Bag));
+ }
+
+ @Test
+ public void testGetPropertyOneArg() {
+ final String uri = "http://aldabaran.hpl.hp.com/rdf/test4/a" + 130;
+ final Property p = model.getProperty(uri);
+ Assert.assertEquals(uri, p.getURI());
+ }
+
+ @Test
+ public void testGetPropertyTwoArgs() {
+ final String ns = "http://aldabaran.hpl.hp.com/rdf/test4/a" + 140 + "/";
+ final Property p = model.getProperty(ns, "foo");
+ Assert.assertEquals(ns + "foo", p.getURI());
+ }
+
+ @Test
+ public void testGetResource() {
+ final String uri = "http://aldabaran.hpl.hp.com/rdf/test4/a" + 110;
+ final Resource r = model.getResource(uri);
+ Assert.assertEquals(uri, r.getURI());
+ }
+
+ @Test
+ public void testGetSeq() {
+ final String uri = "http://aldabaran.hpl.hp.com/rdf/test4/" + 170;
+ model.createSeq(uri);
+ final Seq s = model.getSeq(uri);
+ Assert.assertEquals(uri, s.getURI());
+ Assert.assertTrue(model.contains(s, RDF.type, RDF.Seq));
+ }
+
+ private void addReification( final Model m, final String tag,
+ final String statement )
+ {
+ m.createReifiedStatement(tag, statement( statement));
+ }
+
+
+
+ @Test
+ public void testAddModelWithReifications()
+ {
+ final Model m = modelWithStatements(getModelProducer(), "a P b");
+ addReification(m, "x", "S P O");
+ addReification(m, "a", "x R y");
+ final Model target = getModelProducer().newModel();
+ target.add(m);
+ target.setNsPrefixes(PrefixMapping.Standard);
+ assertIsoModels(m, target);
+ }
+
+ @Test
+ public void testRemoveModelWithReifications()
+ {
+ final Model target = getModelProducer().newModel();
+ addReification(target, "x", "S P O");
+ addReification(target, "y", "A P B");
+ final Model remove = getModelProducer().newModel();
+ addReification(remove, "y", "A P B");
+ final Model answer = getModelProducer().newModel();
+ addReification(answer, "x", "S P O");
+ target.remove(remove);
+ assertIsoModels(answer, target);
+ }
+
+ @Test
+ public void testRemoveModelSelf()
+ {
+ final Model m = modelWithStatements(getModelProducer(),
+ "they sing together; he sings alone");
+ m.remove(m);
+ assertEquals("", 0, m.size());
+ }
+
+ private void testContains( final Model m, final Statement[] statements )
+ {
+ for (final Statement statement : statements)
+ {
+ Assert.assertTrue(String.format( "%s should be here", statement), m.contains(statement));
+ }
+ }
+
+ @Test
+ public void testRemoveStatementArray()
+ {
+ Statement[] remove = statements( "moon orbits earth; earth orbits sun");
+ Statement[] keep = statements( "I drink tea; you drink coffee");
+ model.add(remove).add(keep);
+ testContains(model, remove);
+ testContains(model, keep);
+
+ model.remove(remove);
+ testOmits(model, remove);
+ testContains(model, keep);
+ }
+
+ @Test
+ public void testCreatingStatementDoesNotAddToModel()
+ {
+ assertEquals( "Model should be empty", 0, model.size() );
+ model.createStatement( resource("a"), property("b"), resource("c"));
+ assertEquals( "Model should be empty", 0, model.size() );
+ }
+
+ @Test
+ public void testRemoveStatementList()
+ {
+ Statement[] remove = statements( "moon orbits earth; earth orbits sun");
+ Statement[] keep = statements( "I drink tea; you drink coffee");
+ model.add( remove ).add( keep );
+ testContains(model, remove);
+ testContains(model, keep);
+
+ model.remove(Arrays.asList(remove));
+ testOmits(model, remove);
+ testContains(model, keep);
+ }
+
+ private void testOmits( final Model m, final Statement[] statements )
+ {
+ for (final Statement statement : statements)
+ {
+ Assert.assertFalse("it should not be here", m.contains(statement));
+ }
+ }
+
+ @Test
+ public void testRemoveSPOCallsGraphDeleteTriple()
+ {
+ modelAdd(model, "a P b; c P d; x Q 1; y Q 2");
+ final List<Triple> deleted = new ArrayList<Triple>();
+ RecordingListener listener = new RecordingListener();
+ model.getGraph().getEventManager().register(listener);
+ Model model2 = model.remove(resource("c"), property("P"), rdfNode("d"));
+ listener.assertHas(new Object[] { "delete", model.getGraph(), triple("c P d") });
+ Assert.assertSame(model,model2);
+ }
+
+ private void checkNumericContent( final Container cont2, final int num )
+ {
+ final NodeIterator nit = cont2.iterator();
+ for (int i = 0; i < num; i += 1)
+ {
+ Assert.assertEquals(i, ((Literal) nit.nextNode()).getInt());
+ }
+ Assert.assertFalse(nit.hasNext());
+ }
+
+ private void retainOnlySpecified( final Container cont2, final int num,
+ final boolean[] retain )
+ {
+ final NodeIterator nit = cont2.iterator();
+ for (int i = 0; i < num; i++)
+ {
+ nit.nextNode();
+ if (retain[i] == false)
+ {
+ nit.remove();
+ }
+ }
+ Assert.assertFalse(nit.hasNext());
+ }
+
+ private void seeWhatsThere( final Container cont2, final boolean[] found )
+ {
+ final NodeIterator nit = cont2.iterator();
+ while (nit.hasNext())
+ {
+ final int v = ((Literal) nit.nextNode()).getInt();
+ Assert.assertFalse(found[v]);
+ found[v] = true;
+ }
+ }
+
+ private Set<Object> setOf( final Object x )
+ {
+ final Set<Object> result = new HashSet<Object>();
+ result.add(x);
+ return result;
+ }
+
+ private void testContainer( final Model model, final Container cont1,
+ final Container cont2 )
+ {
+ final Literal tvLiteral = model.createLiteral("test 12 string 2");
+ // Resource tvResObj = model.createResource( new ResTestObjF() );
+ final Object tvLitObj = new LitTestObj(1234);
+ model.createBag();
+ model.createAlt();
+ model.createSeq();
+ final String lang = "en";
+ //
+ Assert.assertEquals(0, cont1.size());
+ Assert.assertEquals(0, cont2.size());
+ //
+ Assert.assertTrue(cont1.add(tvBoolean).contains(
+ tvBoolean));
+ Assert.assertTrue(cont1.add(tvByte).contains(
+ tvByte));
+ Assert.assertTrue(cont1.add(tvShort).contains(
+ tvShort));
+ Assert.assertTrue(cont1.add(tvInt).contains(
+ tvInt));
+ Assert.assertTrue(cont1.add(tvLong).contains(
+ tvLong));
+ Assert.assertTrue(cont1.add(tvFloat).contains(
+ tvFloat));
+ Assert.assertTrue(cont1.add(tvDouble).contains(
+ tvDouble));
+ Assert.assertTrue(cont1.add(tvChar).contains(
+ tvChar));
+ Assert.assertTrue(cont1.add(tvString).contains(
+ tvString));
+ Assert.assertFalse(cont1.contains(tvString, lang));
+ Assert.assertTrue(cont1.add(tvString, lang)
+ .contains(tvString, lang));
+ Assert.assertTrue(cont1.add(tvLiteral).contains(tvLiteral));
+ // assertTrue( cont1.add( tvResObj ).contains( tvResObj ) );
+ Assert.assertTrue(cont1.add(tvLitObj).contains(tvLitObj));
+ Assert.assertEquals(12, cont1.size());
+ //
+ final int num = 10;
+ for (int i = 0; i < num; i += 1)
+ {
+ cont2.add(i);
+ }
+ Assert.assertEquals(num, cont2.size());
+ checkNumericContent(cont2, num);
+ //
+ final boolean[] found = new boolean[num];
+ final boolean[] retain = { true, true, true, false, false, false,
+ false, false, true, true };
+ retainOnlySpecified(cont2, num, retain);
+ seeWhatsThere(cont2, found);
+ for (int i = 0; i < num; i += 1)
+ {
+ Assert.assertEquals(i + "th element of array", retain[i], found[i]);
+ }
+ }
+
+ // public void testCreateAnonByFactory()
+ // {
+ // assertTrue( model.createResource( new ResTestObjF() ).isAnon() );
+ // }
+
+ // public void testCreateResourceByFactory()
+ // {
+ // String uri = "http://aldabaran.hpl.hp.com/foo";
+ // assertEquals( uri, model.createResource( uri, new ResTestObjF()
+ // ).getURI() );
+ // }
+ @Test
+ public void testCreateAnonResource()
+ {
+ final Resource r = model.createResource();
+ Assert.assertTrue(r.isAnon());
+ Assert.assertNull(r.getURI());
+ Assert.assertNull(r.getNameSpace());
+ Assert.assertNull(r.getLocalName());
+ }
+
+ @Test
+ public void testCreateAnonResourceWithNull()
+ {
+ final Resource r = model.createResource((String) null);
+ Assert.assertTrue(r.isAnon());
+ Assert.assertNull(r.getURI());
+ Assert.assertNull(r.getNameSpace());
+ Assert.assertNull(r.getLocalName());
+ }
+
+ @Test
+ public void testCreateNamedResource()
+ {
+ final String uri = "http://aldabaran.hpl.hp.com/foo";
+ Assert.assertEquals(uri, model.createResource(uri).getURI());
+ }
+
+ @Test
+ public void testCreateNullPropertyFails()
+ {
+ try
+ {
+ model.createProperty(null);
+ Assert.fail("should not create null property");
+ }
+ catch (final InvalidPropertyURIException e)
+ {
+ // expected
+ }
+ }
+
+ @Test
+ public void testCreatePropertyOneArg()
+ {
+ final Property p = model.createProperty("abc/def");
+ Assert.assertEquals("abc/", p.getNameSpace());
+ Assert.assertEquals("def", p.getLocalName());
+ Assert.assertEquals("abc/def", p.getURI());
+ }
+
+ @Test
+ public void testCreatePropertyStrangeURI()
+ {
+ final String uri = RDF.getURI() + "_345";
+ final Property p = model.createProperty(uri);
+ Assert.assertEquals(RDF.getURI(), p.getNameSpace());
+ Assert.assertEquals("_345", p.getLocalName());
+ Assert.assertEquals(uri, p.getURI());
+ }
+
+ @Test
+ public void testCreatePropertyStrangeURITwoArgs()
+ {
+ final String local = "_345";
+ final Property p = model.createProperty(RDF.getURI(), local);
+ Assert.assertEquals(RDF.getURI(), p.getNameSpace());
+ Assert.assertEquals(local, p.getLocalName());
+ Assert.assertEquals(RDF.getURI() + local, p.getURI());
+ }
+
+ @Test
+ public void testCreatePropertyTwoArgs()
+ {
+ final Property p = model.createProperty("abc/", "def");
+ Assert.assertEquals("abc/", p.getNameSpace());
+ Assert.assertEquals("def", p.getLocalName());
+ Assert.assertEquals("abc/def", p.getURI());
+ }
+
+ @Test
+ public void testCreateTypedAnonResource()
+ {
+ final Resource r = model.createResource(RDF.Property);
+ Assert.assertTrue(r.isAnon());
+ Assert.assertTrue(model.contains(r, RDF.type, RDF.Property));
+ }
+
+ @Test
+ public void testCreateTypedNamedresource()
+ {
+ final String uri = "http://aldabaran.hpl.hp.com/foo";
+ final Resource r = model.createResource(uri, RDF.Property);
+ Assert.assertEquals(uri, r.getURI());
+ Assert.assertTrue(model.contains(r, RDF.type, RDF.Property));
+ }
+
+ @Test
+ public void testEnhancedResources()
+ {
+ final Resource r = model.createResource();
+ testResource(model, r, 0);
+
+ testResource(model, model.createBag(), 1);
+ testContainer(model, model.createBag(), model.createBag());
+
+ testResource(model, model.createAlt(), 1);
+ testContainer(model, model.createAlt(), model.createAlt());
+
+ testResource(model, model.createSeq(), 1);
+ testContainer(model, model.createSeq(), model.createSeq());
+ // testSeq( model, model.createSeq(), model.createSeq(),
+ // model.createSeq(),
+ // model.createSeq(), model.createSeq(), model.createSeq(),
+ // model.createSeq() );
+ }
+
+ private void testResource( final Model model, final Resource r,
+ final int numProps )
+ {
+ final Literal tvLiteral = model.createLiteral("test 12 string 2");
+ final Resource tvResource = model.createResource();
+ final String lang = "fr";
+ //
+ Assert.assertTrue(r.addLiteral(RDF.value,
+ tvBoolean).hasLiteral(RDF.value,
+ tvBoolean));
+ Assert.assertTrue(r.addLiteral(RDF.value, tvByte)
+ .hasLiteral(RDF.value, tvByte));
+ Assert.assertTrue(r
+ .addLiteral(RDF.value, tvShort)
+ .hasLiteral(RDF.value, tvShort));
+ Assert.assertTrue(r.addLiteral(RDF.value, tvInt)
+ .hasLiteral(RDF.value, tvInt));
+ Assert.assertTrue(r.addLiteral(RDF.value, tvLong)
+ .hasLiteral(RDF.value, tvLong));
+ Assert.assertTrue(r.addLiteral(RDF.value, tvChar)
+ .hasLiteral(RDF.value, tvChar));
+ Assert.assertTrue(r
+ .addLiteral(RDF.value, tvFloat)
+ .hasLiteral(RDF.value, tvFloat));
+ Assert.assertTrue(r.addLiteral(RDF.value,
+ tvDouble).hasLiteral(RDF.value,
+ tvDouble));
+ Assert.assertTrue(r.addProperty(RDF.value,
+ tvString).hasProperty(RDF.value,
+ tvString));
+ Assert.assertTrue(r.addProperty(RDF.value,
+ tvString, lang).hasProperty(RDF.value,
+ tvString, lang));
+ Assert.assertTrue(r.addLiteral(RDF.value,
+ tvObject).hasLiteral(RDF.value,
+ tvObject));
+ Assert.assertTrue(r.addProperty(RDF.value, tvLiteral).hasProperty(
+ RDF.value, tvLiteral));
+ Assert.assertTrue(r.addProperty(RDF.value, tvResource).hasProperty(
+ RDF.value, tvResource));
+ Assert.assertTrue(r.getRequiredProperty(RDF.value).getSubject()
+ .equals(r));
+ //
+ final Property p = model.createProperty("foo/", "bar");
+ try
+ {
+ r.getRequiredProperty(p);
+ Assert.fail("should detect missing property");
+ }
+ catch (final PropertyNotFoundException e)
+ {
+ // expected
+ }
+ //
+ Assert.assertEquals(13,
+ iteratorToSet(r.listProperties(RDF.value)).size());
+ Assert.assertEquals(setOf(r), iteratorToSet(r
+ .listProperties(RDF.value).mapWith(Statement.Util.getSubject)));
+ //
+ Assert.assertEquals(0, iteratorToSet(r.listProperties(p))
+ .size());
+ Assert.assertEquals(
+ new HashSet<Resource>(),
+ iteratorToSet(r.listProperties(p).mapWith(
+ Statement.Util.getSubject)));
+ //
+ Assert.assertEquals(13 + numProps,
+ iteratorToSet(r.listProperties()).size());
+ Assert.assertEquals(
+ setOf(r),
+ iteratorToSet(r.listProperties().mapWith(
+ Statement.Util.getSubject)));
+ //
+ r.removeProperties();
+ Assert.assertEquals(0,
+ model.query(new SimpleSelector(r, null, (RDFNode) null)).size());
+ }
+
+ static final String subjURI = "http://aldabaran.hpl.hp.com/foo";
+ static final String predURI = "http://aldabaran.hpl.hp.com/bar";
+
+ public void testCreateStatementByteMax()
+ {
+ final Resource r = resource( subjURI );
+ final Property p = property(predURI);
+ final Statement s = model.createLiteralStatement(r, p, Byte.MAX_VALUE);
+ Assert.assertEquals(r, s.getSubject());
+ Assert.assertEquals(p, s.getPredicate());
+ Assert.assertEquals(Byte.MAX_VALUE, s.getByte());
+ }
+
+ public void testCreateStatementChar()
+ {
+ final Resource r = resource( subjURI );
+ final Property p = property( predURI );
+ final Statement s = model.createLiteralStatement(r, p, '$');
+ Assert.assertEquals(r, s.getSubject());
+ Assert.assertEquals(p, s.getPredicate());
+ Assert.assertEquals('$', s.getChar());
+ }
+
+ public void testCreateStatementDouble()
+ {
+ final Resource r = resource( subjURI );
+ final Property p = property( predURI );
+ final Statement s = model.createStatement(r, p,
+ model.createTypedLiteral(12345.67890d));
+ Assert.assertEquals(r, s.getSubject());
+ Assert.assertEquals(p, s.getPredicate());
+ Assert.assertEquals(12345.67890d, s.getDouble(), 0.0000005);
+ }
+
+ public void testCreateStatementFactory()
+ {
+ final Resource r = resource( subjURI );
+ final Property p = property( predURI );
+ final LitTestObj tv = new LitTestObj(Long.MIN_VALUE);
+ final Statement s = model.createLiteralStatement(r, p, tv);
+ Assert.assertEquals(r, s.getSubject());
+ Assert.assertEquals(p, s.getPredicate());
+ // assertEquals( tv, s.getObject( new LitTestObjF() ) );
+ }
+
+ public void testCreateStatementFloat()
+ {
+ final Resource r = resource( subjURI );
+ final Property p = property( predURI );
+ final Statement s = model.createStatement(r, p,
+ model.createTypedLiteral(123.456f));
+ Assert.assertEquals(r, s.getSubject());
+ Assert.assertEquals(p, s.getPredicate());
+ Assert.assertEquals(123.456f, s.getFloat(), 0.0005);
+ }
+
+ public void testCreateStatementIntMax()
+ {
+ final Resource r = resource( subjURI );
+ final Property p = property( predURI );
+ final Statement s = model.createLiteralStatement(r, p,
+ Integer.MAX_VALUE);
+ Assert.assertEquals(r, s.getSubject());
+ Assert.assertEquals(p, s.getPredicate());
+ Assert.assertEquals(Integer.MAX_VALUE, s.getInt());
+ }
+
+ public void testCreateStatementLongMax()
+ {
+ final Resource r = resource( subjURI );
+ final Property p = property( predURI );
+ final Statement s = model.createLiteralStatement(r, p, Long.MAX_VALUE);
+ Assert.assertEquals(r, s.getSubject());
+ Assert.assertEquals(p, s.getPredicate());
+ Assert.assertEquals(Long.MAX_VALUE, s.getLong());
+ }
+
+ public void testCreateStatementResource()
+ {
+ final Resource r = resource( subjURI );
+ final Property p = property( predURI );
+ final Resource tv = resource();
+ final Statement s = model.createStatement(r, p, tv);
+ Assert.assertEquals(r, s.getSubject());
+ Assert.assertEquals(p, s.getPredicate());
+ Assert.assertEquals(tv, s.getResource());
+ }
+
+ public void testCreateStatementShortMax()
+ {
+ final Resource r = resource( subjURI );
+ final Property p = property( predURI );
+ final Statement s = model.createLiteralStatement(r, p, Short.MAX_VALUE);
+ Assert.assertEquals(r, s.getSubject());
+ Assert.assertEquals(p, s.getPredicate());
+ Assert.assertEquals(Short.MAX_VALUE, s.getShort());
+ }
+
+ public void testCreateStatementString()
+ {
+ final Resource r = resource( subjURI );
+ final Property p = property( predURI );
+ final String string = "this is a plain string";
+ final Statement s = model.createStatement(r, p, string);
+ Assert.assertEquals(r, s.getSubject());
+ Assert.assertEquals(p, s.getPredicate());
+ Assert.assertEquals(string, s.getString());
+ Assert.assertNull( s.getLanguage() );
+ Assert.assertEquals("en", model.createStatement(r, p, string, "en")
+ .getLanguage());
+ Assert.assertEquals("fr", model.createStatement(r, p, string, "fr")
+ .getLanguage());
+ }
+
+ public void testCreateStatementTrue()
+ {
+ final Resource r = resource( subjURI );
+ final Property p = property( predURI );
+ final Statement s = model.createLiteralStatement(r, p, true);
+ Assert.assertEquals(r, s.getSubject());
+ Assert.assertEquals(p, s.getPredicate());
+ Assert.assertEquals(true, s.getBoolean());
+ }
+
+ public void testCreateStatementTypeLiteral()
+ {
+ final Model model = ModelFactory.createDefaultModel();
+ final Resource R = model.createResource("http://example/r");
+ final Property P = model.createProperty("http://example/p");
+ model.add(R, P, "2", XSDDatatype.XSDinteger);
+ final Literal L = ResourceFactory.createTypedLiteral("2",
+ XSDDatatype.XSDinteger);
+ Assert.assertTrue(model.contains(R, P, L));
+ Assert.assertFalse(model.contains(R, P, "2"));
+ }
+
+ public void testNotReified()
+ {
+ Statement s1 = model
+ .createStatement(model.createResource(), RDF.type, RDFS.Class);
+
+ Assert.assertFalse("Should not be reified", s1.isReified());
+ model.add(s1);
+ Assert.assertFalse("Should not be reified", s1.isReified());
+
+ Statement s2 = model
+ .createStatement(model.createResource(), RDF.type, RDFS.Class);
+ Assert.assertFalse("Should not be reified", s2.isReified());
+ model.add(s2);
+ Assert.assertFalse("Should not be reified", s2.isReified());
+ }
+
+ private void checkChangedStatementSP( final Resource r, final Statement changed )
+ {
+ Assert.assertEquals(r, changed.getSubject());
+ Assert.assertEquals(RDF.value, changed.getPredicate());
+ }
+
+ private void checkCorrectStatements( final Resource r, final Statement sTrue,
+ final Statement changed )
+ {
+ Assert.assertFalse(model.contains(sTrue));
+ Assert.assertFalse(model.containsLiteral(r, RDF.value, true));
+ Assert.assertTrue(model.contains(changed));
+ }
+
+ private Statement loadInitialStatement(final Resource r)
+ {
+ final Statement sTrue = model
+ .createLiteralStatement(r, RDF.value, true);
+ model.add(sTrue);
+ return sTrue;
+ }
+
+
+ @Test
+ public void testCreateAlt()
+ {
+ final Alt tvAlt = model.createAlt();
+ Assert.assertEquals(tvAlt, model.createStatement(resource(), RDF.value, tvAlt)
+ .getAlt());
+ }
+
+ @Test
+ public void testCreateAltURI()
+ {
+ final Alt tvAlt = model.createAlt( subjURI );
+ Assert.assertEquals(tvAlt, model.createStatement(resource( subjURI ), RDF.value, tvAlt)
+ .getAlt());
+ }
+
+ @Test
+ public void testCreateBag()
+ {
+ final Bag tvBag = model.createBag();
+ Assert.assertEquals(tvBag, model.createStatement(resource(), RDF.value, tvBag)
+ .getBag());
+ }
+
+ @Test
+ public void testCreateBagURI()
+ {
+ final Bag tvBag = model.createBag(subjURI);
+ Assert.assertEquals(tvBag, model.createStatement(resource(subjURI), RDF.value, tvBag)
+ .getBag());
+ }
+
+ @Test
+ public void testLiteralStatementBoolean()
+ {
+ final Statement s = model.createLiteralStatement(resource(), RDF.value, true);
+ Assert.assertEquals(model.createTypedLiteral(true), s.getObject());
+ Assert.assertEquals(true, s.getBoolean());
+ }
+
+ @Test
+ public void testLiteralStatementBooleanURI()
+ {
+ final Statement s = model.createLiteralStatement(resource(subjURI), RDF.value, false);
+ Assert.assertEquals(model.createTypedLiteral(false), s.getObject());
+ Assert.assertEquals(false, s.getBoolean());
+ }
+
+
+ @Test
+ public void testLiteralStatementByte()
+ {
+ final Statement s = model.createLiteralStatement(resource(), RDF.value,
+ tvByte);
+ Assert.assertEquals(
+ model.createTypedLiteral(tvByte),
+ s.getObject());
+ Assert.assertEquals(tvByte, s.getLong());
+ }
+
+ @Test
+ public void testLiteralStatementByteURL()
+ {
+ final Statement s = model.createLiteralStatement(resource(subjURI), RDF.value,
+ tvByte);
+ Assert.assertEquals(
+ model.createTypedLiteral(tvByte),
+ s.getObject());
+ Assert.assertEquals(tvByte, s.getLong());
+ }
+
+ @Test
+ public void testChangeObjectBoolean()
+ {
+ Resource r = resource();
+ final Statement sTrue = loadInitialStatement(r);
+ final Statement sFalse = sTrue.changeLiteralObject(false);
+ checkChangedStatementSP(r,sFalse);
+ Assert.assertEquals(model.createTypedLiteral(false), sFalse.getObject());
+ Assert.assertEquals(false, sFalse.getBoolean());
+ checkCorrectStatements(r,sTrue, sFalse);
+ Assert.assertTrue(model.containsLiteral(r, RDF.value, false));
+ }
+
+ @Test
+ public void testChangeObjectByte()
+ {
+ Resource r = resource();
+ final Statement sTrue = loadInitialStatement(r);
+ final Statement changed = sTrue
+ .changeLiteralObject(tvByte);
+ checkChangedStatementSP(r,changed);
+ Assert.assertEquals(
+ model.createTypedLiteral(tvByte),
+ changed.getObject());
+ Assert.assertEquals(tvByte, changed.getByte());
+ checkCorrectStatements(r,sTrue, changed);
+ Assert.assertTrue(model.containsLiteral(r, RDF.value,
+ tvByte));
+ }
+
+ @Test
+ public void testChangeObjectChar()
+ {
+ Resource r = resource();
+ final Statement sTrue = loadInitialStatement(r);
+ final Statement changed = sTrue
+ .changeLiteralObject(tvChar);
+ checkChangedStatementSP(r,changed);
+ Assert.assertEquals(tvChar, changed.getChar());
+ Assert.assertEquals(
+ model.createTypedLiteral(tvChar),
+ changed.getObject());
+ checkCorrectStatements(r,sTrue, changed);
+ Assert.assertTrue(model.containsLiteral(r, RDF.value,
+ tvChar));
+ }
+
+ @Test
+ public void testChangeObjectDouble()
+ {
+ Resource r = resource();
+ final Statement sTrue = loadInitialStatement(r);
+ final Statement changed = sTrue
+ .changeLiteralObject(tvDouble);
+ checkChangedStatementSP(r,changed);
+ Assert.assertEquals(
+ model.createTypedLiteral(tvDouble),
+ changed.getObject());
+ Assert.assertEquals(tvDouble,
+ changed.getDouble(), dDelta);
+ checkCorrectStatements(r,sTrue, changed);
+ Assert.assertTrue(model.containsLiteral(r, RDF.value,
+ tvDouble));
+ }
+
+ @Test
+ public void testChangeObjectFloat()
+ {
+ Resource r = resource();
+ final Statement sTrue = loadInitialStatement(r);
+ final Statement changed = sTrue
+ .changeLiteralObject(tvFloat);
+ checkChangedStatementSP(r,changed);
+ Assert.assertEquals(
+ model.createTypedLiteral(tvFloat),
+ changed.getObject());
+ Assert.assertEquals(tvFloat, changed.getFloat(),
+ fDelta);
+ checkCorrectStatements(r,sTrue, changed);
+ Assert.assertTrue(model.containsLiteral(r, RDF.value,
+ tvFloat));
+ }
+
+ @Test
+ public void testChangeObjectInt()
+ {
+ Resource r = resource();
+ final Statement sTrue = loadInitialStatement(r);
+ final Statement changed = sTrue
+ .changeLiteralObject(tvInt);
+ checkChangedStatementSP(r,changed);
+ Assert.assertEquals(
+ model.createTypedLiteral(tvInt),
+ changed.getObject());
+ Assert.assertEquals(tvInt, changed.getInt());
+ checkCorrectStatements(r,sTrue, changed);
+ Assert.assertTrue(model.containsLiteral(r, RDF.value,
+ tvInt));
+ }
+
+ @Test
+ public void testChangeObjectLiteral()
+ {
+ Resource r = resource();
+ final Statement sTrue = loadInitialStatement(r);
+ model.remove(sTrue);
+ Assert.assertFalse(model.contains(sTrue));
+ Assert.assertFalse(model.containsLiteral(r, RDF.value, true));
+ }
+
+ // public void testResObj()
+ // {
+ // Resource tvResObj = model.createResource( new ResTestObjF() );
+ // assertEquals( tvResObj, model.createStatement( r, RDF.value, tvResObj
+ // ).getResource() );
+ // }
+
+ // public void testLitObj()
+ // {
+ // assertEquals( tvLitObj, model.createLiteralStatement( r, RDF.value,
+ // tvLitObj ).getObject( new LitTestObjF() ) );
+ // }
+
+ @Test
+ public void testChangeObjectLong()
+ {
+ Resource r = resource();
+ final Statement sTrue = loadInitialStatement(r);
+ final Statement changed = sTrue
+ .changeLiteralObject(tvLong);
+ checkChangedStatementSP(r,changed);
+ Assert.assertEquals(
+ model.createTypedLiteral(tvLong),
+ changed.getObject());
+ Assert.assertEquals(tvLong, changed.getLong());
+ checkCorrectStatements(r,sTrue, changed);
+ Assert.assertTrue(model.containsLiteral(r, RDF.value,
+ tvLong));
+ }
+
+ @Test
+ public void testChangeObjectShort()
+ {
+ Resource r = resource();
+ final Statement sTrue = loadInitialStatement(r);
+ final Statement changed = sTrue
+ .changeLiteralObject(tvShort);
+ checkChangedStatementSP(r,changed);
+ Assert.assertEquals(
+ model.createTypedLiteral(tvShort),
+ changed.getObject());
+ Assert.assertEquals(tvShort, changed.getShort());
+ checkCorrectStatements(r,sTrue, changed);
+ Assert.assertTrue(model.containsLiteral(r, RDF.value,
+ tvShort));
+ }
+
+ @Test
+ public void testChangeObjectString()
+ {
+ Resource r = resource();
+ final Statement sTrue = loadInitialStatement(r);
+ final Statement changed = sTrue
+ .changeObject(tvString);
+ checkChangedStatementSP(r,changed);
+ Assert.assertEquals(tvString, changed.getString());
+ checkCorrectStatements(r,sTrue, changed);
+ Assert.assertTrue(model.contains(r, RDF.value,
+ tvString));
+ }
+
+ @Test
+ public void testChangeObjectStringWithLanguage()
+ {
+ Resource r = resource();
+ final String lang = "en";
+ final Statement sTrue = loadInitialStatement(r);
+ final Statement changed = sTrue.changeObject(
+ tvString, lang);
+ checkChangedStatementSP(r,changed);
+ Assert.assertEquals(tvString, changed.getString());
+ Assert.assertEquals(lang, changed.getLanguage());
+ checkCorrectStatements(r,sTrue, changed);
+ Assert.assertTrue(model.contains(r, RDF.value,
+ tvString, lang));
+ }
+
+ @Test
+ public void testChangeObjectYByte()
+ {
+ Resource r = resource();
+ final Statement sTrue = loadInitialStatement(r);
+ final Statement changed = sTrue
+ .changeLiteralObject(tvByte);
+ checkChangedStatementSP(r,changed);
+ Assert.assertEquals(tvByte, changed.getByte());
+ checkCorrectStatements(r,sTrue, changed);
+ Assert.assertTrue(model.containsLiteral(r, RDF.value,
+ tvByte));
+ }
+
+ @Test
+ public void testLiteralStatementChar()
+ {
+ Resource r = resource();
+ final Statement s = model.createLiteralStatement(r, RDF.value,
+ tvChar);
+ Assert.assertEquals(
+ model.createTypedLiteral(tvChar),
+ s.getObject());
+ Assert.assertEquals(tvChar, s.getChar());
+ }
+
+ @Test
+ public void testLiteralStatementDouble()
+ {
+ Resource r = resource();
+ final Statement s = model.createLiteralStatement(r, RDF.value,
+ tvDouble);
+ Assert.assertEquals(
+ model.createTypedLiteral(tvDouble),
+ s.getObject());
+ Assert.assertEquals(tvDouble, s.getDouble(),
+ dDelta);
+ }
+
+ @Test
+ public void testLiteralStatementFloat()
+ {
+ Resource r = resource();
+ final Statement s = model.createLiteralStatement(r, RDF.value,
+ tvFloat);
+ Assert.assertEquals(
+ model.createTypedLiteral(tvFloat),
+ s.getObject());
+ Assert.assertEquals(tvFloat, s.getFloat(),
+ fDelta);
+ }
+
+ @Test
+ public void testGetLiteralFailure()
+ {
+ Resource r = resource();
+ try
+ {
+ model.createStatement(r, RDF.value, r).getLiteral();
+ Assert.fail("should trap non-literal object");
+ }
+ catch (final LiteralRequiredException e)
+ {
+ // expected
+ }
+ }
+
+ @Test
+ public void testGetResourceFailure()
+ {
+ Resource r = resource();
+ try
+ {
+ model.createLiteralStatement(r, RDF.value, false).getResource();
+ Assert.fail("should trap non-resource object");
+ }
+ catch (final ResourceRequiredException e)
+ {
+ // expected
+ }
+ }
+
+ @Test
+ public void testGetTrueBoolean()
+ {
+ Resource r = resource();
+ Assert.assertEquals(true,
+ model.createLiteralStatement(r, RDF.value, true).getLiteral()
+ .getBoolean());
+ }
+
+ @Test
+ public void testInt()
+ {
+ Resource r = resource();
+ final Statement s = model.createLiteralStatement(r, RDF.value,
+ tvInt);
+ Assert.assertEquals(
+ model.createTypedLiteral(tvInt),
+ s.getObject());
+ Assert.assertEquals(tvInt, s.getInt());
+ }
+
+ // public void testChangeObjectResObject()
+ // {
+ // Resource tvResObj = model.createResource( new ResTestObjF() );
+ // Statement sTrue = loadInitialStatement();
+ // Statement changed = sTrue.changeObject( tvResObj );
+ // checkChangedStatementSP( changed );
+ // assertEquals( tvResObj, changed.getResource() );
+ // checkCorrectStatements( sTrue, changed );
+ // assertTrue( model.contains( r, RDF.value, tvResObj ) );
+ // }
+
+ @Test
+ public void testLiteralStatementLong()
+ {
+ Resource r = resource();
+ final Statement s = model.createLiteralStatement(r, RDF.value,
+ tvLong);
+ Assert.assertEquals(
+ model.createTypedLiteral(tvLong),
+ s.getObject());
+ Assert.assertEquals(tvLong, s.getLong());
+ }
+
+ @Test
+ public void testCreateSeq()
+ {
+ Resource r = resource();
+ final Seq tvSeq = model.createSeq();
+ Assert.assertEquals(tvSeq, model.createStatement(r, RDF.value, tvSeq)
+ .getSeq());
+ }
+
+ @Test
+ public void testLiteralStatementShort()
+ {
+ Resource r = resource();
+ final Statement s = model.createLiteralStatement(r, RDF.value,
+ tvShort);
+ Assert.assertEquals(
+ model.createTypedLiteral(tvShort),
+ s.getObject());
+ Assert.assertEquals(tvShort, s.getShort());
+ }
+
+ static class OL extends ObjectListener
+ {
+ private Object recorded;
+ private String how;
+
+ @Override
+ public void added( final Object x )
+ {
+ recorded = x;
+ how = "add";
+ }
+
+ private Object comparable( final Object x )
+ {
+ if (x instanceof Statement[])
+ {
+ return Arrays.asList((Statement[]) x);
+ }
+ if (x instanceof Iterator<?>)
+ {
+ return iteratorToList((Iterator<?>) x);
+ }
+ return x;
+ }
+
+ public void recent( final String wantHow, final Object value )
+ {
+ Assert.assertTrue(RecordingModelListener.checkEquality(
+ comparable(value), comparable(recorded)));
+ // Assert.assertEquals(comparable(value), comparable(recorded));
+ Assert.assertEquals(wantHow, how);
+ recorded = how = null;
+ }
+
+ @Override
+ public void removed( final Object x )
+ {
+ recorded = x;
+ how = "rem";
+ }
+ }
+
+ /**
+ * Local test class to see that a StatementListener funnels all the changes
+ * through
+ * add/remove a single ModelHelper.statement
+ */
+ public static class WatchStatementListener extends StatementListener
+ {
+ List<Statement> statements = new ArrayList<Statement>();
+ String addOrRem = "<unset>";
+
+ @Override
+ public void addedStatement( final Statement s )
+ {
+ statements.add(s);
+ addOrRem = "add";
+ }
+
+ public List<Statement> contents()
+ {
+ try
+ {
+ return statements;
+ }
+ finally
+ {
+ statements = new ArrayList<Statement>();
+ }
+ }
+
+ public String getAddOrRem()
+ {
+ return addOrRem;
+ }
+
+ @Override
+ public void removedStatement( final Statement s )
+ {
+ statements.add(s);
+ addOrRem = "rem";
+ }
+ }
+
+ protected RecordingModelListener SL;
+
+ private void another( final Map<Object, Integer> m, final Object x )
+ {
+ Integer n = m.get(x);
+ if (n == null)
+ {
+ n = new Integer(0);
+ }
+ m.put(x, new Integer(n.intValue() + 1));
+ }
+
+ private Map<Object, Integer> asBag( final List<Statement> l )
+ {
+ final Map<Object, Integer> result = new HashMap<Object, Integer>();
+ for (int i = 0; i < l.size(); i += 1)
+ {
+ another(result, l.get(i).asTriple());
+ }
+ return result;
+ }
+
+ private StmtIterator asIterator( final Statement[] statements )
+ {
+ return new StmtIteratorImpl(Arrays.asList(statements).iterator());
+ }
+
+ private void assertSameBag( final List<Statement> wanted,
+ final List<Statement> got )
+ {
+
+ Assert.assertEquals(asBag(wanted), asBag(got));
+ }
+
+ @Test
+ public void testAddInPieces()
+ {
+ model.register(SL);
+ model.add(resource(model, "S"),
+ property(model, "P"),
+ resource(model, "O"));
+ SL.assertHas(new Object[] { "add",
+ statement(model, "S P O") });
+ }
+
+ @Test
+ public void testAddSingleStatements()
+ {
+ final Statement S1 = statement(model, "S P O");
+ final Statement S2 = statement(model, "A B C");
+ Assert.assertFalse(SL.has(new Object[] { "add", S1 }));
+ model.register(SL);
+ model.add(S1);
+ SL.assertHas(new Object[] { "add", S1 });
+ model.add(S2);
+ SL.assertHas(new Object[] { "add", S1, "add", S2 });
+ model.add(S1);
+ SL.assertHas(new Object[] { "add", S1, "add", S2, "add", S1 });
+ }
+
+ @Test
+ public void testAddStatementArray()
+ {
+ model.register(SL);
+ final Statement[] s = statements(model, "a P b; c Q d");
+ model.add(s);
+ SL.assertHas(new Object[] { "add[]", Arrays.asList(s) });
+ }
+
+ @Test
+ public void testAddStatementIterator()
+ {
+ model.register(SL);
+ final Statement[] sa = statements(model,
+ "x R y; a P b; x R y");
+ final StmtIterator it = asIterator(sa);
+ model.add(it);
+ SL.assertHas(new Object[] { "addIterator", Arrays.asList(sa) });
+ }
+
+ @Test
+ public void testAddStatementList()
+ {
+ model.register(SL);
+ final List<Statement> L = Arrays.asList(statements(model,
+ "b I g; model U g"));
+ model.add(L);
+ SL.assertHas(new Object[] { "addList", L });
+ }
+
+ @Test
+ public void testChangedListener()
+ {
+ final ChangedListener CL = new ChangedListener();
+ model.register(CL);
+ Assert.assertFalse(CL.hasChanged());
+ model.add(statement(model, "S P O"));
+ Assert.assertTrue(CL.hasChanged());
+ Assert.assertFalse(CL.hasChanged());
+ model.remove(statement(model, "ab CD ef"));
+ Assert.assertTrue(CL.hasChanged());
+ model.add(statements(model, "gh IJ kl"));
+ Assert.assertTrue(CL.hasChanged());
+ model.remove(statements(model, "mn OP qr"));
+ Assert.assertTrue(CL.hasChanged());
+ model.add(asIterator(statements(model, "st UV wx")));
+ Assert.assertTrue(CL.hasChanged());
+ Assert.assertFalse(CL.hasChanged());
+ model.remove(asIterator(statements(model, "yz AB cd")));
+ Assert.assertTrue(CL.hasChanged());
+ model.add(modelWithStatements(getModelProducer(), "ef GH ij"));
+ Assert.assertTrue(CL.hasChanged());
+ model.remove(modelWithStatements(getModelProducer(), "kl MN op"));
+ Assert.assertTrue(CL.hasChanged());
+ model.add(Arrays.asList(statements(model, "rs TU vw")));
+ Assert.assertTrue(CL.hasChanged());
+ model.remove(Arrays.asList(statements(model, "xy wh q")));
+ Assert.assertTrue(CL.hasChanged());
+ }
+
+ @Test
+ public void testDeleteModel()
+ {
+ model.register(SL);
+ final Model m = modelWithStatements(getModelProducer(),
+ "NT beats S; S beats H; H beats D");
+ model.remove(m);
+ SL.assertHas(new Object[] { "removeModel", m });
+ }
+
+ @Test
+ public void testDeleteStatementArray()
+ {
+ model.register(SL);
+ final Statement[] s = statements(model, "a P b; c Q d");
+ model.remove(s);
+ SL.assertHas(new Object[] { "remove[]", Arrays.asList(s) });
+ }
+
+ @Test
+ public void testDeleteStatementIterator()
+ {
+ model.register(SL);
+ final Statement[] sa = statements(model,
+ "x R y; a P b; x R y");
+ final StmtIterator it = asIterator(sa);
+ model.remove(it);
+ SL.assertHas(new Object[] { "removeIterator", Arrays.asList(sa) });
+ }
+
+ @Test
+ public void testDeleteStatementList()
+ {
+ model.register(SL);
+ final List<Statement> lst = Arrays.asList(statements(model,
+ "b I g; model U g"));
+ model.remove(lst);
+ SL.assertHas(new Object[] { "removeList", lst });
+ }
+
+ @Test
+ public void testGeneralEvent()
+ {
+ model.register(SL);
+ final Object e = new int[] {};
+ model.notifyEvent(e);
+ SL.assertHas(new Object[] { "someEvent", model, e });
+ }
+
+ private void testGot( final WatchStatementListener sl, final String how,
+ final String template )
+ {
+ assertSameBag(Arrays.asList(statements(model, template)),
+ sl.contents());
+ Assert.assertEquals(how, sl.getAddOrRem());
+ Assert.assertTrue(sl.contents().size() == 0);
+ }
+
+ /**
+ * Test that the null listener doesn't appear to do anything. Or at least
+ * doesn't crash ....
+ */
+ @Test
+ public void testNullListener()
+ {
+ final ModelChangedListener NL = new NullListener();
+ model.register(NL);
+ model.add(statement(model, "S P O "));
+ model.remove(statement(model, "X Y Z"));
+ model.add(statements(model, "a B c; d E f"));
+ model.remove(statements(model, "g H i; j K l"));
+ model.add(asIterator(statements(model, "model N o; p Q r")));
+ model.remove(asIterator(statements(model, "s T u; v W x")));
+ model.add(modelWithStatements(getModelProducer(), "leaves fall softly"));
+ model.remove(modelWithStatements(getModelProducer(),
+ "water drips endlessly"));
+ model.add(Arrays.asList(statements(model, "xx RR yy")));
+ model.remove(Arrays.asList(statements(model, "aa VV rr")));
+ }
+
+ @Test
+ public void testObjectListener()
+ {
+ final OL ll = new OL();
+ model.register(ll);
+ final Statement s = statement(model, "aa BB cc"), s2 = statement(model, "dd EE ff");
+ model.add(s);
+ ll.recent("add", s);
+ model.remove(s2);
+ ll.recent("rem", s2);
+ /* */
+ final List<Statement> sList = Arrays.asList(statements(
+ model, "gg HH ii; jj KK ll"));
+ model.add(sList);
+ ll.recent("add", sList);
+ final List<Statement> sList2 = Arrays.asList(statements(
+ model, "mm NN oo; pp QQ rr; ss TT uu"));
+ model.remove(sList2);
+ ll.recent("rem", sList2);
+ /* */
+ final Model m1 = modelWithStatements(getModelProducer(),
+ "vv WW xx; yy ZZ aa");
+ model.add(m1);
+ ll.recent("add", m1);
+ final Model m2 = modelWithStatements(getModelProducer(), "a B g; d E z");
+ model.remove(m2);
+ ll.recent("rem", m2);
+ /* */
+ final Statement[] sa1 = statements(model,
+ "th i k; l model n");
+ model.add(sa1);
+ ll.recent("add", sa1);
+ final Statement[] sa2 = statements(model, "x o p; r u ch");
+ model.remove(sa2);
+ ll.recent("rem", sa2);
+ /* */
+ final Statement[] si1 = statements(model,
+ "u ph ch; psi om eh");
+ model.add(asIterator(si1));
+ ll.recent("add", asIterator(si1));
+ final Statement[] si2 = statements(model,
+ "at last the; end of these; tests ok guv");
+ model.remove(asIterator(si2));
+ ll.recent("rem", asIterator(si2));
+ }
+
+ @Test
+ public void testRegistrationCompiles()
+ {
+ Assert.assertSame(model, model.register(new RecordingModelListener()));
+ }
+
+ @Test
+ public void testRemoveSingleStatements()
+ {
+ final Statement S = statement(model, "D E F");
+ model.register(SL);
+ model.add(S);
+ model.remove(S);
+ SL.assertHas(new Object[] { "add", S, "remove", S });
+ }
+
+ @Test
+ public void testTripleListener()
+ {
+ final WatchStatementListener sl = new WatchStatementListener();
+ model.register(sl);
+ model.add(statement(model, "b C d"));
+ testGot(sl, "add", "b C d");
+ model.remove(statement(model, "e F g"));
+ testGot(sl, "rem", "e F g");
+ /* */
+ model.add(statements(model, "h I j; k L model"));
+ testGot(sl, "add", "h I j; k L model");
+ model.remove(statements(model, "n O p; q R s"));
+ testGot(sl, "rem", "n O p; q R s");
+ /* */
+ model.add(Arrays.asList(statements(model, "t U v; w X y")));
+ testGot(sl, "add", "t U v; w X y");
+ model.remove(Arrays.asList(statements(model, "z A b; c D e")));
+ testGot(sl, "rem", "z A b; c D e");
+ /* */
+ model.add(asIterator(statements(model, "f G h; i J k")));
+ testGot(sl, "add", "f G h; i J k");
+ model.remove(asIterator(statements(model, "l M n; o P q")));
+ testGot(sl, "rem", "l M n; o P q");
+ /* */
+ model.add(modelWithStatements(getModelProducer(), "r S t; u V w; x Y z"));
+ testGot(sl, "add", "r S t; u V w; x Y z");
+ model.remove(modelWithStatements(getModelProducer(), "a E i; o U y"));
+ testGot(sl, "rem", "a E i; o U y");
+ }
+
+ @Test
+ public void testTwoListeners()
+ {
+ final Statement S = statement(model, "S P O");
+ final RecordingModelListener SL1 = new RecordingModelListener();
+ final RecordingModelListener SL2 = new RecordingModelListener();
+ model.register(SL1).register(SL2);
+ model.add(S);
+ SL2.assertHas(new Object[] { "add", S });
+ SL1.assertHas(new Object[] { "add", S });
+ }
+
+ @Test
+ public void testUnregisterWorks()
+ {
+ model.register(SL);
+ model.unregister(SL);
+ model.add(statement(model, "X R Y"));
+ SL.assertHas(new Object[] {});
+ }
+
+ @Test
+ public void testUnregistrationCompiles()
+ {
+ model.unregister(new RecordingModelListener());
+ }
+
+ @Test
+ public void testCreateInfModel()
+ {
+ final String rule = "-> (eg:r eg:p eg:v).";
+ final Reasoner r = new GenericRuleReasoner(Rule.parseRules(rule));
+ final InfGraph ig = r.bind(model.getGraph());
+ final InfModel im = ModelFactory.createInfModel(ig);
+ assertInstanceOf(InfModel.class, im);
+ Assert.assertEquals(1, im.size());
+ }
+
+ //TODO add additional tests here
+// @Test
+// public void testAssembleModelFromModel()
+// {
+// Assert.assertNotNull( ModelFactory.assembleModelFrom( ModelFactory.createDefaultModel()) );
+// }
+
+// public void testFindAssemblerRoots()
+// {
+// // TODO Set ModelFactory.findAssemblerRoots( Model model )
+// }
+
+ @Test
+ public void testPoly()
+ {
+ final Resource r = model
+ .createResource("http://www.electric-hedgehog.net/a-o-s.html");
+ Assert.assertFalse("the Resouce should not be null", r == null);
+ Assert.assertTrue("the Resource can be a Property",
+ r.canAs(Property.class));
+ final Property p = r.as(Property.class);
+ Assert.assertFalse("the Property should not be null", p == null);
+ Assert.assertFalse("the Resource and Property should not be identical",
+ r == p);
+ }
+
+ @Test
+ public void testContentNegotiation()
+ {
+ try
+ {
+ model.read("http://jena.sourceforge.net/test/mime/test1");
+ Assert.assertEquals(model.size(), 1);
+ }
+ catch (final JenaException jx)
+ {
+ if ((jx.getCause() instanceof NoRouteToHostException)
+ || (jx.getCause() instanceof UnknownHostException)
+ || (jx.getCause() instanceof ConnectException)
+ || (jx.getCause() instanceof IOException))
+ {
+ log.warn("Cannot access public internet - content negotiation test not executed");
+ }
+ else
+ {
+ throw jx;
+ }
+ }
+ }
+
+ @Test
+ public void testDefaultLangXML() throws FileNotFoundException
+ {
+ model.read(getFileName( "modelReading/plain.rdf"), null, null);
+ }
+
+ @Test
+ public void testGRDDLConfigMessage()
+ {
+ try
+ {
+ model.read("http://www.w3.org/", "GRDDL");
+ // ok.
+ }
+ catch (final ConfigException e)
+ {
+ // expected.
+ }
+ }
+
+ /*
+ * Suppressed, since the other Model::read(String url) operations apparently
+ * don't retry failing URLs as filenames. But the code text remains, so that
+ * when-and-if, we have a basis.
+ */
+ // public void testLoadsSimpleModelWithoutProtocol()
+ // {
+ // Model expected = ModelFactory.createDefaultModel();
+ // Model model = ModelFactory.createDefaultModel();
+ // expected.read( "testing/modelReading/simple.n3", "RDF/XML" );
+ // assertSame( model, model.read( "testing/modelReading/simple.n3", "base",
+ // "N3" ) );
+ // assertIsoModels( expected, model );
+ // }
+
+ @Test
+ public void testLoadsSimpleModel() throws FileNotFoundException
+ {
+ final Model expected = getModelProducer().newModel();
+ expected.read( getFileName("modelReading/simple.n3"), "N3");
+ Assert.assertSame(model,
+ model.read( getFileName("modelReading/simple.n3"), "base", "N3"));
+ assertIsoModels(expected, model);
+ }
+
+ @Test
+ public void testReturnsSelf() throws FileNotFoundException
+ {
+
+ Assert.assertSame(model,
+ model.read( getFileName("modelReading/empty.n3"), "base", "N3"));
+ Assert.assertTrue(model.isEmpty());
+ }
+
+ @Test
+ public void testSimpleLoadExplicitBase() throws FileNotFoundException
+ {
+ final Model mBasedExplicit = getModelProducer().newModel();
+ mBasedExplicit.read( getFileName("modelReading/based.n3"),
+ "http://example/", "N3");
+ assertIsoModels(modelWithStatements(getModelProducer(),
+ "http://example/ ja:predicate ja:object"), mBasedExplicit);
+ }
+
+ @Test
+ public void testSimpleLoadImplictBase() throws IRIException, FileNotFoundException
+ {
+ final Model mBasedImplicit = getModelProducer().newModel();
+ final String fn = IRIResolver
+ .resolveFileURL( getFileName("modelReading/based.n3"));
+ final Model wanted = getModelProducer().newModel().add(resource(fn),
+ property("ja:predicate"),
+ resource("ja:object"));
+ mBasedImplicit.read(fn, "N3");
+ assertIsoModels(wanted, mBasedImplicit);
+ }
+
+ @Test
+ public void testDifference()
+ {
+ Model model2 = getModelProducer().newModel();
+ modelAdd(model, "a P b; w R x");
+ modelAdd(model2, "w R x; y S z");
+ final Model dm = model.difference(model2);
+ for (final StmtIterator it = dm.listStatements(); it.hasNext();)
+ {
+ final Statement s = it.nextStatement();
+ Assert.assertTrue(model.contains(s) && !model2.contains(s));
+ }
+ for (final StmtIterator it = model.union(model2).listStatements(); it
+ .hasNext();)
+ {
+ final Statement s = it.nextStatement();
+ Assert.assertEquals(model.contains(s) && !model2.contains(s),
+ dm.contains(s));
+ }
+ Assert.assertTrue(dm.containsAny(model));
+ Assert.assertTrue(dm.containsAny(model.listStatements()));
+ Assert.assertFalse(dm.containsAny(model2));
+ Assert.assertFalse(dm.containsAny(model2.listStatements()));
+ Assert.assertTrue(model.containsAll(dm));
+
+ }
+
+ @Test
+ public void testIntersection()
+ {
+ Model model2 = getModelProducer().newModel();
+ modelAdd(model, "a P b; w R x");
+ modelAdd(model2, "w R x; y S z");
+ final Model im = model.intersection(model2);
+ Assert.assertFalse(model.containsAll(model2));
+ Assert.assertFalse(model2.containsAll(model));
+ Assert.assertTrue(model.containsAll(im));
+ Assert.assertTrue(model2.containsAll(im));
+ for (final StmtIterator it = im.listStatements(); it.hasNext();)
+ {
+ final Statement s = it.nextStatement();
+ Assert.assertTrue(model.contains(s) && model2.contains(s));
+ }
+ for (final StmtIterator it = im.listStatements(); it.hasNext();)
+ {
+ Assert.assertTrue(model.contains(it.nextStatement()));
+ }
+ for (final StmtIterator it = im.listStatements(); it.hasNext();)
+ {
+ Assert.assertTrue(model2.contains(it.nextStatement()));
+ }
+ Assert.assertTrue(model.containsAll(im.listStatements()));
+ Assert.assertTrue(model2.containsAll(im.listStatements()));
+
+ }
+
+ @Test
+ public void testUnion()
+ {
+ Model model2 = getModelProducer().newModel();
+ modelAdd(model, "a P b; w R x");
+ modelAdd(model2, "w R x; y S z");
+ final Model um = model.union(model2);
+ Assert.assertFalse(model.containsAll(model2));
+ Assert.assertFalse(model2.containsAll(model));
+ Assert.assertTrue(um.containsAll(model));
+ Assert.assertTrue(um.containsAll(model2));
+ for (final StmtIterator it = um.listStatements(); it.hasNext();)
+ {
+ final Statement s = it.nextStatement();
+ Assert.assertTrue(model.contains(s) || model2.contains(s));
+ }
+ for (final StmtIterator it = model.listStatements(); it.hasNext();)
+ {
+ Assert.assertTrue(um.contains(it.nextStatement()));
+ }
+ for (final StmtIterator it = model2.listStatements(); it.hasNext();)
+ {
+ Assert.assertTrue(um.contains(it.nextStatement()));
+ }
+ Assert.assertTrue(um.containsAll(model.listStatements()));
+ Assert.assertTrue(um.containsAll(model2.listStatements()));
+
+ }
+
+
+ /**
+ * turn a semi-separated set of P=U definitions into a namespace map.
+ */
+ private Map<String, Set<String>> makePrefixes( final String prefixes )
+ {
+ final Map<String, Set<String>> result = new HashMap<String, Set<String>>();
+ final StringTokenizer st = new StringTokenizer(prefixes, ";");
+ while (st.hasMoreTokens())
+ {
+ final String def = st.nextToken();
+ // System.err.println( "| def is " + def );
+ final int eq = def.indexOf('=');
+ result.put(def.substring(0, eq), set(def.substring(eq + 1)));
+ }
+ // result.put( "spoo", set( "http://spoo.net/" ) );
+ return result;
+ }
+
+ /**
+ * make a single-element set.
+ *
+ * @param element
+ * the single element to contain
+ * @return a set whose only element == element
+ */
+ private Set<String> set( final String element )
+ {
+ final Set<String> s = CollectionFactory.createHashedSet();
+ s.add(element);
+ return s;
+ }
+
+ /**
+ * a simple test of the prefix reader on a known file. test0014.rdf is known
+ * to
+ * have a namespace definition for eg and rdf, and not for spoo so we see if
+ * we
+ * can extract them (or not, for spoo).
+ */
+ @Test
+ public void testReadPrefixes()
+ {
+ model.read(getFileName( "wg/rdf-ns-prefix-confusion/test0014.rdf" ));
+ final Map<String, String> ns = model.getNsPrefixMap();
+ // System.err.println( ">> " + ns );
+ Assert.assertEquals("namespace eg", "http://example.org/", ns.get("eg"));
+ Assert.assertEquals("namespace rdf",
+ "http://www.w3.org/1999/02/22-rdf-syntax-ns#", ns.get("rdf"));
+ Assert.assertEquals("not present", null, ns.get("spoo"));
+ }
+
+ @Test
+ public void testUseEasyPrefix()
+ {
+ AbstractPrefixMappingTest.testUseEasyPrefix(model);
+ }
+
+ /**
+ * a horridly written test to write out a model with some known namespace
+ * prefixes and see if they can be read back in again.
+ *
+ * TODO tidy and abstract this - we want some more tests.
+ *
+ * TODO there's a problem: namespaces that aren't used on properties
+ * don't reliably get used. Maybe they shouldn't be - but it seems odd.
+ */
+ @Test
+ public void testWritePrefixes() throws IOException
+ {
+ ModelCom.addNamespaces(model,
+ makePrefixes("fred=ftp://net.fred.org/;spoo=http://spoo.net/"));
+ final File f = File.createTempFile("hedgehog", ".rdf");
+ model.add(statement(model,
+ "http://spoo.net/S http://spoo.net/P http://spoo.net/O"));
+ model.add(statement(model,
+ "http://spoo.net/S ftp://net.fred.org/P http://spoo.net/O"));
+ model.write(new FileOutputStream(f));
+ /* */
+ final Model m2 = ModelFactory.createDefaultModel();
+ m2.read("file:" + f.getAbsolutePath());
+ final Map<String, String> ns = m2.getNsPrefixMap();
+ Assert.assertEquals("namespace spoo", "http://spoo.net/",
+ ns.get("spoo"));
+ Assert.assertEquals("namespace fred", "ftp://net.fred.org/",
+ ns.get("fred"));
+ /* */
+ f.deleteOnExit();
+ }
+
+
+ public void checkReturns( final String things, final StmtIterator it )
+ {
+ final Model wanted = modelWithStatements(getModelProducer(), things);
+ final Model got = modelWithStatements(getModelProducer(),it);
+ if (wanted.isIsomorphicWith(got) == false)
+ {
+ Assert.fail("wanted " + wanted + " got " + got);
+ }
+ }
+
+ private void setUpList( Model model ) throws Exception
+ {
+
+ model.createResource("http://example.org/boolean").addLiteral(
+ RDF.value, booleanValue);
+ model.createResource("http://example.org/char").addLiteral(RDF.value,
+ charValue);
+ model.createResource("http://example.org/long").addLiteral(RDF.value,
+ longValue);
+ model.createResource("http://example.org/float").addLiteral(RDF.value,
+ floatValue);
+ model.createResource("http://example.org/double").addLiteral(RDF.value,
+ doubleValue);
+ model.createResource("http://example.org/string").addProperty(
+ RDF.value, stringValue);
+ model.createResource("http://example.org/langString").addProperty(
+ RDF.value, stringValue,
+ langValue);
+
+ }
+
+ @Test
+ public void testListStatementsAll() throws Exception
+ {
+ setUpList( model );
+ final StmtIterator iter = model.listStatements(null, null,
+ (RDFNode) null);
+ int i = 0;
+ while (iter.hasNext())
+ {
+ i++;
+ iter.next();
+ }
+ Assert.assertEquals(7, i);
+ }
+
+ @Test
+ public void testAllString() throws Exception
+ {
+ setUpList( model );
+ final StmtIterator iter = model.listStatements(null, null,
+ (String) null);
+ int i = 0;
+ while (iter.hasNext())
+ {
+ i++;
+ iter.next();
+ }
+ Assert.assertEquals(7, i);
+ }
+
+ @Test
+ public void testBoolean() throws Exception
+ {
+ setUpList( model );
+ final List<Statement> got = model.listLiteralStatements(null, null,
+ booleanValue).toList();
+ Assert.assertEquals(1, got.size());
+ final Statement it = got.get(0);
+ Assert.assertEquals(resource("http://example.org/boolean"),
+ it.getSubject());
+ Assert.assertEquals(
+ model.createTypedLiteral(booleanValue),
+ it.getObject());
+ }
+
+ @Test
+ public void testChar() throws Exception
+ {
+ setUpList( model );
+ final List<Statement> got = model.listLiteralStatements(null, null,
+ charValue).toList();
+ Assert.assertEquals(1, got.size());
+ final Statement it = got.get(0);
+ Assert.assertEquals(resource("http://example.org/char"),
+ it.getSubject());
+ Assert.assertEquals(
+ model.createTypedLiteral(charValue),
+ it.getObject());
+ }
+
+ @Test
+ public void testDouble() throws Exception
+ {
+ setUpList( model );
+ final List<Statement> got = model.listLiteralStatements(null, null,
+ doubleValue).toList();
+ Assert.assertEquals(1, got.size());
+ final Statement it = got.get(0);
+ Assert.assertEquals(resource("http://example.org/double"),
+ it.getSubject());
+ Assert.assertEquals(
+ model.createTypedLiteral(doubleValue),
+ it.getObject());
+ }
+
+ @Test
+ public void testFloat() throws Exception
+ {
+ setUpList( model );
+ final List<Statement> got = model.listLiteralStatements(null, null,
+ floatValue).toList();
+ Assert.assertEquals(1, got.size());
+ final Statement it = got.get(0);
+ Assert.assertEquals(resource("http://example.org/float"),
+ it.getSubject());
+ Assert.assertEquals(
+ model.createTypedLiteral(floatValue),
+ it.getObject());
+ }
+
+ @Test
+ public void testLangString() throws Exception
+ {
+ setUpList( model );
+ final StmtIterator iter = model.listStatements(null, null,
+ stringValue,
+ langValue);
+ int i = 0;
+ while (iter.hasNext())
+ {
+ i++;
+ Assert.assertEquals(iter.nextStatement().getSubject().getURI(),
+ "http://example.org/langString");
+ }
+ Assert.assertEquals(1, i);
+ }
+
+ @Test
+ public void testListStatementsClever() throws Exception
+ {
+ setUpList( model );
+ modelAdd(model, "S P O; S P O2; S P2 O; S2 P O");
+ final Selector sel = new SimpleSelector(null, null, (RDFNode) null) {
+ @Override
+ public boolean isSimple()
+ {
+ return false;
+ }
+
+ @Override
+ public boolean test( final Statement st )
+ {
+ return (st.getSubject().toString().length()
+ + st.getPredicate().toString().length() + st
+ .getObject().toString().length()) == 15; /*
+ * eh:/S + eh:/P
+ * + eh:/O
+ */
+ }
+ };
+ checkReturns("S P O", model.listStatements(sel));
+ }
+
+ @Test
+ public void testListStatementsSPO() throws Exception
+ {
+ setUpList( model );
+ final Resource A = resource(model, "A"), X = resource(model, "X");
+ final Property P = property(model, "P"), P1 = property(model, "P1");
+ final RDFNode O = resource(model, "O"), Y = resource(model, "Y");
+ final String S1 = "S P O; S1 P O; S2 P O";
+ final String S2 = "A P1 B; A P1 B; A P1 C";
+ final String S3 = "X P1 Y; X P2 Y; X P3 Y";
+ modelAdd(model, S1);
+ modelAdd(model, S2);
+ modelAdd(model, S3);
+ checkReturns(S1, model.listStatements(null, P, O));
+ checkReturns(S2, model.listStatements(A, P1, (RDFNode) null));
+ checkReturns(S3, model.listStatements(X, null, Y));
+ }
+
+ @Test
+ public void testLong() throws Exception
+ {
+ setUpList( model );
+ final List<Statement> got = model.listLiteralStatements(null, null,
+ longValue).toList();
+ Assert.assertEquals(1, got.size());
+ final Statement it = got.get(0);
+ Assert.assertEquals(resource("http://example.org/long"),
+ it.getSubject());
+ Assert.assertEquals(
+ model.createTypedLiteral(longValue),
+ it.getObject());
+ }
+
+ @Test
+ public void testString() throws Exception
+ {
+ setUpList( model );
+ final StmtIterator iter = model.listStatements(null, null,
+ stringValue);
+ int i = 0;
+ while (iter.hasNext())
+ {
+ i++;
+ Assert.assertEquals(iter.nextStatement().getSubject().getURI(),
+ "http://example.org/string");
+ }
+ Assert.assertEquals(1, i);
+ }
+
+ // FIXME
+ @Test
+ public void testN3ReaderEvents()
+ {
+ testReaderEvent("N3", "");
+ }
+
+ @Test
+ public void testNTriplesReaderEvents()
+ {
+ testReaderEvent("N-TRIPLE", "");
+ }
+
+ private void testReaderEvent( final String language, final String emptyModel )
+ {
+ final RecordingModelListener L = new RecordingModelListener();
+ model.register(L);
+ final RDFReader r = model.getReader(language);
+ final StringReader stringReader = new StringReader(emptyModel);
+ r.read(model, stringReader, "");
+ L.assertHasStart(new Object[] { "someEvent", model,
+ GraphEvents.startRead });
+ L.assertHasEnd(new Object[] { "someEvent", model,
+ GraphEvents.finishRead });
+ }
+
+ @Test
+ public void testXMLReaderEvents()
+ {
+ final String emptyModel = "<rdf:RDF xmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#'></rdf:RDF>";
+ testReaderEvent("RDF/XML", emptyModel);
+ }
+
+ /**
+ * Test to ensure that the reader is set.
+ */
+ @Test
+ public void testGetNTripleReader()
+ {
+ final RDFReader reader = model.getReader("N-TRIPLE");
+ Assert.assertNotNull(reader);
+ }
+
+ @Test
+ public void testMissingReader()
+ {
+ model.setReaderClassName("foobar", "");
+ try
+ {
+ model.getReader("foobar");
+ Assert.fail("Should have thrown NoReaderForLangException");
+ }
+ catch (final NoReaderForLangException expected)
+ {
+ // that's what we expect
+ }
+
+ try
+ {
+ model.setReaderClassName("foobar",
+ com.hp.hpl.jena.rdf.arp.JenaReader.class.getName());
+ final RDFReader reader = model.getReader("foobar");
+ Assert.assertTrue("Wrong reader type",
+ reader instanceof com.hp.hpl.jena.rdf.arp.JenaReader);
+ }
+ finally
+ {
+ // unset the reader
+ model.setReaderClassName("foobar", "");
+ }
+
+ }
+
+ @Test
+ public void testReadLocalNTriple() throws IOException
+ {
+ model.read(getInputStream("TestReaders.nt"), "", "N-TRIPLE");
+ Assert.assertEquals("Wrong size model", 5, model.size());
+ final StmtIterator iter = model.listStatements(null, null,
+ "foo\"\\\n\r\tbar");
+ Assert.assertTrue("No next statement found", iter.hasNext());
+
+ }
+
+ @Test
+ public void testReadLocalRDF() throws IOException
+ {
+ model.read(getInputStream("TestReaders.rdf"), "http://example.org/");
+ }
+
+ @Test
+ public void testReadRemoteNTriple()
+ {
+ try
+ {
+ model.read("http://www.w3.org/2000/10/rdf-tests/rdfcore/"
+ + "rdf-containers-syntax-vs-schema/test001.nt", "N-TRIPLE");
+ }
+ catch (final JenaException jx)
+ {
+ if ((jx.getCause() instanceof NoRouteToHostException)
+ || (jx.getCause() instanceof UnknownHostException)
+ || (jx.getCause() instanceof ConnectException)
+ || (jx.getCause() instanceof IOException))
+ {
+ Assert.fail("Cannot access public internet - part of test not executed");
+ }
+ else
+ {
+ throw jx;
+ }
+ }
+ }
+
+ @Test
+ public void testReadRemoteRDF()
+ {
+
+ try
+ {
+
+ model.read("http://www.w3.org/2000/10/rdf-tests/rdfcore/"
+ + "rdf-containers-syntax-vs-schema/test001.rdf");
+
+ }
+ catch (final JenaException jx)
+ {
+ if ((jx.getCause() instanceof NoRouteToHostException)
+ || (jx.getCause() instanceof UnknownHostException)
+ || (jx.getCause() instanceof ConnectException)
+ || (jx.getCause() instanceof IOException))
+ {
+ Assert.fail("Cannot access public internet - part of test not executed");
+ }
+ else
+ {
+ throw jx;
+ }
+ }
+
+ }
+
+ // FIXME ---
+
+ boolean tvBooleans[] = { false, true };
+ long tvLongs[] = { 123, 321 };
+ char tvChars[] = { '@', ';' };
+ double tvDoubles[] = { 123.456, 456.123 };
+ String tvStrings[] = { "testing string 1", "testing string 2" };
+ String langs[] = { "en", "fr" };
+ Literal tvLitObjs[];
+
+ // Resource tvResObjs[] = { model.createResource(new ResTestObjF()),
+ // model.createResource(new ResTestObjF()) };
+
+ final int num = 2;
+ Resource subject[] = new Resource[num];
+ Property predicate[] = new Property[num];
+
+ String suri = "http://aldabaran/test9/s";
+ String puri = "http://aldabaran/test9/";
+
+ private void setUpSelectorData( Model model ) throws Exception
+ {
+ final Literal tvLitObjs[] = {
+ model.createTypedLiteral(new LitTestObj(1)),
+ model.createTypedLiteral(new LitTestObj(2)) };
+
+ for (int i = 0; i < num; i += 1)
+ {
+ subject[i] = model.createResource(suri + i);
+ predicate[i] = model.createProperty(puri + i, "p");
+ }
+
+ for (int i = 0; i < num; i++)
+ {
+ for (int j = 0; j < num; j++)
+ {
+ model.addLiteral(subject[i], predicate[j], tvBooleans[j]);
+ model.addLiteral(subject[i], predicate[j], tvLongs[j]);
+ model.addLiteral(subject[i], predicate[j], tvChars[j]);
+ model.addLiteral(subject[i], predicate[j], tvDoubles[j]);
+ model.add(subject[i], predicate[j], tvStrings[j]);
+ model.add(subject[i], predicate[j], tvStrings[j], langs[j]);
+ model.add(subject[i], predicate[j], tvLitObjs[j]);
+ // model.add( subject[i], predicate[j], tvResObjs[j] );
+ }
+ }
+
+ }
+
+ @Test
+ public void testListWithLiteralSelector() throws Exception
+ {
+ setUpSelectorData( model );
+ final StmtIterator it6 = model.listStatements(new SimpleSelector(null,
+ null, tvStrings[1], langs[1]));
+ final List<Statement> L6 = iteratorToList(it6);
+ for (int i = 0; i < L6.size(); i += 1)
+ {
+ Assert.assertEquals(langs[1], L6.get(i).getLanguage());
+ }
+ Assert.assertEquals(2, L6.size());
+ }
+
+ @Test
+ public void testListWithNullSelector()throws Exception
+ {
+ setUpSelectorData( model );
+ final StmtIterator it1 = model.listStatements(new SimpleSelector(null,
+ null, (RDFNode) null));
+ final List<Statement> L1 = iteratorToList(it1);
+ Assert.assertEquals(num * num * 7, L1.size());
+
+ }
+
+ @Test
+ public void testListWithPredicateSelector()throws Exception
+ {
+ setUpSelectorData( model );
+ final StmtIterator it3 = model.listStatements(new SimpleSelector(null,
+ predicate[1], (RDFNode) null));
+ final List<Statement> L3 = iteratorToList(it3);
+ for (int i = 0; i < L3.size(); i += 1)
+ {
+ Assert.assertEquals(predicate[1], L3.get(i).getPredicate());
+ }
+ Assert.assertEquals(num * 7, L3.size());
+ }
+
+ // StmtIterator it4 = model.listStatements( new SimpleSelector( null,
+ // null, tvResObjs[1] ) );
+ // List<Statement> L4 = iteratorToList( it4 );
+ // for (int i = 0; i < L4.size(); i += 1)
+ // assertEquals( tvResObjs[1], L4.get(i).getObject() );
+ // assertEquals( 2, L4.size() );
+
+ @Test
+ public void testListWithRDFSelector()throws Exception
+ {
+ setUpSelectorData( model );
+ final StmtIterator it5 = model.listStatements(new SimpleSelector(null,
+ null, model.createTypedLiteral(false)));
+ final List<Statement> L5 = iteratorToList(it5);
+ for (int i = 0; i < L5.size(); i += 1)
+ {
+ Assert.assertEquals(false, L5.get(i).getBoolean());
+ }
+ Assert.assertEquals(2, L5.size());
+ }
+
+ @Test
+ public void testListWithSubjectSelector()throws Exception
+ {
+ setUpSelectorData( model );
+ final StmtIterator it2 = model.listStatements(new SimpleSelector(
+ subject[0], null, (RDFNode) null));
+ final List<Statement> L2 = iteratorToList(it2);
+ for (int i = 0; i < L2.size(); i += 1)
+ {
+ Assert.assertEquals(subject[0], L2.get(i).getSubject());
+ }
+ Assert.assertEquals(num * 7, L2.size());
+ }
+
+ @Test
+ public void testNullArgs()throws Exception
+ {
+ setUpSelectorData( model );
+ /*
+ * the _null_ argument to LiteralImpl was preserved only for backward
+ * compatability. It was be logged and has now become an exception.
+ */
+ try
+ {
+ final Literal lit = model.createLiteral(null, "");
+ model.query(new SimpleSelector(null, null, lit));
+ Assert.fail("SHould have thrown a null pointer exception");
+ }
+ catch (final NullPointerException expected)
+ { // expected}
+ }
+
+ try
+ {
+ final Literal lit = model.createLiteral(null, "en");
+ model.query(new SimpleSelector(null, null, lit));
+ Assert.fail("SHould have thrown a null pointer exception");
+ }
+ catch (final NullPointerException expected)
+ { // expected}
+ }
+
[... 572 lines stripped ...]