pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / instantiated / src / test / org / apache / lucene / store / instantiated / TestRealTime.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 package org.apache.lucene.store.instantiated;
17
18 import org.apache.lucene.search.IndexSearcher;
19 import org.apache.lucene.search.TermQuery;
20 import org.apache.lucene.search.Scorer;
21 import org.apache.lucene.document.Document;
22 import org.apache.lucene.document.Field;
23 import org.apache.lucene.index.Term;
24 import org.apache.lucene.index.IndexReader;
25 import org.apache.lucene.util.LuceneTestCase;
26
27 /**
28  * Assert that the content of an index 
29  * is instantly available
30  * for all open searchers
31  * also after a commit.
32  */
33 public class TestRealTime extends LuceneTestCase {
34
35   public void test() throws Exception {
36
37     InstantiatedIndex index = new InstantiatedIndex();
38     InstantiatedIndexReader reader = new InstantiatedIndexReader(index);
39     IndexSearcher searcher = newSearcher(reader, false);
40     InstantiatedIndexWriter writer = new InstantiatedIndexWriter(index);
41
42     Document doc;
43     Collector collector;
44
45     doc = new Document();
46     doc.add(new Field("f", "a", Field.Store.NO, Field.Index.NOT_ANALYZED));
47     writer.addDocument(doc);
48     writer.commit();
49
50     collector = new Collector();
51     searcher.search(new TermQuery(new Term("f", "a")), collector);
52     assertEquals(1, collector.hits);
53
54     doc = new Document();
55     doc.add(new Field("f", "a", Field.Store.NO, Field.Index.NOT_ANALYZED));
56     writer.addDocument(doc);
57     writer.commit();
58
59     collector = new Collector();
60     searcher.search(new TermQuery(new Term("f", "a")), collector);
61     assertEquals(2, collector.hits);
62
63   }
64
65   public static class Collector extends org.apache.lucene.search.Collector {
66     private int hits = 0;
67     @Override
68     public void setScorer(Scorer scorer) {}
69     @Override
70     public void setNextReader(IndexReader reader, int docBase) {}
71     @Override
72     public boolean acceptsDocsOutOfOrder() { return true; }
73     @Override
74     public void collect(int doc) {
75       hits++;
76     }
77   }
78
79 }