@Test public void testSES1898LeftJoinSemantics2() throws Exception { loadTestData("/testdata-query/dataset-ses1898.trig"); StringBuilder query = new StringBuilder(); query.append(" PREFIX : <http://example.org/> "); query.append(" SELECT * WHERE { "); query.append(" ?s :p1 ?v1 . "); query.append(" ?s :p3 ?v2 . "); query.append(" OPTIONAL {?s :p2 ?v2 } ."); query.append(" } "); TupleQuery tq = conn.prepareTupleQuery(QueryLanguage.SPARQL, query.toString()); try { TupleQueryResult result = tq.evaluate(); assertNotNull(result); int count = 0; while (result.hasNext()) { result.next(); count++; } assertEquals(1, count); } catch (QueryEvaluationException e) { e.printStackTrace(); fail(e.getMessage()); } }
@Test public void testSES1898LeftJoinSemantics1() throws Exception { loadTestData("/testdata-query/dataset-ses1898.trig"); StringBuilder query = new StringBuilder(); query.append(" PREFIX : <http://example.org/> "); query.append(" SELECT * WHERE { "); query.append(" ?s :p1 ?v1 . "); query.append(" OPTIONAL {?s :p2 ?v2 } ."); query.append(" ?s :p3 ?v2 . "); query.append(" } "); TupleQuery tq = conn.prepareTupleQuery(QueryLanguage.SPARQL, query.toString()); try { TupleQueryResult result = tq.evaluate(); assertNotNull(result); int count = 0; while (result.hasNext()) { result.next(); count++; } assertEquals(0, count); } catch (QueryEvaluationException e) { e.printStackTrace(); fail(e.getMessage()); } }
@Test public void testInComparison1() throws Exception { loadTestData("/testdata-query/dataset-ses1913.trig"); StringBuilder query = new StringBuilder(); query.append(" PREFIX : <http://example.org/>\n"); query.append(" SELECT ?y WHERE { :a :p ?y. FILTER(?y in (:c, :d, 1/0 , 1)) } "); TupleQuery tq = conn.prepareTupleQuery(QueryLanguage.SPARQL, query.toString()); TupleQueryResult result = tq.evaluate(); assertNotNull(result); assertTrue(result.hasNext()); BindingSet bs = result.next(); Value y = bs.getValue("y"); assertNotNull(y); assertTrue(y instanceof Literal); assertEquals(f.createLiteral("1", XMLSchema.INTEGER), y); }
@Test public void testInComparison3() throws Exception { loadTestData("/testdata-query/dataset-ses1913.trig"); StringBuilder query = new StringBuilder(); query.append(" PREFIX : <http://example.org/>\n"); query.append(" SELECT ?y WHERE { :a :p ?y. FILTER(?y in (:c, :d, 1, 1/0)) } "); TupleQuery tq = conn.prepareTupleQuery(QueryLanguage.SPARQL, query.toString()); TupleQueryResult result = tq.evaluate(); assertNotNull(result); assertTrue(result.hasNext()); BindingSet bs = result.next(); Value y = bs.getValue("y"); assertNotNull(y); assertTrue(y instanceof Literal); assertEquals(f.createLiteral("1", XMLSchema.INTEGER), y); }
assertTrue(bs.hasBinding("parent")); assertEquals(2, count);
@Test public void testDescribeAWhere() throws Exception { loadTestData("/testdata-query/dataset-describe.trig"); StringBuilder query = new StringBuilder(); query.append(getNamespaceDeclarations()); query.append("DESCRIBE ?x WHERE {?x rdfs:label \"a\". } "); GraphQuery gq = conn.prepareGraphQuery(QueryLanguage.SPARQL, query.toString()); ValueFactory f = conn.getValueFactory(); URI a = f.createURI("http://example.org/a"); URI p = f.createURI("http://example.org/p"); Model result = QueryResults.asModel(gq.evaluate()); Set<Value> objects = result.filter(a, p, null).objects(); assertNotNull(objects); for (Value object : objects) { if (object instanceof BNode) { assertTrue(result.contains((Resource)object, null, null)); assertEquals(2, result.filter((Resource)object, null, null).size()); } } }
@Test public void testDescribeA() throws Exception { loadTestData("/testdata-query/dataset-describe.trig"); StringBuilder query = new StringBuilder(); query.append(getNamespaceDeclarations()); query.append("DESCRIBE ex:a"); GraphQuery gq = conn.prepareGraphQuery(QueryLanguage.SPARQL, query.toString()); ValueFactory f = conn.getValueFactory(); URI a = f.createURI("http://example.org/a"); URI p = f.createURI("http://example.org/p"); Model result = QueryResults.asModel(gq.evaluate()); Set<Value> objects = result.filter(a, p, null).objects(); assertNotNull(objects); for (Value object : objects) { if (object instanceof BNode) { assertTrue(result.contains((Resource)object, null, null)); assertEquals(2, result.filter((Resource)object, null, null).size()); } } }
@Test public void testSES1898LeftJoinSemantics1() throws Exception { loadTestData("/testdata-query/dataset-ses1898.trig"); StringBuilder query = new StringBuilder(); query.append(" PREFIX : <http://example.org/> "); query.append(" SELECT * WHERE { "); query.append(" ?s :p1 ?v1 . "); query.append(" OPTIONAL {?s :p2 ?v2 } ."); query.append(" ?s :p3 ?v2 . "); query.append(" } "); TupleQuery tq = conn.prepareTupleQuery(QueryLanguage.SPARQL, query.toString()); try { TupleQueryResult result = tq.evaluate(); assertNotNull(result); int count = 0; while (result.hasNext()) { result.next(); count++; } assertEquals(0, count); } catch (QueryEvaluationException e) { e.printStackTrace(); fail(e.getMessage()); } }
@Test public void testSES1898LeftJoinSemantics2() throws Exception { loadTestData("/testdata-query/dataset-ses1898.trig"); StringBuilder query = new StringBuilder(); query.append(" PREFIX : <http://example.org/> "); query.append(" SELECT * WHERE { "); query.append(" ?s :p1 ?v1 . "); query.append(" ?s :p3 ?v2 . "); query.append(" OPTIONAL {?s :p2 ?v2 } ."); query.append(" } "); TupleQuery tq = conn.prepareTupleQuery(QueryLanguage.SPARQL, query.toString()); try { TupleQueryResult result = tq.evaluate(); assertNotNull(result); int count = 0; while (result.hasNext()) { result.next(); count++; } assertEquals(1, count); } catch (QueryEvaluationException e) { e.printStackTrace(); fail(e.getMessage()); } }
@Test public void testDescribeF() throws Exception { loadTestData("/testdata-query/dataset-describe.trig"); StringBuilder query = new StringBuilder(); query.append(getNamespaceDeclarations()); query.append("DESCRIBE ex:f"); GraphQuery gq = conn.prepareGraphQuery(QueryLanguage.SPARQL, query.toString()); ValueFactory vf = conn.getValueFactory(); URI f = vf.createURI("http://example.org/f"); URI p = vf.createURI("http://example.org/p"); Model result = QueryResults.asModel(gq.evaluate()); assertNotNull(result); assertEquals(4, result.size()); Set<Value> objects = result.filter(f, p, null).objects(); assertNotNull(objects); for (Value object : objects) { if (object instanceof BNode) { Set<Value> childObjects = result.filter((BNode)object, null, null).objects(); assertNotNull(childObjects); for (Value childObject : childObjects) { if (childObject instanceof BNode) { assertTrue(result.contains((BNode)childObject, null, null)); } } } } }
@Test public void testInComparison1() throws Exception { loadTestData("/testdata-query/dataset-ses1913.trig"); StringBuilder query = new StringBuilder(); query.append(" PREFIX : <http://example.org/>\n"); query.append(" SELECT ?y WHERE { :a :p ?y. FILTER(?y in (:c, :d, 1/0 , 1)) } "); TupleQuery tq = conn.prepareTupleQuery(QueryLanguage.SPARQL, query.toString()); TupleQueryResult result = tq.evaluate(); assertNotNull(result); assertTrue(result.hasNext()); BindingSet bs = result.next(); Value y = bs.getValue("y"); assertNotNull(y); assertTrue(y instanceof Literal); assertEquals(f.createLiteral("1", XMLSchema.INTEGER), y); }
@Test public void testInComparison3() throws Exception { loadTestData("/testdata-query/dataset-ses1913.trig"); StringBuilder query = new StringBuilder(); query.append(" PREFIX : <http://example.org/>\n"); query.append(" SELECT ?y WHERE { :a :p ?y. FILTER(?y in (:c, :d, 1, 1/0)) } "); TupleQuery tq = conn.prepareTupleQuery(QueryLanguage.SPARQL, query.toString()); TupleQueryResult result = tq.evaluate(); assertNotNull(result); assertTrue(result.hasNext()); BindingSet bs = result.next(); Value y = bs.getValue("y"); assertNotNull(y); assertTrue(y instanceof Literal); assertEquals(f.createLiteral("1", XMLSchema.INTEGER), y); }
assertTrue(bs.hasBinding("parent")); assertEquals(4, count);
@Test public void testDescribeA() throws Exception { loadTestData("/testdata-query/dataset-describe.trig"); StringBuilder query = new StringBuilder(); query.append(getNamespaceDeclarations()); query.append("DESCRIBE ex:a"); GraphQuery gq = conn.prepareGraphQuery(QueryLanguage.SPARQL, query.toString()); ValueFactory f = conn.getValueFactory(); URI a = f.createURI("http://example.org/a"); URI p = f.createURI("http://example.org/p"); Model result = QueryResults.asModel(gq.evaluate()); Set<Value> objects = result.filter(a, p, null).objects(); assertNotNull(objects); for (Value object : objects) { if (object instanceof BNode) { assertTrue(result.contains((Resource)object, null, null)); assertEquals(2, result.filter((Resource)object, null, null).size()); } } }
@Test public void testDescribeAWhere() throws Exception { loadTestData("/testdata-query/dataset-describe.trig"); StringBuilder query = new StringBuilder(); query.append(getNamespaceDeclarations()); query.append("DESCRIBE ?x WHERE {?x rdfs:label \"a\". } "); GraphQuery gq = conn.prepareGraphQuery(QueryLanguage.SPARQL, query.toString()); ValueFactory f = conn.getValueFactory(); URI a = f.createURI("http://example.org/a"); URI p = f.createURI("http://example.org/p"); Model result = QueryResults.asModel(gq.evaluate()); Set<Value> objects = result.filter(a, p, null).objects(); assertNotNull(objects); for (Value object : objects) { if (object instanceof BNode) { assertTrue(result.contains((Resource)object, null, null)); assertEquals(2, result.filter((Resource)object, null, null).size()); } } }
@Test public void testSES1991NOWEvaluation() throws Exception { loadTestData("/testdata-query/defaultgraph.ttl"); String query = "SELECT ?d WHERE {?s ?p ?o . BIND(NOW() as ?d) } LIMIT 2"; TupleQuery tq = conn.prepareTupleQuery(QueryLanguage.SPARQL, query); try { TupleQueryResult result = tq.evaluate(); assertNotNull(result); Literal d1 = (Literal)result.next().getValue("d"); Literal d2 = (Literal)result.next().getValue("d"); assertNotNull(d1); assertEquals(d1, d2); result.close(); } catch (QueryEvaluationException e) { e.printStackTrace(); fail(e.getMessage()); } }
@Test public void testSES1970CountDistinctWildcard() throws Exception { loadTestData("/testdata-query/dataset-ses1970.trig"); String query = "SELECT (COUNT(DISTINCT *) AS ?c) {?s ?p ?o }"; TupleQuery tq = conn.prepareTupleQuery(QueryLanguage.SPARQL, query); try { TupleQueryResult result = tq.evaluate(); assertNotNull(result); assertTrue(result.hasNext()); BindingSet s = result.next(); Literal count = (Literal)s.getValue("c"); assertNotNull(count); assertEquals(3, count.intValue()); result.close(); } catch (QueryEvaluationException e) { e.printStackTrace(); fail(e.getMessage()); } }
@Test public void testDescribeF() throws Exception { loadTestData("/testdata-query/dataset-describe.trig"); StringBuilder query = new StringBuilder(); query.append(getNamespaceDeclarations()); query.append("DESCRIBE ex:f"); GraphQuery gq = conn.prepareGraphQuery(QueryLanguage.SPARQL, query.toString()); ValueFactory vf = conn.getValueFactory(); URI f = vf.createURI("http://example.org/f"); URI p = vf.createURI("http://example.org/p"); Model result = QueryResults.asModel(gq.evaluate()); assertNotNull(result); assertEquals(4, result.size()); Set<Value> objects = result.filter(f, p, null).objects(); assertNotNull(objects); for (Value object : objects) { if (object instanceof BNode) { Set<Value> childObjects = result.filter((BNode)object, null, null).objects(); assertNotNull(childObjects); for (Value childObject : childObjects) { if (childObject instanceof BNode) { assertTrue(result.contains((BNode)childObject, null, null)); } } } } }
@Test public void testSES1991NOWEvaluation() throws Exception { loadTestData("/testdata-query/defaultgraph.ttl"); String query = "SELECT ?d WHERE {?s ?p ?o . BIND(NOW() as ?d) } LIMIT 2"; TupleQuery tq = conn.prepareTupleQuery(QueryLanguage.SPARQL, query); try { TupleQueryResult result = tq.evaluate(); assertNotNull(result); Literal d1 = (Literal)result.next().getValue("d"); Literal d2 = (Literal)result.next().getValue("d"); assertNotNull(d1); assertEquals(d1, d2); result.close(); } catch (QueryEvaluationException e) { e.printStackTrace(); fail(e.getMessage()); } }
@Test public void testSES1970CountDistinctWildcard() throws Exception { loadTestData("/testdata-query/dataset-ses1970.trig"); String query = "SELECT (COUNT(DISTINCT *) AS ?c) {?s ?p ?o }"; TupleQuery tq = conn.prepareTupleQuery(QueryLanguage.SPARQL, query); try { TupleQueryResult result = tq.evaluate(); assertNotNull(result); assertTrue(result.hasNext()); BindingSet s = result.next(); Literal count = (Literal)s.getValue("c"); assertNotNull(count); assertEquals(3, count.intValue()); result.close(); } catch (QueryEvaluationException e) { e.printStackTrace(); fail(e.getMessage()); } }