/**
* Test method for {@link SearchQuery#withPreferredVersion(long)}.
*/
@Test
public void testWithPreferredVersion() throws Exception {
SearchResult result = null;
// Make sure the homepage doesn't get into our way
repository.delete(new PageURIImpl(site, "/"), true);
SearchQuery workPreferredQuery = new SearchQueryImpl(site).withPreferredVersion(WORK).sortByCreationDate(Order.Ascending);
SearchQuery livePreferredQuery = new SearchQueryImpl(site).withPreferredVersion(LIVE).sortByCreationDate(Order.Ascending);
SearchQuery workOnlyQuery = new SearchQueryImpl(site).withVersion(WORK).sortByCreationDate(Order.Ascending);
SearchQuery liveOnlyQuery = new SearchQueryImpl(site).withVersion(LIVE).sortByCreationDate(Order.Ascending);
// Test empty repository
assertEquals(0, repository.find(workPreferredQuery).getDocumentCount());
assertEquals(0, repository.find(livePreferredQuery).getDocumentCount());
assertEquals(0, repository.find(workOnlyQuery).getDocumentCount());
assertEquals(0, repository.find(liveOnlyQuery).getDocumentCount());
// Create URI and pages and add them to the repository
ResourceURI liveOnlyURI = new PageURIImpl(site, "/liveonly", LIVE);
ResourceURI liveAndWorkLiveURI = new PageURIImpl(site, "/liveandwork", LIVE);
ResourceURI liveAndWorkWorkURI = new PageURIImpl(site, "/liveandwork", WORK);
ResourceURI workOnlyURI = new PageURIImpl(site, "/workonly", WORK);
Page liveOnly = new PageImpl(liveOnlyURI);
liveOnly.setTemplate(template.getIdentifier());
Page liveAndWorkLive = new PageImpl(liveAndWorkLiveURI);
liveAndWorkLive.setTemplate(template.getIdentifier());
Page liveAndWorkWork = new PageImpl(liveAndWorkWorkURI);
liveAndWorkWork.setTemplate(template.getIdentifier());
Page workOnly = new PageImpl(workOnlyURI);
workOnly.setTemplate(template.getIdentifier());
// Add the live only live page
repository.put(liveOnly);
assertEquals(0, repository.find(workOnlyQuery).getDocumentCount());
assertEquals(1, repository.find(liveOnlyQuery).getDocumentCount());
result = repository.find(workPreferredQuery);
assertEquals(1, result.getDocumentCount());
ResourceSearchResultItem searchResultItem = (ResourceSearchResultItem) result.getItems()[0];
assertEquals(LIVE, searchResultItem.getResourceURI().getVersion());
result = repository.find(livePreferredQuery);
assertEquals(1, result.getDocumentCount());
assertEquals(LIVE, ((ResourceSearchResultItem) result.getItems()[0]).getResourceURI().getVersion());
// Add the work only work page
repository.put(workOnly);
assertEquals(1, repository.find(workOnlyQuery).getDocumentCount());
assertEquals(1, repository.find(liveOnlyQuery).getDocumentCount());
result = repository.find(workPreferredQuery);
assertEquals(2, result.getDocumentCount());
assertEquals(LIVE, ((ResourceSearchResultItem) result.getItems()[0]).getResourceURI().getVersion());
assertEquals(WORK, ((ResourceSearchResultItem) result.getItems()[1]).getResourceURI().getVersion());
result = repository.find(livePreferredQuery);
assertEquals(2, result.getDocumentCount());
assertEquals(LIVE, ((ResourceSearchResultItem) result.getItems()[0]).getResourceURI().getVersion());
assertEquals(WORK, ((ResourceSearchResultItem) result.getItems()[1]).getResourceURI().getVersion());
// Add the live and work live page
repository.put(liveAndWorkLive);
assertEquals(1, repository.find(workOnlyQuery).getDocumentCount());
assertEquals(2, repository.find(liveOnlyQuery).getDocumentCount());
result = repository.find(workPreferredQuery);
assertEquals(3, result.getDocumentCount());
assertEquals(LIVE, ((ResourceSearchResultItem) result.getItems()[0]).getResourceURI().getVersion());
assertEquals(WORK, ((ResourceSearchResultItem) result.getItems()[1]).getResourceURI().getVersion());
assertEquals(LIVE, ((ResourceSearchResultItem) result.getItems()[2]).getResourceURI().getVersion());
result = repository.find(livePreferredQuery);
assertEquals(3, result.getDocumentCount());
assertEquals(LIVE, ((ResourceSearchResultItem) result.getItems()[0]).getResourceURI().getVersion());
assertEquals(WORK, ((ResourceSearchResultItem) result.getItems()[1]).getResourceURI().getVersion());
assertEquals(LIVE, ((ResourceSearchResultItem) result.getItems()[2]).getResourceURI().getVersion());
// Add the live and work work page
repository.put(liveAndWorkWork);
assertEquals(2, repository.find(workOnlyQuery).getDocumentCount());
assertEquals(2, repository.find(liveOnlyQuery).getDocumentCount());
result = repository.find(workPreferredQuery);
assertEquals(3, result.getDocumentCount());
assertEquals(LIVE, ((ResourceSearchResultItem) result.getItems()[0]).getResourceURI().getVersion());
assertEquals(WORK, ((ResourceSearchResultItem) result.getItems()[1]).getResourceURI().getVersion());
assertEquals(WORK, ((ResourceSearchResultItem) result.getItems()[2]).getResourceURI().getVersion());
result = repository.find(livePreferredQuery);
assertEquals(3, result.getDocumentCount());
assertEquals(LIVE, ((ResourceSearchResultItem) result.getItems()[0]).getResourceURI().getVersion());
assertEquals(WORK, ((ResourceSearchResultItem) result.getItems()[1]).getResourceURI().getVersion());
assertEquals(LIVE, ((ResourceSearchResultItem) result.getItems()[2]).getResourceURI().getVersion());
// Lock the page, which will trigger a call to index.update()
repository.lock(liveAndWorkWorkURI, new UserImpl("user"));
assertEquals(2, repository.find(workOnlyQuery).getDocumentCount());
assertEquals(2, repository.find(liveOnlyQuery).getDocumentCount());
result = repository.find(workPreferredQuery);
assertEquals(3, result.getDocumentCount());
assertEquals(LIVE, ((ResourceSearchResultItem) result.getItems()[0]).getResourceURI().getVersion());
assertEquals(WORK, ((ResourceSearchResultItem) result.getItems()[1]).getResourceURI().getVersion());
assertEquals(WORK, ((ResourceSearchResultItem) result.getItems()[2]).getResourceURI().getVersion());
result = repository.find(livePreferredQuery);
assertEquals(3, result.getDocumentCount());
assertEquals(LIVE, ((ResourceSearchResultItem) result.getItems()[0]).getResourceURI().getVersion());
assertEquals(WORK, ((ResourceSearchResultItem) result.getItems()[1]).getResourceURI().getVersion());
assertEquals(LIVE, ((ResourceSearchResultItem) result.getItems()[2]).getResourceURI().getVersion());
}