alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Lucene example source code file (TestIsCurrent.java)

This example Lucene source code file (TestIsCurrent.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Lucene tags/keywords

document, document, indexreader, io, ioexception, one, one, override, randomindexwriter, reader, reader, term, test, test, uuid

The Lucene TestIsCurrent.java source code

package org.apache.lucene.index;

/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field.Index;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.util.*;
import org.apache.lucene.store.*;

import org.junit.Test;

import java.io.IOException;

public class TestIsCurrent extends LuceneTestCase {

  private RandomIndexWriter writer;

  private Directory directory;

  @Override
  public void setUp() throws Exception {
    super.setUp();

    // initialize directory
    directory = newDirectory();
    writer = new RandomIndexWriter(random, directory);

    // write document
    Document doc = new Document();
    doc.add(newField("UUID", "1", Store.YES, Index.ANALYZED));
    writer.addDocument(doc);
    writer.commit();
  }

  @Override
  public void tearDown() throws Exception {
    super.tearDown();
    writer.close();
    directory.close();
  }

  /**
   * Failing testcase showing the trouble
   * 
   * @throws IOException
   */
  @Test
  public void testDeleteByTermIsCurrent() throws IOException {

    // get reader
    IndexReader reader = writer.getReader();

    // assert index has a document and reader is up2date 
    assertEquals("One document should be in the index", 1, writer.numDocs());
    assertTrue("One document added, reader should be current", reader.isCurrent());

    // remove document
    Term idTerm = new Term("UUID", "1");
    writer.deleteDocuments(idTerm);
    writer.commit();

    // assert document has been deleted (index changed), reader is stale
    assertEquals("Document should be removed", 0, writer.numDocs());
    assertFalse("Reader should be stale", reader.isCurrent());

    reader.close();
  }

  /**
   * Testcase for example to show that writer.deleteAll() is working as expected
   * 
   * @throws IOException
   */
  @Test
  public void testDeleteAllIsCurrent() throws IOException {

    // get reader
    IndexReader reader = writer.getReader();

    // assert index has a document and reader is up2date 
    assertEquals("One document should be in the index", 1, writer.numDocs());
    assertTrue("Document added, reader should be stale ", reader.isCurrent());

    // remove all documents
    writer.deleteAll();
    writer.commit();

    // assert document has been deleted (index changed), reader is stale
    assertEquals("Document should be removed", 0, writer.numDocs());
    assertFalse("Reader should be stale", reader.isCurrent());

    reader.close();
  }
}

Other Lucene examples (source code examples)

Here is a short list of links related to this Lucene TestIsCurrent.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.