KickJava   Java API By Example, From Geeks To Geeks.

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


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.Directory;
21 import org.apache.lucene.store.RAMDirectory;
22 import org.apache.lucene.document.Document;
23
24 import java.io.IOException JavaDoc;
25 import java.util.Collection JavaDoc;
26
27 public class TestSegmentMerger extends TestCase {
28   //The variables for the new merged segment
29
private Directory mergedDir = new RAMDirectory();
30   private String JavaDoc mergedSegment = "test";
31   //First segment to be merged
32
private Directory merge1Dir = new RAMDirectory();
33   private Document doc1 = new Document();
34   private String JavaDoc merge1Segment = "test-1";
35   private SegmentReader reader1 = null;
36   //Second Segment to be merged
37
private Directory merge2Dir = new RAMDirectory();
38   private Document doc2 = new Document();
39   private String JavaDoc merge2Segment = "test-2";
40   private SegmentReader reader2 = null;
41   
42
43   public TestSegmentMerger(String JavaDoc 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 JavaDoc 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     //System.out.println("----------------TestMerge------------------");
75
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       //Should be able to open a new SegmentReader against the new directory
83
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       //There are 2 unstored fields on the document
89
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 JavaDoc stored = mergedReader.getIndexedFieldNames(true);
99       assertTrue(stored != null);
100       //System.out.println("stored size: " + stored.size());
101
assertTrue(stored.size() == 2);
102       
103       TermFreqVector vector = mergedReader.getTermFreqVector(0, DocHelper.TEXT_FIELD_2_KEY);
104       assertTrue(vector != null);
105       String JavaDoc [] terms = vector.getTerms();
106       assertTrue(terms != null);
107       //System.out.println("Terms size: " + terms.length);
108
assertTrue(terms.length == 3);
109       int [] freqs = vector.getTermFrequencies();
110       assertTrue(freqs != null);
111       //System.out.println("Freqs size: " + freqs.length);
112

113       for (int i = 0; i < terms.length; i++) {
114         String JavaDoc term = terms[i];
115         int freq = freqs[i];
116         //System.out.println("Term: " + term + " Freq: " + freq);
117
assertTrue(DocHelper.FIELD_2_TEXT.indexOf(term) != -1);
118         assertTrue(DocHelper.FIELD_2_FREQS[i] == freq);
119       }
120     } catch (IOException JavaDoc e) {
121       e.printStackTrace();
122       assertTrue(false);
123     }
124     //System.out.println("---------------------end TestMerge-------------------");
125
}
126 }
127
Popular Tags