[
https://issues.apache.org/jira/browse/JENA-1578?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16566889#comment-16566889
]
ASF GitHub Bot commented on JENA-1578:
--------------------------------------
Github user GregAlbiston commented on a diff in the pull request:
https://github.com/apache/jena/pull/449#discussion_r207253012
--- Diff:
jena-arq/src/test/java/org/apache/jena/query/TestParameterizedSparqlString.java
---
@@ -1925,4 +1926,190 @@ public void test_param_string_bug_04() {
pss.toString();
}
+
+ @Test
+ public void test_set_values_item() {
+ // Tests a single value being added.
+ String str = "SELECT * WHERE { VALUES ?o {?objs} ?s ?p ?o }";
+ ParameterizedSparqlString pss = new ParameterizedSparqlString(str);
+ pss.setValues("objs", ResourceFactory.createPlainLiteral("test"));
+
+ String exp = "SELECT * WHERE { VALUES ?o {\"test\"} ?s ?p ?o }";
+ String res = pss.toString();
+ //System.out.println("Exp: " + exp);
+ //System.out.println("Res: " + res);
+ Assert.assertEquals(exp, res);
+ }
+
+ @Test
+ public void test_set_values_items() {
+ // Tests two values for same variable.
+ String str = "SELECT * WHERE { VALUES ?o {?objs} ?s ?p ?o }";
+ ParameterizedSparqlString pss = new ParameterizedSparqlString(str);
+ List<RDFNode> objs = new ArrayList<>();
+ objs.add(ResourceFactory.createPlainLiteral("obj_A"));
+ objs.add(ResourceFactory.createPlainLiteral("obj_B"));
+ pss.setValues("objs", objs);
+
+ String exp = "SELECT * WHERE { VALUES ?o {\"obj_A\" \"obj_B\"} ?s
?p ?o }";
+ String res = pss.toString();
+ //System.out.println("Exp: " + exp);
+ //System.out.println("Res: " + res);
+ Assert.assertEquals(exp, res);
+ }
+
+ @Test
+ public void test_set_values_items_parenthesis() {
+ // Tests two values for same variable.
+ String str = "SELECT * WHERE { VALUES (?o) {?objs} ?s ?p ?o }";
+ ParameterizedSparqlString pss = new ParameterizedSparqlString(str);
+ List<RDFNode> objs = new ArrayList<>();
+ objs.add(ResourceFactory.createPlainLiteral("obj_A"));
+ objs.add(ResourceFactory.createPlainLiteral("obj_B"));
+ pss.setValues("objs", objs);
+
+ String exp = "SELECT * WHERE { VALUES (?o) {(\"obj_A\")
(\"obj_B\")} ?s ?p ?o }";
+ String res = pss.toString();
+ //System.out.println("Exp: " + exp);
+ //System.out.println("Res: " + res);
+ Assert.assertEquals(exp, res);
+ }
+
+ @Test
+ public void test_set_values_multiple_variables_parenthesis() {
+ // Tests two values for same variable.
+ String str = "SELECT * WHERE { VALUES (?p ?o) {?vars} ?s ?p ?o }";
+ ParameterizedSparqlString pss = new ParameterizedSparqlString(str);
+ List<RDFNode> vars = new ArrayList<>();
+
vars.add(ResourceFactory.createProperty("http://example.org/prop_A"));
+ vars.add(ResourceFactory.createPlainLiteral("obj_A"));
+ pss.setValues("vars", vars);
+
+ String exp = "SELECT * WHERE { VALUES (?p ?o)
{(<http://example.org/prop_A> \"obj_A\")} ?s ?p ?o }";
+ String res = pss.toString();
+ System.out.println("Exp: " + exp);
+ System.out.println("Res: " + res);
+ Assert.assertEquals(exp, res);
+ }
+
+ @Test
+ public void test_set_values_multi_var() {
+ // Tests two variables.
+ String str = "SELECT * WHERE { VALUES ?p {?props} VALUES ?o
{?objs} ?s ?p ?o }";
+ ParameterizedSparqlString pss = new ParameterizedSparqlString(str);
+ List<RDFNode> objs = new ArrayList<>();
+ objs.add(ResourceFactory.createPlainLiteral("obj_A"));
+ objs.add(ResourceFactory.createPlainLiteral("obj_B"));
+ pss.setValues("objs", objs);
+
+ List<RDFNode> props = new ArrayList<>();
+
props.add(ResourceFactory.createProperty("http://example.org/prop_A"));
+
props.add(ResourceFactory.createProperty("http://example.org/prop_B"));
+ pss.setValues("props", props);
+
+ String exp = "SELECT * WHERE { VALUES ?p
{<http://example.org/prop_A> <http://example.org/prop_B>} VALUES ?o {\"obj_A\"
\"obj_B\"} ?s ?p ?o }";
+ String res = pss.toString();
+ //System.out.println("Exp: " + exp);
+ //System.out.println("Res: " + res);
+ Assert.assertEquals(exp, res);
+ }
+
+ @Test
+ public void test_set_values_multi_var_parenthesis() {
+ // Tests two variables with parenthesis for one.
+ String str = "SELECT * WHERE { VALUES (?p) {?props} VALUES ?o
{?objs} ?s ?p ?o }";
+ ParameterizedSparqlString pss = new ParameterizedSparqlString(str);
+ List<RDFNode> objs = new ArrayList<>();
+ objs.add(ResourceFactory.createPlainLiteral("obj_A"));
+ objs.add(ResourceFactory.createPlainLiteral("obj_B"));
+ pss.setValues("objs", objs);
+
+ List<RDFNode> props = new ArrayList<>();
+
props.add(ResourceFactory.createProperty("http://example.org/prop_A"));
+
props.add(ResourceFactory.createProperty("http://example.org/prop_B"));
+ pss.setValues("props", props);
+
+ String exp = "SELECT * WHERE { VALUES (?p)
{(<http://example.org/prop_A>) (<http://example.org/prop_B>)} VALUES ?o
{\"obj_A\" \"obj_B\"} ?s ?p ?o }";
+ String res = pss.toString();
+ //System.out.println("Exp: " + exp);
+ //System.out.println("Res: " + res);
+ Assert.assertEquals(exp, res);
+ }
+
+ @Test
+ public void test_set_values_grouped_var() {
+ // Tests two variables with parenthesis for one.
+ String str = "SELECT * WHERE { VALUES (?p ?o) {?vars} ?s ?p ?o }";
+ ParameterizedSparqlString pss = new ParameterizedSparqlString(str);
+
+ List<List<? extends RDFNode>> vars = new ArrayList<>();
+ List<RDFNode> objsA = new ArrayList<>();
+
objsA.add(ResourceFactory.createProperty("http://example.org/prop_A"));
+ objsA.add(ResourceFactory.createPlainLiteral("obj_A"));
+ vars.add(objsA);
+
+ List<RDFNode> objsB = new ArrayList<>();
+
objsB.add(ResourceFactory.createProperty("http://example.org/prop_B"));
+ objsB.add(ResourceFactory.createPlainLiteral("obj_B"));
+ vars.add(objsB);
+
+ pss.setGroupedValues("vars", vars);
+
+ String exp = "SELECT * WHERE { VALUES (?p ?o)
{(<http://example.org/prop_A> \"obj_A\") (<http://example.org/prop_B>
\"obj_B\")} ?s ?p ?o }";
+ String res = pss.toString();
+ //System.out.println("Exp: " + exp);
+ //System.out.println("Res: " + res);
+ Assert.assertEquals(exp, res);
+ }
+
+ @Test(expected = ARQException.class)
+ public void test_set_values_uri_injection() {
+ // This injection is prevented by forbidding the > character in
URIs
+ String str = "PREFIX : <http://example/>\nSELECT * WHERE { VALUES
?obj {?objVar} <s> <p> ?obj . }";
+ ParameterizedSparqlString pss = new ParameterizedSparqlString(str);
+ pss.setValues(str,
ResourceFactory.createResource("<http://example.org/obj_A>"));
+
+ pss.asQuery();
+ Assert.fail("Attempt to do SPARQL injection should result in an
exception");
+ }
+
+ @Test
+ public void test_extract_target_vars() {
+ // Identifies the vars in the VALUES clause according to the
substituting varName.
+ String cmd = "SELECT * WHERE { VALUES ?o {?objs} ?s ?p ?o }";
+ String varName = "objs";
+ String[] res = ParameterizedSparqlString.extractTargetVars(cmd,
varName);
+ String[] exp = new String[]{"o"};
+
+ //System.out.println("Exp: " + exp);
+ //System.out.println("Res: " + res);
+ Assert.assertArrayEquals(exp, res);
+ }
+
+ @Test
+ public void test_extract_two_target_vars() {
+ // Identifies the vars in the VALUES clause according to the
substituting varName.
+ String cmd = "SELECT * WHERE { VALUES(?p ?o){?vars} ?s ?p ?o }";
+ String varName = "vars";
+ String[] res = ParameterizedSparqlString.extractTargetVars(cmd,
varName);
+ String[] exp = new String[]{"p", "o"};
+
+ //System.out.println("Exp: " + exp);
+ //System.out.println("Res: " + res);
+ Assert.assertArrayEquals(exp, res);
+ }
+
+ @Test
+ public void test_extract_multiple_target_vars() {
+ // Identifies the vars in the VALUES clause according to the
substituting varName.
+ String cmd = "SELECT * WHERE { VALUES ?p {?props} VALUES ?o
{?objs} ?s ?p ?o }";
+ String varName = "objs";
+ String[] res = ParameterizedSparqlString.extractTargetVars(cmd,
varName);
+ String[] exp = new String[]{"o"};
+
+ //System.out.println("Exp: " + exp);
+ //System.out.println("Res: " + res);
+ Assert.assertArrayEquals(exp, res);
+ }
+
--- End diff --
Test cases added for incorrect number of values in a row for the variables.
Query that does not contain the `VALUES` keyword will now be unchanged. The
existing design is that syntax errors are caught by the `Query` or
`UpdateRequest` stage.
> SPARQL VALUES for ParameterizedSparqlString
> -------------------------------------------
>
> Key: JENA-1578
> URL: https://issues.apache.org/jira/browse/JENA-1578
> Project: Apache Jena
> Issue Type: New Feature
> Components: ARQ
> Affects Versions: Jena 3.8.0
> Reporter: Greg Albiston
> Priority: Minor
>
> ParameterizedSparqlString provides an API for substituting variables within
> SPARQL queries with bound values. It does not support the SPARQL VALUES
> keyword which allows multiple values to be specified. The VALUES syntax
> supports multiple values for a single variable, sets of values for multiple
> variables and multiple sets of values for multiple values.
> Inquiry on 24/07/18 the mailing list about this feature. Patch is forthcoming.
--
This message was sent by Atlassian JIRA
(v7.6.3#76005)