use of org.xwiki.query.QueryFilter in project xwiki-platform by xwiki.
the class HqlQueryExecutorTest method createHibernateQueryWhenFilter.
@Test
public void createHibernateQueryWhenFilter() throws Exception {
Session session = mock(Session.class);
DefaultQuery query = new DefaultQuery("where doc.space='Main'", Query.HQL, this.executor);
// Add a Query Filter to verify it's called and can change the statement.
// We also verify that QueryFilter#filterStatement() is called before QueryFilter#filterQuery()
QueryFilter filter = mock(QueryFilter.class);
query.addFilter(filter);
when(filter.filterStatement("select doc.fullName from XWikiDocument doc where doc.space='Main'", Query.HQL)).thenReturn("select doc.fullName from XWikiDocument doc where doc.space='Main2'");
when(filter.filterQuery(any(Query.class))).thenReturn(new WrappingQuery(query) {
@Override
public String getStatement() {
return "select doc.fullName from XWikiDocument doc where doc.space='Main3'";
}
});
this.executor.createHibernateQuery(session, query);
// The test is here!
verify(session).createQuery("select doc.fullName from XWikiDocument doc where doc.space='Main3'");
}
use of org.xwiki.query.QueryFilter in project xwiki-platform by xwiki.
the class HqlQueryExecutorTest method createHibernateQueryAutomaticallyAddEscapeLikeParametersFilterWhenQueryParameter.
@Test
public void createHibernateQueryAutomaticallyAddEscapeLikeParametersFilterWhenQueryParameter() throws Exception {
Session session = mock(Session.class);
DefaultQuery query = new DefaultQuery("where space like :space", Query.HQL, this.executor);
query.bindValue("space").literal("test");
QueryFilter filter = mock(QueryFilter.class);
when(filter.filterStatement(anyString(), anyString())).then(returnsFirstArg());
when(filter.filterQuery(any(Query.class))).then(returnsFirstArg());
ComponentManager cm = this.mocker.getInstance(ComponentManager.class, "context");
when(cm.getInstance(QueryFilter.class, "escapeLikeParameters")).thenReturn(filter);
when(session.createQuery(anyString())).thenReturn(mock(org.hibernate.Query.class));
this.executor.createHibernateQuery(session, query);
// The test is here! We verify that the filter has been called even though we didn't explicitly add it to the
// query, i.e. that it's been added automatically
verify(filter).filterQuery(any(Query.class));
}
use of org.xwiki.query.QueryFilter in project xwiki-platform by xwiki.
the class DatabaseDocumentIteratorTest method iterateOneWiki.
@Test
public void iterateOneWiki() throws Exception {
DocumentReference rootReference = createDocumentReference("gang", Arrays.asList("A", "B"), "C", null);
Query emptyQuery = mock(Query.class);
when(emptyQuery.execute()).thenReturn(Collections.emptyList());
Query query = mock(Query.class);
when(query.setLimit(anyInt())).thenReturn(query);
when(query.setWiki(rootReference.getWikiReference().getName())).thenReturn(query);
when(query.setOffset(0)).thenReturn(query);
when(query.setOffset(100)).thenReturn(emptyQuery);
when(query.execute()).thenReturn(Collections.<Object>singletonList(new Object[] { "A.B", "C", "de", "3.1" }));
Map<String, Object> namedParameters = new HashMap<String, Object>();
namedParameters.put("space", "A.B");
namedParameters.put("name", "C");
when(query.getNamedParameters()).thenReturn(namedParameters);
Query countQuery = mock(Query.class);
when(countQuery.addFilter(mocker.<QueryFilter>getInstance(QueryFilter.class, "count"))).thenReturn(countQuery);
QueryManager queryManager = mocker.getInstance(QueryManager.class);
String whereClause = " where doc.space = :space and doc.name = :name";
when(queryManager.createQuery("select doc.space, doc.name, doc.language, doc.version from XWikiDocument doc" + whereClause + " order by doc.space, doc.name, doc.language", Query.HQL)).thenReturn(query);
when(queryManager.createQuery(whereClause, Query.HQL)).thenReturn(countQuery);
DocumentIterator<String> iterator = mocker.getComponentUnderTest();
iterator.setRootReference(rootReference);
List<Pair<DocumentReference, String>> actualResults = new ArrayList<Pair<DocumentReference, String>>();
while (iterator.hasNext()) {
actualResults.add(iterator.next());
}
List<Pair<DocumentReference, String>> expectedResults = new ArrayList<Pair<DocumentReference, String>>();
expectedResults.add(new ImmutablePair<DocumentReference, String>(new DocumentReference(rootReference, Locale.GERMAN), "3.1"));
assertEquals(expectedResults, actualResults);
verify(query).bindValue("space", "A.B");
verify(query).bindValue("name", "C");
verify(countQuery).bindValue("space", "A.B");
verify(countQuery).bindValue("name", "C");
}
use of org.xwiki.query.QueryFilter in project xwiki-platform by xwiki.
the class XWikiDocumentMockitoTest method getChildrenReferences.
@Test
public void getChildrenReferences() throws Exception {
Query query = mock(Query.class);
when(this.oldcore.getQueryManager().createQuery(any(), eq(Query.XWQL))).thenReturn(query);
QueryFilter hiddenFilter = this.oldcore.getMocker().registerMockComponent(QueryFilter.class, "hidden");
when(query.setLimit(7)).thenReturn(query);
List<String> result = Arrays.asList("X.y", "A.b");
when(query.<String>execute()).thenReturn(result);
List<DocumentReference> childrenReferences = document.getChildrenReferences(7, 3, this.oldcore.getXWikiContext());
verify(query).addFilter(hiddenFilter);
verify(query).setLimit(7);
verify(query).setOffset(3);
Assert.assertEquals(2, childrenReferences.size());
Assert.assertEquals(new DocumentReference("wiki", "X", "y"), childrenReferences.get(0));
Assert.assertEquals(new DocumentReference("wiki", "A", "b"), childrenReferences.get(1));
}
use of org.xwiki.query.QueryFilter in project xwiki-platform by xwiki.
the class DatabaseDocumentIteratorTest method iterateAllWikis.
@Test
public void iterateAllWikis() throws Exception {
Query emptyQuery = mock(Query.class);
when(emptyQuery.execute()).thenReturn(Collections.emptyList());
Query chessQuery = mock(Query.class);
when(chessQuery.setOffset(0)).thenReturn(chessQuery);
when(chessQuery.setOffset(100)).thenReturn(emptyQuery);
when(chessQuery.execute()).thenReturn(Arrays.<Object>asList(new Object[] { "Blog.Code", "WebHome", "", "3.2" }, new Object[] { "Main", "Welcome", "en", "1.1" }, new Object[] { "XWiki.Syntax", "Links", "fr", "2.5" }));
DocumentReference chessBlogCodeWebHome = createDocumentReference("chess", Arrays.asList("Blog", "Code"), "WebHome", null);
DocumentReference chessMainWelcome = createDocumentReference("chess", Arrays.asList("Main"), "Welcome", Locale.ENGLISH);
DocumentReference chessXWikiSyntaxLinks = createDocumentReference("chess", Arrays.asList("XWiki", "Syntax"), "Links", Locale.FRENCH);
Query tennisQuery = mock(Query.class);
when(tennisQuery.setOffset(0)).thenReturn(tennisQuery);
when(tennisQuery.setOffset(100)).thenReturn(emptyQuery);
when(tennisQuery.execute()).thenReturn(Arrays.<Object>asList(new Object[] { "Main", "Welcome", "en", "2.1" }, new Object[] { "XWiki.Syntax", "Links", "fr", "1.3" }));
DocumentReference tennisMainWelcome = createDocumentReference("tennis", Arrays.asList("Main"), "Welcome", Locale.ENGLISH);
DocumentReference tennisXWikiSyntaxLinks = createDocumentReference("tennis", Arrays.asList("XWiki", "Syntax"), "Links", Locale.FRENCH);
Query query = mock(Query.class);
when(query.setLimit(anyInt())).thenReturn(query);
when(query.getNamedParameters()).thenReturn(Collections.<String, Object>emptyMap());
when(query.setWiki("chess")).thenReturn(chessQuery);
when(query.setWiki("tennis")).thenReturn(tennisQuery);
Query chessCountQuery = mock(Query.class);
when(chessCountQuery.execute()).thenReturn(Collections.<Object>singletonList(3L));
Query tennisCountQuery = mock(Query.class);
when(tennisCountQuery.execute()).thenReturn(Collections.<Object>singletonList(2L));
Query countQuery = mock(Query.class);
when(countQuery.addFilter(mocker.<QueryFilter>getInstance(QueryFilter.class, "count"))).thenReturn(countQuery);
when(countQuery.setWiki("chess")).thenReturn(chessCountQuery);
when(countQuery.setWiki("tennis")).thenReturn(tennisCountQuery);
QueryManager queryManager = mocker.getInstance(QueryManager.class);
when(queryManager.createQuery("select doc.space, doc.name, doc.language, doc.version from XWikiDocument doc" + " order by doc.space, doc.name, doc.language", Query.HQL)).thenReturn(query);
when(queryManager.createQuery("", Query.HQL)).thenReturn(countQuery);
DocumentIterator<String> iterator = mocker.getComponentUnderTest();
assertEquals(5L, iterator.size());
List<Pair<DocumentReference, String>> actualResults = new ArrayList<Pair<DocumentReference, String>>();
while (iterator.hasNext()) {
actualResults.add(iterator.next());
}
List<Pair<DocumentReference, String>> expectedResults = new ArrayList<Pair<DocumentReference, String>>();
expectedResults.add(new ImmutablePair<DocumentReference, String>(chessBlogCodeWebHome, "3.2"));
expectedResults.add(new ImmutablePair<DocumentReference, String>(chessMainWelcome, "1.1"));
expectedResults.add(new ImmutablePair<DocumentReference, String>(chessXWikiSyntaxLinks, "2.5"));
expectedResults.add(new ImmutablePair<DocumentReference, String>(tennisMainWelcome, "2.1"));
expectedResults.add(new ImmutablePair<DocumentReference, String>(tennisXWikiSyntaxLinks, "1.3"));
assertEquals(expectedResults, actualResults);
}
Aggregations