FileDocCategorySizeDatePackage
TestDocument.javaAPI DocApache Lucene 1.98191Mon Feb 20 09:19:40 GMT 2006org.apache.lucene.document

TestDocument

public class TestDocument extends TestCase
Tests {@link Document} class.
author
Otis Gospodnetic
version
$Id: TestDocument.java 208846 2005-07-02 16:40:44Z dnaber $

Fields Summary
String
binaryVal
String
binaryVal2
Constructors Summary
Methods Summary
private voiddoAssert(org.apache.lucene.document.Document doc, boolean fromIndex)

        String[] keywordFieldValues   = doc.getValues("keyword");
        String[] textFieldValues      = doc.getValues("text");
        String[] unindexedFieldValues = doc.getValues("unindexed");
        String[] unstoredFieldValues  = doc.getValues("unstored");

        assertTrue(keywordFieldValues.length   == 2);
        assertTrue(textFieldValues.length      == 2);
        assertTrue(unindexedFieldValues.length == 2);
        // this test cannot work for documents retrieved from the index
        // since unstored fields will obviously not be returned
        if (! fromIndex)
        {
            assertTrue(unstoredFieldValues.length  == 2);
        }

        assertTrue(keywordFieldValues[0].equals("test1"));
        assertTrue(keywordFieldValues[1].equals("test2"));
        assertTrue(textFieldValues[0].equals("test1"));
        assertTrue(textFieldValues[1].equals("test2"));
        assertTrue(unindexedFieldValues[0].equals("test1"));
        assertTrue(unindexedFieldValues[1].equals("test2"));
        // this test cannot work for documents retrieved from the index
        // since unstored fields will obviously not be returned
        if (! fromIndex)
        {
            assertTrue(unstoredFieldValues[0].equals("test1"));
            assertTrue(unstoredFieldValues[1].equals("test2"));
        }
    
private org.apache.lucene.document.DocumentmakeDocumentWithFields()

        Document doc = new Document();
        doc.add(new Field(  "keyword",   "test1", Field.Store.YES, Field.Index.UN_TOKENIZED));
        doc.add(new Field(  "keyword",   "test2", Field.Store.YES, Field.Index.UN_TOKENIZED));
        doc.add(new Field(     "text",      "test1", Field.Store.YES, Field.Index.TOKENIZED));
        doc.add(new Field(     "text",      "test2", Field.Store.YES, Field.Index.TOKENIZED));
        doc.add(new Field("unindexed", "test1", Field.Store.YES, Field.Index.NO));
        doc.add(new Field("unindexed", "test2", Field.Store.YES, Field.Index.NO));
        doc.add(new Field( "unstored",  "test1", Field.Store.NO, Field.Index.TOKENIZED));
        doc.add(new Field( "unstored",  "test2", Field.Store.NO, Field.Index.TOKENIZED));
        return doc;
    
public voidtestBinaryField()

  
    
     
  
    Document doc = new Document();
    Field stringFld = new Field("string", binaryVal, Field.Store.YES, Field.Index.NO);
    Field binaryFld = new Field("binary", binaryVal.getBytes(), Field.Store.YES);
    Field binaryFld2 = new Field("binary", binaryVal2.getBytes(), Field.Store.YES);
    
    doc.add(stringFld);
    doc.add(binaryFld);
    
    assertEquals(2, doc.fields.size());
    
    assertTrue(binaryFld.isBinary());
    assertTrue(binaryFld.isStored());
    assertFalse(binaryFld.isIndexed());
    assertFalse(binaryFld.isTokenized());
    
    String binaryTest = new String(doc.getBinaryValue("binary"));
    assertTrue(binaryTest.equals(binaryVal));
    
    String stringTest = doc.get("string");
    assertTrue(binaryTest.equals(stringTest));
    
    doc.add(binaryFld2);
    
    assertEquals(3, doc.fields.size());
    
    byte[][] binaryTests = doc.getBinaryValues("binary");
        
    assertEquals(2, binaryTests.length);
    
    binaryTest = new String(binaryTests[0]);
    String binaryTest2 = new String(binaryTests[1]);
    
    assertFalse(binaryTest.equals(binaryTest2));
    
    assertTrue(binaryTest.equals(binaryVal));
    assertTrue(binaryTest2.equals(binaryVal2));
    
    doc.removeField("string");
    assertEquals(2, doc.fields.size());
    
    doc.removeFields("binary");
    assertEquals(0, doc.fields.size());
  
public voidtestConstructorExceptions()

    new Field("name", "value", Field.Store.YES, Field.Index.NO);  // okay
    new Field("name", "value", Field.Store.NO, Field.Index.UN_TOKENIZED);  // okay
    try {
      new Field("name", "value", Field.Store.NO, Field.Index.NO);
      fail();
    } catch(IllegalArgumentException e) {
      // expected exception
    }
    new Field("name", "value", Field.Store.YES, Field.Index.NO, Field.TermVector.NO); // okay
    try {
      new Field("name", "value", Field.Store.YES, Field.Index.NO, Field.TermVector.YES);
      fail();
    } catch(IllegalArgumentException e) {
      // expected exception
    }
  
public voidtestGetValuesForIndexedDocument()
Tests {@link Document#getValues(String)} method for a Document retrieved from an index.

throws
Exception on error

        RAMDirectory dir = new RAMDirectory();
        IndexWriter writer = new IndexWriter(dir, new StandardAnalyzer(), true);
        writer.addDocument(makeDocumentWithFields());
        writer.close();

        Searcher searcher = new IndexSearcher(dir);

	// search for something that does exists
	Query query = new TermQuery(new Term("keyword", "test1"));

	// ensure that queries return expected results without DateFilter first
        Hits hits = searcher.search(query);
	assertEquals(1, hits.length());

         doAssert(hits.doc(0), true);
         searcher.close();
    
public voidtestGetValuesForNewDocument()
Tests {@link Document#getValues(String)} method for a brand new Document that has not been indexed yet.

throws
Exception on error

        doAssert(makeDocumentWithFields(), false);
    
public voidtestRemoveForNewDocument()
Tests {@link Document#removeField(String)} method for a brand new Document that has not been indexed yet.

throws
Exception on error

    Document doc = makeDocumentWithFields();
    assertEquals(8, doc.fields.size());
    doc.removeFields("keyword");
    assertEquals(6, doc.fields.size());
    doc.removeFields("doesnotexists");      // removing non-existing fields is siltenlty ignored
    doc.removeFields("keyword");		// removing a field more than once
    assertEquals(6, doc.fields.size());
    doc.removeField("text");
    assertEquals(5, doc.fields.size());
    doc.removeField("text");
    assertEquals(4, doc.fields.size());
    doc.removeField("text");
    assertEquals(4, doc.fields.size());
    doc.removeField("doesnotexists");       // removing non-existing fields is siltenlty ignored
    assertEquals(4, doc.fields.size());
    doc.removeFields("unindexed");
    assertEquals(2, doc.fields.size());
    doc.removeFields("unstored");
    assertEquals(0, doc.fields.size());
    doc.removeFields("doesnotexists");	// removing non-existing fields is siltenlty ignored
    assertEquals(0, doc.fields.size());