KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lucene > index > TestDocumentWriter


1 package org.apache.lucene.index;
2
3 /**
4  * Copyright 2004 The Apache Software Foundation
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

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 JavaDoc;
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 JavaDoc 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       //After adding the document, we should be able to read it back in
59
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       //System.out.println("Document: " + doc);
65
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 JavaDoc e) {
79       e.printStackTrace();
80       assertTrue(false);
81     }
82   }
83 }
84
Popular Tags