add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / src / test / org / apache / lucene / search / TestFieldCacheTermsFilter.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 org.apache.lucene.util.LuceneTestCase;
21
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.store.Directory;
27
28 import java.util.ArrayList;
29 import java.util.List;
30
31 /**
32  * A basic unit test for FieldCacheTermsFilter
33  *
34  * @see org.apache.lucene.search.FieldCacheTermsFilter
35  */
36 public class TestFieldCacheTermsFilter extends LuceneTestCase {
37   public void testMissingTerms() throws Exception {
38     String fieldName = "field1";
39     Directory rd = newDirectory();
40     RandomIndexWriter w = new RandomIndexWriter(random, rd);
41     for (int i = 0; i < 100; i++) {
42       Document doc = new Document();
43       int term = i * 10; //terms are units of 10;
44       doc.add(newField(fieldName, "" + term, Field.Store.YES, Field.Index.NOT_ANALYZED));
45       w.addDocument(doc);
46     }
47     IndexReader reader = w.getReader();
48     w.close();
49
50     IndexSearcher searcher = newSearcher(reader);
51     int numDocs = reader.numDocs();
52     ScoreDoc[] results;
53     MatchAllDocsQuery q = new MatchAllDocsQuery();
54
55     List<String> terms = new ArrayList<String>();
56     terms.add("5");
57     results = searcher.search(q, new FieldCacheTermsFilter(fieldName,  terms.toArray(new String[0])), numDocs).scoreDocs;
58     assertEquals("Must match nothing", 0, results.length);
59
60     terms = new ArrayList<String>();
61     terms.add("10");
62     results = searcher.search(q, new FieldCacheTermsFilter(fieldName,  terms.toArray(new String[0])), numDocs).scoreDocs;
63     assertEquals("Must match 1", 1, results.length);
64
65     terms = new ArrayList<String>();
66     terms.add("10");
67     terms.add("20");
68     results = searcher.search(q, new FieldCacheTermsFilter(fieldName,  terms.toArray(new String[0])), numDocs).scoreDocs;
69     assertEquals("Must match 2", 2, results.length);
70
71     searcher.close();
72     reader.close();
73     rd.close();
74   }
75 }