pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / backwards / src / test / org / apache / lucene / search / MultiCollectorTest.java
1 package org.apache.lucene.search;
2
3 /**
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
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  */
19
20 import java.io.IOException;
21
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;
27
28 public class MultiCollectorTest extends LuceneTestCase {
29
30   private static class DummyCollector extends Collector {
31
32     boolean acceptsDocsOutOfOrderCalled = false;
33     boolean collectCalled = false;
34     boolean setNextReaderCalled = false;
35     boolean setScorerCalled = false;
36
37     @Override
38     public boolean acceptsDocsOutOfOrder() {
39       acceptsDocsOutOfOrderCalled = true;
40       return true;
41     }
42
43     @Override
44     public void collect(int doc) throws IOException {
45       collectCalled = true;
46     }
47
48     @Override
49     public void setNextReader(IndexReader reader, int docBase) throws IOException {
50       setNextReaderCalled = true;
51     }
52
53     @Override
54     public void setScorer(Scorer scorer) throws IOException {
55       setScorerCalled = true;
56     }
57
58   }
59
60   @Test
61   public void testNullCollectors() throws Exception {
62     // Tests that the collector rejects all null collectors.
63     try {
64       MultiCollector.wrap(null, null);
65       fail("only null collectors should not be supported");
66     } catch (IllegalArgumentException e) {
67       // expected
68     }
69
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());
75     c.collect(1);
76     c.setNextReader(null, 0);
77     c.setScorer(null);
78   }
79
80   @Test
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));
86   }
87   
88   @Test
89   public void testCollector() throws Exception {
90     // Tests that the collector delegates calls to input collectors properly.
91
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());
97     c.collect(1);
98     c.setNextReader(null, 0);
99     c.setScorer(null);
100
101     for (DummyCollector dc : dcs) {
102       assertTrue(dc.acceptsDocsOutOfOrderCalled);
103       assertTrue(dc.collectCalled);
104       assertTrue(dc.setNextReaderCalled);
105       assertTrue(dc.setScorerCalled);
106     }
107
108   }
109
110 }