1 package org.apache.lucene.index;
4 * Licensed to the Apache Software Foundation (ASF) under one or more
5 * contributor license agreements. See the NOTICE file distributed with
6 * this work for additional information regarding copyright ownership.
7 * The ASF licenses this file to You under the Apache License, Version 2.0
8 * (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
20 import java.io.FileReader;
21 import java.io.FileWriter;
22 import java.io.IOException;
23 import java.io.PrintWriter;
24 import java.io.StringWriter;
26 import java.util.LinkedList;
27 import java.util.Collection;
29 import junit.framework.TestSuite;
30 import junit.textui.TestRunner;
32 import org.apache.lucene.analysis.MockAnalyzer;
33 import org.apache.lucene.document.Document;
34 import org.apache.lucene.document.Field;
35 import org.apache.lucene.index.IndexWriterConfig.OpenMode;
36 import org.apache.lucene.store.Directory;
37 import org.apache.lucene.util.LuceneTestCase;
38 import org.apache.lucene.util._TestUtil;
40 /** JUnit adaptation of an older test case DocTest. */
41 public class TestDoc extends LuceneTestCase {
43 /** Main for running test case by itself. */
44 public static void main(String args[]) {
45 TestRunner.run (new TestSuite(TestDoc.class));
49 private File indexDir;
50 private LinkedList<File> files;
52 /** Set the test case. This test case needs
53 * a few text files created in the current working directory.
56 public void setUp() throws Exception {
58 workDir = _TestUtil.getTempDir("TestDoc");
61 indexDir = _TestUtil.getTempDir("testIndex");
64 Directory directory = newFSDirectory(indexDir);
67 files = new LinkedList<File>();
68 files.add(createOutput("test.txt",
69 "This is the first test file"
72 files.add(createOutput("test2.txt",
73 "This is the second test file"
77 private File createOutput(String name, String text) throws IOException {
79 PrintWriter pw = null;
82 File f = new File(workDir, name);
83 if (f.exists()) f.delete();
85 fw = new FileWriter(f);
86 pw = new PrintWriter(fw);
91 if (pw != null) pw.close();
92 if (fw != null) fw.close();
97 /** This test executes a number of merges and compares the contents of
98 * the segments created when using compound file or not using one.
100 * TODO: the original test used to print the segment contents to System.out
101 * for visual validation. To have the same effect, a new method
102 * checkSegment(String name, ...) should be created that would
103 * assert various things about the segment.
105 public void testIndexAndMerge() throws Exception {
106 StringWriter sw = new StringWriter();
107 PrintWriter out = new PrintWriter(sw, true);
109 Directory directory = newFSDirectory(indexDir);
110 IndexWriter writer = new IndexWriter(
112 newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random)).
113 setOpenMode(OpenMode.CREATE).
114 setMaxBufferedDocs(-1).
115 setMergePolicy(newLogMergePolicy(10))
118 SegmentInfo si1 = indexDoc(writer, "test.txt");
119 printSegment(out, si1);
121 SegmentInfo si2 = indexDoc(writer, "test2.txt");
122 printSegment(out, si2);
125 SegmentInfo siMerge = merge(si1, si2, "merge", false);
126 printSegment(out, siMerge);
128 SegmentInfo siMerge2 = merge(si1, si2, "merge2", false);
129 printSegment(out, siMerge2);
131 SegmentInfo siMerge3 = merge(siMerge, siMerge2, "merge3", false);
132 printSegment(out, siMerge3);
137 String multiFileOutput = sw.getBuffer().toString();
138 //System.out.println(multiFileOutput);
140 sw = new StringWriter();
141 out = new PrintWriter(sw, true);
143 directory = newFSDirectory(indexDir);
144 writer = new IndexWriter(
146 newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random)).
147 setOpenMode(OpenMode.CREATE).
148 setMaxBufferedDocs(-1).
149 setMergePolicy(newLogMergePolicy(10))
152 si1 = indexDoc(writer, "test.txt");
153 printSegment(out, si1);
155 si2 = indexDoc(writer, "test2.txt");
156 printSegment(out, si2);
159 siMerge = merge(si1, si2, "merge", true);
160 printSegment(out, siMerge);
162 siMerge2 = merge(si1, si2, "merge2", true);
163 printSegment(out, siMerge2);
165 siMerge3 = merge(siMerge, siMerge2, "merge3", true);
166 printSegment(out, siMerge3);
171 String singleFileOutput = sw.getBuffer().toString();
173 assertEquals(multiFileOutput, singleFileOutput);
176 private SegmentInfo indexDoc(IndexWriter writer, String fileName)
179 File file = new File(workDir, fileName);
180 Document doc = new Document();
181 doc.add(new Field("contents", new FileReader(file)));
182 writer.addDocument(doc);
184 return writer.newestSegment();
188 private SegmentInfo merge(SegmentInfo si1, SegmentInfo si2, String merged, boolean useCompoundFile)
190 SegmentReader r1 = SegmentReader.get(true, si1, IndexReader.DEFAULT_TERMS_INDEX_DIVISOR);
191 SegmentReader r2 = SegmentReader.get(true, si2, IndexReader.DEFAULT_TERMS_INDEX_DIVISOR);
193 SegmentMerger merger = new SegmentMerger(si1.dir, IndexWriterConfig.DEFAULT_TERM_INDEX_INTERVAL, merged, null, null, new FieldInfos());
201 final SegmentInfo info = new SegmentInfo(merged, si1.docCount + si2.docCount, si1.dir,
203 merger.fieldInfos().hasProx(),
204 merger.fieldInfos().hasVectors());
206 if (useCompoundFile) {
207 Collection<String> filesToDelete = merger.createCompoundFile(merged + ".cfs", info);
208 info.setUseCompoundFile(true);
209 for (final String fileToDelete : filesToDelete)
210 si1.dir.deleteFile(fileToDelete);
217 private void printSegment(PrintWriter out, SegmentInfo si)
219 SegmentReader reader = SegmentReader.get(true, si, IndexReader.DEFAULT_TERMS_INDEX_DIVISOR);
221 for (int i = 0; i < reader.numDocs(); i++)
222 out.println(reader.document(i));
224 TermEnum tis = reader.terms();
226 out.print(tis.term());
227 out.println(" DF=" + tis.docFreq());
229 TermPositions positions = reader.termPositions(tis.term());
231 while (positions.next()) {
232 out.print(" doc=" + positions.doc());
233 out.print(" TF=" + positions.freq());
235 out.print(positions.nextPosition());
236 for (int j = 1; j < positions.freq(); j++)
237 out.print("," + positions.nextPosition());