Search in sources :

Example 76 with Query

use of org.xwiki.query.Query 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 77 with Query

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

the class DefaultWikiCopier method copyDocuments.

@Override
public void copyDocuments(String fromWikiId, String toWikiId, boolean withHistory) throws WikiManagerException {
    XWikiContext context = xcontextProvider.get();
    XWiki xwiki = context.getWiki();
    this.progress.pushLevelProgress(2, this);
    try {
        // Get documents
        this.progress.startStep(this, "Get documents to copy");
        Query query = queryManager.createQuery("select distinct doc.fullName from Document as doc", Query.XWQL);
        query.setWiki(fromWikiId);
        List<String> documentFullnames = query.execute();
        this.progress.endStep(this);
        // Copy documents
        this.progress.startStep(this, "Copy documents");
        this.progress.pushLevelProgress(documentFullnames.size(), this);
        WikiReference fromWikiReference = new WikiReference(fromWikiId);
        try {
            for (String documentFullName : documentFullnames) {
                this.progress.startStep(this);
                DocumentReference origDocReference = documentReferenceResolver.resolve(documentFullName, fromWikiReference);
                DocumentReference newDocReference = origDocReference.setWikiReference(new WikiReference(toWikiId));
                logger.info("Copying document [{}] to [{}].", origDocReference, newDocReference);
                xwiki.copyDocument(origDocReference, newDocReference, null, !withHistory, true, context);
                logger.info("Done copying document [{}] to [{}].", origDocReference, newDocReference);
                this.progress.endStep(this);
            }
        } finally {
            this.progress.popLevelProgress(this);
            this.progress.endStep(this);
        }
    } catch (QueryException e) {
        WikiManagerException thrownException = new WikiManagerException("Unable to get the list of wiki documents to copy.", e);
        logger.error(thrownException.getMessage(), thrownException);
        throw thrownException;
    } catch (XWikiException e) {
        WikiManagerException thrownException = new WikiManagerException("Failed to copy documents.", e);
        logger.error(thrownException.getMessage(), thrownException);
        throw thrownException;
    } finally {
        this.progress.popLevelProgress(this);
    }
}
Also used : QueryException(org.xwiki.query.QueryException) Query(org.xwiki.query.Query) WikiManagerException(org.xwiki.wiki.manager.WikiManagerException) XWikiContext(com.xpn.xwiki.XWikiContext) XWiki(com.xpn.xwiki.XWiki) WikiReference(org.xwiki.model.reference.WikiReference) DocumentReference(org.xwiki.model.reference.DocumentReference) XWikiException(com.xpn.xwiki.XWikiException)

Example 78 with Query

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

the class WikiDescriptorMigratorTest method hibernateMigrateWhenXWikiException.

@Test
public void hibernateMigrateWhenXWikiException() throws Exception {
    List<String> documentList = new ArrayList<>();
    documentList.add("XWiki.XWikiServerSubwiki1");
    Query query = mock(Query.class);
    when(queryManager.createQuery(any(), eq(Query.HQL))).thenReturn(query);
    when(query.<String>execute()).thenReturn(documentList);
    DocumentReference documentReference = new DocumentReference("mainWiki", "XWiki", "XWikiServerSubwiki1");
    when(documentReferenceResolver.resolve(documentList.get(0))).thenReturn(documentReference);
    Exception exception = new XWikiException(0, 0, "error in xwiki.getDocument()");
    when(xwiki.getDocument(documentReference, context)).thenThrow(exception);
    // Test
    mocker.getComponentUnderTest().hibernateMigrate();
    // Verify
    verify(mocker.getMockedLogger()).warn("Failed to get or save the wiki descriptor document [{}]. You" + " will not see the corresponding wiki in the Wiki Index unless you give it a Pretty Name manually. {}", documentList.get(0), ExceptionUtils.getRootCauseMessage(exception));
}
Also used : Query(org.xwiki.query.Query) ArrayList(java.util.ArrayList) DocumentReference(org.xwiki.model.reference.DocumentReference) XWikiException(com.xpn.xwiki.XWikiException) QueryException(org.xwiki.query.QueryException) XWikiException(com.xpn.xwiki.XWikiException) Test(org.junit.Test)

Example 79 with Query

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

the class WikiDescriptorMigratorTest method hibernateMigrate.

@Test
public void hibernateMigrate() throws Exception {
    List<String> documentList = new ArrayList<>();
    documentList.add("XWiki.XWikiServerSubwiki1");
    Query query = mock(Query.class);
    when(queryManager.createQuery(any(), eq(Query.HQL))).thenReturn(query);
    when(query.<String>execute()).thenReturn(documentList);
    DocumentReference documentReference = new DocumentReference("mainWiki", "XWiki", "XWikiServerSubwiki1");
    when(documentReferenceResolver.resolve(documentList.get(0))).thenReturn(documentReference);
    XWikiDocument document = mock(XWikiDocument.class);
    when(xwiki.getDocument(documentReference, context)).thenReturn(document);
    List<BaseObject> objects = new ArrayList<>();
    objects.add(null);
    BaseObject object = mock(BaseObject.class);
    objects.add(object);
    when(document.getXObjects(XWikiServerClassDocumentInitializer.SERVER_CLASS)).thenReturn(objects);
    when(object.getStringValue(XWikiServerClassDocumentInitializer.FIELD_WIKIPRETTYNAME)).thenReturn("");
    // Test
    mocker.getComponentUnderTest().hibernateMigrate();
    // Verify
    verify(object).setStringValue(XWikiServerClassDocumentInitializer.FIELD_WIKIPRETTYNAME, "Subwiki1");
    verify(xwiki).saveDocument(document, "[UPGRADE] Set a default pretty name.", context);
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) Query(org.xwiki.query.Query) ArrayList(java.util.ArrayList) DocumentReference(org.xwiki.model.reference.DocumentReference) BaseObject(com.xpn.xwiki.objects.BaseObject) Test(org.junit.Test)

