Search in sources :

Example 11 with QueryManager

use of org.xwiki.query.QueryManager in project xwiki-platform by xwiki.

the class SolrQueryExecutorTest method testExecutorRegistration.

@Test
public void testExecutorRegistration() throws Exception {
    QueryManager queryManager = this.componentManager.getInstance(QueryManager.class);
    Assert.assertTrue(queryManager.getLanguages().contains(SolrQueryExecutor.SOLR));
}
Also used : DefaultQueryManager(org.xwiki.query.internal.DefaultQueryManager) QueryManager(org.xwiki.query.QueryManager) Test(org.junit.Test)

Example 12 with QueryManager

use of org.xwiki.query.QueryManager 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");
}
Also used : Query(org.xwiki.query.Query) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) QueryFilter(org.xwiki.query.QueryFilter) QueryManager(org.xwiki.query.QueryManager) DocumentReference(org.xwiki.model.reference.DocumentReference) Pair(org.apache.commons.lang3.tuple.Pair) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) Test(org.junit.Test)

Example 13 with QueryManager

use of org.xwiki.query.QueryManager in project xwiki-platform by xwiki.

the class MessageStreamTest method setupForLimitQueries.

private void setupForLimitQueries(final int expectedLimit, final int expectedOffset) throws ComponentLookupException, Exception {
    final Query mockQuery = getMockQuery();
    final QueryManager mockQueryManager = getComponentManager().getInstance(QueryManager.class);
    final EventStream mockEventStream = getComponentManager().getInstance(EventStream.class);
    final DocumentAccessBridge mockBridge = getComponentManager().getInstance(DocumentAccessBridge.class);
    final EntityReferenceSerializer<String> mockSerializer = getComponentManager().getInstance(EntityReferenceSerializer.TYPE_STRING);
    getMockery().checking(new Expectations() {

        {
            allowing(mockBridge).getCurrentUserReference();
            will(returnValue(MessageStreamTest.this.currentUser));
            allowing(mockSerializer).serialize(MessageStreamTest.this.currentUser);
            will(returnValue("wiki:XWiki.JohnDoe"));
            exactly(1).of(mockQuery).setLimit(expectedLimit);
            will(returnValue(mockQuery));
            exactly(1).of(mockQuery).setOffset(expectedOffset);
            will(returnValue(mockQuery));
            allowing(mockQuery).bindValue(with(any(String.class)), with("wiki:XWiki.JohnDoe"));
            allowing(mockQueryManager).createQuery(with(aNonNull(String.class)), with(aNonNull(String.class)));
            will(returnValue(mockQuery));
            exactly(1).of(mockEventStream).searchEvents(with(mockQuery));
            will(returnValue(null));
        }
    });
}
Also used : Expectations(org.jmock.Expectations) Query(org.xwiki.query.Query) EventStream(org.xwiki.eventstream.EventStream) QueryManager(org.xwiki.query.QueryManager) DocumentAccessBridge(org.xwiki.bridge.DocumentAccessBridge)

Example 14 with QueryManager

use of org.xwiki.query.QueryManager 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);
}
Also used : QueryFilter(org.xwiki.query.QueryFilter) Query(org.xwiki.query.Query) QueryManager(org.xwiki.query.QueryManager) ArrayList(java.util.ArrayList) DocumentReference(org.xwiki.model.reference.DocumentReference) Pair(org.apache.commons.lang3.tuple.Pair) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) Test(org.junit.Test)

Example 15 with QueryManager

use of org.xwiki.query.QueryManager in project xwiki-platform by xwiki.

the class ClassSheetBinderTest method setUp.

@Before
public void setUp() throws Exception {
    sheetBindingsQuery = mock(Query.class);
    QueryManager queryManager = mocker.getInstance(QueryManager.class);
    // This is called when the component is initialized.
    when(queryManager.createQuery(any(), any())).thenReturn(sheetBindingsQuery);
}
Also used : Query(org.xwiki.query.Query) QueryManager(org.xwiki.query.QueryManager) Before(org.junit.Before)

Aggregations

QueryManager (org.xwiki.query.QueryManager)22 Query (org.xwiki.query.Query)18 DocumentReference (org.xwiki.model.reference.DocumentReference)11 Before (org.junit.Before)7 Test (org.junit.Test)7 XWikiContext (com.xpn.xwiki.XWikiContext)6 XWiki (com.xpn.xwiki.XWiki)5 ArrayList (java.util.ArrayList)5 List (java.util.List)5 XWikiException (com.xpn.xwiki.XWikiException)4 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)4 Map (java.util.Map)4 BaseObject (com.xpn.xwiki.objects.BaseObject)3 Date (java.util.Date)3 HashMap (java.util.HashMap)3 QueryException (org.xwiki.query.QueryException)3 ImmutablePair (org.apache.commons.lang3.tuple.ImmutablePair)2 Pair (org.apache.commons.lang3.tuple.Pair)2 Expectations (org.jmock.Expectations)2 ConfigurationSource (org.xwiki.configuration.ConfigurationSource)2