package org.apache.lucene.ant;

import java.io.File;

import java.io.IOException;

import junit.framework.TestCase;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.StopAnalyzer;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.Searcher;
import org.apache.lucene.ant.IndexTask;

import org.apache.tools.ant.Project;
import org.apache.tools.ant.types.FileSet;

/**
 *  Test cases for index task
 *
 *@author     Erik Hatcher
 */
public class IndexTaskTest extends TestCase {
    private final static String docHandler =
            "org.apache.lucene.ant.FileExtensionDocumentHandler";

    private String docsDir = System.getProperty("docs.dir");
    private String indexDir = System.getProperty("index.dir");
    
    private Searcher searcher;
    private Analyzer analyzer;


    /**
     *  Constructor for the IndexTaskTest object
     *
     *@param  name  Description of Parameter
     */
    public IndexTaskTest(String name) {
        super(name);
    }


    /**
     *  The JUnit setup method
     *
     *@exception  IOException  Description of Exception
     */
    public void setUp() throws IOException {
        Project project = new Project();

        IndexTask task = new IndexTask();
        FileSet fs = new FileSet();
        fs.setDir(new File(docsDir));
        task.addFileset(fs);
        task.setOverwrite(true);
        task.setDocumentHandler(docHandler);
        task.setIndex(new File(indexDir));
        task.setProject(project);
        task.execute();

        searcher = new IndexSearcher(indexDir);
        analyzer = new StopAnalyzer();
    }


    /**
     *  A unit test for JUnit
     */
    public void testSearch() throws IOException, ParseException {
        System.out.println("sysout");
        System.err.println("syserr");
        Query query = QueryParser.parse("test", "contents", analyzer);

        Hits hits = searcher.search(query);
        
        assertEquals("Find document(s)", 2, hits.length());
    }

    /**
     *  The teardown method for JUnit
     * @todo remove indexDir?
     */
    public void tearDown() throws IOException {
        searcher.close();
    }
}


@@ -206,61 +206,8 @@
 er;%0A
-import org.apache.lucene.queryParser.ParseException;%0A
 impo
@@ -248,24 +248,24 @@
 ueryParser;%0A
+
 import org.a
@@ -890,21 +890,17 @@
 .dir%22);%0A
-    
 %0A
+
     priv
@@ -959,194 +959,8 @@
 ;%0A%0A%0A
-    /**%0A     *  Constructor for the IndexTaskTest object%0A     *%0A     *@param  name  Description of Parameter%0A     */%0A    public IndexTaskTest(String name) %7B%0A        super(name);%0A    %7D%0A%0A%0A
     
@@ -1088,34 +1088,32 @@
  setUp() throws 
-IO
 Exception %7B%0A    
@@ -1578,191 +1578,51 @@
     
-/**%0A     *  A unit test for JUnit%0A     */%0A    public void testSearch() throws IOException, ParseException %7B%0A        System.out.println(%22sysout%22);%0A        System.err.println(%22syserr%22);
+public void testSearch() throws Exception %7B
 %0A   
@@ -1734,24 +1734,16 @@
 query);%0A
-        
 %0A       

test case cleanup