0001:/*
0002: * Copyright Aduna (http://www.aduna-software.com/) (c) 1997-2007.
0003: *
0004: * Licensed under the Aduna BSD-style license.
0005: */
0006:package org.openrdf.repository;
0007:
0008:import java.io.ByteArrayInputStream;
0009:import java.io.ByteArrayOutputStream;
0010:import java.io.IOException;
0011:import java.io.InputStream;
0012:import java.io.InputStreamReader;
0013:import java.io.ObjectInputStream;
0014:import java.io.ObjectOutputStream;
0015:import java.io.Reader;
0016:import java.io.StringReader;
0017:import java.sql.Date;
0018:import java.util.ArrayList;
0019:import java.util.Arrays;
0020:import java.util.Calendar;
0021:import java.util.Collection;
0022:import java.util.HashMap;
0023:import java.util.List;
0024:import java.util.Map;
0025:import java.util.TimeZone;
0026:
0027:import javax.xml.datatype.DatatypeFactory;
0028:import javax.xml.datatype.XMLGregorianCalendar;
0029:
0030:import junit.framework.TestCase;
0031:
0032:import info.aduna.iteration.CloseableIteration;
0033:import info.aduna.iteration.Iterations;
0034:
0035:import org.openrdf.model.BNode;
0036:import org.openrdf.model.Graph;
0037:import org.openrdf.model.Literal;
0038:import org.openrdf.model.Namespace;
0039:import org.openrdf.model.Statement;
0040:import org.openrdf.model.URI;
0041:import org.openrdf.model.Value;
0042:import org.openrdf.model.ValueFactory;
0043:import org.openrdf.model.impl.GraphImpl;
0044:import org.openrdf.model.impl.URIImpl;
0045:import org.openrdf.model.vocabulary.RDF;
0046:import org.openrdf.query.BindingSet;
0047:import org.openrdf.query.BooleanQuery;
0048:import org.openrdf.query.GraphQuery;
0049:import org.openrdf.query.GraphQueryResult;
0050:import org.openrdf.query.QueryLanguage;
0051:import org.openrdf.query.TupleQuery;
0052:import org.openrdf.query.TupleQueryResult;
0053:import org.openrdf.query.impl.DatasetImpl;
0054:import org.openrdf.repository.sail.SailRepository;
0055:import org.openrdf.rio.RDFFormat;
0056:import org.openrdf.rio.RDFHandlerException;
0057:import org.openrdf.rio.RDFParseException;
0058:import org.openrdf.rio.helpers.RDFHandlerBase;
0059:import org.openrdf.sail.memory.MemoryStore;
0060:
0061:public abstract class RepositoryConnectionTest extends TestCase {
0062:
0063: protected static final String FOAF_NS = "http://xmlns.com/foaf/0.1/";
0064:
0065: protected static final String DC_NS = "http://purl.org/dc/elements/1.1/";
0066:
0067: public static final String TEST_DIR_PREFIX = "/testcases/";
0068:
0069: protected Repository testRepository;
0070:
0071: protected RepositoryConnection testCon;
0072:
0073: protected RepositoryConnection testCon2;
0074:
0075: protected ValueFactory vf;
0076:
0077: protected BNode bob;
0078:
0079: protected BNode alice;
0080:
0081: protected BNode alexander;
0082:
0083: protected URI name;
0084:
0085: protected URI mbox;
0086:
0087: protected URI publisher;
0088:
0089: protected URI unknownContext;
0090:
0091: protected URI context1;
0092:
0093: protected URI context2;
0094:
0095: protected Literal nameAlice;
0096:
0097: protected Literal nameBob;
0098:
0099: protected Literal mboxAlice;
0100:
0101: protected Literal mboxBob;
0102:
0103: protected Literal Ð?лекÑ?андър;
0104:
0105: public RepositoryConnectionTest(String name) {
0106: super (name);
0107: }
0108:
0109: @Override
0110: protected void setUp()
0111: throws Exception
0112: {
0113: testRepository = createRepository();
0114: testRepository.initialize();
0115:
0116: testCon = testRepository.getConnection();
0117: testCon.clear();
0118: testCon.clearNamespaces();
0119:
0120: testCon2 = testRepository.getConnection();
0121:
0122: vf = testRepository.getValueFactory();
0123:
0124: // Initialize values
0125: bob = vf.createBNode();
0126: alice = vf.createBNode();
0127: alexander = vf.createBNode();
0128:
0129: name = vf.createURI(FOAF_NS + "name");
0130: mbox = vf.createURI(FOAF_NS + "mbox");
0131:
0132: publisher = vf.createURI(DC_NS + "publisher");
0133:
0134: nameAlice = vf.createLiteral("Alice");
0135: nameBob = vf.createLiteral("Bob");
0136:
0137: mboxAlice = vf.createLiteral("alice@example.org");
0138: mboxBob = vf.createLiteral("bob@example.org");
0139:
0140: Ð?лекÑ?андър = vf.createLiteral("Ð?лекÑ?андър");
0141:
0142: unknownContext = new URIImpl("urn:unknownContext");
0143:
0144: context1 = vf.createURI("urn:x-local:graph1");
0145: context2 = vf.createURI("urn:x-local:graph2");
0146: }
0147:
0148: @Override
0149: protected void tearDown()
0150: throws Exception
0151: {
0152: testCon2.close();
0153: testCon2 = null;
0154:
0155: testCon.close();
0156: testCon = null;
0157:
0158: testRepository.shutDown();
0159: testRepository = null;
0160:
0161: vf = null;
0162: }
0163:
0164: /**
0165: * Gets an (uninitialized) instance of the repository that should be tested.
0166: *
0167: * @return an uninitialized repository.
0168: */
0169: protected abstract Repository createRepository()
0170: throws Exception;
0171:
0172: public void testAddStatement()
0173: throws Exception
0174: {
0175: testCon.add(bob, name, nameBob);
0176:
0177: assertTrue("Repository should contain newly added statement", testCon.hasStatement(bob, name, nameBob,
0178: false));
0179:
0180: Statement statement = vf.createStatement(alice, name, nameAlice);
0181: testCon.add(statement);
0182:
0183: assertTrue("Repository should contain newly added statement", testCon.hasStatement(statement, false));
0184: assertTrue("Repository should contain newly added statement", testCon.hasStatement(alice, name,
0185: nameAlice, false));
0186:
0187: Repository tempRep = new SailRepository(new MemoryStore());
0188: tempRep.initialize();
0189: RepositoryConnection con = tempRep.getConnection();
0190:
0191: con.add(testCon.getStatements(null, null, null, false));
0192:
0193: assertTrue("Temp Repository should contain newly added statement", con.hasStatement(bob, name, nameBob,
0194: false));
0195: con.close();
0196: tempRep.shutDown();
0197: }
0198:
0199: public void testTransactionIsolation()
0200: throws Exception
0201: {
0202: testCon.setAutoCommit(false);
0203: testCon.add(bob, name, nameBob);
0204:
0205: assertTrue(testCon.hasStatement(bob, name, nameBob, false));
0206: assertFalse(testCon2.hasStatement(bob, name, nameBob, false));
0207:
0208: testCon.commit();
0209:
0210: assertTrue(testCon.hasStatement(bob, name, nameBob, false));
0211: assertTrue(testCon2.hasStatement(bob, name, nameBob, false));
0212: }
0213:
0214: public void testAddReader()
0215: throws Exception
0216: {
0217: InputStream defaultGraphStream = RepositoryConnectionTest.class.getResourceAsStream(TEST_DIR_PREFIX
0218: + "default-graph.ttl");
0219: Reader defaultGraph = new InputStreamReader(defaultGraphStream, "UTF-8");
0220:
0221: testCon.add(defaultGraph, "", RDFFormat.TURTLE);
0222:
0223: defaultGraph.close();
0224:
0225: assertTrue("Repository should contain newly added statements", testCon.hasStatement(null, publisher,
0226: nameBob, false));
0227: assertTrue("Repository should contain newly added statements", testCon.hasStatement(null, publisher,
0228: nameAlice, false));
0229:
0230: // add file graph1.ttl to context1
0231: InputStream graph1Stream = RepositoryConnectionTest.class.getResourceAsStream(TEST_DIR_PREFIX
0232: + "graph1.ttl");
0233: Reader graph1 = new InputStreamReader(graph1Stream, "UTF-8");
0234:
0235: testCon.add(graph1, "", RDFFormat.TURTLE, context1);
0236:
0237: graph1.close();
0238:
0239: // add file graph2.ttl to context2
0240: InputStream graph2Stream = RepositoryConnectionTest.class.getResourceAsStream(TEST_DIR_PREFIX
0241: + "graph2.ttl");
0242: Reader graph2 = new InputStreamReader(graph2Stream, "UTF-8");
0243:
0244: testCon.add(graph2, "", RDFFormat.TURTLE, context2);
0245:
0246: graph2.close();
0247:
0248: assertTrue("alice should be known in the store", testCon.hasStatement(null, name, nameAlice, false));
0249:
0250: assertFalse("alice should not be known in context1", testCon.hasStatement(null, name, nameAlice, false,
0251: context1));
0252: assertTrue("alice should be known in context2", testCon.hasStatement(null, name, nameAlice, false,
0253: context2));
0254:
0255: assertTrue("bob should be known in the store", testCon.hasStatement(null, name, nameBob, false));
0256:
0257: assertFalse("bob should not be known in context2", testCon.hasStatement(null, name, nameBob, false,
0258: context2));
0259: assertTrue("bib should be known in context1",
0260: testCon.hasStatement(null, name, nameBob, false, context1));
0261:
0262: }
0263:
0264: public void testAddInputStream()
0265: throws Exception
0266: {
0267: // add file default-graph.ttl to repository, no context
0268: InputStream defaultGraph = RepositoryConnectionTest.class.getResourceAsStream(TEST_DIR_PREFIX
0269: + "default-graph.ttl");
0270:
0271: testCon.add(defaultGraph, "", RDFFormat.TURTLE);
0272:
0273: defaultGraph.close();
0274:
0275: assertTrue("Repository should contain newly added statements", testCon.hasStatement(null, publisher,
0276: nameBob, false));
0277: assertTrue("Repository should contain newly added statements", testCon.hasStatement(null, publisher,
0278: nameAlice, false));
0279:
0280: // add file graph1.ttl to context1
0281: InputStream graph1 = RepositoryConnectionTest.class.getResourceAsStream(TEST_DIR_PREFIX + "graph1.ttl");
0282:
0283: testCon.add(graph1, "", RDFFormat.TURTLE, context1);
0284:
0285: graph1.close();
0286:
0287: // add file graph2.ttl to context2
0288: InputStream graph2 = RepositoryConnectionTest.class.getResourceAsStream(TEST_DIR_PREFIX + "graph2.ttl");
0289:
0290: testCon.add(graph2, "", RDFFormat.TURTLE, context2);
0291:
0292: graph2.close();
0293:
0294: assertTrue("alice should be known in the store", testCon.hasStatement(null, name, nameAlice, false));
0295:
0296: assertFalse("alice should not be known in context1", testCon.hasStatement(null, name, nameAlice, false,
0297: context1));
0298: assertTrue("alice should be known in context2", testCon.hasStatement(null, name, nameAlice, false,
0299: context2));
0300:
0301: assertTrue("bob should be known in the store", testCon.hasStatement(null, name, nameBob, false));
0302:
0303: assertFalse("bob should not be known in context2", testCon.hasStatement(null, name, nameBob, false,
0304: context2));
0305: assertTrue("bib should be known in context1",
0306: testCon.hasStatement(null, name, nameBob, false, context1));
0307:
0308: }
0309:
0310: public void testAutoCommit()
0311: throws Exception
0312: {
0313: testCon.setAutoCommit(false);
0314: testCon.add(alice, name, nameAlice);
0315:
0316: assertTrue("Uncommitted update should be visible to own connection", testCon.hasStatement(alice, name,
0317: nameAlice, false));
0318:
0319: testCon.commit();
0320:
0321: assertTrue("Repository should contain statement after commit", testCon.hasStatement(alice, name,
0322: nameAlice, false));
0323:
0324: testCon.setAutoCommit(true);
0325: }
0326:
0327: public void testRollback()
0328: throws Exception
0329: {
0330: testCon.setAutoCommit(false);
0331: testCon.add(alice, name, nameAlice);
0332:
0333: assertTrue("Uncommitted updates should be visible to own connection", testCon.hasStatement(alice, name,
0334: nameAlice, false));
0335:
0336: testCon.rollback();
0337:
0338: assertFalse("Repository should not contain statement after rollback", testCon.hasStatement(alice, name,
0339: nameAlice, false));
0340:
0341: testCon.setAutoCommit(true);
0342: }
0343:
0344: public void testSimpleTupleQuery()
0345: throws Exception
0346: {
0347: testCon.add(alice, name, nameAlice, context2);
0348: testCon.add(alice, mbox, mboxAlice, context2);
0349: testCon.add(context2, publisher, nameAlice);
0350:
0351: testCon.add(bob, name, nameBob, context1);
0352: testCon.add(bob, mbox, mboxBob, context1);
0353: testCon.add(context1, publisher, nameBob);
0354:
0355: StringBuilder queryBuilder = new StringBuilder();
0356: queryBuilder.append(" SELECT name, mbox");
0357: queryBuilder.append(" FROM {} foaf:name {name};");
0358: queryBuilder.append(" foaf:mbox {mbox}");
0359: queryBuilder.append(" USING NAMESPACE foaf = <" + FOAF_NS + ">");
0360:
0361: TupleQueryResult result = testCon.prepareTupleQuery(QueryLanguage.SERQL, queryBuilder.toString()).evaluate();
0362:
0363: try {
0364: assertTrue(result != null);
0365: assertTrue(result.hasNext());
0366:
0367: while (result.hasNext()) {
0368: BindingSet solution = result.next();
0369: assertTrue(solution.hasBinding("name"));
0370: assertTrue(solution.hasBinding("mbox"));
0371:
0372: Value nameResult = solution.getValue("name");
0373: Value mboxResult = solution.getValue("mbox");
0374:
0375: assertTrue((nameAlice.equals(nameResult) || nameBob.equals(nameResult)));
0376: assertTrue((mboxAlice.equals(mboxResult) || mboxBob.equals(mboxResult)));
0377: }
0378: }
0379: finally {
0380: result.close();
0381: }
0382: }
0383:
0384: public void testSimpleTupleQueryUnicode()
0385: throws Exception
0386: {
0387: testCon.add(alexander, name, Ð?лекÑ?андър);
0388:
0389: StringBuilder queryBuilder = new StringBuilder();
0390: queryBuilder.append(" SELECT person");
0391: queryBuilder.append(" FROM {person} foaf:name {").append(Ð?лекÑ?андър.getLabel()).append("}");
0392: queryBuilder.append(" USING NAMESPACE foaf = <" + FOAF_NS + ">");
0393:
0394: TupleQueryResult result = testCon.prepareTupleQuery(QueryLanguage.SERQL, queryBuilder.toString()).evaluate();
0395:
0396: try {
0397: assertTrue(result != null);
0398: assertTrue(result.hasNext());
0399:
0400: while (result.hasNext()) {
0401: BindingSet solution = result.next();
0402: assertTrue(solution.hasBinding("person"));
0403: assertEquals(alexander, solution.getValue("person"));
0404: }
0405: }
0406: finally {
0407: result.close();
0408: }
0409: }
0410:
0411: public void testPreparedTupleQuery()
0412: throws Exception
0413: {
0414: testCon.add(alice, name, nameAlice, context2);
0415: testCon.add(alice, mbox, mboxAlice, context2);
0416: testCon.add(context2, publisher, nameAlice);
0417:
0418: testCon.add(bob, name, nameBob, context1);
0419: testCon.add(bob, mbox, mboxBob, context1);
0420: testCon.add(context1, publisher, nameBob);
0421:
0422: StringBuilder queryBuilder = new StringBuilder();
0423: queryBuilder.append(" SELECT name, mbox");
0424: queryBuilder.append(" FROM {} foaf:name {name};");
0425: queryBuilder.append(" foaf:mbox {mbox}");
0426: queryBuilder.append(" USING NAMESPACE foaf = <" + FOAF_NS + ">");
0427:
0428: TupleQuery query = testCon.prepareTupleQuery(QueryLanguage.SERQL, queryBuilder.toString());
0429: query.setBinding("name", nameBob);
0430:
0431: TupleQueryResult result = query.evaluate();
0432:
0433: try {
0434: assertTrue(result != null);
0435: assertTrue(result.hasNext());
0436:
0437: while (result.hasNext()) {
0438: BindingSet solution = result.next();
0439: assertTrue(solution.hasBinding("name"));
0440: assertTrue(solution.hasBinding("mbox"));
0441:
0442: Value nameResult = solution.getValue("name");
0443: Value mboxResult = solution.getValue("mbox");
0444:
0445: assertTrue("unexpected value for name: " + nameResult, nameBob.equals(nameResult));
0446: assertTrue("unexpected value for mbox: " + mboxResult, mboxBob.equals(mboxResult));
0447: }
0448: }
0449: finally {
0450: result.close();
0451: }
0452: }
0453:
0454: public void testPreparedTupleQueryUnicode()
0455: throws Exception
0456: {
0457: testCon.add(alexander, name, Ð?лекÑ?андър);
0458:
0459: StringBuilder queryBuilder = new StringBuilder();
0460: queryBuilder.append(" SELECT person");
0461: queryBuilder.append(" FROM {person} foaf:name {name}");
0462: queryBuilder.append(" USING NAMESPACE foaf = <" + FOAF_NS + ">");
0463:
0464: TupleQuery query = testCon.prepareTupleQuery(QueryLanguage.SERQL, queryBuilder.toString());
0465: query.setBinding("name", Ð?лекÑ?андър);
0466:
0467: TupleQueryResult result = query.evaluate();
0468:
0469: try {
0470: assertTrue(result != null);
0471: assertTrue(result.hasNext());
0472:
0473: while (result.hasNext()) {
0474: BindingSet solution = result.next();
0475: assertTrue(solution.hasBinding("person"));
0476: assertEquals(alexander, solution.getValue("person"));
0477: }
0478: }
0479: finally {
0480: result.close();
0481: }
0482: }
0483:
0484: public void testSimpleGraphQuery()
0485: throws Exception
0486: {
0487: testCon.add(alice, name, nameAlice, context2);
0488: testCon.add(alice, mbox, mboxAlice, context2);
0489: testCon.add(context2, publisher, nameAlice);
0490:
0491: testCon.add(bob, name, nameBob, context1);
0492: testCon.add(bob, mbox, mboxBob, context1);
0493: testCon.add(context1, publisher, nameBob);
0494:
0495: StringBuilder queryBuilder = new StringBuilder();
0496: queryBuilder.append(" CONSTRUCT *");
0497: queryBuilder.append(" FROM {} foaf:name {name};");
0498: queryBuilder.append(" foaf:mbox {mbox}");
0499: queryBuilder.append(" USING NAMESPACE foaf = <" + FOAF_NS + ">");
0500:
0501: GraphQueryResult result = testCon.prepareGraphQuery(QueryLanguage.SERQL, queryBuilder.toString()).evaluate();
0502:
0503: try {
0504: assertTrue(result != null);
0505: assertTrue(result.hasNext());
0506:
0507: while (result.hasNext()) {
0508: Statement st = result.next();
0509: if (name.equals(st.getPredicate())) {
0510: assertTrue(nameAlice.equals(st.getObject()) || nameBob.equals(st.getObject()));
0511: }
0512: else {
0513: assertTrue(mbox.equals(st.getPredicate()));
0514: assertTrue(mboxAlice.equals(st.getObject()) || mboxBob.equals(st.getObject()));
0515: }
0516: }
0517: }
0518: finally {
0519: result.close();
0520: }
0521: }
0522:
0523: public void testPreparedGraphQuery()
0524: throws Exception
0525: {
0526: testCon.add(alice, name, nameAlice, context2);
0527: testCon.add(alice, mbox, mboxAlice, context2);
0528: testCon.add(context2, publisher, nameAlice);
0529:
0530: testCon.add(bob, name, nameBob, context1);
0531: testCon.add(bob, mbox, mboxBob, context1);
0532: testCon.add(context1, publisher, nameBob);
0533:
0534: StringBuilder queryBuilder = new StringBuilder();
0535: queryBuilder.append(" CONSTRUCT *");
0536: queryBuilder.append(" FROM {} foaf:name {name};");
0537: queryBuilder.append(" foaf:mbox {mbox}");
0538: queryBuilder.append(" USING NAMESPACE foaf = <" + FOAF_NS + ">");
0539:
0540: GraphQuery query = testCon.prepareGraphQuery(QueryLanguage.SERQL, queryBuilder.toString());
0541: query.setBinding("name", nameBob);
0542:
0543: GraphQueryResult result = query.evaluate();
0544:
0545: try {
0546: assertTrue(result != null);
0547: assertTrue(result.hasNext());
0548:
0549: while (result.hasNext()) {
0550: Statement st = result.next();
0551: assertTrue(name.equals(st.getPredicate()) || mbox.equals(st.getPredicate()));
0552: if (name.equals(st.getPredicate())) {
0553: assertTrue("unexpected value for name: " + st.getObject(), nameBob.equals(st.getObject()));
0554: }
0555: else {
0556: assertTrue(mbox.equals(st.getPredicate()));
0557: assertTrue("unexpected value for mbox: " + st.getObject(), mboxBob.equals(st.getObject()));
0558: }
0559:
0560: }
0561: }
0562: finally {
0563: result.close();
0564: }
0565: }
0566:
0567: public void testSimpleBooleanQuery()
0568: throws Exception
0569: {
0570: testCon.add(alice, name, nameAlice, context2);
0571: testCon.add(alice, mbox, mboxAlice, context2);
0572: testCon.add(context2, publisher, nameAlice);
0573:
0574: testCon.add(bob, name, nameBob, context1);
0575: testCon.add(bob, mbox, mboxBob, context1);
0576: testCon.add(context1, publisher, nameBob);
0577:
0578: StringBuilder queryBuilder = new StringBuilder();
0579: queryBuilder.append("PREFIX foaf: <" + FOAF_NS + "> ");
0580: queryBuilder.append("ASK ");
0581: queryBuilder.append("{ ?p foaf:name ?name }");
0582:
0583: boolean exists = testCon.prepareBooleanQuery(QueryLanguage.SPARQL, queryBuilder.toString()).evaluate();
0584:
0585: assertTrue(exists);
0586: }
0587:
0588: public void testPreparedBooleanQuery()
0589: throws Exception
0590: {
0591: testCon.add(alice, name, nameAlice, context2);
0592: testCon.add(alice, mbox, mboxAlice, context2);
0593: testCon.add(context2, publisher, nameAlice);
0594:
0595: testCon.add(bob, name, nameBob, context1);
0596: testCon.add(bob, mbox, mboxBob, context1);
0597: testCon.add(context1, publisher, nameBob);
0598:
0599: StringBuilder queryBuilder = new StringBuilder();
0600: queryBuilder.append("PREFIX foaf: <" + FOAF_NS + "> ");
0601: queryBuilder.append("ASK ");
0602: queryBuilder.append("{ ?p foaf:name ?name }");
0603:
0604: BooleanQuery query = testCon.prepareBooleanQuery(QueryLanguage.SPARQL, queryBuilder.toString());
0605: query.setBinding("name", nameBob);
0606:
0607: assertTrue(query.evaluate());
0608: }
0609:
0610: public void testDataset()
0611: throws Exception
0612: {
0613: testCon.add(alice, name, nameAlice, context2);
0614: testCon.add(alice, mbox, mboxAlice, context2);
0615: testCon.add(context2, publisher, nameAlice);
0616:
0617: testCon.add(bob, name, nameBob, context1);
0618: testCon.add(bob, mbox, mboxBob, context1);
0619: testCon.add(context1, publisher, nameBob);
0620:
0621: StringBuilder queryBuilder = new StringBuilder();
0622: queryBuilder.append("PREFIX foaf: <" + FOAF_NS + "> ");
0623: queryBuilder.append("ASK ");
0624: queryBuilder.append("{ ?p foaf:name ?name }");
0625:
0626: BooleanQuery query = testCon.prepareBooleanQuery(QueryLanguage.SPARQL, queryBuilder.toString());
0627: query.setBinding("name", nameBob);
0628:
0629: assertTrue(query.evaluate());
0630:
0631: DatasetImpl dataset = new DatasetImpl();
0632:
0633: // default graph: {context1}
0634: dataset.addDefaultGraph(context1);
0635: query.setDataset(dataset);
0636: assertTrue(query.evaluate());
0637:
0638: // default graph: {context1, context2}
0639: dataset.addDefaultGraph(context2);
0640: query.setDataset(dataset);
0641: assertTrue(query.evaluate());
0642:
0643: // default graph: {context2}
0644: dataset.removeDefaultGraph(context1);
0645: query.setDataset(dataset);
0646: assertFalse(query.evaluate());
0647:
0648: queryBuilder.setLength(0);
0649: queryBuilder.append("PREFIX foaf: <" + FOAF_NS + "> ");
0650: queryBuilder.append("ASK ");
0651: queryBuilder.append("{ GRAPH ?g { ?p foaf:name ?name } }");
0652:
0653: query = testCon.prepareBooleanQuery(QueryLanguage.SPARQL, queryBuilder.toString());
0654: query.setBinding("name", nameBob);
0655:
0656: // default graph: {context2}; named graph: {}
0657: query.setDataset(dataset);
0658: assertFalse(query.evaluate());
0659:
0660: // default graph: {context1, context2}; named graph: {context2}
0661: dataset.addDefaultGraph(context1);
0662: dataset.addNamedGraph(context2);
0663: query.setDataset(dataset);
0664: assertFalse(query.evaluate());
0665:
0666: // default graph: {context1, context2}; named graph: {context1, context2}
0667: dataset.addNamedGraph(context1);
0668: query.setDataset(dataset);
0669: assertTrue(query.evaluate());
0670: }
0671:
0672: public void testGetStatements()
0673: throws Exception
0674: {
0675: testCon.add(bob, name, nameBob);
0676:
0677: assertTrue("Repository should contain statement", testCon.hasStatement(bob, name, nameBob, false));
0678:
0679: RepositoryResult<Statement> result = testCon.getStatements(null, name, null, false);
0680:
0681: try {
0682: assertTrue("Iterator should not be null", result != null);
0683: assertTrue("Iterator should not be empty", result.hasNext());
0684:
0685: while (result.hasNext()) {
0686: Statement st = result.next();
0687: assertTrue("Statement should not be in a context ", st.getContext() == null);
0688: assertTrue("Statement predicate should be equal to name ", st.getPredicate().equals(name));
0689: }
0690: }
0691: finally {
0692: result.close();
0693: }
0694:
0695: List<Statement> list = Iterations.addAll(testCon.getStatements(null, name, null, false),
0696: new ArrayList<Statement>());
0697:
0698: assertTrue("List should not be null", list != null);
0699: assertFalse("List should not be empty", list.isEmpty());
0700: }
0701:
0702: public void testGetStatementsInSingleContext()
0703: throws Exception
0704: {
0705: testCon.setAutoCommit(false);
0706: testCon.add(bob, name, nameBob, context1);
0707: testCon.add(bob, mbox, mboxBob, context1);
0708: testCon.add(context1, publisher, nameBob);
0709:
0710: testCon.add(alice, name, nameAlice, context2);
0711: testCon.add(alice, mbox, mboxAlice, context2);
0712: testCon.add(context2, publisher, nameAlice);
0713: testCon.setAutoCommit(true);
0714:
0715: assertTrue("Repository should contain statement", testCon.hasStatement(bob, name, nameBob, false));
0716:
0717: assertTrue("Repository should contain statement in context1", testCon.hasStatement(bob, name, nameBob,
0718: false, context1));
0719:
0720: assertFalse("Repository should not contain statement in context2", testCon.hasStatement(bob, name,
0721: nameBob, false, context2));
0722:
0723: // Check handling of getStatements without context IDs
0724: RepositoryResult<Statement> result = testCon.getStatements(bob, name, null, false);
0725: try {
0726: while (result.hasNext()) {
0727: Statement st = result.next();
0728: assertTrue(bob.equals(st.getSubject()));
0729: assertTrue(name.equals(st.getPredicate()));
0730: assertTrue(nameBob.equals(st.getObject()));
0731: assertTrue(context1.equals(st.getContext()));
0732: }
0733: }
0734: finally {
0735: result.close();
0736: }
0737:
0738: // Check handling of getStatements with a known context ID
0739: result = testCon.getStatements(null, null, null, false, context1);
0740: try {
0741: while (result.hasNext()) {
0742: Statement st = result.next();
0743: assertTrue(context1.equals(st.getContext()));
0744: }
0745: }
0746: finally {
0747: result.close();
0748: }
0749:
0750: // Check handling of getStatements with an unknown context ID
0751: result = testCon.getStatements(null, null, null, false, unknownContext);
0752: try {
0753: assertTrue(result != null);
0754: assertFalse(result.hasNext());
0755: }
0756: finally {
0757: result.close();
0758: }
0759:
0760: List<Statement> list = Iterations.addAll(testCon.getStatements(null, name, null, false, context1),
0761: new ArrayList<Statement>());
0762:
0763: assertTrue("List should not be null", list != null);
0764: assertFalse("List should not be empty", list.isEmpty());
0765: }
0766:
0767: public void testGetStatementsInMultipleContexts()
0768: throws Exception
0769: {
0770: testCon.clear();
0771:
0772: testCon.setAutoCommit(false);
0773: testCon.add(alice, name, nameAlice, context2);
0774: testCon.add(alice, mbox, mboxAlice, context2);
0775: testCon.add(context2, publisher, nameAlice);
0776: testCon.setAutoCommit(true);
0777:
0778: // get statements with either no context or context2
0779: CloseableIteration<? extends Statement, RepositoryException> iter = testCon.getStatements(null, null,
0780: null, false, null, context2);
0781:
0782: try {
0783: int count = 0;
0784: while (iter.hasNext()) {
0785: count++;
0786: Statement st = iter.next();
0787:
0788: assertTrue(st.getContext() == null || context2.equals(st.getContext()));
0789: }
0790:
0791: assertEquals("there should be three statements", 3, count);
0792: }
0793: finally {
0794: iter.close();
0795: }
0796:
0797: // get all statements with context1 or context2. Note that context1 and
0798: // context2 are both known
0799: // in the store because they have been created through the store's own
0800: // value vf.
0801: iter = testCon.getStatements(null, null, null, false, context1, context2);
0802:
0803: try {
0804: int count = 0;
0805: while (iter.hasNext()) {
0806: count++;
0807: Statement st = iter.next();
0808: // we should have _only_ statements from context2
0809: assertTrue(context2.equals(st.getContext()));
0810: }
0811: assertEquals("there should be two statements", 2, count);
0812: }
0813: finally {
0814: iter.close();
0815: }
0816:
0817: // get all statements with unknownContext or context2.
0818: iter = testCon.getStatements(null, null, null, false, unknownContext, context2);
0819:
0820: try {
0821: int count = 0;
0822: while (iter.hasNext()) {
0823: count++;
0824: Statement st = iter.next();
0825: // we should have _only_ statements from context2
0826: assertTrue(context2.equals(st.getContext()));
0827: }
0828: assertEquals("there should be two statements", 2, count);
0829: }
0830: finally {
0831: iter.close();
0832: }
0833:
0834: // add statements to context1
0835: testCon.setAutoCommit(false);
0836: testCon.add(bob, name, nameBob, context1);
0837: testCon.add(bob, mbox, mboxBob, context1);
0838: testCon.add(context1, publisher, nameBob);
0839: testCon.setAutoCommit(true);
0840:
0841: iter = testCon.getStatements(null, null, null, false, context1);
0842: try {
0843: assertTrue(iter != null);
0844: assertTrue(iter.hasNext());
0845: }
0846: finally {
0847: iter.close();
0848: }
0849:
0850: // get statements with either no context or context2
0851: iter = testCon.getStatements(null, null, null, false, null, context2);
0852: try {
0853: int count = 0;
0854: while (iter.hasNext()) {
0855: count++;
0856: Statement st = iter.next();
0857: // we should have _only_ statements from context2, or without
0858: // context
0859: assertTrue(st.getContext() == null || context2.equals(st.getContext()));
0860: }
0861: assertEquals("there should be four statements", 4, count);
0862: }
0863: finally {
0864: iter.close();
0865: }
0866:
0867: // get all statements with context1 or context2
0868: iter = testCon.getStatements(null, null, null, false, context1, context2);
0869:
0870: try {
0871: int count = 0;
0872: while (iter.hasNext()) {
0873: count++;
0874: Statement st = iter.next();
0875: assertTrue(context1.equals(st.getContext()) || context2.equals(st.getContext()));
0876: }
0877: assertEquals("there should be four statements", 4, count);
0878: }
0879: finally {
0880: iter.close();
0881: }
0882: }
0883:
0884: public void testDuplicateFilter()
0885: throws Exception
0886: {
0887: testCon.setAutoCommit(false);
0888: testCon.add(bob, name, nameBob);
0889: testCon.add(bob, name, nameBob, context1);
0890: testCon.add(bob, name, nameBob, context2);
0891: testCon.setAutoCommit(true);
0892:
0893: RepositoryResult<Statement> result = testCon.getStatements(bob, name, null, true);
0894: result.enableDuplicateFilter();
0895:
0896: int count = 0;
0897: while (result.hasNext()) {
0898: result.next();
0899: count++;
0900: }
0901: assertEquals(1, count);
0902: }
0903:
0904: public void testRemoveStatements()
0905: throws Exception
0906: {
0907: testCon.setAutoCommit(false);
0908: testCon.add(bob, name, nameBob);
0909: testCon.add(alice, name, nameAlice);
0910: testCon.setAutoCommit(true);
0911:
0912: assertTrue(testCon.hasStatement(bob, name, nameBob, false));
0913: assertTrue(testCon.hasStatement(alice, name, nameAlice, false));
0914:
0915: testCon.remove(bob, name, nameBob);
0916:
0917: assertFalse(testCon.hasStatement(bob, name, nameBob, false));
0918: assertTrue(testCon.hasStatement(alice, name, nameAlice, false));
0919:
0920: testCon.remove(alice, null, null);
0921: assertFalse(testCon.hasStatement(alice, name, nameAlice, false));
0922: assertTrue(testCon.isEmpty());
0923: }
0924:
0925: public void testRemoveStatementCollection()
0926: throws Exception
0927: {
0928: testCon.setAutoCommit(false);
0929: testCon.add(alice, name, nameAlice);
0930: testCon.add(bob, name, nameBob);
0931: testCon.setAutoCommit(true);
0932:
0933: assertTrue(testCon.hasStatement(bob, name, nameBob, false));
0934: assertTrue(testCon.hasStatement(alice, name, nameAlice, false));
0935:
0936: Collection<Statement> c = Iterations.addAll(testCon.getStatements(null, null, null, false),
0937: new ArrayList<Statement>());
0938:
0939: testCon.remove(c);
0940:
0941: assertFalse(testCon.hasStatement(bob, name, nameBob, false));
0942: assertFalse(testCon.hasStatement(alice, name, nameAlice, false));
0943: }
0944:
0945: public void testRemoveStatementIteration()
0946: throws Exception
0947: {
0948: testCon.setAutoCommit(false);
0949: testCon.add(alice, name, nameAlice);
0950: testCon.add(bob, name, nameBob);
0951: testCon.setAutoCommit(true);
0952:
0953: assertTrue(testCon.hasStatement(bob, name, nameBob, false));
0954: assertTrue(testCon.hasStatement(alice, name, nameAlice, false));
0955:
0956: CloseableIteration<? extends Statement, RepositoryException> iter = testCon.getStatements(null, null,
0957: null, false);
0958:
0959: try {
0960: testCon.remove(iter);
0961: }
0962: finally {
0963: iter.close();
0964: }
0965:
0966: assertFalse(testCon.hasStatement(bob, name, nameBob, false));
0967: assertFalse(testCon.hasStatement(alice, name, nameAlice, false));
0968:
0969: }
0970:
0971: public void testGetNamespaces()
0972: throws Exception
0973: {
0974: StringBuilder rdfFragment = new StringBuilder();
0975: rdfFragment.append("<rdf:RDF\n");
0976: rdfFragment.append(" xmlns:example='http://example.org/'\n");
0977: rdfFragment.append(" xmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#'\n");
0978: rdfFragment.append(" xmlns:rdfs='http://www.w3.org/2000/01/rdf-schema#' >\n");
0979: rdfFragment.append(" <rdf:Description rdf:about='http://example.org/Main'>\n");
0980: rdfFragment.append(" <rdfs:label>Main Node</rdfs:label>\n");
0981: rdfFragment.append(" </rdf:Description>\n");
0982: rdfFragment.append("</rdf:RDF>");
0983:
0984: testCon.add(new StringReader(rdfFragment.toString()), "", RDFFormat.RDFXML);
0985:
0986: CloseableIteration<? extends Namespace, RepositoryException> nsIter = testCon.getNamespaces();
0987: try {
0988: Map<String, String> map = new HashMap<String, String>();
0989: int nsCount = 0;
0990: while (nsIter.hasNext()) {
0991: nsCount++;
0992: Namespace ns = nsIter.next();
0993: map.put(ns.getPrefix(), ns.getName());
0994: }
0995:
0996: assertEquals("There should be exactly three namespaces", 3, nsCount);
0997: assertTrue("namespace for prefix 'example' should exist", map.containsKey("example"));
0998: assertTrue("namespace for prefix 'rdfs' should exist", map.containsKey("rdfs"));
0999: assertTrue("namespace for prefix 'rdf' should exist", map.containsKey("rdf"));
1000:
1001: assertTrue("namespace name for 'example' not well-defined", map.get("example").equals(
1002: "http://example.org/"));
1003: assertTrue("namespace name for 'rdfs' not well-defined", map.get("rdfs").equals(
1004: "http://www.w3.org/2000/01/rdf-schema#"));
1005: assertTrue("namespace name for 'rdf' not well-defined", map.get("rdf").equals(
1006: "http://www.w3.org/1999/02/22-rdf-syntax-ns#"));
1007: }
1008: finally {
1009: nsIter.close();
1010: }
1011: }
1012:
1013: public void testClear()
1014: throws Exception
1015: {
1016: testCon.add(bob, name, nameBob);
1017: assertTrue(testCon.hasStatement(null, name, nameBob, false));
1018: testCon.clear();
1019: assertFalse(testCon.hasStatement(null, name, nameBob, false));
1020: }
1021:
1022: public void testRecoverFromParseError()
1023: throws RepositoryException, IOException
1024: {
1025: String invalidData = "bad";
1026: String validData = "@prefix foo: <http://example.org/foo#>.\nfoo:a foo:b foo:c.";
1027:
1028: try {
1029: testCon.add(new StringReader(invalidData), "", RDFFormat.TURTLE);
1030: fail("Invalid data should result in an exception");
1031: }
1032: catch (RDFParseException e) {
1033: // Expected behaviour
1034: }
1035:
1036: try {
1037: testCon.add(new StringReader(validData), "", RDFFormat.TURTLE);
1038: }
1039: catch (RDFParseException e) {
1040: fail("Valid data should not result in an exception");
1041: }
1042:
1043: assertEquals("Repository contains incorrect number of statements", 1, testCon.size());
1044: }
1045:
1046: public void testStatementSerialization()
1047: throws Exception
1048: {
1049: testCon.add(bob, name, nameBob);
1050:
1051: Statement st;
1052: RepositoryResult<Statement> statements = testCon.getStatements(null, null, null, true);
1053: try {
1054: st = statements.next();
1055: }
1056: finally {
1057: statements.close();
1058: }
1059:
1060: ByteArrayOutputStream baos = new ByteArrayOutputStream();
1061: ObjectOutputStream out = new ObjectOutputStream(baos);
1062: out.writeObject(st);
1063: out.close();
1064:
1065: ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
1066: ObjectInputStream in = new ObjectInputStream(bais);
1067: Statement deserializedStatement = (Statement)in.readObject();
1068: in.close();
1069:
1070: assertTrue(st.equals(deserializedStatement));
1071:
1072: assertTrue(testCon.hasStatement(st, true));
1073: assertTrue(testCon.hasStatement(deserializedStatement, true));
1074: }
1075:
1076: public void testBNodeSerialization()
1077: throws Exception
1078: {
1079: testCon.add(bob, name, nameBob);
1080:
1081: Statement st;
1082: RepositoryResult<Statement> statements = testCon.getStatements(null, null, null, false);
1083: try {
1084: st = statements.next();
1085: }
1086: finally {
1087: statements.close();
1088: }
1089:
1090: BNode bnode = (BNode)st.getSubject();
1091:
1092: ByteArrayOutputStream baos = new ByteArrayOutputStream();
1093: ObjectOutputStream out = new ObjectOutputStream(baos);
1094: out.writeObject(bnode);
1095: out.close();
1096:
1097: ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
1098: ObjectInputStream in = new ObjectInputStream(bais);
1099: BNode deserializedBNode = (BNode)in.readObject();
1100: in.close();
1101:
1102: assertTrue(bnode.equals(deserializedBNode));
1103:
1104: assertTrue(testCon.hasStatement(bnode, name, nameBob, true));
1105: assertTrue(testCon.hasStatement(deserializedBNode, name, nameBob, true));
1106: }
1107:
1108: public void testURISerialization()
1109: throws Exception
1110: {
1111: testCon.add(bob, name, nameBob);
1112:
1113: Statement st;
1114: RepositoryResult<Statement> statements = testCon.getStatements(null, null, null, false);
1115: try {
1116: st = statements.next();
1117: }
1118: finally {
1119: statements.close();
1120: }
1121:
1122: URI uri = st.getPredicate();
1123:
1124: ByteArrayOutputStream baos = new ByteArrayOutputStream();
1125: ObjectOutputStream out = new ObjectOutputStream(baos);
1126: out.writeObject(uri);
1127: out.close();
1128:
1129: ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
1130: ObjectInputStream in = new ObjectInputStream(bais);
1131: URI deserializedURI = (URI)in.readObject();
1132: in.close();
1133:
1134: assertTrue(uri.equals(deserializedURI));
1135:
1136: assertTrue(testCon.hasStatement(bob, uri, nameBob, true));
1137: assertTrue(testCon.hasStatement(bob, deserializedURI, nameBob, true));
1138: }
1139:
1140: public void testLiteralSerialization()
1141: throws Exception
1142: {
1143: testCon.add(bob, name, nameBob);
1144:
1145: Statement st;
1146: RepositoryResult<Statement> statements = testCon.getStatements(null, null, null, false);
1147: try {
1148: st = statements.next();
1149: }
1150: finally {
1151: statements.close();
1152: }
1153:
1154: Literal literal = (Literal)st.getObject();
1155:
1156: ByteArrayOutputStream baos = new ByteArrayOutputStream();
1157: ObjectOutputStream out = new ObjectOutputStream(baos);
1158: out.writeObject(literal);
1159: out.close();
1160:
1161: ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
1162: ObjectInputStream in = new ObjectInputStream(bais);
1163: Literal deserializedLiteral = (Literal)in.readObject();
1164: in.close();
1165:
1166: assertTrue(literal.equals(deserializedLiteral));
1167:
1168: assertTrue(testCon.hasStatement(bob, name, literal, true));
1169: assertTrue(testCon.hasStatement(bob, name, deserializedLiteral, true));
1170: }
1171:
1172: public void testGraphSerialization()
1173: throws Exception
1174: {
1175: testCon.add(bob, name, nameBob);
1176: testCon.add(alice, name, nameAlice);
1177:
1178: Graph graph;
1179: RepositoryResult<Statement> statements = testCon.getStatements(null, null, null, true);
1180: try {
1181: graph = new GraphImpl(vf, statements.asList());
1182: }
1183: finally {
1184: statements.close();
1185: }
1186:
1187: ByteArrayOutputStream baos = new ByteArrayOutputStream();
1188: ObjectOutputStream out = new ObjectOutputStream(baos);
1189: out.writeObject(graph);
1190: out.close();
1191:
1192: ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
1193: ObjectInputStream in = new ObjectInputStream(bais);
1194: Graph deserializedGraph = (Graph)in.readObject();
1195: in.close();
1196:
1197: assertFalse(deserializedGraph.isEmpty());
1198:
1199: for (Statement st : deserializedGraph) {
1200: assertTrue(graph.contains(st));
1201: assertTrue(testCon.hasStatement(st, true));
1202: }
1203: }
1204:
1205: public void testEmptyRollback()
1206: throws Exception
1207: {
1208: assertTrue(testCon.isEmpty());
1209: assertTrue(testCon2.isEmpty());
1210: testCon.setAutoCommit(false);
1211: testCon.add(vf.createBNode(), vf.createURI("urn:pred"), vf.createBNode());
1212: assertFalse(testCon.isEmpty());
1213: assertTrue(testCon2.isEmpty());
1214: testCon.rollback();
1215: assertTrue(testCon.isEmpty());
1216: assertTrue(testCon2.isEmpty());
1217: }
1218:
1219: public void testEmptyCommit()
1220: throws Exception
1221: {
1222: assertTrue(testCon.isEmpty());
1223: assertTrue(testCon2.isEmpty());
1224: testCon.setAutoCommit(false);
1225: testCon.add(vf.createBNode(), vf.createURI("urn:pred"), vf.createBNode());
1226: assertFalse(testCon.isEmpty());
1227: assertTrue(testCon2.isEmpty());
1228: testCon.commit();
1229: assertFalse(testCon.isEmpty());
1230: assertFalse(testCon2.isEmpty());
1231: }
1232:
1233: public void testOpen()
1234: throws Exception
1235: {
1236: assertTrue(testCon.isOpen());
1237: assertTrue(testCon2.isOpen());
1238: testCon.close();
1239: assertFalse(testCon.isOpen());
1240: assertTrue(testCon2.isOpen());
1241: }
1242:
1243: public void testSizeRollback()
1244: throws Exception
1245: {
1246: assertEquals(0, testCon.size());
1247: assertEquals(0, testCon2.size());
1248: testCon.setAutoCommit(false);
1249: testCon.add(vf.createBNode(), vf.createURI("urn:pred"), vf.createBNode());
1250: assertEquals(1, testCon.size());
1251: assertEquals(0, testCon2.size());
1252: testCon.add(vf.createBNode(), vf.createURI("urn:pred"), vf.createBNode());
1253: assertEquals(2, testCon.size());
1254: assertEquals(0, testCon2.size());
1255: testCon.rollback();
1256: assertEquals(0, testCon.size());
1257: assertEquals(0, testCon2.size());
1258: }
1259:
1260: public void testSizeCommit()
1261: throws Exception
1262: {
1263: assertEquals(0, testCon.size());
1264: assertEquals(0, testCon2.size());
1265: testCon.setAutoCommit(false);
1266: testCon.add(vf.createBNode(), vf.createURI("urn:pred"), vf.createBNode());
1267: assertEquals(1, testCon.size());
1268: assertEquals(0, testCon2.size());
1269: testCon.add(vf.createBNode(), vf.createURI("urn:pred"), vf.createBNode());
1270: assertEquals(2, testCon.size());
1271: assertEquals(0, testCon2.size());
1272: testCon.commit();
1273: assertEquals(2, testCon.size());
1274: assertEquals(2, testCon2.size());
1275: }
1276:
1277: public void testAddRemove()
1278: throws Exception
1279: {
1280: URI FOAF_PERSON = vf.createURI("http://xmlns.com/foaf/0.1/Person");
1281: final Statement stmt = vf.createStatement(bob, name, nameBob);
1282:
1283: testCon.add(bob, RDF.TYPE, FOAF_PERSON);
1284:
1285: testCon.setAutoCommit(false);
1286: testCon.add(stmt);
1287: testCon.remove(stmt);
1288: testCon.commit();
1289:
1290: testCon.exportStatements(null, null, null, false, new RDFHandlerBase() {
1291:
1292: @Override
1293: public void handleStatement(Statement st)
1294: throws RDFHandlerException
1295: {
1296: assertTrue(!stmt.equals(st));
1297: }
1298: });
1299: }
1300:
1301: public void testInferredStatementCount()
1302: throws Exception
1303: {
1304: assertTrue(testCon.isEmpty());
1305: int inferred = getTotalStatementCount(testCon);
1306:
1307: URI root = vf.createURI("urn:root");
1308:
1309: testCon.add(root, RDF.TYPE, RDF.LIST);
1310: testCon.remove(root, RDF.TYPE, RDF.LIST);
1311:
1312: assertTrue(testCon.isEmpty());
1313: assertEquals(inferred, getTotalStatementCount(testCon));
1314: }
1315:
1316: public void testGetContextIDs()
1317: throws Exception
1318: {
1319: assertEquals(0, testCon.getContextIDs().asList().size());
1320:
1321: // load data
1322: testCon.setAutoCommit(false);
1323: testCon.add(bob, name, nameBob, context1);
1324: assertEquals(Arrays.asList(context1), testCon.getContextIDs().asList());
1325:
1326: testCon.remove(bob, name, nameBob, context1);
1327: assertEquals(0, testCon.getContextIDs().asList().size());
1328: testCon.setAutoCommit(true);
1329:
1330: assertEquals(0, testCon.getContextIDs().asList().size());
1331:
1332: testCon.add(bob, name, nameBob, context2);
1333: assertEquals(Arrays.asList(context2), testCon.getContextIDs().asList());
1334: }
1335:
1336: public void testXmlCalendarZ() throws Exception {
1337: String NS = "http://example.org/rdf/";
1338: int OFFSET = TimeZone.getDefault().getOffset(new Date(2007, Calendar.NOVEMBER, 6).getTime()) / 1000 / 60;
1339: String SELECT_BY_DATE = "SELECT ?s ?d WHERE { ?s <http://www.w3.org/1999/02/22-rdf-syntax-ns#value> ?d . FILTER (?d <= ?date) }";
1340: DatatypeFactory data = DatatypeFactory.newInstance();
1341: for (int i=1;i<5;i++) {
1342: URI uri = vf.createURI(NS, "date" + i);
1343: XMLGregorianCalendar xcal = data.newXMLGregorianCalendar();
1344: xcal.setYear(2000);
1345: xcal.setMonth(11);
1346: xcal.setDay(i*2);
1347: testCon.add(uri, RDF.VALUE, vf.createLiteral(xcal));
1348: URI uriz = vf.createURI(NS, "dateZ" + i);
1349: xcal = data.newXMLGregorianCalendar();
1350: xcal.setYear(2007);
1351: xcal.setMonth(11);
1352: xcal.setDay(i*2);
1353: xcal.setTimezone(OFFSET);
1354: testCon.add(uriz, RDF.VALUE, vf.createLiteral(xcal));
1355: }
1356: XMLGregorianCalendar xcal = data.newXMLGregorianCalendar();
1357: xcal.setYear(2007);
1358: xcal.setMonth(11);
1359: xcal.setDay(6);
1360: xcal.setTimezone(OFFSET);
1361: TupleQuery query = testCon.prepareTupleQuery(QueryLanguage.SPARQL, SELECT_BY_DATE);
1362: query.setBinding("date", vf.createLiteral(xcal));
1363: TupleQueryResult result = query.evaluate();
1364: List list = new ArrayList();
1365: while (result.hasNext()) {
1366: list.add(result.next());
1367: }
1368: assertEquals(7, list.size());
1369: }
1370:
1371: private int getTotalStatementCount(RepositoryConnection connection)
1372: throws RepositoryException
1373: {
1374: CloseableIteration<? extends Statement, RepositoryException> iter = connection.getStatements(null,
1375: null, null, true);
1376:
1377: try {
1378: int size = 0;
1379:
1380: while (iter.hasNext()) {
1381: iter.next();
1382: ++size;
1383: }
1384:
1385: return size;
1386: }
1387: finally {
1388: iter.close();
1389: }
1390: }
1391:}
|