TestIndex.java
001 /*
002  *  TestIndex.java
003  *
004  *  Copyright (c) 1995-2010, The University of Sheffield. See the file
005  *  COPYRIGHT.txt in the software or at http://gate.ac.uk/gate/COPYRIGHT.txt
006  *
007  *  This file is part of GATE (see http://gate.ac.uk/), and is free
008  *  software, licenced under the GNU Library General Public License,
009  *  Version 2, June 1991 (in the distribution as file licence.html,
010  *  and also available at http://gate.ac.uk/gate/licence.html).
011  *
012  *  Rosen Marinov, 19/Apr/2002
013  *
014  */
015 
016 package gate.creole.ir;
017 
018 import java.io.File;
019 import java.net.URL;
020 import java.util.Iterator;
021 
022 import junit.framework.*;
023 
024 import gate.*;
025 import gate.corpora.TestDocument;
026 import gate.creole.ir.lucene.LuceneSearch;
027 import gate.util.GateException;
028 
029 public class TestIndex extends TestCase{
030 
031   private static String TEMP_LOCATION = null;
032   private Corpus corpus = null;
033   private DataStore sds = null;
034 
035   public TestIndex(String namethrows GateException {
036     super(name);
037   }
038 
039   /** Fixture set up */
040   public void setUp() throws Exception {
041     try {
042       File storageDir = File.createTempFile("TestIndex__""__StorageDir");
043 
044       if (null == TEMP_LOCATION) {
045         File indexDir = File.createTempFile("LuceneIndex__""__Dir");
046         TEMP_LOCATION = indexDir.getAbsolutePath();
047       }
048 
049 //System.out.println("temp=["+TEMP_LOCATION+"]");
050 //System.out.println("temp2=["+indexDir.getAbsoluteFile()+"]");
051 
052       storageDir.delete();
053       // create and open a serial data store
054       sds = Factory.createDataStore(
055         "gate.persist.SerialDataStore", storageDir.toURI().toURL().toString()
056       );
057 
058       sds.open();
059 
060       String server = TestDocument.getTestServerName();
061 
062       Document doc0 = Factory.newDocument(new URL(server + "tests/doc0.html"));
063       doc0.getFeatures().put("author","John Smit");
064 
065       Corpus corp = Factory.newCorpus("LuceneTestCorpus");
066       corp.add(doc0);
067       corpus = (Corpussds.adopt(corp,null);
068       sds.sync(corpus);
069 
070     catch (Exception e) {
071       e.printStackTrace();
072       throw new GateException(e.getMessage());
073     }
074   // setUp
075 
076   /** Put things back as they should be after running tests
077     * (reinitialise the CREOLE register).
078     */
079   public void tearDown() throws Exception {
080     sds.delete();
081   // tearDown
082 
083   /** Test suite routine for the test runner */
084   public static Test suite() {
085     return new TestSuite(TestIndex.class);
086   // suite
087 
088   /** Create new index. */
089   public void testIndex_01() throws IndexException{
090     IndexedCorpus ic = (IndexedCorpuscorpus;
091     DefaultIndexDefinition did = new DefaultIndexDefinition();
092     did.setIrEngineClassName(gate.creole.ir.lucene.
093                              LuceneIREngine.class.getName());
094 
095 //    did.setIndexType(GateConstants.IR_LUCENE_INVFILE);
096 
097     did.setIndexLocation(TEMP_LOCATION);
098     did.addIndexField(new IndexField("content"new DocumentContentReader()false));
099     did.addIndexField(new IndexField("author", null, false));
100 
101     ic.setIndexDefinition(did);
102 
103     ic.getIndexManager().deleteIndex();
104     ic.getIndexManager().createIndex();
105 
106   }
107 
108   /** Optimize existing index. */
109   public void testIndex_02() throws IndexException{
110     IndexedCorpus ic = (IndexedCorpuscorpus;
111     DefaultIndexDefinition did = new DefaultIndexDefinition();
112 //    did.setIndexType(GateConstants.IR_LUCENE_INVFILE);
113     did.setIrEngineClassName(gate.creole.ir.lucene.
114                              LuceneIREngine.class.getName());
115 
116 
117     did.setIndexLocation(TEMP_LOCATION);
118 
119     ic.setIndexDefinition(did);
120 
121     ic.getIndexManager().optimizeIndex();
122   }
123 
124   /** Search in existing index. */
125   public void testIndex_10() throws IndexException, SearchException{
126     IndexedCorpus ic = (IndexedCorpuscorpus;
127     DefaultIndexDefinition did = new DefaultIndexDefinition();
128 //    did.setIndexType(GateConstants.IR_LUCENE_INVFILE);
129     did.setIrEngineClassName(gate.creole.ir.lucene.
130                              LuceneIREngine.class.getName());
131 
132     did.setIndexLocation(TEMP_LOCATION);
133 
134     ic.setIndexDefinition(did);
135 
136     Search search = new LuceneSearch();
137     search.setCorpus(ic);
138 
139     QueryResultList res = search.search("+content:Diller +author:John",10);
140 
141     Iterator it = res.getQueryResults();
142     //while (it.hasNext()) {
143     //  QueryResult qr = (QueryResult) it.next();
144     //  System.out.println("DOCUMENT_ID="+ qr.getDocumentID() +",   scrore="+qr.getScore());
145     //}
146     Assert.assertTrue(it.hasNext());
147   }
148 
149   public void testIndex_11(){
150 
151   }
152 
153   public void testIndex_12(){
154 
155   }
156 
157   /** Delete index. */
158   public void testIndex_101() throws IndexException{
159     IndexedCorpus ic = (IndexedCorpuscorpus;
160     DefaultIndexDefinition did = new DefaultIndexDefinition();
161 //    did.setIndexType(GateConstants.IR_LUCENE_INVFILE);
162     did.setIrEngineClassName(gate.creole.ir.lucene.
163                              LuceneIREngine.class.getName());
164 
165     did.setIndexLocation(TEMP_LOCATION);
166 
167     ic.setIndexDefinition(did);
168 
169     ic.getIndexManager().deleteIndex();
170   }
171 
172 
173   public static void main(String[] args){
174     try{
175       Gate.init();
176 
177       TestIndex test = new TestIndex("");
178 
179       test.setUp();
180       test.testIndex_01();
181       test.tearDown();
182 
183       test.setUp();
184       test.testIndex_02();
185       test.tearDown();
186 
187       test.setUp();
188       test.testIndex_10();
189       test.tearDown();
190 
191       test.setUp();
192       test.testIndex_101();
193       test.tearDown();
194 
195     catch (Exception e){
196       e.printStackTrace();
197     }
198   }
199 }