TestDocpublic class TestDoc extends TestCase JUnit adaptation of an older test case DocTest. |
Fields Summary |
---|
private File | workDir | private File | indexDir | private LinkedList | files |
Methods Summary |
---|
private java.io.File | createOutput(java.lang.String name, java.lang.String text)
FileWriter fw = null;
PrintWriter pw = null;
try {
File f = new File(workDir, name);
if (f.exists()) f.delete();
fw = new FileWriter(f);
pw = new PrintWriter(fw);
pw.println(text);
return f;
} finally {
if (pw != null) pw.close();
if (fw != null) fw.close();
}
| private org.apache.lucene.index.SegmentInfo | indexDoc(java.lang.String segment, java.lang.String fileName)
Directory directory = FSDirectory.getDirectory(indexDir, false);
Analyzer analyzer = new SimpleAnalyzer();
DocumentWriter writer =
new DocumentWriter(directory, analyzer, Similarity.getDefault(), 1000);
File file = new File(workDir, fileName);
Document doc = FileDocument.Document(file);
writer.addDocument(segment, doc);
directory.close();
return new SegmentInfo(segment, 1, directory, false, false);
| public static void | main(java.lang.String[] args)Main for running test case by itself.
TestRunner.run (new TestSuite(TestDoc.class));
| private org.apache.lucene.index.SegmentInfo | merge(org.apache.lucene.index.SegmentInfo si1, org.apache.lucene.index.SegmentInfo si2, java.lang.String merged, boolean useCompoundFile)
Directory directory = FSDirectory.getDirectory(indexDir, false);
SegmentReader r1 = SegmentReader.get(si1);
SegmentReader r2 = SegmentReader.get(si2);
SegmentMerger merger = new SegmentMerger(directory, merged);
merger.add(r1);
merger.add(r2);
merger.merge();
merger.closeReaders();
if (useCompoundFile) {
Vector filesToDelete = merger.createCompoundFile(merged + ".cfs");
for (Iterator iter = filesToDelete.iterator(); iter.hasNext();)
directory.deleteFile((String) iter.next());
}
directory.close();
return new SegmentInfo(merged, si1.docCount + si2.docCount, directory, useCompoundFile, true);
| private void | printSegment(java.io.PrintWriter out, org.apache.lucene.index.SegmentInfo si)
Directory directory = FSDirectory.getDirectory(indexDir, false);
SegmentReader reader = SegmentReader.get(si);
for (int i = 0; i < reader.numDocs(); i++)
out.println(reader.document(i));
TermEnum tis = reader.terms();
while (tis.next()) {
out.print(tis.term());
out.println(" DF=" + tis.docFreq());
TermPositions positions = reader.termPositions(tis.term());
try {
while (positions.next()) {
out.print(" doc=" + positions.doc());
out.print(" TF=" + positions.freq());
out.print(" pos=");
out.print(positions.nextPosition());
for (int j = 1; j < positions.freq(); j++)
out.print("," + positions.nextPosition());
out.println("");
}
} finally {
positions.close();
}
}
tis.close();
reader.close();
directory.close();
| public void | setUp()Set the test case. This test case needs
a few text files created in the current working directory.
workDir = new File(System.getProperty("tempDir"),"TestDoc");
workDir.mkdirs();
indexDir = new File(workDir, "testIndex");
indexDir.mkdirs();
Directory directory = FSDirectory.getDirectory(indexDir, true);
directory.close();
files = new LinkedList();
files.add(createOutput("test.txt",
"This is the first test file"
));
files.add(createOutput("test2.txt",
"This is the second test file"
));
| public void | testIndexAndMerge()This test executes a number of merges and compares the contents of
the segments created when using compound file or not using one.
TODO: the original test used to print the segment contents to System.out
for visual validation. To have the same effect, a new method
checkSegment(String name, ...) should be created that would
assert various things about the segment.
StringWriter sw = new StringWriter();
PrintWriter out = new PrintWriter(sw, true);
Directory directory = FSDirectory.getDirectory(indexDir, true);
directory.close();
SegmentInfo si1 = indexDoc("one", "test.txt");
printSegment(out, si1);
SegmentInfo si2 = indexDoc("two", "test2.txt");
printSegment(out, si2);
SegmentInfo siMerge = merge(si1, si2, "merge", false);
printSegment(out, siMerge);
SegmentInfo siMerge2 = merge(si1, si2, "merge2", false);
printSegment(out, siMerge2);
SegmentInfo siMerge3 = merge(siMerge, siMerge2, "merge3", false);
printSegment(out, siMerge3);
out.close();
sw.close();
String multiFileOutput = sw.getBuffer().toString();
//System.out.println(multiFileOutput);
sw = new StringWriter();
out = new PrintWriter(sw, true);
directory = FSDirectory.getDirectory(indexDir, true);
directory.close();
si1 = indexDoc("one", "test.txt");
printSegment(out, si1);
si2 = indexDoc("two", "test2.txt");
printSegment(out, si2);
siMerge = merge(si1, si2, "merge", true);
printSegment(out, siMerge);
siMerge2 = merge(si1, si2, "merge2", true);
printSegment(out, siMerge2);
siMerge3 = merge(siMerge, siMerge2, "merge3", true);
printSegment(out, siMerge3);
out.close();
sw.close();
String singleFileOutput = sw.getBuffer().toString();
assertEquals(multiFileOutput, singleFileOutput);
|
|