pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / src / test / org / apache / lucene / search / TestDocIdSet.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 import java.util.ArrayList;
22 import java.util.Arrays;
23 import java.util.Iterator;
24
25 import junit.framework.Assert;
26
27 import org.apache.lucene.document.Document;
28 import org.apache.lucene.document.Field.Index;
29 import org.apache.lucene.document.Field.Store;
30 import org.apache.lucene.index.IndexReader;
31 import org.apache.lucene.index.RandomIndexWriter;
32 import org.apache.lucene.store.Directory;
33 import org.apache.lucene.util.LuceneTestCase;
34
35 public class TestDocIdSet extends LuceneTestCase {
36   public void testFilteredDocIdSet() throws Exception {
37     final int maxdoc=10;
38     final DocIdSet innerSet = new DocIdSet() {
39
40         @Override
41         public DocIdSetIterator iterator() {
42           return new DocIdSetIterator() {
43
44             int docid = -1;
45             
46             @Override
47             public int docID() {
48               return docid;
49             }
50             
51             @Override
52             public int nextDoc() throws IOException {
53               docid++;
54               return docid < maxdoc ? docid : (docid = NO_MORE_DOCS);
55             }
56
57             @Override
58             public int advance(int target) throws IOException {
59               while (nextDoc() < target) {}
60               return docid;
61             }
62           };
63         } 
64       };
65           
66                 
67     DocIdSet filteredSet = new FilteredDocIdSet(innerSet){
68         @Override
69         protected boolean match(int docid) {
70           return docid%2 == 0;  //validate only even docids
71         }       
72       };
73           
74     DocIdSetIterator iter = filteredSet.iterator();
75     ArrayList<Integer> list = new ArrayList<Integer>();
76     int doc = iter.advance(3);
77     if (doc != DocIdSetIterator.NO_MORE_DOCS) {
78       list.add(Integer.valueOf(doc));
79       while((doc = iter.nextDoc()) != DocIdSetIterator.NO_MORE_DOCS) {
80         list.add(Integer.valueOf(doc));
81       }
82     }
83           
84     int[] docs = new int[list.size()];
85     int c=0;
86     Iterator<Integer> intIter = list.iterator();
87     while(intIter.hasNext()) {
88       docs[c++] = intIter.next().intValue();
89     }
90     int[] answer = new int[]{4,6,8};
91     boolean same = Arrays.equals(answer, docs);
92     if (!same) {
93       System.out.println("answer: " + Arrays.toString(answer));
94       System.out.println("gotten: " + Arrays.toString(docs));
95       fail();
96     }
97   }
98   
99   public void testNullDocIdSet() throws Exception {
100     // Tests that if a Filter produces a null DocIdSet, which is given to
101     // IndexSearcher, everything works fine. This came up in LUCENE-1754.
102     Directory dir = newDirectory();
103     RandomIndexWriter writer = new RandomIndexWriter(random, dir);
104     Document doc = new Document();
105     doc.add(newField("c", "val", Store.NO, Index.NOT_ANALYZED_NO_NORMS));
106     writer.addDocument(doc);
107     IndexReader reader = writer.getReader();
108     writer.close();
109     
110     // First verify the document is searchable.
111     IndexSearcher searcher = newSearcher(reader);
112     Assert.assertEquals(1, searcher.search(new MatchAllDocsQuery(), 10).totalHits);
113     
114     // Now search w/ a Filter which returns a null DocIdSet
115     Filter f = new Filter() {
116       @Override
117       public DocIdSet getDocIdSet(IndexReader reader) throws IOException {
118         return null;
119       }
120     };
121     
122     Assert.assertEquals(0, searcher.search(new MatchAllDocsQuery(), f, 10).totalHits);
123     searcher.close();
124     reader.close();
125     dir.close();
126   }
127
128 }