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  
39    /** Fixture set up */
40    public void setUp() throws Exception {
41      try {
42        File storageDir = File.createTempFile("TestIndex__", "__StorageDir");
43  
44        if (null == TEMP_LOCATION) {
45          File indexDir = File.createTempFile("LuceneIndex__", "__Dir");
46          TEMP_LOCATION = indexDir.getAbsolutePath();
47        }
48  
49  //System.out.println("temp=["+TEMP_LOCATION+"]");
50  //System.out.println("temp2=["+indexDir.getAbsoluteFile()+"]");
51  
52        storageDir.delete();
53        // create and open a serial data store
54        sds = Factory.createDataStore(
55          "gate.persist.SerialDataStore", storageDir.toURL().toString()
56        );
57  
58        sds.open();
59  
60        String server = TestDocument.getTestServerName();
61  
62        Document doc0 = Factory.newDocument(new URL(server + "tests/doc0.html"));
63        doc0.getFeatures().put("author","John Smit");
64  
65        Corpus corp = Factory.newCorpus("LuceneTestCorpus");
66        corp.add(doc0);
67        corpus = (Corpus) sds.adopt(corp,null);
68        sds.sync(corpus);
69  
70      } catch (Exception e) {
71        e.printStackTrace();
72        throw new GateException(e.getMessage());
73      }
74    } // setUp
75  
76    /** Put things back as they should be after running tests
77      * (reinitialise the CREOLE register).
78      */
79    public void tearDown() throws Exception {
80      sds.delete();
81    } // tearDown
82  
83    /** Test suite routine for the test runner */
84    public static Test suite() {
85      return new TestSuite(TestIndex.class);
86    } // suite
87  
88    /** Create new index. */
89    public void testIndex_01() throws IndexException{
90      IndexedCorpus ic = (IndexedCorpus) corpus;
91      DefaultIndexDefinition did = new DefaultIndexDefinition();
92      did.setIrEngineClassName(gate.creole.ir.lucene.
93                               LuceneIREngine.class.getName());
94  
95  //    did.setIndexType(GateConstants.IR_LUCENE_INVFILE);
96  
97      did.setIndexLocation(TEMP_LOCATION);
98      did.addIndexField(new IndexField("content", new DocumentContentReader(), false));
99      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 = (IndexedCorpus) corpus;
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 = (IndexedCorpus) corpus;
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");
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 = (IndexedCorpus) corpus;
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 }