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/21 22:12:00 UTC
svn commit: r1525287 [4/6] - in /jena/Experimental/new-test/src/test:
java/com/hp/hpl/jena/rdf/model/ java/com/hp/hpl/jena/rdf/model/temp/
java/com/hp/hpl/jena/testing_framework/ resources/
resources/com/hp/hpl/jena/rdf/ resources/com/hp/hpl/jena/rdf/m...
Copied: jena/Experimental/new-test/src/test/java/com/hp/hpl/jena/rdf/model/AbstractModelContractTests.java (from r1523135, 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/AbstractModelContractTests.java?p2=jena/Experimental/new-test/src/test/java/com/hp/hpl/jena/rdf/model/AbstractModelContractTests.java&p1=jena/Experimental/new-test/src/test/java/com/hp/hpl/jena/rdf/model/AbstractModelTest.java&r1=1523135&r2=1525287&rev=1525287&view=diff
==============================================================================
--- jena/Experimental/new-test/src/test/java/com/hp/hpl/jena/rdf/model/AbstractModelTest.java (original)
+++ jena/Experimental/new-test/src/test/java/com/hp/hpl/jena/rdf/model/AbstractModelContractTests.java Sat Sep 21 20:11:59 2013
@@ -20,2550 +20,2017 @@ package com.hp.hpl.jena.rdf.model;
import static com.hp.hpl.jena.testing_framework.ModelHelper.*;
import static org.junit.Assert.*;
+import static org.mockito.Mockito.*;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
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.io.InputStream;
+import java.io.OutputStreamWriter;
+import java.io.Reader;
+import java.io.Writer;
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 javax.xml.XMLConstants;
+import javax.xml.namespace.NamespaceContext;
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.xpath.XPath;
+import javax.xml.xpath.XPathConstants;
+import javax.xml.xpath.XPathFactory;
-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 org.w3c.dom.Document;
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.testing_framework.TestFileData;
import com.hp.hpl.jena.vocabulary.RDF;
import com.hp.hpl.jena.vocabulary.RDFS;
-public abstract class AbstractModelTest extends AbstractModelProducerUser {
+/**
+ * An abstract class that tests the implementation of the Model contract.
+ *
+ * NOTE: does not test independent as only derived classes know if they are
+ * independent or not.
+ *
+ */
+public abstract class AbstractModelContractTests extends AbstractModelProducerUser {
private Model model;
-
- static boolean booleanValue = true;
+ private RecordingModelListener SL;
- 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);
+ private static Logger log = LoggerFactory
+ .getLogger(AbstractModelContractTests.class);
@Before
- public void setupAbstractModelTest() {
+ final 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);
- }
+ /*
+ * MODEL INTERFACE TESTS
+ */
+ /**
+ * If transactions are supported verifies abort:
+ * <ul>
+ * <li>must return model for chaining</li>
+ * <li>must undo all changes to the model done within the transaction
+ * boundary</li>
+ * <li>Listeners are informed as soon as the change is made and not at the
+ * transaction boundary.</li>
+ * </ul>
+ */
@Test
- public void testCreateResourceFromNode() {
- RDFNode S = model.getRDFNode(NodeCreateUtils.create("spoo:S"));
- assertInstanceOf(Resource.class, S);
- assertEquals("spoo:S", ((Resource) S).getURI());
+ public void testAbort() {
+ if (model.supportsTransactions()) {
+ ModelChangedListener mockListener = mock(ModelChangedListener.class);
+ model.register(mockListener);
+ model.begin();
+ model.add(resource("S"), property("P"), rdfNode("O"));
+ assertEquals("abort() must return model for cascading",
+ model == model.abort());
+ assertTrue("abort() must rollback changes", model.isEmpty());
+ verify(mockListener, times(1)).addedStatement((Statement) any());
+ verify(mockListener, times(1)).removedStatement((Statement) any());
+ }
}
+ /**
+ * Tests a list of statements can be added to the model. All additions are
+ * performed within a transaction.
+ * <ul>
+ * <li>must return model for chaining</li>
+ * <li>Listeners are informed that the change list was added.</li>
+ * <li>All statements in the list are in the model</li>
+ * </ul>
+ */
@Test
- public void testCreateLiteralFromNode() {
- RDFNode S = model.getRDFNode(NodeCreateUtils.create("42"));
- assertInstanceOf(Literal.class, S);
- assertEquals("42", ((Literal) S).getLexicalForm());
+ public void testAdd_StatementList() {
+ model.register(SL);
+ final List<Statement> stmts = Arrays
+ .asList(statements("b I g; model U g"));
+ txnBegin(model);
+ assertTrue("add( List<Statement> ) must return model for cascading",
+ model == model.add(stmts));
+ txnCommit(model);
+ SL.assertHas(new Object[] { "addList", stmts });
+ for (Statement s : stmts) {
+ assertTrue(String.format("%s is missing", s), model.contains(s));
+ }
}
+ /**
+ * Tests a model can be added to the model. Added model may not be same type
+ * as the model under test. All additions are performed within a
+ * transaction.
+ * <ul>
+ * <li>must return model for chaining</li>
+ * <li>Listeners are informed that the model was added.</li>
+ * <li>all statements in the added model must be in the model added to</li>
+ * </ul>
+ */
@Test
- public void testCreateBlankFromNode() {
- RDFNode S = model.getRDFNode(NodeCreateUtils.create("_Blank"));
- assertInstanceOf(Resource.class, S);
- assertEquals(new AnonId("_Blank"), ((Resource) S).getId());
- }
+ public void testAdd_Model() {
+ Model model2 = memModel("a P b; c P d; x Q 1; y Q 2");
- @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());
+ model.register(SL);
+ txnBegin(model);
+ assertTrue("add( model ) must return model for cascading",
+ model == model.add(model2));
+ txnCommit(model);
+ SL.assertHas(new Object[] { "addModel", model2 });
+ assertSameStatements(model, model2);
}
- @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")));
- }
+ // /**
+ // * Add reification statements to the model.
+ // * Does not alter transaction state.
+ // * @param m the model
+ // * @param tag The string representation of the node URI to
+ // * @param statement The statement to reify
+ // */
+ // private void addReification( final Model m, final String tag,
+ // final String statement )
+ // {
+ // m.createReifiedStatement(tag, statement( statement));
+ // }
/**
- * Always retuns a value even if the property is no in the model.
+ * Tests a model with reifications can be added to the model. Added model
+ * may not be same type as the model under test. All additions are performed
+ * within a transaction.
+ * <ul>
+ * <li>must return model for chaining</li>
+ * <li>Listeners are informed that the model was added.</li>
+ * <li>Models must be Isomorphic</li>
+ * </ul>
*/
@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 );
+ public void testAdd_ModelWithReifications() {
+ Model m = memModel("a P b");
+ txnBegin(m);
+ m.createReifiedStatement("x", statement("S P O"));
+ m.createReifiedStatement(statement("x R y"));
+ txnCommit(m);
+ model.register(SL);
+ txnBegin(model);
+ assertTrue("add( model ) must return model for cascading",
+ model == model.add(m));
+ txnCommit(model);
+ SL.assertHas(new Object[] { "addModel", m });
+ assertIsoModels(m, model);
}
-
+ /**
+ * Tests a Statement can be added to the model.
+ *
+ * All additions are performed within a transaction.
+ * <ul>
+ * <li>must return model for chaining</li>
+ * <li>Listeners are informed that the statement was added.</li>
+ * <li>the model must contain the statement</li>
+ * </ul>
+ */
@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());
+ public void testAdd_Statement() {
+ Statement s = statement("S P O");
+ model.register(SL);
+ txnBegin(model);
+ assertTrue("add( statement ) must return model for cascading",
+ model == model.add(s));
+ txnCommit(model);
+ SL.assertHas(new Object[] { "add", s });
+ assertTrue("Statement not found in model", model.contains(s));
}
+ /**
+ * Tests an array of Statement can be added to the model.
+ *
+ * All additions are performed within a transaction.
+ * <ul>
+ * <li>must return model for chaining</li>
+ * <li>Listeners are informed that the array of statement was added.</li>
+ * <li>Must contains all the statements in the array</li>
+ * </ul>
+ */
@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);
+ public void testAdd_StatementArray() {
+ final Statement[] stmts = statements("a P b; c Q d");
+ model.register(SL);
+ txnBegin(model);
+ assertTrue("add( statement[] ) must return model for cascading",
+ model == model.add(stmts));
+ txnCommit(model);
+ assertEquals(stmts.length, model.size());
+ SL.assertHas(new Object[] { "add[]", Arrays.asList(stmts) });
+ for (Statement s : stmts) {
+ assertTrue(String.format("%s is missing", s), model.contains(s));
+ }
}
+ /**
+ * Tests an iterator of Statements can be added to the model.
+ *
+ * All additions are performed within a transaction.
+ * <ul>
+ * <li>must return model for chaining</li>
+ * <li>Listeners are informed that the array of statement was added.</li>
+ * <li>All the statements in the iterator were added to the model</li>
+ * </ul>
+ */
@Test
- public void testURINodeAsResource() {
- Node n = node("a");
- Resource r = model.wrapAsResource(n);
- assertSame(n, r.asNode());
+ public void testAdd_StatementIterator() {
+ Statement[] stmts = statements("moon orbits earth; earth orbits sun");
+ StmtIterator iter = new StmtIteratorImpl(Arrays.asList(stmts)
+ .iterator());
+ model.register(SL);
+ txnBegin(model);
+ assertTrue(
+ "add( Iterator<Statement> ) must return model for cascading",
+ model == model.add(iter));
+ txnCommit(model);
+ SL.assertHas(new Object[] { "addIterator", Arrays.asList(stmts) });
+ for (Statement s : stmts) {
+ assertTrue(String.format("%s is missing", s), model.contains(s));
+ }
}
+ /**
+ * If transactions are supported verifies begin:
+ * <ul>
+ * <li>must return model for chaining</li>
+ * </ul>
+ */
@Test
- public void testLiteralNodeAsResourceFails() {
- try {
- model.wrapAsResource(node("17"));
- fail("should fail to convert literal to Resource");
- } catch (UnsupportedOperationException e) {
- // expected
+ public void testBegin() {
+ if (model.supportsTransactions()) {
+ assertEquals("begin() must return model for cascading",
+ model == model.begin());
}
}
+ /**
+ * Test that close functions (ie does not throw an exception)
+ *
+ */
@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());
+ public void testClose() {
+ model.close();
}
/**
- * 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);
- }
+ * If transactions are supported verifies commit:
+ * <ul>
+ * <li>must return model for chaining</li>
+ * <li>must write changes to the model done within the transaction boundary.
+ * </li>
+ * <li>Listeners are informed as soon as the change is made and not at the
+ * transaction boundary.</li>
+ * <li>model must contain data after commit</li>
+ * </ul>
+ */
+ @Test
+ public void testCommit() {
+ if (model.supportsTransactions()) {
+ ModelChangedListener mockListener = mock(ModelChangedListener.class);
+ model.register(mockListener);
+ model.begin();
+ model.add(resource("S"), property("P"), rdfNode("O"));
+ verify(mockListener, times(1)).addedStatement((Statement) any());
+ assertEquals("commit() must return model for cascading",
+ model == model.commit());
+ assertFalse("statement is missing", model.isEmpty());
+ }
}
+ /**
+ * Test that the contains(Resource,Property) method works with all types of
+ * Resources and Properties.
+ */
@Test
- public void testIsClosedDelegatedToGraph() {
- assertFalse(model.isClosed());
- assertFalse( model.getGraph().isClosed());
- model.close();
- assertTrue(model.isClosed());
- assertTrue(model.getGraph().isClosed());
- }
+ public void testContains_Resource_Property() {
+ txnBegin(model);
+ modelAdd(model, "a R b; _1 P d; _ P c");
+ txnCommit(model);
+
+ assertTrue("can not find '<any> <any> ?", model.contains(null, null));
+
+ assertTrue("can not find 'a R ?'",
+ model.contains(resource("a"), property("R")));
+ assertTrue("can not find 'a <any> ?'",
+ model.contains(resource("a"), null));
+ assertFalse("found 'a P ?'",
+ model.contains(resource("a"), property("P")));
+
+ assertTrue("can not find '_1 P ?'",
+ model.contains(resource("_1"), property("P")));
+ assertTrue("can not find '_1 <any> ?'",
+ model.contains(resource("_1"), null));
+ assertFalse("found '_1 R ?'",
+ model.contains(resource("_1"), property("R")));
- protected Model copy(Model m) {
- return getModelProducer().newModel().add(m);
+ assertTrue("can not find <any> R ?",
+ model.contains(null, property("R")));
}
+ /**
+ * Test that the contains(Resource,Property) method works with all types of
+ * Resources and Properties.
+ */
@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));
+ public void testContains_Resource_Property_RDFNode() {
+ txnBegin(model);
+ modelAdd(model,
+ "a R b; a R 5; a R _1; a R 'foo'; _2 R b; _2 R 5; _2 R _1; _2 R 'foo'");
+ txnCommit(model);
+
+ assertTrue("can not find <any> <any> <any>",
+ model.contains(null, null, (RDFNode) null));
+ assertTrue("can not find <any> <any> b",
+ model.contains(null, null, rdfNode("b")));
+ assertTrue("can not find <any> <any> 5",
+ model.contains(null, null, rdfNode("5")));
+ assertTrue("can not find <any> <any> _1",
+ model.contains(null, null, rdfNode("_1")));
+ assertTrue("can not find <any> <any> 'foo'",
+ model.contains(null, null, rdfNode("'foo'")));
+
+ assertTrue("can not find <any> R <any>",
+ model.contains(null, property("R"), (RDFNode) null));
+ assertTrue("can not find <any> R b",
+ model.contains(null, property("R"), rdfNode("b")));
+ assertTrue("can not find <any> R 5",
+ model.contains(null, property("R"), rdfNode("5")));
+ assertTrue("can not find <any> R _1",
+ model.contains(null, property("R"), rdfNode("_1")));
+ assertTrue("can not find <any> R 'foo'",
+ model.contains(null, property("R"), rdfNode("'foo'")));
+
+ assertTrue("can not find a <any> <any>",
+ model.contains(resource("a"), null, (RDFNode) null));
+ assertTrue("can not find a <any> b",
+ model.contains(resource("a"), null, rdfNode("b")));
+ assertTrue("can not find a <any> 5",
+ model.contains(resource("a"), null, rdfNode("5")));
+ assertTrue("can not find a <any> _1",
+ model.contains(resource("a"), null, rdfNode("_1")));
+ assertTrue("can not find a <any> 'foo'",
+ model.contains(resource("a"), null, rdfNode("'foo'")));
+
+ assertTrue("can not find a R <any>",
+ model.contains(resource("a"), property("R"), (RDFNode) null));
+ assertTrue("can not find a R b'",
+ model.contains(resource("a"), property("R"), rdfNode("b")));
+ assertTrue("can not find a R 5",
+ model.contains(resource("a"), property("R"), rdfNode("5")));
+ assertTrue("can not find a R _1",
+ model.contains(resource("a"), property("R"), rdfNode("_1")));
+ assertTrue("can not find a R 'foo'",
+ model.contains(resource("a"), property("R"), rdfNode("'foo'")));
+
+ assertTrue("can not find _2 <any> <any>'",
+ model.contains(resource("_2"), null, (RDFNode) null));
+ assertTrue("can not find _2 <any> b'",
+ model.contains(resource("_2"), null, rdfNode("b")));
+ assertTrue("can not find _2 <any> 5'",
+ model.contains(resource("_2"), null, rdfNode("5")));
+ assertTrue("can not find _2 <any> _1'",
+ model.contains(resource("_2"), null, rdfNode("_1")));
+ assertTrue("can not find _2 <any> 'foo'",
+ model.contains(resource("_2"), null, rdfNode("'foo'")));
+
+ assertTrue("can not find _2 R <any>",
+ model.contains(resource("_2"), property("R"), (RDFNode) null));
+ assertTrue("can not find _2 R b",
+ model.contains(resource("_2"), property("R"), rdfNode("b")));
+ assertTrue("can not find _2 R 5",
+ model.contains(resource("_2"), property("R"), rdfNode("5")));
+ assertTrue("can not find _2 R _1",
+ model.contains(resource("_2"), property("R"), rdfNode("_1")));
+ assertTrue("can not find _2 R 'foo'",
+ model.contains(resource("_2"), property("R"), rdfNode("'foo'")));
+
+ // check for ones that should not be there.
+
+ assertFalse("found <any> <any> c",
+ model.contains(null, null, rdfNode("c")));
+ assertFalse("found <any> <any> 6",
+ model.contains(null, null, rdfNode("6")));
+ assertFalse("found <any> <any> _3",
+ model.contains(null, null, rdfNode("_3")));
+ assertFalse("found <any> <any> 'bar'",
+ model.contains(null, null, rdfNode("'bar'")));
+
+ assertFalse("found <any> P <any>",
+ model.contains(null, property("P"), (RDFNode) null));
+ assertFalse("found <any> P b",
+ model.contains(null, property("P"), rdfNode("b")));
+ assertFalse("found <any> P 5",
+ model.contains(null, property("P"), rdfNode("5")));
+ assertFalse("found <any> P _1",
+ model.contains(null, property("P"), rdfNode("_1")));
+ assertFalse("found <any> P 'foo",
+ model.contains(null, property("P"), rdfNode("'foo'")));
+
+ assertFalse("found c <any> <any>",
+ model.contains(resource("c"), null, (RDFNode) null));
+ assertFalse("found c <any> b",
+ model.contains(resource("c"), null, rdfNode("b")));
+ assertFalse("found c <any> 5",
+ model.contains(resource("c"), null, rdfNode("5")));
+ assertFalse("found c <any> _1",
+ model.contains(resource("c"), null, rdfNode("_1")));
+ assertFalse("found c <any> 'foo",
+ model.contains(resource("c"), null, rdfNode("'foo'")));
+
+ assertFalse("found c R <any>",
+ model.contains(resource("c"), property("R"), (RDFNode) null));
+ assertFalse("found c R b'",
+ model.contains(resource("c"), property("R"), rdfNode("b")));
+ assertFalse("found c R 5",
+ model.contains(resource("c"), property("R"), rdfNode("5")));
+ assertFalse("found c R _1",
+ model.contains(resource("c"), property("R"), rdfNode("_1")));
+ assertFalse("found c R 'foo",
+ model.contains(resource("c"), property("R"), rdfNode("'foo'")));
+
+ assertFalse("found _3 <any> <any>'",
+ model.contains(resource("_3"), null, (RDFNode) null));
+ assertFalse("found _3 <any> b'",
+ model.contains(resource("_3"), null, rdfNode("b")));
+ assertFalse("found _3 <any> 5'",
+ model.contains(resource("_3"), null, rdfNode("5")));
+ assertFalse("found _3 <any> _1'",
+ model.contains(resource("_3"), null, rdfNode("_1")));
+ assertFalse("found _3 <any> 'foo",
+ model.contains(resource("_3"), null, rdfNode("'foo'")));
+
+ assertFalse("found _3 R <any>",
+ model.contains(resource("_3"), property("R"), (RDFNode) null));
+ assertFalse("found _3 R b",
+ model.contains(resource("_3"), property("R"), rdfNode("b")));
+ assertFalse("found _3 R 5",
+ model.contains(resource("_3"), property("R"), rdfNode("5")));
+ assertFalse("found _3 R _1",
+ model.contains(resource("_3"), property("R"), rdfNode("_1")));
+ assertFalse("found _3 R 'foo",
+ model.contains(resource("_3"), property("R"), rdfNode("'foo'")));
+
}
+ /**
+ * test that statements can be found.
+ */
@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));
+ public void testContains_Statement() {
+ txnBegin(model);
+ modelAdd(model,
+ "a R b; a R 5; a R _1; a R 'foo'; _2 R b; _2 R 5; _2 R _1; _2 R 'foo'");
+ txnCommit(model);
+
+ assertTrue("can not find a R b'", model.contains(statement("a R b")));
+ assertTrue("can not find a R 5", model.contains(statement("a R 5")));
+ assertTrue("can not find a R _1", model.contains(statement("a R _1")));
+ assertTrue("can not find a R 'foo'",
+ model.contains(statement("a R 'foo'")));
+
+ assertTrue("can not find _2 R b", model.contains(statement("_2 R b")));
+ assertTrue("can not find _2 R 5", model.contains(statement("_2 R 5")));
+ assertTrue("can not find _2 R _1", model.contains(statement("_2 R _1")));
+ assertTrue("can not find _2 R 'foo'",
+ model.contains(statement("_2 R 'foo'")));
+
+ // check for ones that should not be there.
+
+ assertFalse("found c R b'", model.contains(statement("c R b")));
+ assertFalse("found c R 5", model.contains(statement("c R 5")));
+ assertFalse("found c R _1", model.contains(statement("c R _1")));
+ assertFalse("found c R 'foo'", model.contains(statement("c R 'foo'")));
+
+ assertFalse("found _3 R b", model.contains(statement("_3 R b")));
+ assertFalse("found _3 R 5", model.contains(statement("_3 R 5")));
+ assertFalse("found _3 R _1", model.contains(statement("_3 R _1")));
+ assertFalse("found _3 R 'foo'", model.contains(statement("_3 R `foo'")));
}
+ /**
+ * test that statements can be found.
+ */
@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));
+ public void testContainsAll_Model() {
+ txnBegin(model);
+ modelAdd(
+ model,
+ "a R b; a R 5; a R _1; a R 'foo'; a P 'foo'; _2 R b; _2 R 5; _2 R _1; _2 R 'foo'; _2 P 'foo'");
+ txnCommit(model);
+
+ // subset of model
+ Model subset = memModel("a R b; a R 5; a R _1; a R 'foo'; _2 R b; _2 R 5; _2 R _1; _2 R 'foo'");
+
+ // some not in model
+ Model intersect = memModel("a R b; a R 5; a R _1; a R 'foo'; _3 R b; _3 R 5; _3 R _1; _3 R 'foo'");
+
+ // disjoint
+ Model disjoint = memModel(" _3 R b; _3 R 5; _3 R _1; _3 R 'foo'");
+ assertTrue("did not find subset", model.containsAll(subset));
+ assertFalse("found intersection", model.containsAll(intersect));
+ assertFalse("found disjoint", model.containsAll(disjoint));
}
+ /**
+ * test that statements can be found.
+ */
@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));
+ public void testContainsAll_StmtIterator() {
+ txnBegin(model);
+ modelAdd(
+ model,
+ "a R b; a R 5; a R _1; a R 'foo'; a P 'foo'; _2 R b; _2 R 5; _2 R _1; _2 R 'foo'; _2 P 'foo'");
+ txnCommit(model);
+
+ // subset of model
+ Statement[] subset = statements("a R b; a R 5; a R _1; a R 'foo'; _2 R b; _2 R 5; _2 R _1; _2 R 'foo'");
+
+ // intersection with model
+ Statement[] intersect = statements("a R b; a R 5; a R _1; a R 'foo'; _3 R b; _3 R 5; _3 R _1; _3 R 'foo'");
+
+ Statement[] disjoint = statements(" _3 R b; _3 R 5; _3 R _1; _3 R 'foo'");
+
+ StmtIterator iter = new StmtIteratorImpl(Arrays.asList(subset)
+ .iterator());
+ assertTrue("did not find subset", model.containsAll(iter));
+ iter = new StmtIteratorImpl(Arrays.asList(intersect).iterator());
+ assertFalse("found intersection", model.containsAll(iter));
+ iter = new StmtIteratorImpl(Arrays.asList(disjoint).iterator());
+ assertFalse("found disjoint", model.containsAll(iter));
}
+ /**
+ * test that statements can be found.
+ */
@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));
+ public void testContainsAny_Model() {
+ txnBegin(model);
+ modelAdd(
+ model,
+ "a R b; a R 5; a R _1; a R 'foo'; a P 'foo'; _2 R b; _2 R 5; _2 R _1; _2 R 'foo'; _2 P 'foo'");
+ txnCommit(model);
+
+ // subset of model
+ Model subset = memModel("a R b; a R 5; a R _1; a R 'foo'; _2 R b; _2 R 5; _2 R _1; _2 R 'foo'");
+
+ // intersection with model
+ Model intersection = memModel("a R b; a R 5; a R _1; a R 'foo'; _3 R b; _3 R 5; _3 R _1; _3 R 'foo'");
+
+ // disjoint with model
+ Model disjoint = memModel(" _3 R b; _3 R 5; _3 R _1; _3 R 'foo'");
+
+ assertTrue("did not find subset", model.containsAny(subset));
+ assertTrue("did not find intersection", model.containsAny(intersection));
+ assertFalse("found disjoint", model.containsAny(disjoint));
}
+ /**
+ * test that statements can be found.
+ */
@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));
+ public void testContainsAny_StmtIterator() {
+ txnBegin(model);
+ modelAdd(
+ model,
+ "a R b; a R 5; a R _1; a R 'foo'; a P 'foo'; _2 R b; _2 R 5; _2 R _1; _2 R 'foo'; _2 P 'foo'");
+ txnCommit(model);
+
+ // subset of model
+ Statement[] subset = statements("a R b; a R 5; a R _1; a R 'foo'; _2 R b; _2 R 5; _2 R _1; _2 R 'foo'");
+
+ // intersection with model
+ Statement[] intersect = statements("a R b; a R 5; a R _1; a R 'foo'; _3 R b; _3 R 5; _3 R _1; _3 R 'foo'");
+
+ Statement[] disjoint = statements(" _3 R b; _3 R 5; _3 R _1; _3 R 'foo'");
+
+ StmtIterator iter = new StmtIteratorImpl(Arrays.asList(subset)
+ .iterator());
+ assertTrue("did not find subset", model.containsAny(iter));
+ iter = new StmtIteratorImpl(Arrays.asList(intersect).iterator());
+ assertTrue("did not find intersection", model.containsAny(iter));
+ iter = new StmtIteratorImpl(Arrays.asList(disjoint).iterator());
+ assertFalse("found disjoint", model.containsAny(iter));
}
@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));
+ public void testContains_Resource() {
+ txnBegin(model);
+ modelAdd(model, "x R y; _a P _b");
+ txnCommit(model);
+ 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")));
+ assertFalse(model.containsResource(resource("Q")));
}
+ /**
+ * Test that the model can create an RDFList.
+ *
+ * <ul>
+ * <li>Must must be attached to the model</li>
+ * <li>Must not be in the model</li>
+ * </ul>
+ */
@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"));
+ public void testCreateList() {
+ RDFList list = model.createList();
+ assertTrue("List is not attached to the model",
+ list.getModel() == model);
+ assertFalse("List is in the model", model.containsResource(list));
}
+ /**
+ * Test that the model can create an RDFList.
+ *
+ * <ul>
+ * <li>Must must be attached to the model</li>
+ * <li>Must be in the model</li>
+ * <li>list must contains the nodes in order</li>
+ * </ul>
+ */
@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));
+ public void testCreateList_Iterator() {
+ RDFNode[] nodes = { rdfNode("a"), rdfNode("b"), rdfNode("c"),
+ rdfNode("d") };
+ txnBegin(model);
+ RDFList list = model.createList(Arrays.asList(nodes).iterator());
+ txnCommit(model);
+
+ assertTrue("List is not attached to the model",
+ list.getModel() == model);
+ assertTrue("List is not in the model", model.containsResource(list));
+
+ assertEquals("wrong number of elements in list", nodes.length,
+ list.size());
+ for (int i = 0; i < nodes.length; i++) {
+ assertEquals("element " + i + " is out of sequence", nodes[i],
+ list.get(i));
+ }
}
+ /**
+ * Test that the model can create an RDFList.
+ *
+ * <ul>
+ * <li>Must must be attached to the model</li>
+ * <li>Must be in the model</li>
+ * <li>list must contains the nodes in order</li>
+ * </ul>
+ */
@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 testCreateList_Array() {
+ RDFNode[] nodes = { rdfNode("a"), rdfNode("b"), rdfNode("c"),
+ rdfNode("d") };
+ txnBegin(model);
+ RDFList list = model.createList(nodes);
+ txnCommit(model);
+ assertTrue("List is not attached to the model",
+ list.getModel() == model);
+ assertTrue("List is not in the model", model.containsResource(list));
+
+ assertEquals("wrong number of elements in list", nodes.length,
+ list.size());
+ for (int i = 0; i < nodes.length; i++) {
+ assertEquals("element " + i + " is out of sequence", nodes[i],
+ list.get(i));
+ }
}
- // public void testAddContainsObject()
- // {
- // LitTestObj O = new LitTestObj( 12345 );
- // model.addLiteral( S, P, O );
- // assertTrue( model.containsLiteral( S, P, O ) );
- // }
-
+ /**
+ * Test that the model can create a string literal with language.
+ *
+ * <ul>
+ * <li>Must must be attached to the model</li>
+ * <li>Must not be in the model</li>
+ * <li>must have the language value set</li>
+ * </ul>
+ */
@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));
+ public void testCreateLiteral_String_String() {
+ /*
+ * the _null_ argument to LiteralImpl was preserved only for backward
+ * compatability. It was be logged and has now become an exception.
+ */
+ try {
+ model.createLiteral(null, "");
+ Assert.fail("Should have thrown a null pointer exception");
+ } catch (final NullPointerException expected) { // expected}
+ }
+
+ Literal lit = model.createLiteral("foo", null);
+ assertTrue("List is not attached to the model", lit.getModel() == model);
+ assertFalse("List is in the model", model.containsResource(lit));
+ assertEquals("Language is missing", "", lit.getLanguage());
+ assertEquals("String value is wrong", "foo", lit.getString());
+ assertEquals("Value is missing", "foo", lit.getValue());
+
+ lit = model.createLiteral("foo", "en");
+ assertTrue("List is not attached to the model", lit.getModel() == model);
+ assertFalse("List is in the model", model.containsResource(lit));
+ assertEquals("Language is missing", "en", lit.getLanguage());
+ assertEquals("String value is wrong", "foo", lit.getString());
+ assertEquals("Value is missing", "foo", lit.getValue());
}
+ /**
+ * Test that the model can create a string literal with language.
+ *
+ * <ul>
+ * <li>Must must be attached to the model</li>
+ * <li>Must not be in the model</li>
+ * <li>must not have the language value set</li>
+ * </ul>
+ */
@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));
+ public void testCreateLiteral_String_Boolean() {
+ try {
+ model.createLiteral(null, true);
+ Assert.fail("Should have thrown a null pointer exception");
+ } catch (final NullPointerException expected) { // expected}
+ }
+ try {
+ model.createLiteral(null, false);
+ Assert.fail("Should have thrown a null pointer exception");
+ } catch (final NullPointerException expected) { // expected}
+ }
+
+ Literal lit = model.createLiteral( "13" , true );
+ //"13^^http://www.w3.org/2001/XMLSchema#integer", true);
+ assertTrue("Literal is not attached to the model",
+ lit.getModel() == model);
+ assertFalse("Literal is in the model", model.containsResource(lit));
+ assertEquals("Language is missing", "", lit.getLanguage());
+ assertEquals( "Wrong datatype URI", "http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral", lit.getDatatypeURI());
+ assertEquals("Value is class is wrong", String.class, lit.getValue().getClass());
+ assertEquals("Int value is wrong", "13", lit.getValue());
+
+ lit = model.createLiteral( "13" , false );
+ //"13^^http://www.w3.org/2001/XMLSchema#integer", true);
+ assertTrue("Literal is not attached to the model",
+ lit.getModel() == model);
+ assertFalse("Literal is in the model", model.containsResource(lit));
+ assertEquals("Language is missing", "", lit.getLanguage());
+ assertNull( "Wrong datatype URI", lit.getDatatypeURI());
+ assertEquals("Value is class is wrong", String.class, lit.getValue().getClass());
+ assertEquals("Int value is wrong", "13", lit.getValue());
+
}
+ /**
+ * Test the creation of a property using namespace and local name.
+ * <p>
+ * This tests for the case where the local name is not a valid local name.
+ * In this case the model should create a resource and split the namespace
+ * at the longest possible valid local name.
+ * </p>
+ * <ul>
+ * <li>Must must be attached to the model</li>
+ * <li>Must not be in the model</li>
+ * <li>must have the name space set properly</li>
+ * <li>must have the local name set properly</li>
+ * <li>must have the URI set properly</li>
+ * </ul>
+ */
@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());
+ public void testCreateProperty_String_String() {
+ txnBegin(model);
+ Property p = model.createProperty("abc/", "def");
+ txnCommit(model);
+ assertTrue("Property is not attached to the model",
+ p.getModel() == model);
+ assertFalse("Property is in the model", model.containsResource(p));
+ Assert.assertEquals("abc/", p.getNameSpace());
+ Assert.assertEquals("def", p.getLocalName());
+ Assert.assertEquals("abc/def", p.getURI());
+
+ txnBegin(model);
+ p = model.createProperty("abc#", "def");
+ txnCommit(model);
+ assertTrue("Property is not attached to the model",
+ p.getModel() == model);
+ assertFalse("Property is in the model", model.containsResource(p));
+ Assert.assertEquals("abc#", p.getNameSpace());
+ Assert.assertEquals("def", p.getLocalName());
+ Assert.assertEquals("abc#def", p.getURI());
+
+ txnBegin(model);
+ p = model.createProperty("abc#", "345");
+ txnCommit(model);
+ assertTrue("Property is not attached to the model",
+ p.getModel() == model);
+ assertFalse("Property is in the model", model.containsResource(p));
+ Assert.assertEquals("abc#345", p.getNameSpace());
+ Assert.assertEquals("", p.getLocalName());
+ Assert.assertEquals("abc#345", p.getURI());
+
}
+ /**
+ * Test the creation of a reified statement using a statement.
+ *
+ * <ul>
+ * <li>Must must be attached to the model</li>
+ * <li>Must must be in the model</li>
+ * <li>must be an anonymous node</li>
+ * <li>must report that it reifies the argument statement</li>
+ * <li>must have RDF.type of RDF.Statement</li>
+ * <li>must have RDF.subject of argument statement subject</li>
+ * <li>must have RDF.object of argument statement object</li>
+ * <li>must have RDF.predicate of argument statement predicate</li>
+ * </ul>
+ */
@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"));
+ public void testCreateReifiedStatement_Statement() {
+ Statement s = statement("S P O");
+
+ txnBegin(model);
+ ReifiedStatement rs = model.createReifiedStatement(s);
+ txnCommit(model);
+ assertTrue("Reified statement is not attached to the model",
+ rs.getModel() == model);
+ assertTrue("Reified statement is not in the model",
+ model.containsResource(rs));
+ assertEquals("Reified statement does not report reified statement", s,
+ rs.getStatement());
+
+ assertTrue("Reified statement should be anonymous", rs.isAnon());
+ assertTrue("Reified statement is wrong type",
+ rs.hasProperty(RDF.type, RDF.Statement));
+ assertTrue("Reified statement has wrong subject",
+ rs.hasProperty(RDF.subject, resource("S")));
+ assertTrue("Reified statement has wrong predicate",
+ rs.hasProperty(RDF.predicate, property("P")));
+ assertTrue("Reified statement has wrong object",
+ rs.hasProperty(RDF.object, rdfNode("O")));
+
}
+ /**
+ * Test the creation of a reified statement using a URI and a statement.
+ *
+ * <ul>
+ * <li>Must must be attached to the model</li>
+ * <li>Must be in the model</li>
+ * <li>must report that it reifies the argument statement</li>
+ * <li>must have the URI as its URI</li>
+ * <li>must have RDF.type of RDF.Statement</li>
+ * <li>must have RDF.subject of argument statement subject</li>
+ * <li>must have RDF.object of argument statement object</li>
+ * <li>must have RDF.predicate of argument statement predicate</li>
+ * </ul>
+ */
@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));
- }
+ public void testCreateReifiedStatement_String_Statement() {
+ Statement s = statement("S P O");
+ txnBegin(model);
+ ReifiedStatement rs = model.createReifiedStatement("foo:bar", s);
+ txnCommit(model);
+ assertTrue("Reified statement is not attached to the model",
+ rs.getModel() == model);
+ assertTrue("Reified statement is not in the model",
+ model.containsResource(rs));
+ assertEquals("Reified statement does not report reified statement", s,
+ rs.getStatement());
+
+ assertEquals("Reified statement has wrong URI", "foo:bar", rs.getURI());
+ assertTrue("Reified statement is wrong type",
+ rs.hasProperty(RDF.type, RDF.Statement));
+ assertTrue("Reified statement has wrong subject",
+ rs.hasProperty(RDF.subject, resource("S")));
+ assertTrue("Reified statement has wrong predicate",
+ rs.hasProperty(RDF.predicate, property("P")));
+ assertTrue("Reified statement has wrong object",
+ rs.hasProperty(RDF.object, rdfNode("O")));
+
}
+ /**
+ * Test createResource() creates an anonymous resource.
+ *
+ * <ul>
+ * <li>Must must be attached to the model</li>
+ * <li>Must not be in the model</li>
+ * <li>must be anonymous</li>
+ * </ul>
+ */
@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() );
+ public void testCreateResource() {
+ Resource r = model.createResource();
+ assertTrue("Resource is not attached to the model",
+ r.getModel() == model);
+ assertFalse("Resource is in the model", model.containsResource(r));
+ assertTrue("Resource is not anonymous", r.isAnon());
}
-
+
+ /**
+ * Test createRsource( anonid ) creates an anonymous resource.
+ *
+ * <ul>
+ * <li>Must must be attached to the model</li>
+ * <li>Must not be in the model</li>
+ * <li>must be anonymous</li>
+ * <li>must have the specified id</li>
+ * </ul>
+ */
@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);
+ public void testCreateResource_AnonId() {
+ AnonId anonId = AnonId.create("foo");
+ Resource r = model.createResource(anonId);
+ assertTrue("Resource is not attached to the model",
+ r.getModel() == model);
+ assertFalse("Resource is in the model", model.containsResource(r));
+ assertTrue("Resource is not anonymous", r.isAnon());
+ assertEquals("Resource has wrong anonymous id", anonId, r.getId());
}
+ /**
+ * Test createResource( String ) creates a URI type resource.
+ *
+ * <ul>
+ * <li>Must must be attached to the model</li>
+ * <li>Must not be in the model</li>
+ * <li>must be URI Resource</li>
+ * <li>must have the specified URI</li>
+ * </ul>
+ */
@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());
+ public void testCreateResource_String() {
+ String uri = "foo:bar";
+ Resource r = model.createResource(uri);
+ assertTrue("Resource is not attached to the model",
+ r.getModel() == model);
+ assertFalse("Resource is in the model", model.containsResource(r));
+ assertTrue("Resource is not a URI type", r.isURIResource());
+ assertEquals("Resource has wrong URI", uri, r.getURI());
}
-
+
+ /**
+ * Test createStatement( Resource, Property, RDFNode ) creates a statement.
+ *
+ * <ul>
+ * <li>Must must be attached to the model</li>
+ * <li>Must not be in the model</li>
+ * <li>Statement must have the resource as subject</li>
+ * <li>Statment must have the property as predicate</li>
+ * <li>Statement must have the RDFNode as object</li>
+ * <li>must have the specified URI</li>
+ * </ul>
+ */
@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]);
+ public void testCreateStatement_Resource_Property_RDFNode() {
+ Resource s = resource("S");
+ Property p = property("P");
+ RDFNode o = rdfNode("O");
+
+ txnBegin(model);
+ Statement stmt = model.createStatement(s, p, o);
+ txnCommit(model);
+
+ assertTrue("Statement is not attached to the model",
+ stmt.getModel() == model);
+ assertFalse("Statement is in the model", model.contains(stmt));
+ assertEquals("Statement does not have Resource as subject", s,
+ stmt.getSubject());
+ assertEquals("Statement does not have Property as predicate", p,
+ stmt.getPredicate());
+ assertEquals("Statement does not have RDFNode as object", o,
+ stmt.getObject());
+
}
/**
- * 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());
+ * Test that the model can create a string literal with language.
+ *
+ * <ul>
+ * <li>Must must be attached to the model</li>
+ * <li>Must not be in the model</li>
+ * <li>must not have the language value set</li>
+ * <li>value must be set</li>
+ * <li>value must be of proper type</li>
+ * </ul>
+ */
+ @Test
+ public void testCreateTypedLiteral_Object() {
+ Literal lit = model.createTypedLiteral(Boolean.TRUE);
+ assertTrue("List is not attached to the model", lit.getModel() == model);
+ assertFalse("List is in the model", model.containsResource(lit));
+ assertEquals("Language is not empty", "", lit.getLanguage());
+ assertEquals("String value is wrong", "true", lit.getString());
+ assertNotNull("Value is missing", lit.getValue());
+ assertEquals("value is wrong type", Boolean.class, lit.getValue()
+ .getClass());
}
- /*
- * 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();
- *
+ /**
+ * Test that the model can create a string literal with language.
*
- * } catch (Exception e) { error(test, n, e); } } catch (Exception e) {
- * logger.error( "test " + test + "[" + n + "]", e ); errors = true; } //
- * System.out.println("End of " + test); }
+ * <ul>
+ * <li>Must must be attached to the model</li>
+ * <li>Must not be in the model</li>
+ * <li>must not have the language value set</li>
+ * <li>value must be set</li>
+ * <li>value must be of proper type</li>
+ * </ul>
*/
-
@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));
+ public void testCreateTypedLiteral_Object_RDFDatatype() {
+ Float f = 4.3f;
+ Literal lit = model.createTypedLiteral( f , XSDDatatype.XSDfloat);
+ assertTrue("List is not attached to the model", lit.getModel() == model);
+ assertFalse("List is in the model", model.containsResource(lit));
+ assertEquals("Language is not empty", "", lit.getLanguage());
+ assertEquals("String value is wrong", "4.3", lit.getString());
+ assertNotNull("Value is missing", lit.getValue());
+ assertEquals("value is wrong type", Float.class, lit.getValue()
+ .getClass());
}
- // 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 that the model can create a string literal with language.
+ *
+ * <ul>
+ * <li>Must must be attached to the model</li>
+ * <li>Must not be in the model</li>
+ * <li>must not have the language value set</li>
+ * <li>value must be set</li>
+ * <li>value must be of proper type</li>
+ * </ul>
+ */
@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));
+ public void testCreateTypedLiteral_String_RDFDatatype() {
+ Literal lit = model.createTypedLiteral("4.3", XSDDatatype.XSDfloat);
+ assertTrue("List is not attached to the model", lit.getModel() == model);
+ assertFalse("List is in the model", model.containsResource(lit));
+ assertEquals("Language is not empty", "", lit.getLanguage());
+ assertEquals("String value is wrong", "4.3", lit.getString());
+ assertNotNull("Value is missing", lit.getValue());
+ assertEquals("value is wrong type", Float.class, lit.getValue()
+ .getClass());
}
@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());
+ public void testDifference() {
+ if (model.supportsSetOperations())
+ {
+ txnBegin(model);
+ modelAdd(
+ model,
+ "a R b; a R 5; a R _1; a R 'foo'; a P 'foo'; _2 R b; _2 R 5; _2 R _1; _2 R 'foo'; _2 P 'foo'");
+ txnCommit(model);
+
+ // subset of model
+ Model subset = memModel("a R b; a R 5; a R _1; a R 'foo'; _2 R b; _2 R 5; _2 R _1; _2 R 'foo'");
+
+ // disjoint with model
+ Model disjoint = memModel(" _3 R b; _3 R 5; _3 R _1; _3 R 'foo'");
+
+ Model result = model.difference(subset);
+
+ assertTrue("model does not contain some results",
+ model.containsAll(result));
+ assertFalse("result contains some subset nodes",
+ result.containsAny(subset));
+
+ result = model.difference(disjoint);
+ assertFalse("result contains some disjoint nodes",
+ result.containsAny(disjoint));
+ assertTrue("model does not contain some results",
+ model.containsAll(result));
+ }
}
+ /**
+ * Test that a model is equal to iteself and and another model created from
+ * the same graph.
+ */
@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());
+ public void testEquals() {
+ txnBegin(model);
+ modelAdd(model, "a R b; a R 5; a R _1; a R 'foo'; a P 'foo'");
+ txnCommit(model);
+
+ assertEquals("Model not equal to itself", model, model);
+
+ Model model2 = ModelFactory.createModelForGraph(model.getGraph());
+
+ assertTrue(
+ "Model is not equal to model created from copy of its graph",
+ model.equals(model2));
+
}
+ /**
+ * Test that a model is equal to iteself and and another model created from
+ * the same graph.
+ */
@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());
+ public void testExecuteInTransaction() {
+ if (model.supportsTransactions()) {
+ model.register(SL);
+
+ Command cmd = new Command() {
+ @Override
+ public Object execute() {
+ Statement s = statement("S P O");
+ return model.add(s);
+ }
+ };
+ Object o = model.executeInTransaction(cmd);
+
+ SL.assertHas(new Object[] { "add", statement("S P O") });
+ assertTrue("Statement not found in model",
+ model.contains(statement("S P O")));
+ assertTrue("Command result was not returned", model == o);
+ }
}
@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));
- }
+ public void testGetAnyReifiedStatement() {
+ Statement s = statement("S P O");
+ txnBegin(model);
+ Resource rs = model.getAnyReifiedStatement(s);
+ txnCommit(model);
-
+ txnBegin(model);
+ Resource rs2 = model.getAnyReifiedStatement(s);
+ txnCommit(model);
- @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);
- }
+ assertEquals("not same reified URI", rs.getURI(), rs2.getURI());
+
+ assertEquals("not same reified statement", rs, rs2);
- @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());
+ public void testGetLock() {
+ assertNotNull("Has a lock object", model.getLock());
}
- 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));
- }
- }
-
+ /**
+ * Verify that the getProperty() works
+ */
@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 testGetProperty_Resource_Property() {
+ txnBegin(model);
+ modelAdd(model, "x P a; x P b; x R c; z P d");
+ txnCommit(model);
- // 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());
- }
+ Resource S = resource("x");
+ Property P = property("R");
+ Statement stmt = model.getProperty(S, P);
+ Assert.assertNotNull(stmt);
+ Assert.assertEquals(S, stmt.getSubject());
+ Assert.assertEquals(P, stmt.getPredicate());
+ Assert.assertEquals(rdfNode("c"), stmt.getObject());
+
+ stmt = model.getProperty(resource("y"), P);
+ Assert.assertNull(stmt);
+
+ stmt = model.getProperty(resource("y"), null);
+ Assert.assertNull(stmt);
+
+ stmt = model.getProperty(null, property("Q"));
+ Assert.assertNull(stmt);
+
+ stmt = model.getProperty(null, P);
+ Assert.assertNotNull(stmt);
+ Assert.assertEquals(S, stmt.getSubject());
+ Assert.assertEquals(P, stmt.getPredicate());
+ Assert.assertEquals(rdfNode("c"), stmt.getObject());
+
+ stmt = model.getProperty(resource("z"), null);
+ Assert.assertNotNull(stmt);
+ Assert.assertEquals(resource("z"), stmt.getSubject());
+ Assert.assertEquals(property("P"), stmt.getPredicate());
+ Assert.assertEquals(rdfNode("d"), stmt.getObject());
- @Test
- public void testCreateNamedResource()
- {
- final String uri = "http://aldabaran.hpl.hp.com/foo";
- Assert.assertEquals(uri, model.createResource(uri).getURI());
- }
+ stmt = model.getProperty(null, (Property) null);
+ Assert.assertNotNull(stmt);
- @Test
- public void testCreateNullPropertyFails()
- {
- try
- {
- model.createProperty(null);
- Assert.fail("should not create null property");
- }
- catch (final InvalidPropertyURIException e)
- {
- // expected
- }
}
+ /**
+ * Test the creation of a property using namespace and local name.
+ * <p>
+ * This tests for the case where the local name is not a valid local name.
+ * In this case the model should create a resource and split the namespace
+ * at the longest possible valid local name.
+ * </p>
+ * <ul>
+ * <li>Must must be attached to the model</li>
+ * <li>Must not be in the model</li>
+ * <li>must have the name space set properly</li>
+ * <li>must have the local name set properly</li>
+ * <li>must have the URI set properly</li>
+ * </ul>
+ */
@Test
- public void testCreatePropertyOneArg()
- {
- final Property p = model.createProperty("abc/def");
+ public void testGetProperty_String_String() {
+ txnBegin(model);
+ Property p = model.getProperty("abc/", "def");
+ txnCommit(model);
+ assertTrue("Property is not attached to the model",
+ p.getModel() == model);
+ assertFalse("Property is in the model", model.containsResource(p));
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());
+ txnBegin(model);
+ p = model.getProperty("abc#", "def");
+ txnCommit(model);
+ assertTrue("Property is not attached to the model",
+ p.getModel() == model);
+ assertFalse("Property is in the model", model.containsResource(p));
+ 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),
[... 3940 lines stripped ...]