1 package org.apache.lucene.search;
4 * Licensed to the Apache Software Foundation (ASF) under one or more
5 * contributor license agreements. See the NOTICE file distributed with
6 * this work for additional information regarding copyright ownership.
7 * The ASF licenses this file to You under the Apache License, Version 2.0
8 * (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
20 import java.io.IOException;
22 import org.apache.lucene.index.IndexReader;
23 import org.apache.lucene.search.Collector;
24 import org.apache.lucene.search.Scorer;
25 import org.apache.lucene.util.LuceneTestCase;
26 import org.junit.Test;
28 public class MultiCollectorTest extends LuceneTestCase {
30 private static class DummyCollector extends Collector {
32 boolean acceptsDocsOutOfOrderCalled = false;
33 boolean collectCalled = false;
34 boolean setNextReaderCalled = false;
35 boolean setScorerCalled = false;
38 public boolean acceptsDocsOutOfOrder() {
39 acceptsDocsOutOfOrderCalled = true;
44 public void collect(int doc) throws IOException {
49 public void setNextReader(IndexReader reader, int docBase) throws IOException {
50 setNextReaderCalled = true;
54 public void setScorer(Scorer scorer) throws IOException {
55 setScorerCalled = true;
61 public void testNullCollectors() throws Exception {
62 // Tests that the collector rejects all null collectors.
64 MultiCollector.wrap(null, null);
65 fail("only null collectors should not be supported");
66 } catch (IllegalArgumentException e) {
70 // Tests that the collector handles some null collectors well. If it
71 // doesn't, an NPE would be thrown.
72 Collector c = MultiCollector.wrap(new DummyCollector(), null, new DummyCollector());
73 assertTrue(c instanceof MultiCollector);
74 assertTrue(c.acceptsDocsOutOfOrder());
76 c.setNextReader(null, 0);
81 public void testSingleCollector() throws Exception {
82 // Tests that if a single Collector is input, it is returned (and not MultiCollector).
83 DummyCollector dc = new DummyCollector();
84 assertSame(dc, MultiCollector.wrap(dc));
85 assertSame(dc, MultiCollector.wrap(dc, null));
89 public void testCollector() throws Exception {
90 // Tests that the collector delegates calls to input collectors properly.
92 // Tests that the collector handles some null collectors well. If it
93 // doesn't, an NPE would be thrown.
94 DummyCollector[] dcs = new DummyCollector[] { new DummyCollector(), new DummyCollector() };
95 Collector c = MultiCollector.wrap(dcs);
96 assertTrue(c.acceptsDocsOutOfOrder());
98 c.setNextReader(null, 0);
101 for (DummyCollector dc : dcs) {
102 assertTrue(dc.acceptsDocsOutOfOrderCalled);
103 assertTrue(dc.collectCalled);
104 assertTrue(dc.setNextReaderCalled);
105 assertTrue(dc.setScorerCalled);