FileDocCategorySizeDatePackage
TestTermVectorsWriter.javaAPI DocApache Lucene 1.96537Mon Feb 20 09:19:36 GMT 2006org.apache.lucene.index

TestTermVectorsWriter

public class TestTermVectorsWriter extends TestCase
Copyright 2004 The Apache Software Foundation Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Fields Summary
private String[]
testTerms
private String[]
testFields
private int[]
positions
private RAMDirectory
dir
private String
seg
private FieldInfos
fieldInfos
Constructors Summary
public TestTermVectorsWriter(String s)


     
    super(s);
  
Methods Summary
private voidcheckTermVector(org.apache.lucene.index.TermVectorsReader reader, int docNum, java.lang.String field)

    TermFreqVector vector = reader.get(docNum, field);
    assertTrue(vector != null);
    String[] terms = vector.getTerms();
    assertTrue(terms != null);
    assertTrue(terms.length == testTerms.length);
    for (int i = 0; i < terms.length; i++) {
      String term = terms[i];
      assertTrue(term.equals(testTerms[i]));
    }
  
protected voidsetUp()


    for (int i = 0; i < testFields.length; i++) {
      fieldInfos.add(testFields[i], true, true);
    }
    

    for (int i = 0; i < testTerms.length; i++) {
      positions[i] = new int[5];
      for (int j = 0; j < positions[i].length; j++) {
        positions[i][j] = j * 10;
      }
    }
  
protected voidtearDown()

  
public voidtest()

    assertTrue(dir != null);
    assertTrue(positions != null);
  
public voidtestBadSegment()
Test that no NullPointerException will be raised, when adding one document with a single, empty field and term vectors enabled.

throws
IOException

    dir = new RAMDirectory();
    IndexWriter ir = new IndexWriter(dir, new StandardAnalyzer(), true);
    
    Document document = new Document();
    document.add(new Field("tvtest", "", Field.Store.NO, Field.Index.TOKENIZED,
        Field.TermVector.YES));
    ir.addDocument(document);
    ir.close();
    dir.close();
  
public voidtestMultipleDocuments()

    TermVectorsWriter writer = new TermVectorsWriter(dir, seg, fieldInfos);
    assertTrue(writer != null);
    for (int i = 0; i < 10; i++) {
      writeDocument(writer, testFields.length);
    }
    writer.close();
    //Do some arbitrary tests
    TermVectorsReader reader = new TermVectorsReader(dir, seg, fieldInfos);
    for (int i = 0; i < 10; i++) {        
      assertTrue(reader != null);
      checkTermVector(reader, 5, testFields[0]);
      checkTermVector(reader, 2, testFields[2]);
    }
  
public voidtestMultipleFields()
Test one document, multiple fields

throws
IOException

    TermVectorsWriter writer = new TermVectorsWriter(dir, seg, fieldInfos);
    writeDocument(writer, testFields.length);

    writer.close();

    assertTrue(writer.isDocumentOpen() == false);
    //Check to see the files were created
    assertTrue(dir.fileExists(seg + TermVectorsWriter.TVD_EXTENSION));
    assertTrue(dir.fileExists(seg + TermVectorsWriter.TVX_EXTENSION));
    //Now read it back in
    TermVectorsReader reader = new TermVectorsReader(dir, seg, fieldInfos);
    assertTrue(reader != null);

    for (int j = 0; j < testFields.length; j++) {
      checkTermVector(reader, 0, testFields[j]);
    }
  
public voidtestWriter()

    TermVectorsWriter writer = new TermVectorsWriter(dir, seg, fieldInfos);
    writer.openDocument();
    assertTrue(writer.isDocumentOpen() == true);
    writeField(writer, testFields[0]);
    writer.closeDocument();
    writer.close();
    assertTrue(writer.isDocumentOpen() == false);
    //Check to see the files were created
    assertTrue(dir.fileExists(seg + TermVectorsWriter.TVD_EXTENSION));
    assertTrue(dir.fileExists(seg + TermVectorsWriter.TVX_EXTENSION));
    //Now read it back in
    TermVectorsReader reader = new TermVectorsReader(dir, seg, fieldInfos);
    assertTrue(reader != null);
    checkTermVector(reader, 0, testFields[0]);
  
private voidwriteDocument(org.apache.lucene.index.TermVectorsWriter writer, int numFields)

    writer.openDocument();
    assertTrue(writer.isDocumentOpen() == true);

    for (int j = 0; j < numFields; j++) {
      writeField(writer, testFields[j]);
    }
    writer.closeDocument();
    assertTrue(writer.isDocumentOpen() == false);
  
private voidwriteField(org.apache.lucene.index.TermVectorsWriter writer, java.lang.String f)

param
writer The writer to write to
param
f The field name
throws
IOException

    writer.openField(f);
    assertTrue(writer.isFieldOpen() == true);
    for (int i = 0; i < testTerms.length; i++) {
      writer.addTerm(testTerms[i], i);
    }
    writer.closeField();