FileDocCategorySizeDatePackage
TestBinaryDocument.javaAPI DocApache Lucene 2.1.04053Wed Feb 14 10:46:36 GMT 2007org.apache.lucene.document

TestBinaryDocument

public class TestBinaryDocument extends TestCase
Tests {@link Document} class.
author
Bernhard Messer
version
$Id: TestBinaryDocument.java 472959 2006-11-09 16:21:50Z yonik $

Fields Summary
String
binaryValStored
String
binaryValCompressed
Constructors Summary
Methods Summary
public voidtestBinaryFieldInIndex()

  
    
     
  
    Fieldable binaryFldStored = new Field("binaryStored", binaryValStored.getBytes(), Field.Store.YES);
    Fieldable binaryFldCompressed = new Field("binaryCompressed", binaryValCompressed.getBytes(), Field.Store.COMPRESS);
    Fieldable stringFldStored = new Field("stringStored", binaryValStored, Field.Store.YES, Field.Index.NO, Field.TermVector.NO);
    Fieldable stringFldCompressed = new Field("stringCompressed", binaryValCompressed, Field.Store.COMPRESS, Field.Index.NO, Field.TermVector.NO);
    
    try {
      // binary fields with store off are not allowed
      new Field("fail", binaryValCompressed.getBytes(), Field.Store.NO);
      fail();
    }
    catch (IllegalArgumentException iae) {
      ;
    }
    
    Document doc = new Document();
    
    doc.add(binaryFldStored);
    doc.add(binaryFldCompressed);
    
    doc.add(stringFldStored);
    doc.add(stringFldCompressed);
    
    /** test for field count */
    assertEquals(4, doc.fields.size());
    
    /** add the doc to a ram index */
    RAMDirectory dir = new RAMDirectory();
    IndexWriter writer = new IndexWriter(dir, new StandardAnalyzer(), true);
    writer.addDocument(doc);
    writer.close();
    
    /** open a reader and fetch the document */ 
    IndexReader reader = IndexReader.open(dir);
    Document docFromReader = reader.document(0);
    assertTrue(docFromReader != null);
    
    /** fetch the binary stored field and compare it's content with the original one */
    String binaryFldStoredTest = new String(docFromReader.getBinaryValue("binaryStored"));
    assertTrue(binaryFldStoredTest.equals(binaryValStored));
    
    /** fetch the binary compressed field and compare it's content with the original one */
    String binaryFldCompressedTest = new String(docFromReader.getBinaryValue("binaryCompressed"));
    assertTrue(binaryFldCompressedTest.equals(binaryValCompressed));
    
    /** fetch the string field and compare it's content with the original one */
    String stringFldStoredTest = new String(docFromReader.get("stringStored"));
    assertTrue(stringFldStoredTest.equals(binaryValStored));
    
    /** fetch the compressed string field and compare it's content with the original one */
    String stringFldCompressedTest = new String(docFromReader.get("stringCompressed"));
    assertTrue(stringFldCompressedTest.equals(binaryValCompressed));
    
    /** delete the document from index */
    reader.deleteDocument(0);
    assertEquals(0, reader.numDocs());
    
    reader.close();