01: package org.apache.lucene.ant;
02:
03: /**
04: * Licensed to the Apache Software Foundation (ASF) under one or more
05: * contributor license agreements. See the NOTICE file distributed with
06: * this work for additional information regarding copyright ownership.
07: * The ASF licenses this file to You under the Apache License, Version 2.0
08: * (the "License"); you may not use this file except in compliance with
09: * the License. You may obtain a copy of the License at
10: *
11: * http://www.apache.org/licenses/LICENSE-2.0
12: *
13: * Unless required by applicable law or agreed to in writing, software
14: * distributed under the License is distributed on an "AS IS" BASIS,
15: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16: * See the License for the specific language governing permissions and
17: * limitations under the License.
18: */
19:
20: import java.io.File;
21:
22: import java.io.IOException;
23:
24: import junit.framework.TestCase;
25:
26: import org.apache.lucene.analysis.Analyzer;
27: import org.apache.lucene.analysis.StopAnalyzer;
28: import org.apache.lucene.queryParser.QueryParser;
29: import org.apache.lucene.search.Hits;
30: import org.apache.lucene.search.IndexSearcher;
31: import org.apache.lucene.search.Query;
32: import org.apache.lucene.search.Searcher;
33:
34: import org.apache.tools.ant.Project;
35: import org.apache.tools.ant.types.FileSet;
36:
37: /**
38: * Test cases for index task
39: *
40: *@author Erik Hatcher
41: */
42: public class IndexTaskTest extends TestCase {
43: private final static String docHandler = "org.apache.lucene.ant.FileExtensionDocumentHandler";
44:
45: private String docsDir = System.getProperty("docs.dir");
46: private String indexDir = System.getProperty("index.dir");
47:
48: private Searcher searcher;
49: private Analyzer analyzer;
50:
51: /**
52: * The JUnit setup method
53: *
54: *@exception IOException Description of Exception
55: */
56: public void setUp() throws Exception {
57: Project project = new Project();
58:
59: IndexTask task = new IndexTask();
60: FileSet fs = new FileSet();
61: fs.setDir(new File(docsDir));
62: task.addFileset(fs);
63: task.setOverwrite(true);
64: task.setDocumentHandler(docHandler);
65: task.setIndex(new File(indexDir));
66: task.setProject(project);
67: task.execute();
68:
69: searcher = new IndexSearcher(indexDir);
70: analyzer = new StopAnalyzer();
71: }
72:
73: public void testSearch() throws Exception {
74: Query query = new QueryParser("contents", analyzer)
75: .parse("test");
76:
77: Hits hits = searcher.search(query);
78:
79: assertEquals("Find document(s)", 2, hits.length());
80: }
81:
82: /**
83: * The teardown method for JUnit
84: * @todo remove indexDir?
85: */
86: public void tearDown() throws IOException {
87: searcher.close();
88: }
89: }
|