FileDocCategorySizeDatePackage
TestRAMDirectory.javaAPI DocApache Lucene 1.94743Mon Feb 20 09:19:34 GMT 2006org.apache.lucene.index.store

TestRAMDirectory

public class TestRAMDirectory extends TestCase
JUnit testcase to test RAMDirectory. RAMDirectory itself is used in many testcases, but not one of them uses an different constructor other than the default constructor.
author
Bernhard Messer
version
$Id: RAMDirectory.java 150537 2004-09-28 22:45:26 +0200 (Di, 28 Sep 2004) cutting $

Fields Summary
private File
indexDir
private final int
docsToAdd
Constructors Summary
Methods Summary
private voidrmDir(java.io.File dir)

    File[] files = dir.listFiles();
    for (int i = 0; i < files.length; i++) {
      files[i].delete();
    }
    dir.delete();
  
public voidsetUp()

  
  // setup the index
        
    String tempDir = System.getProperty("java.io.tmpdir");
    if (tempDir == null)
      throw new IOException("java.io.tmpdir undefined, cannot run test");
    indexDir = new File(tempDir, "RAMDirIndex");
    
    IndexWriter writer  = new IndexWriter(indexDir, new WhitespaceAnalyzer(), true);
    // add some documents
    Document doc = null;
    for (int i = 0; i < docsToAdd; i++) {
      doc = new Document();
      doc.add(new Field("content", English.intToEnglish(i).trim(), Field.Store.YES, Field.Index.UN_TOKENIZED));
      writer.addDocument(doc);
    }
    assertEquals(docsToAdd, writer.docCount());
    writer.optimize();
    writer.close();
  
public voidtearDown()

    // cleanup 
    if (indexDir != null && indexDir.exists()) {
      rmDir (indexDir);
    }
  
public voidtestRAMDirectory()

    
    Directory dir = FSDirectory.getDirectory(indexDir, false);
    RAMDirectory ramDir = new RAMDirectory(dir);
    
    // close the underlaying directory and delete the index
    dir.close();
    
    // open reader to test document count
    IndexReader reader = IndexReader.open(ramDir);
    assertEquals(docsToAdd, reader.numDocs());
    
    // open search zo check if all doc's are there
    IndexSearcher searcher = new IndexSearcher(reader);
    
    // search for all documents
    for (int i = 0; i < docsToAdd; i++) {
      Document doc = searcher.doc(i);
      assertTrue(doc.getField("content") != null);
    }

    // cleanup
    reader.close();
    searcher.close();
  
public voidtestRAMDirectoryFile()

    
    RAMDirectory ramDir = new RAMDirectory(indexDir);
    
    // open reader to test document count
    IndexReader reader = IndexReader.open(ramDir);
    assertEquals(docsToAdd, reader.numDocs());
    
    // open search zo check if all doc's are there
    IndexSearcher searcher = new IndexSearcher(reader);
    
    // search for all documents
    for (int i = 0; i < docsToAdd; i++) {
      Document doc = searcher.doc(i);
      assertTrue(doc.getField("content") != null);
    }

    // cleanup
    reader.close();
    searcher.close();
  
public voidtestRAMDirectoryString()

    
    RAMDirectory ramDir = new RAMDirectory(indexDir.getCanonicalPath());
    
    // open reader to test document count
    IndexReader reader = IndexReader.open(ramDir);
    assertEquals(docsToAdd, reader.numDocs());
    
    // open search zo check if all doc's are there
    IndexSearcher searcher = new IndexSearcher(reader);
    
    // search for all documents
    for (int i = 0; i < docsToAdd; i++) {
      Document doc = searcher.doc(i);
      assertTrue(doc.getField("content") != null);
    }

    // cleanup
    reader.close();
    searcher.close();