KickJava   Java API By Example, From Geeks To Geeks.

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


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.document.Document;
21 import org.apache.lucene.store.Directory;
22 import org.apache.lucene.store.RAMDirectory;
23
24 import java.io.IOException JavaDoc;
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 JavaDoc 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 JavaDoc e) {
52       e.printStackTrace();
53     }
54   }
55 /*IndexWriter writer = new IndexWriter(dir, new WhitespaceAnalyzer(), true);
56       writer.addDocument(doc1);
57       writer.addDocument(doc2);
58       writer.close();*/

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 JavaDoc 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 JavaDoc e) {
94       e.printStackTrace();
95       assertTrue(false);
96     }
97   }
98 }
99
Popular Tags