You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@lucene.apache.org by ot...@apache.org on 2003/09/15 14:37:49 UTC

cvs commit: jakarta-lucene/src/test/org/apache/lucene/index TestIndexReader.java

otis        2003/09/15 05:37:49

  Modified:    src/test/org/apache/lucene/index TestIndexReader.java
  Log:
  - Added testDeleteReaderWriterConflict() and testDeleteReaderReaderConflict()
    methods, provided by Christoph Goller.
  - Moved the license ahead of imports, to match other sources.
  
  Revision  Changes    Path
  1.3       +177 -9    jakarta-lucene/src/test/org/apache/lucene/index/TestIndexReader.java
  
  Index: TestIndexReader.java
  ===================================================================
  RCS file: /home/cvs/jakarta-lucene/src/test/org/apache/lucene/index/TestIndexReader.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- TestIndexReader.java	1 May 2003 01:12:10 -0000	1.2
  +++ TestIndexReader.java	15 Sep 2003 12:37:49 -0000	1.3
  @@ -1,14 +1,5 @@
   package org.apache.lucene.index;
   
  -import junit.framework.TestCase;
  -import org.apache.lucene.store.RAMDirectory;
  -import org.apache.lucene.analysis.standard.StandardAnalyzer;
  -import org.apache.lucene.document.Document;
  -import org.apache.lucene.document.Field;
  -
  -import java.util.Collection;
  -import java.io.IOException;
  -
   /* ====================================================================
    * The Apache Software License, Version 1.1
    *
  @@ -63,6 +54,23 @@
    * <http://www.apache.org/>.
    */
   
  +
  +import junit.framework.TestCase;
  +
  +import org.apache.lucene.search.IndexSearcher;
  +import org.apache.lucene.search.Searcher;
  +import org.apache.lucene.search.Hits;
  +import org.apache.lucene.search.TermQuery;
  +import org.apache.lucene.store.Directory;
  +import org.apache.lucene.store.RAMDirectory;
  +import org.apache.lucene.analysis.standard.StandardAnalyzer;
  +import org.apache.lucene.analysis.WhitespaceAnalyzer;
  +import org.apache.lucene.document.Document;
  +import org.apache.lucene.document.Field;
  +
  +import java.util.Collection;
  +import java.io.IOException;
  +
   public class TestIndexReader extends TestCase
   {
       /**
  @@ -123,6 +131,151 @@
           assertTrue(fieldNames.contains("unindexed"));
       }
   
  +    public void testDeleteReaderWriterConflict()
  +    {
  +        Directory dir = new RAMDirectory();
  +        IndexWriter writer = null;
  +        IndexReader reader = null;
  +        Searcher searcher = null;
  +        Term searchTerm = new Term("content", "aaa");
  +        Hits hits = null;
  +
  +        try
  +        {
  +            //  add 100 documents with term : aaa
  +            writer  = new IndexWriter(dir, new WhitespaceAnalyzer(), true);
  +            for (int i = 0; i < 100; i++)
  +            {
  +                addDoc(writer, "aaa");
  +            }
  +            writer.close();
  +            reader = IndexReader.open(dir);
  +
  +            //  add 100 documents with term : bbb
  +            writer  = new IndexWriter(dir, new WhitespaceAnalyzer(), false);
  +            for (int i = 0; i < 100; i++)
  +            {
  +                addDoc(writer, "bbb");
  +            }
  +            writer.optimize();
  +            writer.close();
  +        }
  +        catch (IOException e)
  +        {
  +            e.printStackTrace();
  +        }
  +
  +        try
  +        {
  +            // delete documents containing term: aaa
  +            reader.delete(searchTerm);
  +            reader.close();
  +        }
  +        catch (IOException e)
  +        {
  +            try
  +            {
  +                // if reader throws IOException try once more to delete documents with a new reader
  +                reader.close();
  +                reader = IndexReader.open(dir);
  +                reader.delete(searchTerm);
  +                reader.close();
  +            }
  +            catch (IOException e1)
  +            {
  +                e1.printStackTrace();
  +            }
  +        }
  +
  +        try
  +        {
  +            searcher = new IndexSearcher(dir);
  +            hits = searcher.search(new TermQuery(searchTerm));
  +            assertEquals(0, hits.length());
  +            searcher.close();
  +        }
  +        catch (IOException e1)
  +        {
  +            e1.printStackTrace();
  +        }
  +    }
  +
  +    public void testDeleteReaderReaderConflict()
  +    {
  +        Directory dir = new RAMDirectory();
  +        IndexWriter writer = null;
  +        IndexReader reader1 = null;
  +        IndexReader reader2 = null;
  +        Searcher searcher = null;
  +        Hits hits = null;
  +        Term searchTerm1 = new Term("content", "aaa");
  +        Term searchTerm2 = new Term("content", "bbb");
  +
  +        try
  +        {
  +            //  add 100 documents with term : aaa
  +            //  add 100 documents with term : bbb
  +            //  add 100 documents with term : ccc
  +            writer  = new IndexWriter(dir, new WhitespaceAnalyzer(), true);
  +            for (int i = 0; i < 100; i++)
  +            {
  +                addDoc(writer, "aaa");
  +                addDoc(writer, "bbb");
  +                addDoc(writer, "ccc");
  +            }
  +            writer.optimize();
  +            writer.close();
  +        }
  +        catch (IOException e)
  +        {
  +            e.printStackTrace();
  +        }
  +
  +        try
  +        {
  +            reader1 = IndexReader.open(dir);
  +            reader2 = IndexReader.open(dir);
  +
  +            // delete documents containing term: aaa
  +            reader2.delete(searchTerm1);
  +            reader2.close();
  +
  +            // delete documents containing term: bbb
  +            reader1.delete(searchTerm2);
  +            reader1.close();
  +        }
  +        catch (IOException e)
  +        {
  +            try
  +            {
  +                // if reader throws IOException try once more to delete documents with a new reader
  +                reader1.close();
  +                reader1 = IndexReader.open(dir);
  +                reader1.delete(searchTerm2);
  +                reader1.close();
  +            }
  +            catch (IOException e1)
  +            {
  +                e1.printStackTrace();
  +            }
  +        }
  +
  +        try
  +        {
  +            searcher = new IndexSearcher(dir);
  +            hits = searcher.search(new TermQuery(searchTerm1));
  +            assertEquals(0, hits.length());
  +            hits = searcher.search(new TermQuery(searchTerm2));
  +            assertEquals(0, hits.length());
  +            searcher.close();
  +        }
  +        catch (IOException e1)
  +        {
  +            e1.printStackTrace();
  +        }
  +    }
  +
  +
       private void addDocumentWithFields(IndexWriter writer) throws IOException
       {
           Document doc = new Document();
  @@ -141,5 +294,20 @@
           doc.add(Field.UnIndexed("unindexed2","test1"));
           doc.add(Field.UnStored("unstored2","test1"));
           writer.addDocument(doc);
  +    }
  +
  +    private void addDoc(IndexWriter writer, String value)
  +    {
  +        Document doc = new Document();
  +        doc.add(Field.UnStored("content", value));
  +
  +        try
  +        {
  +            writer.addDocument(doc);
  +        }
  +        catch (IOException e)
  +        {
  +            e.printStackTrace();
  +        }
       }
   }