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());
}
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());
}
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);
}
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());
}
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());
}
Aggregations