| 1 |
|
package org.apache.lucene.index; |
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 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 |
|
|
| 36 |
|
@author |
| 37 |
|
@version |
| 38 |
|
|
|
|
|
| 93.7% |
Uncovered Elements: 8 (126) |
Complexity: 16 |
Complexity Density: 0.16 |
|
| 39 |
|
public class TestDoc extends TestCase { |
| 40 |
|
|
| 41 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 42 |
0
|
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 |
|
|
| 53 |
|
|
| 54 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 1 |
Complexity Density: 0.11 |
|
| 55 |
1
|
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 |
|
|
|
|
|
| 80% |
Uncovered Elements: 4 (20) |
Complexity: 4 |
Complexity Density: 0.29 |
|
| 75 |
2
|
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 |
|
|
| 96 |
|
|
| 97 |
|
|
| 98 |
|
|
| 99 |
|
|
| 100 |
|
|
| 101 |
|
|
| 102 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (35) |
Complexity: 1 |
Complexity Density: 0.03 |
1
PASS
|
|
| 103 |
1
|
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 |
|
|
| 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 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
|
| 159 |
4
|
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 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (17) |
Complexity: 3 |
Complexity Density: 0.23 |
|
| 176 |
6
|
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. |