001: package org.apache.lucene.index;
002:
003: /**
004: * Licensed to the Apache Software Foundation (ASF) under one or more
005: * contributor license agreements. See the NOTICE file distributed with
006: * this work for additional information regarding copyright ownership.
007: * The ASF licenses this file to You under the Apache License, Version 2.0
008: * (the "License"); you may not use this file except in compliance with
009: * the License. You may obtain a copy of the License at
010: *
011: * http://www.apache.org/licenses/LICENSE-2.0
012: *
013: * Unless required by applicable law or agreed to in writing, software
014: * distributed under the License is distributed on an "AS IS" BASIS,
015: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016: * See the License for the specific language governing permissions and
017: * limitations under the License.
018: */
019:
020: import org.apache.lucene.util.LuceneTestCase;
021: import org.apache.lucene.store.Directory;
022: import org.apache.lucene.store.RAMDirectory;
023: import org.apache.lucene.document.Document;
024:
025: import java.io.IOException;
026: import java.util.Collection;
027:
028: public class TestSegmentMerger extends LuceneTestCase {
029: //The variables for the new merged segment
030: private Directory mergedDir = new RAMDirectory();
031: private String mergedSegment = "test";
032: //First segment to be merged
033: private Directory merge1Dir = new RAMDirectory();
034: private Document doc1 = new Document();
035: private SegmentReader reader1 = null;
036: //Second Segment to be merged
037: private Directory merge2Dir = new RAMDirectory();
038: private Document doc2 = new Document();
039: private SegmentReader reader2 = null;
040:
041: public TestSegmentMerger(String s) {
042: super (s);
043: }
044:
045: protected void setUp() throws Exception {
046: super .setUp();
047: DocHelper.setupDoc(doc1);
048: SegmentInfo info1 = DocHelper.writeDoc(merge1Dir, doc1);
049: DocHelper.setupDoc(doc2);
050: SegmentInfo info2 = DocHelper.writeDoc(merge2Dir, doc2);
051: reader1 = SegmentReader.get(info1);
052: reader2 = SegmentReader.get(info2);
053: }
054:
055: public void test() {
056: assertTrue(mergedDir != null);
057: assertTrue(merge1Dir != null);
058: assertTrue(merge2Dir != null);
059: assertTrue(reader1 != null);
060: assertTrue(reader2 != null);
061: }
062:
063: public void testMerge() throws IOException {
064: SegmentMerger merger = new SegmentMerger(mergedDir,
065: mergedSegment);
066: merger.add(reader1);
067: merger.add(reader2);
068: int docsMerged = merger.merge();
069: merger.closeReaders();
070: assertTrue(docsMerged == 2);
071: //Should be able to open a new SegmentReader against the new directory
072: SegmentReader mergedReader = SegmentReader.get(new SegmentInfo(
073: mergedSegment, docsMerged, mergedDir, false, true));
074: assertTrue(mergedReader != null);
075: assertTrue(mergedReader.numDocs() == 2);
076: Document newDoc1 = mergedReader.document(0);
077: assertTrue(newDoc1 != null);
078: //There are 2 unstored fields on the document
079: assertTrue(DocHelper.numFields(newDoc1) == DocHelper
080: .numFields(doc1)
081: - DocHelper.unstored.size());
082: Document newDoc2 = mergedReader.document(1);
083: assertTrue(newDoc2 != null);
084: assertTrue(DocHelper.numFields(newDoc2) == DocHelper
085: .numFields(doc2)
086: - DocHelper.unstored.size());
087:
088: TermDocs termDocs = mergedReader.termDocs(new Term(
089: DocHelper.TEXT_FIELD_2_KEY, "field"));
090: assertTrue(termDocs != null);
091: assertTrue(termDocs.next() == true);
092:
093: Collection stored = mergedReader
094: .getFieldNames(IndexReader.FieldOption.INDEXED_WITH_TERMVECTOR);
095: assertTrue(stored != null);
096: //System.out.println("stored size: " + stored.size());
097: assertTrue(
098: "We do not have 4 fields that were indexed with term vector",
099: stored.size() == 4);
100:
101: TermFreqVector vector = mergedReader.getTermFreqVector(0,
102: DocHelper.TEXT_FIELD_2_KEY);
103: assertTrue(vector != null);
104: String[] terms = vector.getTerms();
105: assertTrue(terms != null);
106: //System.out.println("Terms size: " + terms.length);
107: assertTrue(terms.length == 3);
108: int[] freqs = vector.getTermFrequencies();
109: assertTrue(freqs != null);
110: //System.out.println("Freqs size: " + freqs.length);
111: assertTrue(vector instanceof TermPositionVector == true);
112:
113: for (int i = 0; i < terms.length; i++) {
114: String 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:
121: TestSegmentReader.checkNorms(mergedReader);
122: }
123: }
|