1 package info.jtrac.lucene; 2 3 import info.jtrac.domain.Item; 4 import java.io.File ; 5 import java.util.List ; 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 16 public void setUp() { 17 File home = new File ("target/home"); 18 if (!home.exists()) { 19 home.mkdir(); 20 } 21 File file = new File ("target/home/indexes"); 22 if (!file.exists()) { 23 file.mkdir(); 24 } else { 25 for (File 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 { 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 File file = new File ("target/home/indexes/foo.bar"); 41 file.createNewFile(); 42 indexer.clearIndexes(); 43 indexer.index(item); 44 IndexSearcher searcher = (IndexSearcher) context.getBean("indexSearcher"); 45 List 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 |