TestLuceneDictionarypublic class TestLuceneDictionary extends TestCase Test case for LuceneDictionary.
It first creates a simple index and then a couple of instances of LuceneDictionary
on different fields and checks if all the right text comes back. |
Fields Summary |
---|
private Directory | store | private IndexReader | indexReader | private LuceneDictionary | ld | private Iterator | it |
Methods Summary |
---|
public void | setUp()
IndexWriter writer = new IndexWriter(store, new WhitespaceAnalyzer(), true);
Document doc;
doc = new Document();
doc.add(new Field("aaa", "foo", Field.Store.YES, Field.Index.TOKENIZED));
writer.addDocument(doc);
doc = new Document();
doc.add(new Field("aaa", "foo", Field.Store.YES, Field.Index.TOKENIZED));
writer.addDocument(doc);
doc = new Document();
doc.add(new Field("contents", "Tom", Field.Store.YES, Field.Index.TOKENIZED));
writer.addDocument(doc);
doc = new Document();
doc.add(new Field("contents", "Jerry", Field.Store.YES, Field.Index.TOKENIZED));
writer.addDocument(doc);
doc = new Document();
doc.add(new Field("zzz", "bar", Field.Store.YES, Field.Index.TOKENIZED));
writer.addDocument(doc);
writer.optimize();
writer.close();
| public void | testFieldAaa()
try {
indexReader = IndexReader.open(store);
ld = new LuceneDictionary(indexReader, "aaa");
it = ld.getWordsIterator();
assertTrue("First element doesn't exist.", it.hasNext());
assertTrue("First element isn't correct", it.next().equals("foo"));
assertFalse("More elements than expected", it.hasNext());
assertTrue("Nonexistent element is really null", it.next() == null);
} finally {
if (indexReader != null) { indexReader.close(); }
}
| public void | testFieldContents_1()
try {
indexReader = IndexReader.open(store);
ld = new LuceneDictionary(indexReader, "contents");
it = ld.getWordsIterator();
assertTrue("First element doesn't exist.", it.hasNext());
assertTrue("First element isn't correct", it.next().equals("Jerry"));
assertTrue("Second element doesn't exist.", it.hasNext());
assertTrue("Second element isn't correct", it.next().equals("Tom"));
assertFalse("More elements than expected", it.hasNext());
assertTrue("Nonexistent element is really null", it.next() == null);
ld = new LuceneDictionary(indexReader, "contents");
it = ld.getWordsIterator();
int counter = 2;
while (it.hasNext()) {
it.next();
counter--;
}
assertTrue("Number of words incorrect", counter == 0);
}
finally {
if (indexReader != null) { indexReader.close(); }
}
| public void | testFieldContents_2()
try {
indexReader = IndexReader.open(store);
ld = new LuceneDictionary(indexReader, "contents");
it = ld.getWordsIterator();
// hasNext() should have no side effects
assertTrue("First element isn't were it should be.", it.hasNext());
assertTrue("First element isn't were it should be.", it.hasNext());
assertTrue("First element isn't were it should be.", it.hasNext());
// just iterate through words
assertTrue("First element isn't correct", it.next().equals("Jerry"));
assertTrue("Second element isn't correct", it.next().equals("Tom"));
assertTrue("Nonexistent element is really null", it.next() == null);
// hasNext() should still have no side effects ...
assertFalse("There should be any more elements", it.hasNext());
assertFalse("There should be any more elements", it.hasNext());
assertFalse("There should be any more elements", it.hasNext());
// .. and there are really no more words
assertTrue("Nonexistent element is really null", it.next() == null);
assertTrue("Nonexistent element is really null", it.next() == null);
assertTrue("Nonexistent element is really null", it.next() == null);
}
finally {
if (indexReader != null) { indexReader.close(); }
}
| public void | testFieldNonExistent()
try {
indexReader = IndexReader.open(store);
ld = new LuceneDictionary(indexReader, "nonexistent_field");
it = ld.getWordsIterator();
assertFalse("More elements than expected", it.hasNext());
assertTrue("Nonexistent element is really null", it.next() == null);
} finally {
if (indexReader != null) { indexReader.close(); }
}
| public void | testFieldZzz()
try {
indexReader = IndexReader.open(store);
ld = new LuceneDictionary(indexReader, "zzz");
it = ld.getWordsIterator();
assertTrue("First element doesn't exist.", it.hasNext());
assertTrue("First element isn't correct", it.next().equals("bar"));
assertFalse("More elements than expected", it.hasNext());
assertTrue("Nonexistent element is really null", it.next() == null);
}
finally {
if (indexReader != null) { indexReader.close(); }
}
| public void | testSpellchecker()
SpellChecker sc = new SpellChecker(new RAMDirectory());
indexReader = IndexReader.open(store);
sc.indexDictionary(new LuceneDictionary(indexReader, "contents"));
String[] suggestions = sc.suggestSimilar("Tam", 1);
assertEquals(1, suggestions.length);
assertEquals("Tom", suggestions[0]);
suggestions = sc.suggestSimilar("Jarry", 1);
assertEquals(1, suggestions.length);
assertEquals("Jerry", suggestions[0]);
indexReader.close();
|
|