### Question:
AttachmentQueryFilter extends AbstractWhereQueryFilter { @Override public String filterStatement(String statement, String language) { if (Query.HQL.equals(language)) { int selectEnd = statement.indexOf(" from "); if (selectEnd > 0) { StringBuilder filteredStatement = new StringBuilder(statement); filteredStatement.insert(selectEnd, ", attachment.filename"); int fromDocTable = filteredStatement.indexOf(FROM_DOC_TABLE); if (fromDocTable > 0) { filteredStatement.insert(fromDocTable + FROM_DOC_TABLE.length(), ", XWikiAttachment attachment"); return insertWhereClause("doc.id = attachment.docId", filteredStatement.toString(), language); } } } return statement; }  @Override String filterStatement(String statement, String language); @Override List filterResults(List results);  }### Answer:
@Test public void filterStatementWithoutFromAndWhere() { String result = this.queryFilter.filterStatement( "select doc.fullName from XWikiDocument doc order by attachment.date", Query.HQL); assertEquals("select doc.fullName, attachment.filename " + "from XWikiDocument doc, XWikiAttachment attachment " + "where doc.id = attachment.docId " + "order by attachment.date", result); }
@Test public void filterStatementWithWhere() { String result = this.queryFilter.filterStatement( "select doc.fullName from XWikiDocument doc where attachment.mimeType like 'image/%'", Query.HQL); assertEquals("select doc.fullName, attachment.filename " + "from XWikiDocument doc, XWikiAttachment attachment " + "where doc.id = attachment.docId and (attachment.mimeType like 'image/%')", result); }
@Test public void filterStatementWithFromAndWhere() { String result = this.queryFilter.filterStatement( "select doc.fullName from XWikiDocument doc, BaseObject as obj where doc.fullName = obj.name and " + "obj.className = 'XWiki.XWikiUsers'", Query.HQL); assertEquals("select doc.fullName, attachment.filename " + "from XWikiDocument doc, XWikiAttachment attachment, BaseObject as obj " + "where doc.id = attachment.docId and " + "(doc.fullName = obj.name and obj.className = 'XWiki.XWikiUsers')", result); }
@Test public void filterStatementNonHQL() { String statement = "select doc.fullName from XWikiDocument doc ..."; assertSame(statement, this.queryFilter.filterStatement(statement, Query.XWQL)); }
@Test public void filterStatementThatDoesNotMatch() { String statement = "one two three"; assertSame(statement, this.queryFilter.filterStatement(statement, Query.HQL)); statement = "select space.reference from XWikiSpace space ..."; assertSame(statement, this.queryFilter.filterStatement(statement, Query.HQL)); }