### Question:
HqlQueryExecutor implements QueryExecutor, Initializable { @Override public <T> List<T> execute(final Query query) throws QueryException { checkAllowed(query); String oldDatabase = getContext().getWikiId(); try { this.progress.startStep(query, "query.hql.progress.execute", "Execute HQL query [{}]", query); if (query.getWiki() != null) { getContext().setWikiId(query.getWiki()); } return getStore().executeRead(getContext(), session -> { org.hibernate.query.Query<T> hquery = createHibernateQuery(session, query); List<T> results = hquery.list(); if (query.getFilters() != null && !query.getFilters().isEmpty()) { for (QueryFilter filter : query.getFilters()) { results = filter.filterResults(results); } } return results; }); } catch (XWikiException e) { throw new QueryException("Exception while executing query", query, e); } finally { getContext().setWikiId(oldDatabase); this.progress.endStep(query); } }  @Override void initialize(); @Override List<T> execute(final Query query);  }### Answer:
@Test public void executeWhenStoreException() throws Exception { XWikiException exception = mock(XWikiException.class); when(exception.getMessage()).thenReturn("nestedmessage"); when(this.store.executeRead(any(XWikiContext.class), any(XWikiHibernateBaseStore.HibernateCallback.class))) .thenThrow(exception); try { execute("statement", null); fail("Should have thrown an exception here"); } catch (QueryException expected) { assertEquals("Exception while executing query. Query statement = [statement]", expected.getMessage()); assertEquals("nestedmessage", expected.getCause().getMessage()); } }
@Test public void executeShortWhereHQLQueryWithProgrammingRights() throws QueryException { execute("where doc.space='Main'", true); }
@Test public void executeShortFromHQLQueryWithProgrammingRights() throws QueryException { execute(", BaseObject as obj", true); }
@Test public void executeCompleteHQLQueryWithProgrammingRights() throws QueryException { execute("select u from XWikiDocument as doc", true); }
@Test public void executeShortWhereHQLQueryWithoutProgrammingRights() throws QueryException { execute("where doc.space='Main'", false); }
@Test public void executeShortFromHQLQueryWithoutProgrammingRights() throws QueryException { execute(", BaseObject as obj", false); }
@Test public void executeWhenNotAllowedSelect() throws Exception { try { execute("select notallowed.name from NotAllowedTable notallowed", false); fail("Should have thrown an exception here"); } catch (QueryException expected) { assertEquals( "The query requires programming right." + " Query statement = [select notallowed.name from NotAllowedTable notallowed]", expected.getMessage()); } }
@Test public void executeDeleteWithoutProgrammingRight() throws Exception { try { execute("delete from XWikiDocument as doc", false); fail("Should have thrown an exception here"); } catch (QueryException expected) { assertEquals("The query requires programming right. Query statement = [delete from XWikiDocument as doc]", expected.getMessage()); } }
@Test public void executeUpdateWithoutProgrammingRight() throws Exception { try { execute("update XWikiDocument set name='name'", false); fail("Should have thrown an exception here"); } catch (QueryException expected) { assertEquals( "The query requires programming right. Query statement = [update XWikiDocument set name='name']", expected.getMessage()); } }