lucene-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From o...@apache.org
Subject cvs commit: jakarta-lucene/src/test/org/apache/lucene/index TestIndexWriter.java
Date Wed, 10 Sep 2003 12:58:37 GMT
otis        2003/09/10 05:58:37

  Added:       src/test/org/apache/lucene/index TestIndexWriter.java
  Log:
  - Initial checkin.
    Converted to JUnit-based unit test from Christoph Goller's test class.
  
  Revision  Changes    Path
  1.1                  jakarta-lucene/src/test/org/apache/lucene/index/TestIndexWriter.java
  
  Index: TestIndexWriter.java
  ===================================================================
  package org.apache.lucene.index;
  
  import java.io.IOException;
  
  import junit.framework.TestCase;
  
  import org.apache.lucene.analysis.WhitespaceAnalyzer;
  import org.apache.lucene.document.Document;
  import org.apache.lucene.document.Field;
  import org.apache.lucene.index.IndexReader;
  import org.apache.lucene.index.IndexWriter;
  import org.apache.lucene.store.Directory;
  import org.apache.lucene.store.RAMDirectory;
  
  
  /**
   *
   * @author goller
   */
  public class TestIndexWriter extends TestCase
  {
      private int docCount = 0;
  
      public void testDocCount()
      {
          Directory dir = new RAMDirectory();
  
          IndexWriter writer = null;
          IndexReader reader = null;
          int i;
  
          try {
            writer  = new IndexWriter(dir, new WhitespaceAnalyzer(), true);
  
            // add 100 documents
            for (i = 0; i < 100; i++) {
                addDoc(writer);
            }
            assertEquals(100, writer.docCount());
            writer.close();
  
            // delete 50 documents
            reader = IndexReader.open(dir);
            for (i = 0; i < 50; i++) {
                reader.delete(i);
            }
            reader.close();
  
            writer  = new IndexWriter(dir, new WhitespaceAnalyzer(), false);
            assertEquals(50, writer.docCount());
            writer.optimize();
            assertEquals(50, writer.docCount());
            writer.close();
          }
          catch (IOException e) {
            e.printStackTrace();
          }
      }
  
      private void addDoc(IndexWriter writer)
      {
          Document doc = new Document();
  
          doc.add(Field.Keyword("id","id" + docCount));
          doc.add(Field.UnStored("content","aaa"));
  
          try {
            writer.addDocument(doc);
          }
          catch (IOException e) {
            e.printStackTrace();
          }
          docCount++;
      }
  }
  
  
  

Mime
View raw message