add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / spellchecker / src / test / org / apache / lucene / search / spell / TestLuceneDictionary.java
1 package org.apache.lucene.search.spell;
2
3 /**
4  * Licensed to the Apache Software Foundation (ASF) under one or more
5  * contributor license agreements.  See the NOTICE file distributed with
6  * this work for additional information regarding copyright ownership.
7  * The ASF licenses this file to You under the Apache License, Version 2.0
8  * (the "License"); you may not use this file except in compliance with
9  * the License.  You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 import java.io.IOException;
21 import java.util.Iterator;
22
23 import org.apache.lucene.analysis.MockAnalyzer;
24 import org.apache.lucene.analysis.MockTokenizer;
25 import org.apache.lucene.analysis.WhitespaceAnalyzer;
26 import org.apache.lucene.document.Document;
27 import org.apache.lucene.document.Field;
28 import org.apache.lucene.index.IndexReader;
29 import org.apache.lucene.index.IndexWriter;
30 import org.apache.lucene.store.Directory;
31 import org.apache.lucene.util.LuceneTestCase;
32
33 /**
34  * Test case for LuceneDictionary.
35  * It first creates a simple index and then a couple of instances of LuceneDictionary
36  * on different fields and checks if all the right text comes back.
37  */
38 public class TestLuceneDictionary extends LuceneTestCase {
39
40   private Directory store;
41
42   private IndexReader indexReader = null;
43   private LuceneDictionary ld;
44   private Iterator<String> it;
45
46   @Override
47   public void setUp() throws Exception {
48     super.setUp();
49     store = newDirectory();
50     IndexWriter writer = new IndexWriter(store, newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random, MockTokenizer.WHITESPACE, false)));
51
52     Document doc;
53
54     doc = new  Document();
55     doc.add(newField("aaa", "foo", Field.Store.YES, Field.Index.ANALYZED));
56     writer.addDocument(doc);
57
58     doc = new  Document();
59     doc.add(newField("aaa", "foo", Field.Store.YES, Field.Index.ANALYZED));
60     writer.addDocument(doc);
61
62     doc = new  Document();
63     doc.add(new  Field("contents", "Tom", Field.Store.YES, Field.Index.ANALYZED));
64     writer.addDocument(doc);
65
66     doc = new  Document();
67     doc.add(new  Field("contents", "Jerry", Field.Store.YES, Field.Index.ANALYZED));
68     writer.addDocument(doc);
69
70     doc = new Document();
71     doc.add(newField("zzz", "bar", Field.Store.YES, Field.Index.ANALYZED));
72     writer.addDocument(doc);
73
74     writer.optimize();
75     writer.close();
76   }
77
78   @Override
79   public void tearDown() throws Exception {
80     if (indexReader != null)
81       indexReader.close();
82     store.close();
83     super.tearDown();
84   }
85   
86   public void testFieldNonExistent() throws IOException {
87     try {
88       indexReader = IndexReader.open(store, true);
89
90       ld = new LuceneDictionary(indexReader, "nonexistent_field");
91       it = ld.getWordsIterator();
92
93       assertFalse("More elements than expected", it.hasNext());
94       assertTrue("Nonexistent element is really null", it.next() == null);
95     } finally {
96       if  (indexReader != null) { indexReader.close(); }
97     }
98   }
99
100   public void testFieldAaa() throws IOException {
101     try {
102       indexReader = IndexReader.open(store, true);
103
104       ld = new LuceneDictionary(indexReader, "aaa");
105       it = ld.getWordsIterator();
106
107       assertTrue("First element doesn't exist.", it.hasNext());
108       assertTrue("First element isn't correct", it.next().equals("foo"));
109       assertFalse("More elements than expected", it.hasNext());
110       assertTrue("Nonexistent element is really null", it.next() == null);
111     } finally {
112       if  (indexReader != null) { indexReader.close(); }
113     }
114   }
115
116   public void testFieldContents_1() throws IOException {
117     try {
118       indexReader = IndexReader.open(store, true);
119
120       ld = new LuceneDictionary(indexReader, "contents");
121       it = ld.getWordsIterator();
122
123       assertTrue("First element doesn't exist.", it.hasNext());
124       assertTrue("First element isn't correct", it.next().equals("Jerry"));
125       assertTrue("Second element doesn't exist.", it.hasNext());
126       assertTrue("Second element isn't correct", it.next().equals("Tom"));
127       assertFalse("More elements than expected", it.hasNext());
128       assertTrue("Nonexistent element is really null", it.next() == null);
129
130       ld = new LuceneDictionary(indexReader, "contents");
131       it = ld.getWordsIterator();
132
133       int counter = 2;
134       while (it.hasNext()) {
135         it.next();
136         counter--;
137       }
138
139       assertTrue("Number of words incorrect", counter == 0);
140     }
141     finally {
142       if  (indexReader != null) { indexReader.close(); }
143     }
144   }
145
146   public void testFieldContents_2() throws IOException {
147     try {
148       indexReader = IndexReader.open(store, true);
149
150       ld = new LuceneDictionary(indexReader, "contents");
151       it = ld.getWordsIterator();
152
153       // hasNext() should have no side effects
154       assertTrue("First element isn't were it should be.", it.hasNext());
155       assertTrue("First element isn't were it should be.", it.hasNext());
156       assertTrue("First element isn't were it should be.", it.hasNext());
157
158       // just iterate through words
159       assertTrue("First element isn't correct", it.next().equals("Jerry"));
160       assertTrue("Second element isn't correct", it.next().equals("Tom"));
161       assertTrue("Nonexistent element is really null", it.next() == null);
162
163       // hasNext() should still have no side effects ...
164       assertFalse("There should be any more elements", it.hasNext());
165       assertFalse("There should be any more elements", it.hasNext());
166       assertFalse("There should be any more elements", it.hasNext());
167
168       // .. and there are really no more words
169       assertTrue("Nonexistent element is really null", it.next() == null);
170       assertTrue("Nonexistent element is really null", it.next() == null);
171       assertTrue("Nonexistent element is really null", it.next() == null);
172     }
173     finally {
174       if  (indexReader != null) { indexReader.close(); }
175     }
176   }
177
178   public void testFieldZzz() throws IOException {
179     try {
180       indexReader = IndexReader.open(store, true);
181
182       ld = new LuceneDictionary(indexReader, "zzz");
183       it = ld.getWordsIterator();
184
185       assertTrue("First element doesn't exist.", it.hasNext());
186       assertTrue("First element isn't correct", it.next().equals("bar"));
187       assertFalse("More elements than expected", it.hasNext());
188       assertTrue("Nonexistent element is really null", it.next() == null);
189     }
190     finally {
191       if  (indexReader != null) { indexReader.close(); }
192     }
193   }
194   
195   public void testSpellchecker() throws IOException {
196     Directory dir = newDirectory();
197     SpellChecker sc = new SpellChecker(dir);
198     indexReader = IndexReader.open(store, true);
199     sc.indexDictionary(new LuceneDictionary(indexReader, "contents"));
200     String[] suggestions = sc.suggestSimilar("Tam", 1);
201     assertEquals(1, suggestions.length);
202     assertEquals("Tom", suggestions[0]);
203     suggestions = sc.suggestSimilar("Jarry", 1);
204     assertEquals(1, suggestions.length);
205     assertEquals("Jerry", suggestions[0]);
206     indexReader.close();
207     sc.close();
208     dir.close();
209   }
210   
211 }