public String apply(String command, SerializationContext context) { if (rowItems.isEmpty()) { return command; } String[] targetVars = extractTargetVars(command, valueName); if (targetVars.length == 0) { //VALUES keyword has not been found or there is another issue so do not modify the command. return command; } validateValuesSafeToInject(command, targetVars); String target = createTarget(); String replacement = buildReplacement(targetVars.length, context); return command.replaceAll(target, replacement); }
@Test public void test_extract_multiple_target_vars_missing_values() { // Missing VALUES keyword so should return empty array. String cmd = "SELECT * WHERE { VALUES ?p {?props} ?o {?objs} ?s ?p ?o }"; String valueName = "objs"; String[] res = ParameterizedSparqlString.extractTargetVars(cmd, valueName); String[] exp = new String[]{}; //System.out.println("Exp: " + String.join(",", exp)); //System.out.println("Res: " + String.join(",", res)); Assert.assertArrayEquals(exp, res); }
@Test public void test_extract_multiple_target_vars_no_braces() { // Missing braces and VALUES keyword so should return empty array. String cmd = "SELECT * WHERE { VALUES ?p ?props ?o ?objs ?s ?p ?o }"; String valueName = "objs"; String[] res = ParameterizedSparqlString.extractTargetVars(cmd, valueName); String[] exp = new String[]{}; //System.out.println("Exp: " + String.join(",", exp)); //System.out.println("Res: " + String.join(",", res)); Assert.assertArrayEquals(exp, res); }
@Test public void test_extract_target_vars() { // Identifies the vars in the VALUES clause according to the substituting valueName. String cmd = "SELECT * WHERE { VALUES ?o {?objs} ?s ?p ?o }"; String valueName = "objs"; String[] res = ParameterizedSparqlString.extractTargetVars(cmd, valueName); String[] exp = new String[]{"o"}; //System.out.println("Exp: " + String.join(",", exp)); //System.out.println("Res: " + String.join(",", res)); Assert.assertArrayEquals(exp, res); }
@Test public void test_extract_target_vars_missing_target() { // Missing target variable name so should return empty array. String cmd = "SELECT * WHERE { VALUES ?o {} ?s ?p ?o }"; String valueName = "objs"; String[] res = ParameterizedSparqlString.extractTargetVars(cmd, valueName); String[] exp = new String[]{}; //System.out.println("Exp: " + String.join(",", exp)); //System.out.println("Res: " + String.join(",", res)); Assert.assertArrayEquals(exp, res); }
@Test public void test_extract_target_vars_missing_brace() { // Missing brace so should return empty array. String cmd = "SELECT * WHERE { VALUES ?o ?objs} ?s ?p ?o }"; String valueName = "objs"; String[] res = ParameterizedSparqlString.extractTargetVars(cmd, valueName); String[] exp = new String[]{}; //System.out.println("Exp: " + String.join(",", exp)); //System.out.println("Res: " + String.join(",", res)); Assert.assertArrayEquals(exp, res); }
@Test public void test_extract_two_target_vars() { // Identifies the vars in the VALUES clause according to the substituting valueName. String cmd = "SELECT * WHERE { VALUES(?p ?o){?valuesName} ?s ?p ?o }"; String valueName = "valuesName"; String[] res = ParameterizedSparqlString.extractTargetVars(cmd, valueName); String[] exp = new String[]{"p", "o"}; ///System.out.println("Exp: " + String.join(",", exp)); //System.out.println("Res: " + String.join(",", res)); Assert.assertArrayEquals(exp, res); }
@Test public void test_extract_multiple_target_vars() { // Identifies the vars in the VALUES clause according to the substituting valueName. String cmd = "SELECT * WHERE { VALUES ?p {?props} VALUES ?o {?objs} ?s ?p ?o }"; String valueName = "objs"; String[] res = ParameterizedSparqlString.extractTargetVars(cmd, valueName); String[] exp = new String[]{"o"}; //System.out.println("Exp: " + String.join(",", exp)); //System.out.println("Res: " + String.join(",", res)); Assert.assertArrayEquals(exp, res); }
@Test public void test_extract_multiple_target_vars_missing_brace() { // Missing brace so should return empty array. String cmd = "SELECT * WHERE { VALUES ?p {?props} VALUES ?o ?objs} ?s ?p ?o }"; String valueName = "objs"; String[] res = ParameterizedSparqlString.extractTargetVars(cmd, valueName); String[] exp = new String[]{}; //System.out.println("Exp: " + String.join(",", exp)); //System.out.println("Res: " + String.join(",", res)); Assert.assertArrayEquals(exp, res); }
@Test public void test_extract_target_vars_missing_values() { // Missing VALUES keyword so should return empty array. String cmd = "SELECT * WHERE { ?o {?objs} ?s ?p ?o }"; String valueName = "objs"; String[] res = ParameterizedSparqlString.extractTargetVars(cmd, valueName); String[] exp = new String[]{}; //System.out.println("Exp: " + String.join(",", exp)); //System.out.println("Res: " + String.join(",", res)); Assert.assertArrayEquals(exp, res); }