Search in sources :

Example 61 with ContractTest

use of org.xenei.junit.contract.ContractTest in project jena by apache.

the class WhereClauseTest method testBindStringVar.

@ContractTest
public void testBindStringVar() throws ParseException {
    Var v = Var.alloc("foo");
    WhereClause<?> whereClause = getProducer().newInstance();
    AbstractQueryBuilder<?> builder = whereClause.addBind("rand()", v);
    assertContainsRegex(OPEN_CURLY + BIND + OPEN_PAREN + "rand\\(\\)" + SPACE + "AS" + SPACE + var("foo") + CLOSE_PAREN + CLOSE_CURLY, builder.buildString());
    builder.setVar(v, NodeFactory.createURI("three"));
    Query q = builder.build();
    ElementGroup eg = (ElementGroup) q.getQueryPattern();
    List<Element> lst = eg.getElements();
    assertEquals("Should only be one element", 1, lst.size());
    assertTrue("Should have an ElementTriplesBlock", lst.get(0) instanceof ElementTriplesBlock);
    ElementTriplesBlock etb = (ElementTriplesBlock) lst.get(0);
    assertTrue("ElementGroup should be empty", etb.isEmpty());
}
Also used : Query(org.apache.jena.query.Query) Var(org.apache.jena.sparql.core.Var) Element(org.apache.jena.sparql.syntax.Element) ElementTriplesBlock(org.apache.jena.sparql.syntax.ElementTriplesBlock) ElementGroup(org.apache.jena.sparql.syntax.ElementGroup) ContractTest(org.xenei.junit.contract.ContractTest)

Example 62 with ContractTest

use of org.xenei.junit.contract.ContractTest in project jena by apache.

the class WhereClauseTest method testAddOptionalGroupPattern.

@ContractTest
public void testAddOptionalGroupPattern() throws ParseException {
    Var s = Var.alloc("s");
    Node q = NodeFactory.createURI("urn:q");
    Node v = NodeFactory.createURI("urn:v");
    Var x = Var.alloc("x");
    Node n123 = NodeFactory.createLiteral(LiteralLabelFactory.createTypedLiteral(123));
    SelectBuilder pattern = new SelectBuilder();
    pattern.addWhere(new Triple(s, q, n123));
    pattern.addWhere(new Triple(s, v, x));
    pattern.addFilter("?x>56");
    WhereClause<?> whereClause = getProducer().newInstance();
    AbstractQueryBuilder<?> builder = whereClause.addOptional(pattern);
    Query expected = QueryFactory.create("SELECT * WHERE { OPTIONAL { ?s <urn:q> '123'^^<http://www.w3.org/2001/XMLSchema#int> . ?s <urn:v> ?x . FILTER(?x>56) }}");
    Assert.assertEquals(expected.getQueryPattern(), builder.build().getQueryPattern());
}
Also used : Triple(org.apache.jena.graph.Triple) Query(org.apache.jena.query.Query) Var(org.apache.jena.sparql.core.Var) Node(org.apache.jena.graph.Node) SelectBuilder(org.apache.jena.arq.querybuilder.SelectBuilder) ContractTest(org.xenei.junit.contract.ContractTest)

Example 63 with ContractTest

use of org.xenei.junit.contract.ContractTest in project jena by apache.

the class WhereClauseTest method testAddUnion.

@ContractTest
public void testAddUnion() {
    SelectBuilder sb = new SelectBuilder();
    sb.addPrefix("pfx", "uri").addVar("?x").addWhere("<one>", "<two>", "three");
    WhereClause<?> whereClause = getProducer().newInstance();
    whereClause.getWhereHandler().addWhere(new TriplePath(Triple.ANY));
    AbstractQueryBuilder<?> builder = whereClause.addUnion(sb);
    String str = builder.buildString();
    assertContainsRegex(PREFIX + "pfx:" + SPACE + uri("uri") + SPACE, str);
    assertContainsRegex(WHERE + OPEN_CURLY + OPEN_CURLY + "ANY" + SPACE + "ANY" + SPACE + "ANY" + CLOSE_CURLY + SPACE + UNION + OPEN_CURLY + SELECT + var("x") + SPACE + WHERE + OPEN_CURLY + uri("one") + SPACE + uri("two") + SPACE + quote("three") + presentStringType() + OPT_SPACE + CLOSE_CURLY + CLOSE_CURLY, str);
}
Also used : TriplePath(org.apache.jena.sparql.core.TriplePath) SelectBuilder(org.apache.jena.arq.querybuilder.SelectBuilder) ContractTest(org.xenei.junit.contract.ContractTest)

