public void testInferredStatementCount() throws Exception { assertTrue(testCon.isEmpty()); int inferred = getTotalStatementCount(testCon); URI root = vf.createURI("urn:root"); testCon.add(root, RDF.TYPE, RDF.LIST); testCon.remove(root, RDF.TYPE, RDF.LIST); assertTrue(testCon.isEmpty()); assertEquals(inferred, getTotalStatementCount(testCon)); }
public void testOpen() throws Exception { assertTrue(testCon.isOpen()); assertTrue(testCon2.isOpen()); testCon.close(); assertFalse(testCon.isOpen()); assertTrue(testCon2.isOpen()); }
@Test public void testGetStatementsMalformedTypedLiteral() throws Exception { Literal invalidIntegerLiteral = vf.createLiteral("the number four", XMLSchema.INTEGER); try { URI pred = vf.createURI(URN_PRED); testCon.add(bob, pred, invalidIntegerLiteral); RepositoryResult<Statement> statements = testCon.getStatements(bob, pred, null, true); assertNotNull(statements); assertTrue(statements.hasNext()); Statement st = statements.next(); assertTrue(st.getObject() instanceof Literal); assertTrue(st.getObject().equals(invalidIntegerLiteral)); } catch (RepositoryException e) { // shouldn't happen fail(e.getMessage()); } }
public void testRecoverFromParseError() throws RepositoryException, IOException { String invalidData = "bad"; String validData = "@prefix foo: <http://example.org/foo#>.\nfoo:a foo:b foo:c."; try { testCon.add(new StringReader(invalidData), "", RDFFormat.TURTLE); fail("Invalid data should result in an exception"); } catch (RDFParseException e) { // Expected behaviour } try { testCon.add(new StringReader(validData), "", RDFFormat.TURTLE); } catch (RDFParseException e) { fail("Valid data should not result in an exception"); } assertEquals("Repository contains incorrect number of statements", 1, testCon.size()); }
@Test public void testGetStatements() throws Exception { testCon.add(bob, name, nameBob); assertTrue("Repository should contain statement", testCon.hasStatement(bob, name, nameBob, false)); RepositoryResult<Statement> result = testCon.getStatements(null, name, null, false); try { assertNotNull("Iterator should not be null", result); assertTrue("Iterator should not be empty", result.hasNext()); while (result.hasNext()) { Statement st = result.next(); assertNull("Statement should not be in a context ", st.getContext()); assertTrue("Statement predicate should be equal to name ", st.getPredicate().equals(name)); } } finally { result.close(); } List<Statement> list = Iterations.addAll(testCon.getStatements(null, name, null, false), new ArrayList<Statement>()); assertNotNull("List should not be null", list); assertFalse("List should not be empty", list.isEmpty()); }
@Test public void testAddZipFile() throws Exception { testCon.add(RepositoryConnectionTest.class.getResourceAsStream(TEST_DIR_PREFIX + "graphs.zip"), "", RDFFormat.TURTLE); assertTrue(NEWLY_ADDED, testCon.hasStatement(null, publisher, nameBob, false)); assertTrue(NEWLY_ADDED, testCon.hasStatement(null, publisher, nameAlice, false)); assertTrue("alice should be known in the store", testCon.hasStatement(null, name, nameAlice, false)); assertTrue("bob should be known in the store", testCon.hasStatement(null, name, nameBob, false)); }
assertEquals("There should be exactly three namespaces", 3, nsCount); assertTrue("namespace for prefix 'example' should exist", map.containsKey("example")); assertTrue("namespace for prefix 'rdfs' should exist", map.containsKey("rdfs")); assertTrue("namespace for prefix 'rdf' should exist", map.containsKey("rdf")); assertTrue("namespace name for 'example' not well-defined", map.get("example").equals( "http://example.org/")); assertTrue("namespace name for 'rdfs' not well-defined", map.get("rdfs").equals( "http://www.w3.org/2000/01/rdf-schema#")); assertTrue("namespace name for 'rdf' not well-defined", map.get("rdf").equals( "http://www.w3.org/1999/02/22-rdf-syntax-ns#"));
testCon.add(context2, publisher, nameAlice); testCon.commit(); assertTrue("Repository should contain statement", testCon.hasStatement(bob, name, nameBob, false)); assertTrue("Repository should contain statement in context1", testCon.hasStatement(bob, name, nameBob, false, context1)); assertFalse("Repository should not contain statement in context2", testCon.hasStatement(bob, name, nameBob, false, context2)); assertNotNull("List should not be null", list); assertFalse("List should not be empty", list.isEmpty());
public void testSizeCommit() throws Exception { assertEquals(0, testCon.size()); assertEquals(0, testCon2.size()); testCon.setAutoCommit(false); testCon.add(vf.createBNode(), vf.createURI("urn:pred"), vf.createBNode()); assertEquals(1, testCon.size()); assertEquals(0, testCon2.size()); testCon.add(vf.createBNode(), vf.createURI("urn:pred"), vf.createBNode()); assertEquals(2, testCon.size()); assertEquals(0, testCon2.size()); testCon.commit(); assertEquals(2, testCon.size()); assertEquals(2, testCon2.size()); }
@Test public void testOrderByQueriesAreInterruptable() throws Exception { testCon.begin(); for (int index = 0; index < 512; index++) { testCon.add(RDFS.CLASS, RDFS.COMMENT, testCon.getValueFactory().createBNode()); } testCon.commit(); final TupleQuery query = testCon.prepareTupleQuery(QueryLanguage.SPARQL, "SELECT * WHERE { ?s ?p ?o . ?s1 ?p1 ?o1 . ?s2 ?p2 ?o2 . ?s3 ?p3 ?o3 } ORDER BY ?s1 ?p1 ?o1 LIMIT 1000"); query.setMaxQueryTime(2); final TupleQueryResult result = query.evaluate(); final long startTime = System.currentTimeMillis(); try { result.hasNext(); fail("Query should have been interrupted"); } catch (QueryInterruptedException e) { // Expected final long duration = System.currentTimeMillis() - startTime; assertTrue("Query not interrupted quickly enough, should have been ~2s, but was " + (duration / 1000) + "s", duration < 5000); } }
@Test public void testQueryDefaultGraph() throws Exception { URI graph = vf.createURI("urn:test:default"); testCon.add(vf.createURI(URN_TEST_S1), vf.createURI(URN_TEST_P1), vf.createURI(URN_TEST_O1)); assertThat(size(graph), is(equalTo(0))); testCon.add(vf.createURI("urn:test:s2"), vf.createURI(URN_TEST_P2), vf.createURI("urn:test:o2"), graph); assertThat(size(graph), is(equalTo(1))); }
@Before public void setUp() throws Exception { testRepository = createRepository(); testRepository.initialize(); testCon = testRepository.getConnection(); testCon.clear(); testCon.clearNamespaces(); testCon2 = testRepository.getConnection(); vf = testRepository.getValueFactory(); // Initialize values bob = vf.createBNode(); alice = vf.createBNode(); alexander = vf.createBNode(); name = vf.createURI(FOAF_NS + NAME); mbox = vf.createURI(FOAF_NS + MBOX); nameAlice = vf.createLiteral("Alice"); nameBob = vf.createLiteral("Bob"); mboxAlice = vf.createLiteral("alice@example.org"); mboxBob = vf.createLiteral("bob@example.org"); unknownContext = new URIImpl("urn:unknownContext"); context1 = vf.createURI("urn:x-local:graph1"); context2 = vf.createURI("urn:x-local:graph2"); }
@Test public void testInferredStatementCount() throws Exception { assertThat(testCon.isEmpty(), is(equalTo(true))); int inferred = getTotalStatementCount(testCon); URI root = vf.createURI("urn:root"); testCon.add(root, RDF.TYPE, RDF.LIST); testCon.remove(root, RDF.TYPE, RDF.LIST); assertThat(testCon.isEmpty(), is(equalTo(true))); assertThat(getTotalStatementCount(testCon), is(equalTo(inferred))); }
@Test public void testSES713() throws Exception { String queryString = "SELECT * { ?sub ?pred ?obj . FILTER ( 'not a number' + 1 = ?obj )}"; TupleQuery query = testCon.prepareTupleQuery(QueryLanguage.SPARQL, queryString); TupleQueryResult tqr = query.evaluate(); try { assertFalse("Query should not return any results", tqr.hasNext()); } finally { tqr.close(); } }
@Test public void testGetNamespaces() throws Exception { setupNamespaces(); Map<String, String> map = Namespaces.asMap(Iterations.asSet(testCon.getNamespaces())); assertThat(map.size(), is(equalTo(3))); assertThat(map.keySet(), hasItems(EXAMPLE, RDFS_PREFIX, RDF_PREFIX)); assertThat(map.get(EXAMPLE), is(equalTo(EXAMPLE_NS))); assertThat(map.get(RDFS_PREFIX), is(equalTo(RDFS_NS))); assertThat(map.get(RDF_PREFIX), is(equalTo("http://www.w3.org/1999/02/22-rdf-syntax-ns#"))); }
@Test public void testGetStatements() throws Exception { testCon.add(bob, name, nameBob); assertTrue("Repository should contain statement", testCon.hasStatement(bob, name, nameBob, false)); RepositoryResult<Statement> result = testCon.getStatements(null, name, null, false); try { assertNotNull("Iterator should not be null", result); assertTrue("Iterator should not be empty", result.hasNext()); while (result.hasNext()) { Statement st = result.next(); assertNull("Statement should not be in a context ", st.getContext()); assertTrue("Statement predicate should be equal to name ", st.getPredicate().equals(name)); } } finally { result.close(); } List<Statement> list = Iterations.addAll(testCon.getStatements(null, name, null, false), new ArrayList<Statement>()); assertNotNull("List should not be null", list); assertFalse("List should not be empty", list.isEmpty()); }
@Test public void testAddGzipInputStream() throws Exception { // add file default-graph.ttl to repository, no context InputStream defaultGraph = RepositoryConnectionTest.class.getResourceAsStream(TEST_DIR_PREFIX + "default-graph.ttl.gz"); try { testCon.add(defaultGraph, "", RDFFormat.TURTLE); } finally { defaultGraph.close(); } assertTrue(NEWLY_ADDED, testCon.hasStatement(null, publisher, nameBob, false)); assertTrue(NEWLY_ADDED, testCon.hasStatement(null, publisher, nameAlice, false)); }
public void testSimpleTupleQueryUnicode() throws Exception { testCon.add(alexander, name, Александър); StringBuilder queryBuilder = new StringBuilder(); queryBuilder.append(" SELECT person"); queryBuilder.append(" FROM {person} foaf:name {").append(Александър.getLabel()).append("}"); queryBuilder.append(" USING NAMESPACE foaf = <" + FOAF_NS + ">"); TupleQueryResult result = testCon.prepareTupleQuery(QueryLanguage.SERQL, queryBuilder.toString()).evaluate(); try { assertTrue(result != null); assertTrue(result.hasNext()); while (result.hasNext()) { BindingSet solution = result.next(); assertTrue(solution.hasBinding("person")); assertEquals(alexander, solution.getValue("person")); } } finally { result.close(); } }
testCon.add(context2, publisher, nameAlice); testCon.commit(); assertTrue("Repository should contain statement", testCon.hasStatement(bob, name, nameBob, false)); assertTrue("Repository should contain statement in context1", testCon.hasStatement(bob, name, nameBob, false, context1)); assertFalse("Repository should not contain statement in context2", testCon.hasStatement(bob, name, nameBob, false, context2)); assertNotNull("List should not be null", list); assertFalse("List should not be empty", list.isEmpty());