1
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
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
51 storageDir.delete();
52 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 }
75
78 public void tearDown() throws Exception {
79 sds.delete();
80 }
82
83 public static Test suite() {
84 return new TestSuite(TestIndex.class);
85 }
87
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
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
108 public void testIndex_02() throws IndexException{
109 IndexedCorpus ic = (IndexedCorpus) corpus;
110 DefaultIndexDefinition did = new DefaultIndexDefinition();
111 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
124 public void testIndex_10() throws IndexException, SearchException{
125 IndexedCorpus ic = (IndexedCorpus) corpus;
126 DefaultIndexDefinition did = new DefaultIndexDefinition();
127 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 Assert.assertTrue(it.hasNext());
146 }
147
148 public void testIndex_11(){
149
150 }
151
152 public void testIndex_12(){
153
154 }
155
156
157 public void testIndex_101() throws IndexException{
158 IndexedCorpus ic = (IndexedCorpus) corpus;
159 DefaultIndexDefinition did = new DefaultIndexDefinition();
160 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 }