QueryInvestigator { public static boolean isConstruct(final String sparql) throws MalformedQueryException { requireNonNull(sparql); try { PARSER.parseQuery(sparql, null); return Pattern.matches("[\\s\\S]*?construct\\s*?\\{[\\s\\S]*?\\}[\\s\\S]*?where\\s*\\{[\\s\\S]*?\\}", sparql.toLowerCase()); } catch(final MalformedQueryException queryE) { try { PARSER.parseUpdate(sparql, null); return false; } catch(final MalformedQueryException updateE) { throw queryE; } } } private  QueryInvestigator(); static boolean isConstruct(final String sparql); static boolean isInsertWhere(final String sparql);  }
@Test public void isConstruct_true() throws Exception { final String sparql = "PREFIX vCard: <http: "PREFIX foaf: <http: "CONSTRUCT { " + "?X vCard:FN ?name . " + "?X vCard:URL ?url . " + "?X vCard:TITLE ?title . " + "} " + "FROM <http: "WHERE { " + "OPTIONAL { ?X foaf:name ?name . FILTER isLiteral(?name) . } " + "OPTIONAL { ?X foaf:homepage ?url . FILTER isURI(?url) . } " + "OPTIONAL { ?X foaf:title ?title . FILTER isLiteral(?title) . } " + "}"; assertTrue( QueryInvestigator.isConstruct(sparql) ); }
@Test public void isConstruct_false_notAConstruct() throws Exception { final String sparql = "SELECT * WHERE { ?a ?b ?c . }"; assertFalse( QueryInvestigator.isConstruct(sparql) ); }
@Test public void isConstruct_false_notAConstructWithKeywords() throws Exception { final String sparql = "SELECT ?construct " + "WHERE {" + " ?construct <urn:built> <urn:skyscraper> ." + "}"; assertFalse( QueryInvestigator.isConstruct(sparql) ); }
@Test public void isConstruct_false_notAQuery() throws Exception { final String sparql = "PREFIX Sensor: <http: "INSERT { " + "?subject Sensor:test2 ?newValue " + "} WHERE {" + "values (?oldValue ?newValue) {" + "('testValue1' 'newValue1')" + "('testValue2' 'newValue2')" + "}" + "?subject Sensor:test1 ?oldValue" + "}"; assertFalse( QueryInvestigator.isConstruct(sparql) ); }
@Test(expected = MalformedQueryException.class) public void isConstruct_false_malformed() throws MalformedQueryException { assertFalse( QueryInvestigator.isConstruct("not sparql") ); }