1 package org.apache.lucene.index; 2 3 18 19 import junit.framework.TestCase; 20 import org.apache.lucene.document.Document; 21 import org.apache.lucene.store.Directory; 22 import org.apache.lucene.store.RAMDirectory; 23 24 import java.io.IOException ; 25 26 public class TestMultiReader extends TestCase { 27 private Directory dir = new RAMDirectory(); 28 private Document doc1 = new Document(); 29 private Document doc2 = new Document(); 30 private SegmentReader reader1; 31 private SegmentReader reader2; 32 private SegmentReader [] readers = new SegmentReader[2]; 33 private SegmentInfos sis = new SegmentInfos(); 34 35 public TestMultiReader(String s) { 36 super(s); 37 } 38 39 protected void setUp() { 40 DocHelper.setupDoc(doc1); 41 DocHelper.setupDoc(doc2); 42 DocHelper.writeDoc(dir, "seg-1", doc1); 43 DocHelper.writeDoc(dir, "seg-2", doc2); 44 45 try { 46 sis.write(dir); 47 reader1 = new SegmentReader(new SegmentInfo("seg-1", 1, dir)); 48 reader2 = new SegmentReader(new SegmentInfo("seg-2", 1, dir)); 49 readers[0] = reader1; 50 readers[1] = reader2; 51 } catch (IOException e) { 52 e.printStackTrace(); 53 } 54 } 55 59 protected void tearDown() { 60 61 } 62 63 public void test() { 64 assertTrue(dir != null); 65 assertTrue(reader1 != null); 66 assertTrue(reader2 != null); 67 assertTrue(sis != null); 68 } 69 70 public void testDocument() { 71 try { 72 sis.read(dir); 73 MultiReader reader = new MultiReader(dir, sis, false, readers); 74 assertTrue(reader != null); 75 Document newDoc1 = reader.document(0); 76 assertTrue(newDoc1 != null); 77 assertTrue(DocHelper.numFields(newDoc1) == DocHelper.numFields(doc1) - 2); 78 Document newDoc2 = reader.document(1); 79 assertTrue(newDoc2 != null); 80 assertTrue(DocHelper.numFields(newDoc2) == DocHelper.numFields(doc2) - 2); 81 TermFreqVector vector = reader.getTermFreqVector(0, DocHelper.TEXT_FIELD_2_KEY); 82 assertTrue(vector != null); 83 } catch (IOException e) { 84 e.printStackTrace(); 85 assertTrue(false); 86 } 87 } 88 89 public void testTermVectors() { 90 try { 91 MultiReader reader = new MultiReader(dir, sis, false, readers); 92 assertTrue(reader != null); 93 } catch (IOException e) { 94 e.printStackTrace(); 95 assertTrue(false); 96 } 97 } 98 } 99 | Popular Tags |