FileDocCategorySizeDatePackage
TestTermVectorsWriter.javaAPI DocApache Lucene 1.4.36420Tue Mar 30 00:48:06 BST 2004org.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] = i * 100;
      }
    }
  
protected voidtearDown()

  
public voidtest()

    assertTrue(dir != null);
    assertTrue(positions != null);
  
public voidtestMultipleDocuments()


    try {
      TermVectorsWriter writer = new TermVectorsWriter(dir, seg, fieldInfos);
      assertTrue(writer != null);
      for (int i = 0; i < 10; i++) {
        writeDocument(writer, testFields.length);
      }
      writer.close();
    } catch (IOException e) {
      e.printStackTrace();
      assertTrue(false);
    }      
    //Do some arbitrary tests
    try {
      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]);
      }
    } catch (IOException e) {
      e.printStackTrace();
      assertTrue(false);
    }
  
public voidtestMultipleFields()
Test one document, multiple fields

    try {
      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]);
      }
    } catch (IOException e) {
      e.printStackTrace();
      assertTrue(false);
    }
  
public voidtestWriter()

    try {
      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]);
    } catch (IOException e) {
      e.printStackTrace();
      assertTrue(false);
    }
  
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
j The field number
throws
IOException

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