1 package org.apache.lucene.index; 2 3 18 19 import junit.framework.TestCase; 20 import org.apache.lucene.store.RAMDirectory; 21 import org.apache.lucene.analysis.Analyzer; 22 import org.apache.lucene.analysis.WhitespaceAnalyzer; 23 import org.apache.lucene.search.Similarity; 24 import org.apache.lucene.document.Document; 25 import org.apache.lucene.document.Field; 26 27 import java.io.IOException ; 28 29 public class TestDocumentWriter extends TestCase { 30 private RAMDirectory dir = new RAMDirectory(); 31 private Document testDoc = new Document(); 32 33 34 public TestDocumentWriter(String s) { 35 super(s); 36 } 37 38 protected void setUp() { 39 DocHelper.setupDoc(testDoc); 40 } 41 42 protected void tearDown() { 43 44 } 45 46 public void test() { 47 assertTrue(dir != null); 48 49 } 50 51 public void testAddDocument() { 52 Analyzer analyzer = new WhitespaceAnalyzer(); 53 Similarity similarity = Similarity.getDefault(); 54 DocumentWriter writer = new DocumentWriter(dir, analyzer, similarity, 50); 55 assertTrue(writer != null); 56 try { 57 writer.addDocument("test", testDoc); 58 SegmentReader reader = new SegmentReader(new SegmentInfo("test", 1, dir)); 60 assertTrue(reader != null); 61 Document doc = reader.document(0); 62 assertTrue(doc != null); 63 64 Field [] fields = doc.getFields("textField2"); 66 assertTrue(fields != null && fields.length == 1); 67 assertTrue(fields[0].stringValue().equals(DocHelper.FIELD_2_TEXT)); 68 assertTrue(fields[0].isTermVectorStored() == true); 69 70 fields = doc.getFields("textField1"); 71 assertTrue(fields != null && fields.length == 1); 72 assertTrue(fields[0].stringValue().equals(DocHelper.FIELD_1_TEXT)); 73 assertTrue(fields[0].isTermVectorStored() == false); 74 75 fields = doc.getFields("keyField"); 76 assertTrue(fields != null && fields.length == 1); 77 assertTrue(fields[0].stringValue().equals(DocHelper.KEYWORD_TEXT)); 78 } catch (IOException e) { 79 e.printStackTrace(); 80 assertTrue(false); 81 } 82 } 83 } 84 | Popular Tags |