add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / backwards / src / test / org / apache / lucene / index / TestIsCurrent.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.document.Document;
21 import org.apache.lucene.document.Field.Index;
22 import org.apache.lucene.document.Field.Store;
23 import org.apache.lucene.util.*;
24 import org.apache.lucene.store.*;
25
26 import org.junit.Test;
27
28 import java.io.IOException;
29
30 public class TestIsCurrent extends LuceneTestCase {
31
32   private RandomIndexWriter writer;
33
34   private Directory directory;
35
36   @Override
37   public void setUp() throws Exception {
38     super.setUp();
39
40     // initialize directory
41     directory = newDirectory();
42     writer = new RandomIndexWriter(random, directory);
43
44     // write document
45     Document doc = new Document();
46     doc.add(newField("UUID", "1", Store.YES, Index.ANALYZED));
47     writer.addDocument(doc);
48     writer.commit();
49   }
50
51   @Override
52   public void tearDown() throws Exception {
53     super.tearDown();
54     writer.close();
55     directory.close();
56   }
57
58   /**
59    * Failing testcase showing the trouble
60    * 
61    * @throws IOException
62    */
63   @Test
64   public void testDeleteByTermIsCurrent() throws IOException {
65
66     // get reader
67     IndexReader reader = writer.getReader();
68
69     // assert index has a document and reader is up2date 
70     assertEquals("One document should be in the index", 1, writer.numDocs());
71     assertTrue("One document added, reader should be current", reader.isCurrent());
72
73     // remove document
74     Term idTerm = new Term("UUID", "1");
75     writer.deleteDocuments(idTerm);
76     writer.commit();
77
78     // assert document has been deleted (index changed), reader is stale
79     assertEquals("Document should be removed", 0, writer.numDocs());
80     assertFalse("Reader should be stale", reader.isCurrent());
81
82     reader.close();
83   }
84
85   /**
86    * Testcase for example to show that writer.deleteAll() is working as expected
87    * 
88    * @throws IOException
89    */
90   @Test
91   public void testDeleteAllIsCurrent() throws IOException {
92
93     // get reader
94     IndexReader reader = writer.getReader();
95
96     // assert index has a document and reader is up2date 
97     assertEquals("One document should be in the index", 1, writer.numDocs());
98     assertTrue("Document added, reader should be stale ", reader.isCurrent());
99
100     // remove all documents
101     writer.deleteAll();
102     writer.commit();
103
104     // assert document has been deleted (index changed), reader is stale
105     assertEquals("Document should be removed", 0, writer.numDocs());
106     assertFalse("Reader should be stale", reader.isCurrent());
107
108     reader.close();
109   }
110 }