Clover Coverage Report
Coverage timestamp: Fri May 9 2008 10:54:27 EST
101   234   16   14.43
18   148   0.16   7
7     2.29  
1    
 
  TestDoc       Line # 39 101 16 93.7% 0.93650794
 
  (1)
 
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 150536 2004-09-28 18:15:52Z cutting $
38    */
 
39    public class TestDoc extends TestCase {
40   
41    /** Main for running test case by itself. */
 
42  0 toggle public static void main(String args[]) {
43  0 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  1 toggle public void setUp() throws IOException {
56  1 workDir = new File(System.getProperty("tempDir"),"TestDoc");
57  1 workDir.mkdirs();
58   
59  1 indexDir = new File(workDir, "testIndex");
60  1 indexDir.mkdirs();
61   
62  1 Directory directory = FSDirectory.getDirectory(indexDir, true);
63  1 directory.close();
64   
65  1 files = new LinkedList();
66  1 files.add(createOutput("test.txt",
67    "This is the first test file"
68    ));
69   
70  1 files.add(createOutput("test2.txt",
71    "This is the second test file"
72    ));
73    }
74   
 
75  2 toggle private File createOutput(String name, String text) throws IOException {
76  2 FileWriter fw = null;
77  2 PrintWriter pw = null;
78   
79  2 try {
80  2 File f = new File(workDir, name);
81  0 if (f.exists()) f.delete();
82   
83  2 fw = new FileWriter(f);
84  2 pw = new PrintWriter(fw);
85  2 pw.println(text);
86  2 return f;
87   
88    } finally {
89  2 if (pw != null) pw.close();
90  2 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  1 toggle public void testIndexAndMerge() throws Exception {
104  1 StringWriter sw = new StringWriter();
105  1 PrintWriter out = new PrintWriter(sw, true);
106   
107  1 Directory directory = FSDirectory.getDirectory(indexDir, true);
108  1 directory.close();
109   
110  1 indexDoc("one", "test.txt");
111  1 printSegment(out, "one");
112   
113  1 indexDoc("two", "test2.txt");
114  1 printSegment(out, "two");
115   
116  1 merge("one", "two", "merge", false);
117  1 printSegment(out, "merge");
118   
119  1 merge("one", "two", "merge2", false);
120  1 printSegment(out, "merge2");
121   
122  1 merge("merge", "merge2", "merge3", false);
123  1 printSegment(out, "merge3");
124   
125  1 out.close();
126  1 sw.close();
127  1 String multiFileOutput = sw.getBuffer().toString();
128    //System.out.println(multiFileOutput);
129   
130  1 sw = new StringWriter();
131  1 out = new PrintWriter(sw, true);
132   
133  1 directory = FSDirectory.getDirectory(indexDir, true);
134  1 directory.close();
135   
136  1 indexDoc("one", "test.txt");
137  1 printSegment(out, "one");
138   
139  1 indexDoc("two", "test2.txt");
140  1 printSegment(out, "two");
141   
142  1 merge("one", "two", "merge", true);
143  1 printSegment(out, "merge");
144   
145  1 merge("one", "two", "merge2", true);
146  1 printSegment(out, "merge2");
147   
148  1 merge("merge", "merge2", "merge3", true);
149  1 printSegment(out, "merge3");
150   
151  1 out.close();
152  1 sw.close();
153  1 String singleFileOutput = sw.getBuffer().toString();
154   
155  1 assertEquals(multiFileOutput, singleFileOutput);
156    }
157   
158   
 
159  4 toggle private void indexDoc(String segment, String fileName)
160    throws Exception
161    {
162  4 Directory directory = FSDirectory.getDirectory(indexDir, false);
163  4 Analyzer analyzer = new SimpleAnalyzer();
164  4 DocumentWriter writer =
165    new DocumentWriter(directory, analyzer, Similarity.getDefault(), 1000);
166   
167  4 File file = new File(workDir, fileName);
168  4 Document doc = FileDocument.Document(file);
169   
170  4 writer.addDocument(segment, doc);
171   
172  4 directory.close();
173    }
174   
175   
 
176  6 toggle private void merge(String seg1, String seg2, String merged, boolean useCompoundFile)
177    throws Exception {
178  6 Directory directory = FSDirectory.getDirectory(indexDir, false);
179   
180  6 SegmentReader r1 = SegmentReader.get(new SegmentInfo(seg1, 1, directory));
181  6 SegmentReader r2 = SegmentReader.get(new SegmentInfo(seg2, 1, directory));
182   
183  6 SegmentMerger merger =
184    new SegmentMerger(directory, merged);
185   
186  6 merger.add(r1);
187  6 merger.add(r2);
188  6 merger.merge();
189  6 merger.closeReaders();
190   
191  6 if (useCompoundFile) {
192  3 Vector filesToDelete = merger.createCompoundFile(merged + ".cfs");
193  33 for (Iterator iter = filesToDelete.iterator(); iter.