1 package org.apache.lucene.index; 2 3 18 19 import junit.framework.TestCase; 20 import org.apache.lucene.store.Directory; 21 import org.apache.lucene.store.RAMDirectory; 22 import org.apache.lucene.document.Document; 23 24 import java.io.IOException ; 25 import java.util.Collection ; 26 27 public class TestSegmentMerger extends TestCase { 28 private Directory mergedDir = new RAMDirectory(); 30 private String mergedSegment = "test"; 31 private Directory merge1Dir = new RAMDirectory(); 33 private Document doc1 = new Document(); 34 private String merge1Segment = "test-1"; 35 private SegmentReader reader1 = null; 36 private Directory merge2Dir = new RAMDirectory(); 38 private Document doc2 = new Document(); 39 private String merge2Segment = "test-2"; 40 private SegmentReader reader2 = null; 41 42 43 public TestSegmentMerger(String s) { 44 super(s); 45 } 46 47 protected void setUp() { 48 DocHelper.setupDoc(doc1); 49 DocHelper.writeDoc(merge1Dir, merge1Segment, doc1); 50 DocHelper.setupDoc(doc2); 51 DocHelper.writeDoc(merge2Dir, merge2Segment, doc2); 52 try { 53 reader1 = new SegmentReader(new SegmentInfo(merge1Segment, 1, merge1Dir)); 54 reader2 = new SegmentReader(new SegmentInfo(merge2Segment, 1, merge2Dir)); 55 } catch (IOException e) { 56 e.printStackTrace(); 57 } 58 59 } 60 61 protected void tearDown() { 62 63 } 64 65 public void test() { 66 assertTrue(mergedDir != null); 67 assertTrue(merge1Dir != null); 68 assertTrue(merge2Dir != null); 69 assertTrue(reader1 != null); 70 assertTrue(reader2 != null); 71 } 72 73 public void testMerge() { 74 SegmentMerger merger = new SegmentMerger(mergedDir, mergedSegment, false); 76 merger.add(reader1); 77 merger.add(reader2); 78 try { 79 int docsMerged = merger.merge(); 80 merger.closeReaders(); 81 assertTrue(docsMerged == 2); 82 SegmentReader mergedReader = new SegmentReader(new SegmentInfo(mergedSegment, docsMerged, mergedDir)); 84 assertTrue(mergedReader != null); 85 assertTrue(mergedReader.numDocs() == 2); 86 Document newDoc1 = mergedReader.document(0); 87 assertTrue(newDoc1 != null); 88 assertTrue(DocHelper.numFields(newDoc1) == DocHelper.numFields(doc1) - 2); 90 Document newDoc2 = mergedReader.document(1); 91 assertTrue(newDoc2 != null); 92 assertTrue(DocHelper.numFields(newDoc2) == DocHelper.numFields(doc2) - 2); 93 94 TermDocs termDocs = mergedReader.termDocs(new Term(DocHelper.TEXT_FIELD_2_KEY, "field")); 95 assertTrue(termDocs != null); 96 assertTrue(termDocs.next() == true); 97 98 Collection stored = mergedReader.getIndexedFieldNames(true); 99 assertTrue(stored != null); 100 assertTrue(stored.size() == 2); 102 103 TermFreqVector vector = mergedReader.getTermFreqVector(0, DocHelper.TEXT_FIELD_2_KEY); 104 assertTrue(vector != null); 105 String [] terms = vector.getTerms(); 106 assertTrue(terms != null); 107 assertTrue(terms.length == 3); 109 int [] freqs = vector.getTermFrequencies(); 110 assertTrue(freqs != null); 111 113 for (int i = 0; i < terms.length; i++) { 114 String term = terms[i]; 115 int freq = freqs[i]; 116 assertTrue(DocHelper.FIELD_2_TEXT.indexOf(term) != -1); 118 assertTrue(DocHelper.FIELD_2_FREQS[i] == freq); 119 } 120 } catch (IOException e) { 121 e.printStackTrace(); 122 assertTrue(false); 123 } 124 } 126 } 127 | Popular Tags |