1 package org.apache.lucene.index;
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
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.*;
26 import org.junit.Test;
28 import java.io.IOException;
30 public class TestIsCurrent extends LuceneTestCase {
32 private RandomIndexWriter writer;
34 private Directory directory;
37 public void setUp() throws Exception {
40 // initialize directory
41 directory = newDirectory();
42 writer = new RandomIndexWriter(random, directory);
45 Document doc = new Document();
46 doc.add(newField("UUID", "1", Store.YES, Index.ANALYZED));
47 writer.addDocument(doc);
52 public void tearDown() throws Exception {
59 * Failing testcase showing the trouble
64 public void testDeleteByTermIsCurrent() throws IOException {
67 IndexReader reader = writer.getReader();
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());
74 Term idTerm = new Term("UUID", "1");
75 writer.deleteDocuments(idTerm);
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());
86 * Testcase for example to show that writer.deleteAll() is working as expected
91 public void testDeleteAllIsCurrent() throws IOException {
94 IndexReader reader = writer.getReader();
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());
100 // remove all documents
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());