1   /*
2    *  TestIndex.java
3    *
4    *  Copyright (c) 1998-2001, The University of Sheffield.
5    *
6    *  This file is part of GATE (see http://gate.ac.uk/), and is free
7    *  software, licenced under the GNU Library General Public License,
8    *  Version 2, June 1991 (in the distribution as file licence.html,
9    *  and also available at http://gate.ac.uk/gate/licence.html).
10   *
11   *  Rosen Marinov, 19/Apr/2002
12   *
13   */
14  
15  package gate.creole.ir;
16  
17  import gate.*;
18  import gate.corpora.*;
19  import gate.util.*;
20  import gate.creole.ir.*;
21  import gate.creole.ir.lucene.*;
22  
23  import junit.framework.*;
24  
25  import java.io.*;
26  import java.net.*;
27  import java.util.*;
28  
29  public class TestIndex extends TestCase{
30  
31    private static String TEMP_LOCATION = null;
32    private Corpus corpus = null;
33    private DataStore sds = null;
34  
35    public TestIndex(String name) throws GateException {
36      super(name);
37  
38      try {
39        File storageDir = File.createTempFile("TestIndex__", "__StorageDir");
40  
41        if (null == TEMP_LOCATION) {
42          File indexDir = File.createTempFile("LuceneIndex__", "__Dir");
43          TEMP_LOCATION = indexDir.getAbsolutePath();
44        }
45  
46  //System.out.println("temp=["+TEMP_LOCATION+"]");
47  //System.out.println("temp2=["+indexDir.getAbsoluteFile()+"]");
48  
49        storageDir.delete();
50        // create and open a serial data store
51        sds = Factory.createDataStore(
52          "gate.persist.SerialDataStore", storageDir.toURL().toString()
53        );
54  
55        sds.open();
56  
57        String server = TestDocument.getTestServerName();
58  
59        Document doc0 = Factory.newDocument(new URL(server + "tests/doc0.html"));
60        doc0.getFeatures().put("author","John Smit");
61  
62        Corpus corp = Factory.newCorpus("LuceneTestCorpus");
63        corp.add(doc0);
64        corpus = (Corpus) sds.adopt(corp,null);
65        sds.sync(corpus);
66  
67      } catch (Exception e) {
68        e.printStackTrace();
69        throw new GateException(e.getMessage());
70      }
71  
72    }
73  
74    /** Fixture set up */
75    public void setUp() throws Exception {
76  
77    } // setUp
78  
79    /** Put things back as they should be after running tests
80      * (reinitialise the CREOLE register).
81      */
82    public void tearDown() throws Exception {
83    } // tearDown
84  
85    /** Test suite routine for the test runner */
86    public static Test suite() {
87      return new TestSuite(TestIndex.class);
88    } // suite
89  
90    /** Create new index. */
91    public void testIndex_01() throws IndexException{
92      IndexedCorpus ic = (IndexedCorpus) corpus;
93      DefaultIndexDefinition did = new DefaultIndexDefinition();
94      did.setIrEngineClassName(gate.creole.ir.lucene.
95                               LuceneIREngine.class.getName());
96  
97  //    did.setIndexType(GateConstants.IR_LUCENE_INVFILE);
98  
99      did.setIndexLocation(TEMP_LOCATION);
100     did.addIndexField(new IndexField("content", new DocumentContentReader(), false));
101     did.addIndexField(new IndexField("author", null, false));
102 
103     ic.setIndexDefinition(did);
104 
105     ic.getIndexManager().deleteIndex();
106     ic.getIndexManager().createIndex();
107 
108   }
109 
110   /** Optimize existing index. */
111   public void testIndex_02() throws IndexException{
112     IndexedCorpus ic = (IndexedCorpus) corpus;
113     DefaultIndexDefinition did = new DefaultIndexDefinition();
114 //    did.setIndexType(GateConstants.IR_LUCENE_INVFILE);
115     did.setIrEngineClassName(gate.creole.ir.lucene.
116                              LuceneIREngine.class.getName());
117 
118 
119     did.setIndexLocation(TEMP_LOCATION);
120 
121     ic.setIndexDefinition(did);
122 
123     ic.getIndexManager().optimizeIndex();
124   }
125 
126   /** Search in existing index. */
127   public void testIndex_10() throws IndexException, SearchException{
128     IndexedCorpus ic = (IndexedCorpus) corpus;
129     DefaultIndexDefinition did = new DefaultIndexDefinition();
130 //    did.setIndexType(GateConstants.IR_LUCENE_INVFILE);
131     did.setIrEngineClassName(gate.creole.ir.lucene.
132                              LuceneIREngine.class.getName());
133 
134     did.setIndexLocation(TEMP_LOCATION);
135 
136     ic.setIndexDefinition(did);
137 
138     Search search = new LuceneSearch();
139     search.setCorpus(ic);
140 
141     QueryResultList res = search.search("+content:Diller +author:John");
142 
143     Iterator it = res.getQueryResults();
144     //while (it.hasNext()) {
145     //  QueryResult qr = (QueryResult) it.next();
146     //  System.out.println("DOCUMENT_ID="+ qr.getDocumentID() +",   scrore="+qr.getScore());
147     //}
148     Assert.assertTrue(it.hasNext());
149   }
150 
151   public void testIndex_11(){
152 
153   }
154 
155   public void testIndex_12(){
156 
157   }
158 
159   /** Delete index. */
160   public void testIndex_101() throws IndexException{
161     IndexedCorpus ic = (IndexedCorpus) corpus;
162     DefaultIndexDefinition did = new DefaultIndexDefinition();
163 //    did.setIndexType(GateConstants.IR_LUCENE_INVFILE);
164     did.setIrEngineClassName(gate.creole.ir.lucene.
165                              LuceneIREngine.class.getName());
166 
167     did.setIndexLocation(TEMP_LOCATION);
168 
169     ic.setIndexDefinition(did);
170 
171     ic.getIndexManager().deleteIndex();
172   }
173 
174 
175   public static void main(String[] args){
176     try{
177       Gate.init();
178 
179       TestIndex test = new TestIndex("");
180 
181       test.setUp();
182       test.testIndex_01();
183       test.tearDown();
184 
185       test.setUp();
186       test.testIndex_02();
187       test.tearDown();
188 
189       test.setUp();
190       test.testIndex_10();
191       test.tearDown();
192 
193       test.setUp();
194       test.testIndex_101();
195       test.tearDown();
196 
197     } catch (Exception e){
198       e.printStackTrace();
199     }
200   }
201 }