Example 64 with ContractTest

use of org.xenei.junit.contract.ContractTest in project jena by apache.

the class WhereClauseTest method testSetVarsInSubQuery.

@ContractTest
public void testSetVarsInSubQuery() {
    Var v = Var.alloc("v");
    SelectBuilder sb = new SelectBuilder();
    sb.addPrefix("pfx", "uri").addWhere("<one>", "<two>", v);
    WhereClause<?> whereClause = getProducer().newInstance();
    AbstractQueryBuilder<?> builder = whereClause.addSubQuery(sb);
    assertContainsRegex(WHERE + OPEN_CURLY + uri("one") + SPACE + uri("two") + SPACE + var("v") + CLOSE_CURLY, builder.buildString());
    builder.setVar(v, NodeFactory.createURI("three"));
    assertContainsRegex(WHERE + OPEN_CURLY + uri("one") + SPACE + uri("two") + SPACE + uri("three") + CLOSE_CURLY, builder.buildString());
}
Also used : Var(org.apache.jena.sparql.core.Var) SelectBuilder(org.apache.jena.arq.querybuilder.SelectBuilder) ContractTest(org.xenei.junit.contract.ContractTest)

Example 65 with ContractTest

use of org.xenei.junit.contract.ContractTest in project jena by apache.

the class WhereClauseTest method testSetVarsInUnion.

@ContractTest
public void testSetVarsInUnion() {
    Var v = Var.alloc("v");
    SelectBuilder sb = new SelectBuilder();
    sb.addPrefix("pfx", "uri").addWhere("<one>", "<two>", v);
    WhereClause<?> whereClause = getProducer().newInstance();
    AbstractQueryBuilder<?> builder = whereClause.addUnion(sb);
    assertContainsRegex(WHERE + OPEN_CURLY + UNION + OPEN_CURLY + uri("one") + SPACE + uri("two") + SPACE + var("v") + CLOSE_CURLY, builder.buildString());
    builder.setVar(v, NodeFactory.createURI("three"));
    assertContainsRegex(WHERE + OPEN_CURLY + UNION + OPEN_CURLY + uri("one") + SPACE + uri("two") + SPACE + uri("three") + SPACE + CLOSE_CURLY, builder.buildString());
}
Also used : Var(org.apache.jena.sparql.core.Var) SelectBuilder(org.apache.jena.arq.querybuilder.SelectBuilder) ContractTest(org.xenei.junit.contract.ContractTest)

Aggregations

ContractTest (org.xenei.junit.contract.ContractTest)102 GraphHelper.memGraph (org.apache.jena.testing_framework.GraphHelper.memGraph)51 Graph (org.apache.jena.graph.Graph)16 Var (org.apache.jena.sparql.core.Var)13 Query (org.apache.jena.query.Query)9 E_Random (org.apache.jena.sparql.expr.E_Random)9 SelectBuilder (org.apache.jena.arq.querybuilder.SelectBuilder)6 DeleteDeniedException (org.apache.jena.shared.DeleteDeniedException)6 VarExprList (org.apache.jena.sparql.core.VarExprList)6 MalformedURLException (java.net.MalformedURLException)5 URISyntaxException (java.net.URISyntaxException)5 ClosedException (org.apache.jena.shared.ClosedException)5 Expr (org.apache.jena.sparql.expr.Expr)5 Triple (org.apache.jena.graph.Triple)4 PrefixMapping (org.apache.jena.shared.PrefixMapping)3 TriplePath (org.apache.jena.sparql.core.TriplePath)3 Node (org.apache.jena.graph.Node)2 TransactionHandler (org.apache.jena.graph.TransactionHandler)2 Intersection (org.apache.jena.graph.compose.Intersection)2 GraphWithPerform (org.apache.jena.graph.impl.GraphWithPerform)2