public void testEmptyBucketWithMoreDocs() throws Exception { // This test checks the logic of nextDoc() when all sub scorers have docs // beyond the first bucket (for example). Currently, the code relies on the // 'more' variable to work properly, and this test ensures that if the logic // changes, we have a test to back it up. Similarity sim = Similarity.getDefault(); Scorer[] scorers = new Scorer[] { new Scorer(sim) { private int doc = -1; @Override public float score() throws IOException { return 0; } @Override public int docID() { return doc; } @Override public int nextDoc() throws IOException { return doc = doc == -1 ? 3000 : NO_MORE_DOCS; } @Override public int advance(int target) throws IOException { return doc = target <= 3000 ? 3000 : NO_MORE_DOCS; } } }; BooleanScorer bs = new BooleanScorer(sim, 1, Arrays.asList(scorers), null); assertEquals("should have received 3000", 3000, bs.nextDoc()); assertEquals("should have received NO_MORE_DOCS", DocIdSetIterator.NO_MORE_DOCS, bs.nextDoc()); }
public void testEmptyBucketWithMoreDocs() throws Exception { // This test checks the logic of nextDoc() when all sub scorers have docs // beyond the first bucket (for example). Currently, the code relies on the // 'more' variable to work properly, and this test ensures that if the logic // changes, we have a test to back it up. Directory directory = newDirectory(); RandomIndexWriter writer = new RandomIndexWriter(random(), directory); writer.commit(); IndexReader ir = writer.getReader(); writer.close(); IndexSearcher searcher = newSearcher(ir); BooleanWeight weight = (BooleanWeight) new BooleanQuery().createWeight(searcher); Scorer[] scorers = new Scorer[] { new Scorer(weight) { private int doc = -1; @Override public float score() { return 0; } @Override public float freq() { return 0; } @Override public int docID() { return doc; } @Override public int nextDoc() { return doc = doc == -1 ? 3000 : NO_MORE_DOCS; } @Override public int advance(int target) { return doc = target <= 3000 ? 3000 : NO_MORE_DOCS; } } }; BooleanScorer bs = new BooleanScorer(weight, false, 1, Arrays.asList(scorers), null, scorers.length); final List<Integer> hits = new ArrayList<Integer>(); bs.score( new Collector() { int docBase; @Override public void setScorer(Scorer scorer) {} @Override public void collect(int doc) { hits.add(docBase + doc); } @Override public void setNextReader(AtomicReaderContext context) { docBase = context.docBase; } @Override public boolean acceptsDocsOutOfOrder() { return true; } }); assertEquals("should have only 1 hit", 1, hits.size()); assertEquals("hit should have been docID=3000", 3000, hits.get(0).intValue()); ir.close(); directory.close(); }