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