001: package org.apache.lucene.search.spell;
002:
003: /**
004: * Licensed to the Apache Software Foundation (ASF) under one or more
005: * contributor license agreements. See the NOTICE file distributed with
006: * this work for additional information regarding copyright ownership.
007: * The ASF licenses this file to You under the Apache License, Version 2.0
008: * (the "License"); you may not use this file except in compliance with
009: * the License. You may obtain a copy of the License at
010: *
011: * http://www.apache.org/licenses/LICENSE-2.0
012: *
013: * Unless required by applicable law or agreed to in writing, software
014: * distributed under the License is distributed on an "AS IS" BASIS,
015: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016: * See the License for the specific language governing permissions and
017: * limitations under the License.
018: */
019:
020: import java.io.IOException;
021: import java.util.Iterator;
022:
023: import junit.framework.TestCase;
024:
025: import org.apache.lucene.analysis.WhitespaceAnalyzer;
026: import org.apache.lucene.document.Document;
027: import org.apache.lucene.document.Field;
028: import org.apache.lucene.index.IndexReader;
029: import org.apache.lucene.index.IndexWriter;
030: import org.apache.lucene.store.Directory;
031: import org.apache.lucene.store.RAMDirectory;
032:
033: /**
034: * Test case for LuceneDictionary.
035: * It first creates a simple index and then a couple of instances of LuceneDictionary
036: * on different fields and checks if all the right text comes back.
037: *
038: * @author Christian Mallwitz
039: */
040: public class TestLuceneDictionary extends TestCase {
041:
042: private Directory store = new RAMDirectory();
043:
044: private IndexReader indexReader = null;
045:
046: private LuceneDictionary ld;
047: private Iterator it;
048:
049: public void setUp() throws Exception {
050:
051: IndexWriter writer = new IndexWriter(store,
052: new WhitespaceAnalyzer(), true);
053:
054: Document doc;
055:
056: doc = new Document();
057: doc.add(new Field("aaa", "foo", Field.Store.YES,
058: Field.Index.TOKENIZED));
059: writer.addDocument(doc);
060:
061: doc = new Document();
062: doc.add(new Field("aaa", "foo", Field.Store.YES,
063: Field.Index.TOKENIZED));
064: writer.addDocument(doc);
065:
066: doc = new Document();
067: doc.add(new Field("contents", "Tom", Field.Store.YES,
068: Field.Index.TOKENIZED));
069: writer.addDocument(doc);
070:
071: doc = new Document();
072: doc.add(new Field("contents", "Jerry", Field.Store.YES,
073: Field.Index.TOKENIZED));
074: writer.addDocument(doc);
075:
076: doc = new Document();
077: doc.add(new Field("zzz", "bar", Field.Store.YES,
078: Field.Index.TOKENIZED));
079: writer.addDocument(doc);
080:
081: writer.optimize();
082: writer.close();
083: }
084:
085: public void testFieldNonExistent() throws IOException {
086: try {
087: indexReader = IndexReader.open(store);
088:
089: ld = new LuceneDictionary(indexReader, "nonexistent_field");
090: it = ld.getWordsIterator();
091:
092: assertFalse("More elements than expected", it.hasNext());
093: assertTrue("Nonexistent element is really null",
094: it.next() == null);
095: } finally {
096: if (indexReader != null) {
097: indexReader.close();
098: }
099: }
100: }
101:
102: public void testFieldAaa() throws IOException {
103: try {
104: indexReader = IndexReader.open(store);
105:
106: ld = new LuceneDictionary(indexReader, "aaa");
107: it = ld.getWordsIterator();
108:
109: assertTrue("First element doesn't exist.", it.hasNext());
110: assertTrue("First element isn't correct", it.next().equals(
111: "foo"));
112: assertFalse("More elements than expected", it.hasNext());
113: assertTrue("Nonexistent element is really null",
114: it.next() == null);
115: } finally {
116: if (indexReader != null) {
117: indexReader.close();
118: }
119: }
120: }
121:
122: public void testFieldContents_1() throws IOException {
123: try {
124: indexReader = IndexReader.open(store);
125:
126: ld = new LuceneDictionary(indexReader, "contents");
127: it = ld.getWordsIterator();
128:
129: assertTrue("First element doesn't exist.", it.hasNext());
130: assertTrue("First element isn't correct", it.next().equals(
131: "Jerry"));
132: assertTrue("Second element doesn't exist.", it.hasNext());
133: assertTrue("Second element isn't correct", it.next()
134: .equals("Tom"));
135: assertFalse("More elements than expected", it.hasNext());
136: assertTrue("Nonexistent element is really null",
137: it.next() == null);
138:
139: ld = new LuceneDictionary(indexReader, "contents");
140: it = ld.getWordsIterator();
141:
142: int counter = 2;
143: while (it.hasNext()) {
144: it.next();
145: counter--;
146: }
147:
148: assertTrue("Number of words incorrect", counter == 0);
149: } finally {
150: if (indexReader != null) {
151: indexReader.close();
152: }
153: }
154: }
155:
156: public void testFieldContents_2() throws IOException {
157: try {
158: indexReader = IndexReader.open(store);
159:
160: ld = new LuceneDictionary(indexReader, "contents");
161: it = ld.getWordsIterator();
162:
163: // hasNext() should have no side effects
164: assertTrue("First element isn't were it should be.", it
165: .hasNext());
166: assertTrue("First element isn't were it should be.", it
167: .hasNext());
168: assertTrue("First element isn't were it should be.", it
169: .hasNext());
170:
171: // just iterate through words
172: assertTrue("First element isn't correct", it.next().equals(
173: "Jerry"));
174: assertTrue("Second element isn't correct", it.next()
175: .equals("Tom"));
176: assertTrue("Nonexistent element is really null",
177: it.next() == null);
178:
179: // hasNext() should still have no side effects ...
180: assertFalse("There should be any more elements", it
181: .hasNext());
182: assertFalse("There should be any more elements", it
183: .hasNext());
184: assertFalse("There should be any more elements", it
185: .hasNext());
186:
187: // .. and there are really no more words
188: assertTrue("Nonexistent element is really null",
189: it.next() == null);
190: assertTrue("Nonexistent element is really null",
191: it.next() == null);
192: assertTrue("Nonexistent element is really null",
193: it.next() == null);
194: } finally {
195: if (indexReader != null) {
196: indexReader.close();
197: }
198: }
199: }
200:
201: public void testFieldZzz() throws IOException {
202: try {
203: indexReader = IndexReader.open(store);
204:
205: ld = new LuceneDictionary(indexReader, "zzz");
206: it = ld.getWordsIterator();
207:
208: assertTrue("First element doesn't exist.", it.hasNext());
209: assertTrue("First element isn't correct", it.next().equals(
210: "bar"));
211: assertFalse("More elements than expected", it.hasNext());
212: assertTrue("Nonexistent element is really null",
213: it.next() == null);
214: } finally {
215: if (indexReader != null) {
216: indexReader.close();
217: }
218: }
219: }
220:
221: public void testSpellchecker() throws IOException {
222: SpellChecker sc = new SpellChecker(new RAMDirectory());
223: indexReader = IndexReader.open(store);
224: sc
225: .indexDictionary(new LuceneDictionary(indexReader,
226: "contents"));
227: String[] suggestions = sc.suggestSimilar("Tam", 1);
228: assertEquals(1, suggestions.length);
229: assertEquals("Tom", suggestions[0]);
230: suggestions = sc.suggestSimilar("Jarry", 1);
231: assertEquals(1, suggestions.length);
232: assertEquals("Jerry", suggestions[0]);
233: indexReader.close();
234: }
235:
236: }
|