FileDocCategorySizeDatePackage
TestDoc.javaAPI DocApache Lucene 1.96974Mon Feb 20 09:19:36 GMT 2006org.apache.lucene.index

TestDoc

public class TestDoc extends TestCase
JUnit adaptation of an older test case DocTest.
author
dmitrys@earthlink.net
version
$Id: TestDoc.java 150536 2004-09-28 18:15:52Z cutting $

Fields Summary
private File
workDir
private File
indexDir
private LinkedList
files
Constructors Summary
Methods Summary
private java.io.FilecreateOutput(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 voidindexDoc(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();
   
public static voidmain(java.lang.String[] args)
Main for running test case by itself.

        TestRunner.run (new TestSuite(TestDoc.class));
    
private voidmerge(java.lang.String seg1, java.lang.String seg2, java.lang.String merged, boolean useCompoundFile)

      Directory directory = FSDirectory.getDirectory(indexDir, false);

      SegmentReader r1 = SegmentReader.get(new SegmentInfo(seg1, 1, directory));
      SegmentReader r2 = SegmentReader.get(new SegmentInfo(seg2, 1, directory));

      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();
   
private voidprintSegment(java.io.PrintWriter out, java.lang.String segment)

      Directory directory = FSDirectory.getDirectory(indexDir, false);
      SegmentReader reader =
        SegmentReader.get(new SegmentInfo(segment, 1, directory));

      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 voidsetUp()
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 voidtestIndexAndMerge()
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();

      indexDoc("one", "test.txt");
      printSegment(out, "one");

      indexDoc("two", "test2.txt");
      printSegment(out, "two");

      merge("one", "two", "merge", false);
      printSegment(out, "merge");

      merge("one", "two", "merge2", false);
      printSegment(out, "merge2");

      merge("merge", "merge2", "merge3", false);
      printSegment(out, "merge3");

      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();

      indexDoc("one", "test.txt");
      printSegment(out, "one");

      indexDoc("two", "test2.txt");
      printSegment(out, "two");

      merge("one", "two", "merge", true);
      printSegment(out, "merge");

      merge("one", "two", "merge2", true);
      printSegment(out, "merge2");

      merge("merge", "merge2", "merge3", true);
      printSegment(out, "merge3");

      out.close();
      sw.close();
      String singleFileOutput = sw.getBuffer().toString();

      assertEquals(multiFileOutput, singleFileOutput);