KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > services > indexing > test > TestIndexingService


1 /***************************************************************************
2  * Copyright 2001-2003 The eXo Platform SARL All rights reserved. *
3  * Please look at license.txt in info directory for more license detail. *
4  **************************************************************************/

5 package org.exoplatform.services.indexing.test;
6
7 import org.apache.lucene.document.Document;
8 import org.exoplatform.container.PortalContainer;
9 import org.exoplatform.services.indexing.FileIndexerPlugin;
10 import org.exoplatform.services.indexing.IndexingService;
11 import org.exoplatform.services.indexing.Searcher;
12 import org.exoplatform.services.indexing.SingleFieldSearchInput;
13 import org.exoplatform.services.indexing.impl.FileIndexerPluginImpl;
14 import org.exoplatform.test.BasicTestCase;
15
16 /**
17  * Thu, May 15, 2003 @
18  * @author: Tuan Nguyen
19  * @version: $Id: TestIndexingService.java,v 1.7 2004/10/25 03:41:16 tuan08 Exp $
20  * @since: 0.0
21  * @email: tuan08@yahoo.com
22  */

23 public class TestIndexingService extends BasicTestCase {
24   static private IndexingService service_ ;
25   public TestIndexingService(String JavaDoc name) {
26     super(name);
27   }
28
29   public void setUp() throws Exception JavaDoc {
30     if(service_ == null) {
31       PortalContainer pcontainer = PortalContainer.getInstance() ;
32       service_ = (IndexingService) pcontainer.getComponentInstanceOfType(IndexingService.class) ;
33     }
34   }
35  
36   public void testIndexingService() throws Exception JavaDoc {
37     FileIndexerPluginImpl plugin =
38       (FileIndexerPluginImpl)service_.getIndexerPlugin(FileIndexerPlugin.IDENTIFIER) ;
39     String JavaDoc[] acceptExt = { "java" , "xml"} ;
40     plugin.indexDirectory("src","guest", acceptExt , true) ;
41     // wait to make sure that indexer thread finish its job
42
Thread.sleep(3500) ;
43     Searcher searcher = plugin.getSearcher() ;
44     SingleFieldSearchInput documentSearchInput =
45       new SingleFieldSearchInput(IndexingService.DOCUMENT_FIELD) ;
46     documentSearchInput.setTerm("Tuan") ;
47     //SingleFieldSearchInput[] inputs = { documentSearchInput } ;
48
java.util.List JavaDoc hits = searcher.search(documentSearchInput).getAll() ;
49     Document foundDoc = null ;
50     for(int i = 0; i < hits.size(); i ++) {
51       foundDoc = (Document )hits.get(i) ;
52       System.out.println("F O U N D : " + foundDoc.getField(IndexingService.IDENTIFIER_FIELD).stringValue()) ;
53     }
54     if(foundDoc != null ) {
55       String JavaDoc objectId = foundDoc.getField(IndexingService.IDENTIFIER_FIELD).stringValue() ;
56       System.out.println(plugin.getObjectAsText(null, objectId)) ;
57     }
58     
59     plugin.removeIndex() ;
60     Thread.sleep(3500) ;
61     searcher = plugin.getSearcher() ;
62     hits = searcher.search(documentSearchInput).getAll() ;
63     assertEquals("exect no result is found", 0 , hits.size()) ;
64     service_.optimizeDatabase() ;
65   }
66 }
Popular Tags