Example 80 with Query

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

the class DefaultWikiCopierTest method copyDocuments.

@Test
public void copyDocuments() throws Exception {
    // Mocks
    Query query = mock(Query.class);
    when(queryManager.createQuery("select distinct doc.fullName from Document as doc", Query.XWQL)).thenReturn(query);
    List<String> documentList = Arrays.asList("Space.Doc1", "Space.Doc2", "Space.Doc3");
    when(query.<String>execute()).thenReturn(documentList);
    WikiReference fromWikiReference = new WikiReference("wikiA");
    DocumentReference docRef1 = new DocumentReference("wikiA", "Space", "Doc1");
    DocumentReference docRef2 = new DocumentReference("wikiA", "Space", "Doc2");
    DocumentReference docRef3 = new DocumentReference("wikiA", "Space", "Doc3");
    DocumentReference copydocRef1 = new DocumentReference("wikiB", "Space", "Doc1");
    DocumentReference copydocRef2 = new DocumentReference("wikiB", "Space", "Doc2");
    DocumentReference copydocRef3 = new DocumentReference("wikiB", "Space", "Doc3");
    when(documentReferenceResolver.resolve(eq("Space.Doc1"), eq(fromWikiReference))).thenReturn(docRef1);
    when(documentReferenceResolver.resolve(eq("Space.Doc2"), eq(fromWikiReference))).thenReturn(docRef2);
    when(documentReferenceResolver.resolve(eq("Space.Doc3"), eq(fromWikiReference))).thenReturn(docRef3);
    // Test
    mocker.getComponentUnderTest().copyDocuments("wikiA", "wikiB", false);
    // Verify
    verify(query).setWiki("wikiA");
    InOrder inOrder = inOrder(progress, xwiki, mocker.getMockedLogger());
    inOrder.verify(progress).pushLevelProgress(3, mocker.getComponentUnderTest());
    inOrder.verify(progress).startStep(mocker.getComponentUnderTest());
    inOrder.verify(mocker.getMockedLogger()).info("Copying document [{}] to [{}].", docRef1, copydocRef1);
    inOrder.verify(xwiki).copyDocument(docRef1, copydocRef1, null, true, true, xcontext);
    inOrder.verify(mocker.getMockedLogger()).info("Done copying document [{}] to [{}].", docRef1, copydocRef1);
    inOrder.verify(progress).startStep(mocker.getComponentUnderTest());
    inOrder.verify(mocker.getMockedLogger()).info("Copying document [{}] to [{}].", docRef2, copydocRef2);
    inOrder.verify(xwiki).copyDocument(docRef2, copydocRef2, null, true, true, xcontext);
    inOrder.verify(mocker.getMockedLogger()).info("Done copying document [{}] to [{}].", docRef2, copydocRef2);
    inOrder.verify(progress).startStep(mocker.getComponentUnderTest());
    inOrder.verify(mocker.getMockedLogger()).info("Copying document [{}] to [{}].", docRef3, copydocRef3);
    inOrder.verify(xwiki).copyDocument(docRef3, copydocRef3, null, true, true, xcontext);
    inOrder.verify(mocker.getMockedLogger()).info("Done copying document [{}] to [{}].", docRef3, copydocRef3);
    inOrder.verify(progress).popLevelProgress(mocker.getComponentUnderTest());
}
Also used : InOrder(org.mockito.InOrder) Query(org.xwiki.query.Query) WikiReference(org.xwiki.model.reference.WikiReference) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Aggregations

Query (org.xwiki.query.Query)129 DocumentReference (org.xwiki.model.reference.DocumentReference)41 Test (org.junit.Test)39 QueryException (org.xwiki.query.QueryException)36 ArrayList (java.util.ArrayList)29 XWikiException (com.xpn.xwiki.XWikiException)18 QueryFilter (org.xwiki.query.QueryFilter)18 QueryManager (org.xwiki.query.QueryManager)18 XWikiContext (com.xpn.xwiki.XWikiContext)15 HashMap (java.util.HashMap)14 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)12 Map (java.util.Map)11 List (java.util.List)9 Before (org.junit.Before)9 SecureQuery (org.xwiki.query.SecureQuery)9 XWiki (com.xpn.xwiki.XWiki)8 BaseObject (com.xpn.xwiki.objects.BaseObject)8 SQLQuery (org.hibernate.SQLQuery)8 WikiReference (org.xwiki.model.reference.WikiReference)8 WrappingQuery (org.xwiki.query.WrappingQuery)8