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);
}
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);
}
}
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));
}
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);
}
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());
}
Aggregations