KickJava   Java API By Example, From Geeks To Geeks.

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


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 import junit.framework.TestCase;
19 import junit.framework.TestSuite;
20 import junit.textui.TestRunner;
21
22
23 import org.apache.lucene.analysis.SimpleAnalyzer;
24 import org.apache.lucene.analysis.Analyzer;
25 import org.apache.lucene.store.FSDirectory;
26 import org.apache.lucene.store.Directory;
27 import org.apache.lucene.document.Document;
28 import org.apache.lucene.search.Similarity;
29 import org.apache.lucene.demo.FileDocument;
30
31 import java.io.*;
32 import java.util.*;
33
34
35 /** JUnit adaptation of an older test case DocTest.
36  * @author dmitrys@earthlink.net
37  * @version $Id: TestDoc.java,v 1.5 2004/04/20 19:33:35 goller Exp $
38  */

39 public class TestDoc extends TestCase {
40
41     /** Main for running test case by itself. */
42     public static void main(String JavaDoc args[]) {
43         TestRunner.run (new TestSuite(TestDoc.class));
44     }
45
46
47     private File workDir;
48     private File indexDir;
49     private LinkedList files;
50
51
52     /** Set the test case. This test case needs
53      * a few text files created in the current working directory.
54      */

55     public void setUp() throws IOException {
56         workDir = new File(System.getProperty("tempDir"),"TestDoc");
57         workDir.mkdirs();
58
59         indexDir = new File(workDir, "testIndex");
60         indexDir.mkdirs();
61
62         Directory directory = FSDirectory.getDirectory(indexDir, true);
63         directory.close();
64
65         files = new LinkedList();
66         files.add(createFile("test.txt",
67             "This is the first test file"
68         ));
69
70         files.add(createFile("test2.txt",
71             "This is the second test file"
72         ));
73     }
74
75     private File createFile(String JavaDoc name, String JavaDoc text) throws IOException {
76         FileWriter fw = null;
77         PrintWriter pw = null;
78
79         try {
80             File f = new File(workDir, name);
81             if (f.exists()) f.delete();
82
83             fw = new FileWriter(f);
84             pw = new PrintWriter(fw);
85             pw.println(text);
86             return f;
87
88         } finally {
89             if (pw != null) pw.close();
90             if (fw != null) fw.close();
91         }
92     }
93
94
95     /** This test executes a number of merges and compares the contents of
96      * the segments created when using compound file or not using one.
97      *
98      * TODO: the original test used to print the segment contents to System.out
99      * for visual validation. To have the same effect, a new method
100      * checkSegment(String name, ...) should be created that would
101      * assert various things about the segment.
102      */

103     public void testIndexAndMerge() throws Exception JavaDoc {
104       StringWriter sw = new StringWriter();
105       PrintWriter out = new PrintWriter(sw, true);
106
107       Directory directory = FSDirectory.getDirectory(indexDir, true);
108       directory.close();
109
110       indexDoc("one", "test.txt");
111       printSegment(out, "one");
112
113       indexDoc("two", "test2.txt");
114       printSegment(out, "two");
115
116       merge("one", "two", "merge", false);
117       printSegment(out, "merge");
118
119       merge("one", "two", "merge2", false);
120       printSegment(out, "merge2");
121
122       merge("merge", "merge2", "merge3", false);
123       printSegment(out, "merge3");
124
125       out.close();
126       sw.close();
127       String JavaDoc multiFileOutput = sw.getBuffer().toString();
128       //System.out.println(multiFileOutput);
129

130       sw = new StringWriter();
131       out = new PrintWriter(sw, true);
132
133       directory = FSDirectory.getDirectory(indexDir, true);
134       directory.close();
135
136       indexDoc("one", "test.txt");
137       printSegment(out, "one");
138
139       indexDoc("two", "test2.txt");
140       printSegment(out, "two");
141
142       merge("one", "two", "merge", true);
143       printSegment(out, "merge");
144
145       merge("one", "two", "merge2", true);
146       printSegment(out, "merge2");
147
148       merge("merge", "merge2", "merge3", true);
149       printSegment(out, "merge3");
150
151       out.close();
152       sw.close();
153       String JavaDoc singleFileOutput = sw.getBuffer().toString();
154
155       assertEquals(multiFileOutput, singleFileOutput);
156    }
157
158
159    private void indexDoc(String JavaDoc segment, String JavaDoc fileName)
160    throws Exception JavaDoc
161    {
162       Directory directory = FSDirectory.getDirectory(indexDir, false);
163       Analyzer analyzer = new SimpleAnalyzer();
164       DocumentWriter writer =
165          new DocumentWriter(directory, analyzer, Similarity.getDefault(), 1000);
166
167       File file = new File(workDir, fileName);
168       Document doc = FileDocument.Document(file);
169
170       writer.addDocument(segment, doc);
171
172       directory.close();
173    }
174
175
176    private void merge(String JavaDoc seg1, String JavaDoc seg2, String JavaDoc merged, boolean useCompoundFile)
177    throws Exception JavaDoc {
178       Directory directory = FSDirectory.getDirectory(indexDir, false);
179
180       SegmentReader r1 = new SegmentReader(new SegmentInfo(seg1, 1, directory));
181       SegmentReader r2 = new SegmentReader(new SegmentInfo(seg2, 1, directory));
182
183       SegmentMerger merger =
184         new SegmentMerger(directory, merged, useCompoundFile);
185
186       merger.add(r1);
187       merger.add(r2);
188       merger.merge();
189       merger.closeReaders();
190
191       directory.close();
192    }
193
194
195    private void printSegment(PrintWriter out, String JavaDoc segment)
196    throws Exception JavaDoc {
197       Directory directory = FSDirectory.getDirectory(indexDir, false);
198       SegmentReader reader =
199         new SegmentReader(new SegmentInfo(segment, 1, directory));
200
201       for (int i = 0; i < reader.numDocs(); i++)
202         out.println(reader.document(i));
203
204       TermEnum tis = reader.terms();
205       while (tis.next()) {
206         out.print(tis.term());
207         out.println(" DF=" + tis.docFreq());
208
209         TermPositions positions = reader.termPositions(tis.term());
210         try {
211           while (positions.next()) {
212             out.print(" doc=" + positions.doc());
213             out.print(" TF=" + positions.freq());
214             out.print(" pos=");
215             out.print(positions.nextPosition());
216             for (int j = 1; j < positions.freq(); j++)
217               out.print("," + positions.nextPosition());
218             out.println("");
219           }
220         } finally {
221           positions.close();
222         }
223       }
224       tis.close();
225       reader.close();
226       directory.close();
227     }
228 }
229
Popular Tags