JenaStatement implements Statement { @Override public ResultSet executeQuery(String sparql) throws JenaDriverException { ensureOpen(); final Query query = parseQuery(Objects.requireNonNull(sparql)); closeCurrentResultSet(); final AbstractResultSet resultSet; if (query.isAskType()) { resultSet = executor.executeAskQuery(query, targetOntology); } else { resultSet = executor.executeSelectQuery(query, targetOntology); } resultSet.setStatement(this); this.currentResultSet = resultSet; return resultSet; } JenaStatement(StatementExecutor executor); @Override ResultSet executeQuery(String sparql); @Override void executeUpdate(String sparql); @Override void useOntology(StatementOntology ontology); @Override StatementOntology getStatementOntology(); @Override void close(); @Override boolean isOpen();  }
@Test public void executeQueryExecutesSelect() throws JenaDriverException { final SelectResultSet rsMock = mock(SelectResultSet.class); when(executor.executeSelectQuery(any(), any())).thenReturn(rsMock); final String query = "SELECT * WHERE { ?x ?y ?z .}"; final ResultSet rs = statement.executeQuery(query); verify(executor).executeSelectQuery(any(Query.class), eq(Statement.StatementOntology.CENTRAL)); assertSame(rsMock, rs); }
@Test public void executeQueryExecutesAsk() throws JenaDriverException { final AskResultSet rsMock = mock(AskResultSet.class); when(executor.executeAskQuery(any(), any())).thenReturn(rsMock); final String query = "ASK { ?x a <http: final ResultSet rs = statement.executeQuery(query); verify(executor).executeAskQuery(any(Query.class), eq(Statement.StatementOntology.CENTRAL)); assertSame(rsMock, rs); }
@Test public void executeQueryExecutesAskWhenPrefixesAreDeclared() throws JenaDriverException { final AskResultSet rsMock = mock(AskResultSet.class); when(executor.executeAskQuery(any(), any())).thenReturn(rsMock); final String query = "PREFIX foaf: <http: "ASK { ?x a foaf:Person . }"; statement.executeQuery(query); verify(executor).executeAskQuery(any(Query.class), eq(Statement.StatementOntology.CENTRAL)); }
@Test public void executeQuerySetsStatementOnResultSet() throws JenaDriverException { final SelectResultSet rsMock = mock(SelectResultSet.class); when(executor.executeSelectQuery(any(), any())).thenReturn(rsMock); final String query = "SELECT * WHERE { ?x ?y ?z .}"; final AbstractResultSet rs = (AbstractResultSet) statement.executeQuery(query); verify(rs).setStatement(statement); }
@Test public void executeQueryThrowsJenaDriverExceptionForMalformedQuery() throws JenaDriverException { final String query = "SELECT * WHERE { ?x ?y df .}"; thrown.expect(JenaDriverException.class); thrown.expectMessage(containsString("Unable to parse query ")); statement.executeQuery(query); }