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: import org.apache.lucene.util.LuceneTestCase;
020: import junit.framework.TestSuite;
021: import junit.textui.TestRunner;
022:
023: import org.apache.lucene.analysis.SimpleAnalyzer;
024: import org.apache.lucene.analysis.Analyzer;
025: import org.apache.lucene.store.FSDirectory;
026: import org.apache.lucene.store.Directory;
027: import org.apache.lucene.document.Document;
028: import org.apache.lucene.search.Similarity;
029: import org.apache.lucene.demo.FileDocument;
030:
031: import java.io.*;
032: import java.util.*;
033:
034: /** JUnit adaptation of an older test case DocTest.
035: *
036: * @version $Id: TestDoc.java 583534 2007-10-10 16:46:35Z mikemccand $
037: */
038: public class TestDoc extends LuceneTestCase {
039:
040: /** Main for running test case by itself. */
041: public static void main(String args[]) {
042: TestRunner.run(new TestSuite(TestDoc.class));
043: }
044:
045: private File workDir;
046: private File indexDir;
047: private LinkedList files;
048:
049: /** Set the test case. This test case needs
050: * a few text files created in the current working directory.
051: */
052: public void setUp() throws Exception {
053: super .setUp();
054: workDir = new File(System.getProperty("tempDir"), "TestDoc");
055: workDir.mkdirs();
056:
057: indexDir = new File(workDir, "testIndex");
058: indexDir.mkdirs();
059:
060: Directory directory = FSDirectory.getDirectory(indexDir, true);
061: directory.close();
062:
063: files = new LinkedList();
064: files.add(createOutput("test.txt",
065: "This is the first test file"));
066:
067: files.add(createOutput("test2.txt",
068: "This is the second test file"));
069: }
070:
071: private File createOutput(String name, String text)
072: throws IOException {
073: FileWriter fw = null;
074: PrintWriter pw = null;
075:
076: try {
077: File f = new File(workDir, name);
078: if (f.exists())
079: f.delete();
080:
081: fw = new FileWriter(f);
082: pw = new PrintWriter(fw);
083: pw.println(text);
084: return f;
085:
086: } finally {
087: if (pw != null)
088: pw.close();
089: if (fw != null)
090: fw.close();
091: }
092: }
093:
094: /** This test executes a number of merges and compares the contents of
095: * the segments created when using compound file or not using one.
096: *
097: * TODO: the original test used to print the segment contents to System.out
098: * for visual validation. To have the same effect, a new method
099: * checkSegment(String name, ...) should be created that would
100: * assert various things about the segment.
101: */
102: public void testIndexAndMerge() throws Exception {
103: StringWriter sw = new StringWriter();
104: PrintWriter out = new PrintWriter(sw, true);
105:
106: Directory directory = FSDirectory.getDirectory(indexDir);
107: IndexWriter writer = new IndexWriter(directory,
108: new SimpleAnalyzer(), true);
109:
110: SegmentInfo si1 = indexDoc(writer, "test.txt");
111: printSegment(out, si1);
112:
113: SegmentInfo si2 = indexDoc(writer, "test2.txt");
114: printSegment(out, si2);
115: writer.close();
116: directory.close();
117:
118: SegmentInfo siMerge = merge(si1, si2, "merge", false);
119: printSegment(out, siMerge);
120:
121: SegmentInfo siMerge2 = merge(si1, si2, "merge2", false);
122: printSegment(out, siMerge2);
123:
124: SegmentInfo siMerge3 = merge(siMerge, siMerge2, "merge3", false);
125: printSegment(out, siMerge3);
126:
127: out.close();
128: sw.close();
129: String multiFileOutput = sw.getBuffer().toString();
130: //System.out.println(multiFileOutput);
131:
132: sw = new StringWriter();
133: out = new PrintWriter(sw, true);
134:
135: directory = FSDirectory.getDirectory(indexDir);
136: writer = new IndexWriter(directory, new SimpleAnalyzer(), true);
137:
138: si1 = indexDoc(writer, "test.txt");
139: printSegment(out, si1);
140:
141: si2 = indexDoc(writer, "test2.txt");
142: printSegment(out, si2);
143: writer.close();
144: directory.close();
145:
146: siMerge = merge(si1, si2, "merge", true);
147: printSegment(out, siMerge);
148:
149: siMerge2 = merge(si1, si2, "merge2", true);
150: printSegment(out, siMerge2);
151:
152: siMerge3 = merge(siMerge, siMerge2, "merge3", true);
153: printSegment(out, siMerge3);
154:
155: out.close();
156: sw.close();
157: String singleFileOutput = sw.getBuffer().toString();
158:
159: assertEquals(multiFileOutput, singleFileOutput);
160: }
161:
162: private SegmentInfo indexDoc(IndexWriter writer, String fileName)
163: throws Exception {
164: File file = new File(workDir, fileName);
165: Document doc = FileDocument.Document(file);
166: writer.addDocument(doc);
167: writer.flush();
168: return writer.newestSegment();
169: }
170:
171: private SegmentInfo merge(SegmentInfo si1, SegmentInfo si2,
172: String merged, boolean useCompoundFile) throws Exception {
173: Directory directory = FSDirectory.getDirectory(indexDir, false);
174:
175: SegmentReader r1 = SegmentReader.get(si1);
176: SegmentReader r2 = SegmentReader.get(si2);
177:
178: SegmentMerger merger = new SegmentMerger(directory, merged);
179:
180: merger.add(r1);
181: merger.add(r2);
182: merger.merge();
183: merger.closeReaders();
184:
185: if (useCompoundFile) {
186: Vector filesToDelete = merger.createCompoundFile(merged
187: + ".cfs");
188: for (Iterator iter = filesToDelete.iterator(); iter
189: .hasNext();)
190: directory.deleteFile((String) iter.next());
191: }
192:
193: directory.close();
194: return new SegmentInfo(merged, si1.docCount + si2.docCount,
195: directory, useCompoundFile, true);
196: }
197:
198: private void printSegment(PrintWriter out, SegmentInfo si)
199: throws Exception {
200: Directory directory = FSDirectory.getDirectory(indexDir, false);
201: SegmentReader reader = SegmentReader.get(si);
202:
203: for (int i = 0; i < reader.numDocs(); i++)
204: out.println(reader.document(i));
205:
206: TermEnum tis = reader.terms();
207: while (tis.next()) {
208: out.print(tis.term());
209: out.println(" DF=" + tis.docFreq());
210:
211: TermPositions positions = reader.termPositions(tis.term());
212: try {
213: while (positions.next()) {
214: out.print(" doc=" + positions.doc());
215: out.print(" TF=" + positions.freq());
216: out.print(" pos=");
217: out.print(positions.nextPosition());
218: for (int j = 1; j < positions.freq(); j++)
219: out.print("," + positions.nextPosition());
220: out.println("");
221: }
222: } finally {
223: positions.close();
224: }
225: }
226: tis.close();
227: reader.close();
228: directory.close();
229: }
230: }
|