Skip to content

Instantly share code, notes, and snippets.

@breun
Created November 4, 2016 16:44
Show Gist options
  • Save breun/7d2072b3b6ae8c2a66e3057a603ebcdc to your computer and use it in GitHub Desktop.
Save breun/7d2072b3b6ae8c2a66e3057a603ebcdc to your computer and use it in GitHub Desktop.
package org.apache.jackrabbit.core.query.lucene;
import org.apache.jackrabbit.core.id.NodeId;
import org.apache.jackrabbit.core.security.AccessManager;
import org.apache.jackrabbit.core.session.SessionContext;
import org.apache.jackrabbit.spi.Path;
import org.apache.jackrabbit.spi.commons.query.qom.ColumnImpl;
import org.apache.lucene.search.Query;
import org.junit.Test;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import java.util.UUID;
import static org.mockito.Matchers.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public class QueryResultImplTest {
/*
* This test simulates a query result with 20000 hits and an offset of 12000, which sizeEstimate enabled.
*
* The internal state of QueryResultImpl is not observable, but setting a breakpoint on the last line of the
* collectScoreNodes method shows a list with 12000 offset objects is created (and discarded immediately afterwards).
*
* In my opinion this list shouldn't be constructed at all for this query and the code should just call skip(offset)
* on the query hits and then get its result nodes.
*/
@Test
public void largeOffsetNodesList() throws Exception {
final QueryHits queryHits = mock(QueryHits.class);
when(queryHits.nextScoreNode()).thenAnswer(new Answer<ScoreNode>() {
private int count = 20000;
@Override
public ScoreNode answer(InvocationOnMock invocation) throws Throwable {
count--;
return count > 0 ? new ScoreNode(new NodeId(UUID.randomUUID()), 1.0f) : null;
}
});
final SearchIndex index = mock(SearchIndex.class);
when(index.getSizeEstimate()).thenReturn(true);
when(index.executeQuery(anyObject(), anyObject(), anyObject(), anyObject(), any(boolean[].class), any(String[].class), anyLong()))
.thenReturn(new FilterMultiColumnQueryHits(new QueryHitsAdapter(queryHits, QueryImpl.DEFAULT_SELECTOR_NAME)));
final AccessManager accessManager = mock(AccessManager.class);
when(accessManager.canRead(anyObject(), anyObject())).thenReturn(true);
final SessionContext sessionContext = mock(SessionContext.class);
when(sessionContext.getAccessManager()).thenReturn(accessManager);
final AbstractQueryImpl queryImpl = mock(AbstractQueryImpl.class);
final Query query = mock(Query.class);
final SpellSuggestion spellSuggestion = mock(SpellSuggestion.class);
final ColumnImpl[] columns = new ColumnImpl[0];
final Path[] orderProps = null;
final boolean[] orderSpecs = null;
final String[] orderFuncs = new String[0];
final int offset = 12000;
final int limit = 20;
final boolean documentOrder = true;
new SingleColumnQueryResult(index, sessionContext, queryImpl, query, spellSuggestion, columns, orderProps, orderSpecs, orderFuncs, documentOrder, offset, limit);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment