add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / src / test / org / apache / lucene / search / TestSpanQueryFilter.java
1 package org.apache.lucene.search;
2
3 /**
4  * Copyright 2004 The Apache Software Foundation
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18
19 import java.util.List;
20
21 import org.apache.lucene.analysis.MockAnalyzer;
22 import org.apache.lucene.document.Document;
23 import org.apache.lucene.document.Field;
24 import org.apache.lucene.index.IndexReader;
25 import org.apache.lucene.index.RandomIndexWriter;
26 import org.apache.lucene.index.Term;
27 import org.apache.lucene.search.spans.SpanTermQuery;
28 import org.apache.lucene.store.Directory;
29 import org.apache.lucene.util.English;
30 import org.apache.lucene.util.LuceneTestCase;
31
32 public class TestSpanQueryFilter extends LuceneTestCase {
33
34   public void testFilterWorks() throws Exception {
35     Directory dir = newDirectory();
36     RandomIndexWriter writer = new RandomIndexWriter(random, dir, newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random)).setMergePolicy(newLogMergePolicy()));
37     for (int i = 0; i < 500; i++) {
38       Document document = new Document();
39       document.add(newField("field", English.intToEnglish(i) + " equals " + English.intToEnglish(i),
40               Field.Store.NO, Field.Index.ANALYZED));
41       writer.addDocument(document);
42     }
43     IndexReader reader = writer.getReader();
44     writer.close();
45
46     SpanTermQuery query = new SpanTermQuery(new Term("field", English.intToEnglish(10).trim()));
47     SpanQueryFilter filter = new SpanQueryFilter(query);
48     SpanFilterResult result = filter.bitSpans(reader);
49     DocIdSet docIdSet = result.getDocIdSet();
50     assertTrue("docIdSet is null and it shouldn't be", docIdSet != null);
51     assertContainsDocId("docIdSet doesn't contain docId 10", docIdSet, 10);
52     List<SpanFilterResult.PositionInfo> spans = result.getPositions();
53     assertTrue("spans is null and it shouldn't be", spans != null);
54     int size = getDocIdSetSize(docIdSet);
55     assertTrue("spans Size: " + spans.size() + " is not: " + size, spans.size() == size);
56     for (final SpanFilterResult.PositionInfo info: spans) {
57       assertTrue("info is null and it shouldn't be", info != null);
58       //The doc should indicate the bit is on
59       assertContainsDocId("docIdSet doesn't contain docId " + info.getDoc(), docIdSet, info.getDoc());
60       //There should be two positions in each
61       assertTrue("info.getPositions() Size: " + info.getPositions().size() + " is not: " + 2, info.getPositions().size() == 2);
62     }
63     reader.close();
64     dir.close();
65   }
66   
67   int getDocIdSetSize(DocIdSet docIdSet) throws Exception {
68     int size = 0;
69     DocIdSetIterator it = docIdSet.iterator();
70     while (it.nextDoc() != DocIdSetIterator.NO_MORE_DOCS) {
71       size++;
72     }
73     return size;
74   }
75   
76   public void assertContainsDocId(String msg, DocIdSet docIdSet, int docId) throws Exception {
77     DocIdSetIterator it = docIdSet.iterator();
78     assertTrue(msg, it.advance(docId) != DocIdSetIterator.NO_MORE_DOCS);
79     assertTrue(msg, it.docID() == docId);
80   }
81 }