### Question:
CountDocumentFilter extends AbstractQueryFilter { @Override public String filterStatement(String statement, String language) { String result = statement.trim(); String lcOriginal = result.toLowerCase(); String original = result; if (Query.HQL.equals(language) && isFilterable(lcOriginal)) { String distinct = getSelectColumns(statement).contains(DISTINCT_FULLNAME_COLUMN) ? "distinct " : ""; result = "select count(" + distinct + "doc.fullName) " + result.substring(lcOriginal.indexOf("from xwikidocument")); int oidx = StringUtils.indexOfIgnoreCase(result, "order by "); int gidx = StringUtils.indexOfIgnoreCase(result, "group by "); if (oidx > -1) { if (gidx > -1 && gidx > oidx) { result = result.substring(0, oidx) + result.substring(gidx); } else { result = result.substring(0, oidx); } } } if (!original.equals(result)) { logger.debug("Query [{}] has been transformed into [{}]", original, result); } return result; }  @Override String filterStatement(String statement, String language); @Override List filterResults(List results);  }### Answer:
@Test public void filterSelectStatement() { assertEquals("select count(doc.fullName) from XWikiDocument doc", this.filter.filterStatement("select doc.fullName from XWikiDocument doc", Query.HQL)); assertEquals("select count(doc.fullName) FROM XWikiDocument doc", this.filter.filterStatement("SELECT doc.fullName FROM XWikiDocument doc", Query.HQL)); }
@Test public void filterSelectStatementWithWhitespace() { assertEquals("select count(doc.fullName) from XWikiDocument as doc", this.filter.filterStatement(" select doc.fullName from XWikiDocument as doc ", Query.HQL)); }
@Test public void filterSelectDistinctStatement() { assertEquals("select count(distinct doc.fullName) from XWikiDocument doc", this.filter.filterStatement("select distinct doc.fullName from XWikiDocument doc", Query.HQL)); }
@Test public void filterSelectWithAsStatement() { assertEquals("select count(doc.fullName) from XWikiDocument as doc", this.filter.filterStatement("select doc.fullName from XWikiDocument as doc", Query.HQL)); }
@Test public void filterSelectStatementWithMismatchingDocAlias() { assertEquals("select mydoc.fullName from XWikiDocument mydoc", this.filter.filterStatement("select mydoc.fullName from XWikiDocument mydoc", Query.HQL)); }
@Test public void filterStatementWhenStatementAlreadyContainsCount() { assertEquals("select count(distinct doc.fullName) from XWikiDocument doc", this.filter.filterStatement("select count(distinct doc.fullName) from XWikiDocument doc", Query.HQL)); }
@Test public void filterStatementWhenStatementContainsOrderBy() { assertEquals("select count(doc.fullName) from XWikiDocument doc ", this.filter.filterStatement("select doc.fullName from XWikiDocument doc order by doc.name", Query.HQL)); }
@Test public void filterStatementWhenStatementContainsOrderByAndGroupBy() { assertEquals("select count(doc.fullName) from XWikiDocument doc group by doc.web", this.filter.filterStatement("select doc.fullName from XWikiDocument doc order by doc.name group by doc.web", Query.HQL) ); }
@Test public void filterStatementWhenStatementContainsDistinct() { assertEquals("select count(distinct doc.fullName) from XWikiDocument doc", this.filter.filterStatement("select distinct doc.fullName from XWikiDocument doc", Query.HQL)); }
@Test public void filterStatementWhenStatementContainsMultipleColumns() { assertEquals("select count(doc.fullName) from XWikiDocument doc group by doc.web", this.filter.filterStatement( "select doc.fullName, doc.name, doc.space from XWikiDocument doc order by doc.name group by doc.web", Query.HQL) ); }