KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > jtrac > lucene > IndexSearcherTest


1 package info.jtrac.lucene;
2
3 import info.jtrac.domain.Item;
4 import java.io.File JavaDoc;
5 import java.util.List JavaDoc;
6 import junit.framework.TestCase;
7 import org.apache.lucene.store.FSDirectory;
8 import org.springframework.context.ApplicationContext;
9 import org.springframework.context.support.FileSystemXmlApplicationContext;
10
11 public class IndexSearcherTest extends TestCase {
12     
13     private ApplicationContext context;
14     
15     @Override JavaDoc
16     public void setUp() {
17         File JavaDoc home = new File JavaDoc("target/home");
18         if (!home.exists()) {
19             home.mkdir();
20         }
21         File JavaDoc file = new File JavaDoc("target/home/indexes");
22         if (!file.exists()) {
23             file.mkdir();
24         } else {
25             for (File JavaDoc f : file.listFiles()) {
26                 f.delete();
27             }
28         }
29         System.setProperty("jtrac.home", home.getAbsolutePath());
30         context = new FileSystemXmlApplicationContext("src/main/webapp/WEB-INF/applicationContext-lucene.xml");
31     }
32     
33     public void testFindItemIdsBySearchingWithinSummaryAndDetailFields() throws Exception JavaDoc {
34         Item item = new Item();
35         item.setId(1);
36         item.setSummary("this is a test summary");
37         item.setDetail("the quick brown fox jumped over the lazy dogs");
38         Indexer indexer = (Indexer) context.getBean("indexer");
39         // ensures code coverage of clearIndexes method
40
File JavaDoc file = new File JavaDoc("target/home/indexes/foo.bar");
41         file.createNewFile();
42         indexer.clearIndexes();
43         indexer.index(item);
44         IndexSearcher searcher = (IndexSearcher) context.getBean("indexSearcher");
45         List JavaDoc list = searcher.findItemIdsContainingText("lazy");
46         assertEquals(1, list.size());
47         list = searcher.findItemIdsContainingText("foo");
48         assertEquals(0, list.size());
49         list = searcher.findItemIdsContainingText("summary");
50         assertEquals(1, list.size());
51     }
52     
53 }
54
Popular Tags