pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / instantiated / src / test / org / apache / lucene / store / instantiated / TestEmptyIndex.java
1 /**
2  * Copyright 2006 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.apache.lucene.store.instantiated;
18
19 import java.io.IOException;
20 import java.util.Arrays;
21
22 import org.apache.lucene.analysis.MockAnalyzer;
23 import org.apache.lucene.index.IndexReader;
24 import org.apache.lucene.index.IndexWriter;
25 import org.apache.lucene.index.Term;
26 import org.apache.lucene.index.TermEnum;
27 import org.apache.lucene.search.IndexSearcher;
28 import org.apache.lucene.search.TermQuery;
29 import org.apache.lucene.search.TopDocs;
30 import org.apache.lucene.store.Directory;
31 import org.apache.lucene.util.LuceneTestCase;
32
33 public class TestEmptyIndex extends LuceneTestCase {
34
35   public void testSearch() throws Exception {
36
37     InstantiatedIndex ii = new InstantiatedIndex();
38
39     IndexReader r = new InstantiatedIndexReader(ii);
40     IndexSearcher s = newSearcher(r);
41
42     TopDocs td = s.search(new TermQuery(new Term("foo", "bar")), 1);
43
44     assertEquals(0, td.totalHits);
45
46     s.close();
47     r.close();
48     ii.close();
49
50   }
51
52   public void testNorms() throws Exception {
53
54     InstantiatedIndex ii = new InstantiatedIndex();
55     IndexReader r = new InstantiatedIndexReader(ii);
56     testNorms(r);
57     r.close();
58     ii.close();
59
60     // make sure a Directory acts the same
61     Directory d = newDirectory();
62     new IndexWriter(d, newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random))).close();
63     r = IndexReader.open(d, false);
64     testNorms(r);
65     r.close();
66     d.close();
67
68   }
69
70   private void testNorms(IndexReader r) throws IOException {
71     byte[] norms;
72     norms = r.norms("foo");
73     if (norms != null) {
74       assertEquals(0, norms.length);
75       norms = new byte[10];
76       Arrays.fill(norms, (byte)10);
77       r.norms("foo", norms, 10);
78       for (byte b : norms) {
79         assertEquals((byte)10, b);
80       }
81     }
82   }
83
84   public void testTermEnum() throws Exception {
85
86     InstantiatedIndex ii = new InstantiatedIndex();
87     IndexReader r = new InstantiatedIndexReader(ii);
88     termEnumTest(r);
89     r.close();
90     ii.close();
91
92     // make sure a Directory acts the same
93     Directory d = newDirectory();
94     new IndexWriter(d, newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random))).close();
95     r = IndexReader.open(d, false);
96     termEnumTest(r);
97     r.close();
98     d.close();
99   }
100
101   public void termEnumTest(IndexReader r) throws Exception {
102     TermEnum terms = r.terms();
103
104     assertNull(terms.term());
105     assertFalse(terms.next());
106
107   }
108
109 }