1 package org.apache.lucene.search.spell;
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 import java.io.IOException;
21 import java.util.Iterator;
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;
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.
38 public class TestLuceneDictionary extends LuceneTestCase {
40 private Directory store;
42 private IndexReader indexReader = null;
43 private LuceneDictionary ld;
44 private Iterator<String> it;
47 public void setUp() throws Exception {
49 store = newDirectory();
50 IndexWriter writer = new IndexWriter(store, newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random, MockTokenizer.WHITESPACE, false)));
55 doc.add(newField("aaa", "foo", Field.Store.YES, Field.Index.ANALYZED));
56 writer.addDocument(doc);
59 doc.add(newField("aaa", "foo", Field.Store.YES, Field.Index.ANALYZED));
60 writer.addDocument(doc);
63 doc.add(new Field("contents", "Tom", Field.Store.YES, Field.Index.ANALYZED));
64 writer.addDocument(doc);
67 doc.add(new Field("contents", "Jerry", Field.Store.YES, Field.Index.ANALYZED));
68 writer.addDocument(doc);
71 doc.add(newField("zzz", "bar", Field.Store.YES, Field.Index.ANALYZED));
72 writer.addDocument(doc);
79 public void tearDown() throws Exception {
80 if (indexReader != null)
86 public void testFieldNonExistent() throws IOException {
88 indexReader = IndexReader.open(store, true);
90 ld = new LuceneDictionary(indexReader, "nonexistent_field");
91 it = ld.getWordsIterator();
93 assertFalse("More elements than expected", it.hasNext());
94 assertTrue("Nonexistent element is really null", it.next() == null);
96 if (indexReader != null) { indexReader.close(); }
100 public void testFieldAaa() throws IOException {
102 indexReader = IndexReader.open(store, true);
104 ld = new LuceneDictionary(indexReader, "aaa");
105 it = ld.getWordsIterator();
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);
112 if (indexReader != null) { indexReader.close(); }
116 public void testFieldContents_1() throws IOException {
118 indexReader = IndexReader.open(store, true);
120 ld = new LuceneDictionary(indexReader, "contents");
121 it = ld.getWordsIterator();
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);
130 ld = new LuceneDictionary(indexReader, "contents");
131 it = ld.getWordsIterator();
134 while (it.hasNext()) {
139 assertTrue("Number of words incorrect", counter == 0);
142 if (indexReader != null) { indexReader.close(); }
146 public void testFieldContents_2() throws IOException {
148 indexReader = IndexReader.open(store, true);
150 ld = new LuceneDictionary(indexReader, "contents");
151 it = ld.getWordsIterator();
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());
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);
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());
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);
174 if (indexReader != null) { indexReader.close(); }
178 public void testFieldZzz() throws IOException {
180 indexReader = IndexReader.open(store, true);
182 ld = new LuceneDictionary(indexReader, "zzz");
183 it = ld.getWordsIterator();
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);
191 if (indexReader != null) { indexReader.close(); }
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]);