FileDocCategorySizeDatePackage
TestSegmentMerger.javaAPI DocApache Lucene 1.4.34677Tue Apr 20 21:33:36 BST 2004org.apache.lucene.index

TestSegmentMerger

public class TestSegmentMerger 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 Directory
mergedDir
private String
mergedSegment
private Directory
merge1Dir
private Document
doc1
private String
merge1Segment
private SegmentReader
reader1
private Directory
merge2Dir
private Document
doc2
private String
merge2Segment
private SegmentReader
reader2
Constructors Summary
public TestSegmentMerger(String s)

  

     
    super(s);
  
Methods Summary
protected voidsetUp()

    DocHelper.setupDoc(doc1);
    DocHelper.writeDoc(merge1Dir, merge1Segment, doc1);
    DocHelper.setupDoc(doc2);
    DocHelper.writeDoc(merge2Dir, merge2Segment, doc2);
    try {
      reader1 = new SegmentReader(new SegmentInfo(merge1Segment, 1, merge1Dir));
      reader2 = new SegmentReader(new SegmentInfo(merge2Segment, 1, merge2Dir));
    } catch (IOException e) {
      e.printStackTrace();                                                      
    }

  
protected voidtearDown()


  
public voidtest()

    assertTrue(mergedDir != null);
    assertTrue(merge1Dir != null);
    assertTrue(merge2Dir != null);
    assertTrue(reader1 != null);
    assertTrue(reader2 != null);
  
public voidtestMerge()

                             
    //System.out.println("----------------TestMerge------------------");
    SegmentMerger merger = new SegmentMerger(mergedDir, mergedSegment, false);
    merger.add(reader1);
    merger.add(reader2);
    try {
      int docsMerged = merger.merge();
      merger.closeReaders();
      assertTrue(docsMerged == 2);
      //Should be able to open a new SegmentReader against the new directory
      SegmentReader mergedReader = new SegmentReader(new SegmentInfo(mergedSegment, docsMerged, mergedDir));
      assertTrue(mergedReader != null);
      assertTrue(mergedReader.numDocs() == 2);
      Document newDoc1 = mergedReader.document(0);
      assertTrue(newDoc1 != null);
      //There are 2 unstored fields on the document
      assertTrue(DocHelper.numFields(newDoc1) == DocHelper.numFields(doc1) - 2);
      Document newDoc2 = mergedReader.document(1);
      assertTrue(newDoc2 != null);
      assertTrue(DocHelper.numFields(newDoc2) == DocHelper.numFields(doc2) - 2);
      
      TermDocs termDocs = mergedReader.termDocs(new Term(DocHelper.TEXT_FIELD_2_KEY, "field"));
      assertTrue(termDocs != null);
      assertTrue(termDocs.next() == true);
      
      Collection stored = mergedReader.getIndexedFieldNames(true);
      assertTrue(stored != null);
      //System.out.println("stored size: " + stored.size());
      assertTrue(stored.size() == 2);
      
      TermFreqVector vector = mergedReader.getTermFreqVector(0, DocHelper.TEXT_FIELD_2_KEY);
      assertTrue(vector != null);
      String [] terms = vector.getTerms();
      assertTrue(terms != null);
      //System.out.println("Terms size: " + terms.length);
      assertTrue(terms.length == 3);
      int [] freqs = vector.getTermFrequencies();
      assertTrue(freqs != null);
      //System.out.println("Freqs size: " + freqs.length);
      
      for (int i = 0; i < terms.length; i++) {
        String term = terms[i];
        int freq = freqs[i];
        //System.out.println("Term: " + term + " Freq: " + freq);
        assertTrue(DocHelper.FIELD_2_TEXT.indexOf(term) != -1);
        assertTrue(DocHelper.FIELD_2_FREQS[i] == freq);
      }                                                
    } catch (IOException e) {
      e.printStackTrace();
      assertTrue(false);
    }
    //System.out.println("---------------------end TestMerge-------------------");