pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / src / test / org / apache / lucene / index / TestReaderClosed.java
1 package org.apache.lucene.index;
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.analysis.MockAnalyzer;
21 import org.apache.lucene.analysis.MockTokenizer;
22 import org.apache.lucene.document.Document;
23 import org.apache.lucene.document.Field;
24 import org.apache.lucene.search.IndexSearcher;
25 import org.apache.lucene.search.TermRangeQuery;
26 import org.apache.lucene.store.AlreadyClosedException;
27 import org.apache.lucene.store.Directory;
28 import org.apache.lucene.util.LuceneTestCase;
29 import org.apache.lucene.util._TestUtil;
30
31 public class TestReaderClosed extends LuceneTestCase {
32   private IndexSearcher searcher;
33   private IndexReader reader;
34   private Directory dir;
35
36   @Override
37   public void setUp() throws Exception {
38     super.setUp();
39     dir = newDirectory();
40     RandomIndexWriter writer = new RandomIndexWriter(random, dir, 
41         newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random, MockTokenizer.KEYWORD, false))
42         .setMaxBufferedDocs(_TestUtil.nextInt(random, 50, 1000)));
43     
44     Document doc = new Document();
45     Field field = newField("field", "", Field.Store.NO, Field.Index.NOT_ANALYZED);
46     doc.add(field);
47
48     // we generate aweful prefixes: good for testing.
49     // but for preflex codec, the test can be very slow, so use less iterations.
50     int num = atLeast(10);
51     for (int i = 0; i < num; i++) {
52       field.setValue(_TestUtil.randomUnicodeString(random, 10));
53       writer.addDocument(doc);
54     }
55     reader = writer.getReader();
56     searcher = newSearcher(reader);
57     writer.close();
58   }
59   
60   public void test() throws Exception {
61     TermRangeQuery query = new TermRangeQuery("field", "a", "z", true, true);
62     searcher.search(query, 5);
63     searcher.close();
64     reader.close();
65     try {
66       searcher.search(query, 5);
67     } catch (AlreadyClosedException ace) {
68       // expected
69     }
70   }
71   
72   public void tearDown() throws Exception {
73     dir.close();
74     super.tearDown();
75   }
76 }