LUCENE-2510: Add a test that actually "tries" to create a instance from the factory, which currently does not work for all factories (like ResourceLoader required - we have no default Impl outside Solr, or required parameters missing). At least it tests that we get InitializationException. Unfortunatley if ResourceLoaderAware.inform() is not called, some factories NPE or similar. We should fix later and make it better useable outside Solr

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/branches/lucene2510@1365494 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Uwe Schindler 2012-07-25 09:01:51 +00:00
parent 6751eefb8e
commit 7c12d83a4c
2 changed files with 76 additions and 27 deletions

View File

@ -18,10 +18,13 @@ package org.apache.lucene.analysis.core;
*/
import java.lang.reflect.Modifier;
import java.io.Reader;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.IdentityHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.lucene.analysis.CachingTokenFilter;
@ -37,14 +40,17 @@ import org.apache.lucene.analysis.MockTokenizer;
import org.apache.lucene.analysis.MockVariableLengthPayloadFilter;
import org.apache.lucene.analysis.TokenFilter;
import org.apache.lucene.analysis.Tokenizer;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.ValidatingTokenFilter;
import org.apache.lucene.analysis.core.TestRandomChains;
import org.apache.lucene.analysis.path.ReversePathHierarchyTokenizer;
import org.apache.lucene.analysis.sinks.TeeSinkTokenFilter;
import org.apache.lucene.analysis.snowball.SnowballFilter;
import org.apache.lucene.analysis.util.CharFilterFactory;
import org.apache.lucene.analysis.util.ResourceLoaderAware;
import org.apache.lucene.analysis.util.TokenFilterFactory;
import org.apache.lucene.analysis.util.TokenizerFactory;
import org.apache.lucene.analysis.util.InitializationException;
import org.apache.lucene.util.LuceneTestCase;
/**
@ -111,16 +117,59 @@ public class TestAllAnalyzersHaveFactories extends LuceneTestCase {
if (Tokenizer.class.isAssignableFrom(c)) {
String clazzName = c.getSimpleName();
assertTrue(clazzName.endsWith("Tokenizer"));
assertNotNull(TokenizerFactory.forName(clazzName.substring(0, clazzName.length() - 9)));
String simpleName = clazzName.substring(0, clazzName.length() - 9);
TokenizerFactory instance = TokenizerFactory.forName(simpleName);
assertNotNull(instance);
try {
instance.setLuceneMatchVersion(TEST_VERSION_CURRENT);
instance.init(Collections.<String,String>emptyMap());
// TODO: provide fake ResourceLoader
if (!(instance instanceof ResourceLoaderAware)) {
assertSame(c, instance.create(new StringReader("")).getClass());
}
} catch (InitializationException e) {
// TODO: For now pass because some factories have not yet a default config that always works, some require ResourceLoader
}
} else if (TokenFilter.class.isAssignableFrom(c)) {
String clazzName = c.getSimpleName();
assertTrue(clazzName.endsWith("Filter"));
String simpleName = clazzName.substring(0, clazzName.length() - (clazzName.endsWith("TokenFilter") ? 11 : 6));
assertNotNull(TokenFilterFactory.forName(simpleName));
TokenFilterFactory instance = TokenFilterFactory.forName(simpleName);
assertNotNull(instance);
try {
instance.setLuceneMatchVersion(TEST_VERSION_CURRENT);
instance.init(Collections.<String,String>emptyMap());
// TODO: provide fake ResourceLoader
if (!(instance instanceof ResourceLoaderAware)) {
Class<? extends TokenStream> createdClazz = instance.create(new KeywordTokenizer(new StringReader(""))).getClass();
// only check instance if factory have wrapped at all!
if (KeywordTokenizer.class != createdClazz) {
assertSame(c, createdClazz);
}
}
} catch (InitializationException e) {
// TODO: For now pass because some factories have not yet a default config that always works, some require ResourceLoader
}
} else if (CharFilter.class.isAssignableFrom(c)) {
String clazzName = c.getSimpleName();
assertTrue(clazzName.endsWith("CharFilter"));
assertNotNull(CharFilterFactory.forName(clazzName.substring(0, clazzName.length() - 10)));
String simpleName = clazzName.substring(0, clazzName.length() - 10);
CharFilterFactory instance = CharFilterFactory.forName(simpleName);
assertNotNull(instance);
try {
instance.setLuceneMatchVersion(TEST_VERSION_CURRENT);
instance.init(Collections.<String,String>emptyMap());
// TODO: provide fake ResourceLoader
if (!(instance instanceof ResourceLoaderAware)) {
Class<? extends Reader> createdClazz = instance.create(new StringReader("")).getClass();
// only check instance if factory have wrapped at all!
if (StringReader.class != createdClazz) {
assertSame(c, createdClazz);
}
}
} catch (InitializationException e) {
// TODO: For now pass because some factories have not yet a default config that always works, some require ResourceLoader
}
}
}
}

View File

@ -26,9 +26,9 @@ import org.apache.lucene.util.LuceneTestCase;
public class TestAnalysisSPILoader extends LuceneTestCase {
public void testLookupTokenizer() {
assertEquals(WhitespaceTokenizerFactory.class, TokenizerFactory.forName("Whitespace").getClass());
assertEquals(WhitespaceTokenizerFactory.class, TokenizerFactory.forName("WHITESPACE").getClass());
assertEquals(WhitespaceTokenizerFactory.class, TokenizerFactory.forName("whitespace").getClass());
assertSame(WhitespaceTokenizerFactory.class, TokenizerFactory.forName("Whitespace").getClass());
assertSame(WhitespaceTokenizerFactory.class, TokenizerFactory.forName("WHITESPACE").getClass());
assertSame(WhitespaceTokenizerFactory.class, TokenizerFactory.forName("whitespace").getClass());
}
public void testBogusLookupTokenizer() {
@ -48,9 +48,9 @@ public class TestAnalysisSPILoader extends LuceneTestCase {
}
public void testLookupTokenizerClass() {
assertEquals(WhitespaceTokenizerFactory.class, TokenizerFactory.lookupClass("Whitespace"));
assertEquals(WhitespaceTokenizerFactory.class, TokenizerFactory.lookupClass("WHITESPACE"));
assertEquals(WhitespaceTokenizerFactory.class, TokenizerFactory.lookupClass("whitespace"));
assertSame(WhitespaceTokenizerFactory.class, TokenizerFactory.lookupClass("Whitespace"));
assertSame(WhitespaceTokenizerFactory.class, TokenizerFactory.lookupClass("WHITESPACE"));
assertSame(WhitespaceTokenizerFactory.class, TokenizerFactory.lookupClass("whitespace"));
}
public void testBogusLookupTokenizerClass() {
@ -74,13 +74,13 @@ public class TestAnalysisSPILoader extends LuceneTestCase {
}
public void testLookupTokenFilter() {
assertEquals(LowerCaseFilterFactory.class, TokenFilterFactory.forName("Lowercase").getClass());
assertEquals(LowerCaseFilterFactory.class, TokenFilterFactory.forName("LOWERCASE").getClass());
assertEquals(LowerCaseFilterFactory.class, TokenFilterFactory.forName("lowercase").getClass());
assertSame(LowerCaseFilterFactory.class, TokenFilterFactory.forName("Lowercase").getClass());
assertSame(LowerCaseFilterFactory.class, TokenFilterFactory.forName("LOWERCASE").getClass());
assertSame(LowerCaseFilterFactory.class, TokenFilterFactory.forName("lowercase").getClass());
assertEquals(RemoveDuplicatesTokenFilterFactory.class, TokenFilterFactory.forName("RemoveDuplicates").getClass());
assertEquals(RemoveDuplicatesTokenFilterFactory.class, TokenFilterFactory.forName("REMOVEDUPLICATES").getClass());
assertEquals(RemoveDuplicatesTokenFilterFactory.class, TokenFilterFactory.forName("removeduplicates").getClass());
assertSame(RemoveDuplicatesTokenFilterFactory.class, TokenFilterFactory.forName("RemoveDuplicates").getClass());
assertSame(RemoveDuplicatesTokenFilterFactory.class, TokenFilterFactory.forName("REMOVEDUPLICATES").getClass());
assertSame(RemoveDuplicatesTokenFilterFactory.class, TokenFilterFactory.forName("removeduplicates").getClass());
}
public void testBogusLookupTokenFilter() {
@ -100,13 +100,13 @@ public class TestAnalysisSPILoader extends LuceneTestCase {
}
public void testLookupTokenFilterClass() {
assertEquals(LowerCaseFilterFactory.class, TokenFilterFactory.lookupClass("Lowercase"));
assertEquals(LowerCaseFilterFactory.class, TokenFilterFactory.lookupClass("LOWERCASE"));
assertEquals(LowerCaseFilterFactory.class, TokenFilterFactory.lookupClass("lowercase"));
assertSame(LowerCaseFilterFactory.class, TokenFilterFactory.lookupClass("Lowercase"));
assertSame(LowerCaseFilterFactory.class, TokenFilterFactory.lookupClass("LOWERCASE"));
assertSame(LowerCaseFilterFactory.class, TokenFilterFactory.lookupClass("lowercase"));
assertEquals(RemoveDuplicatesTokenFilterFactory.class, TokenFilterFactory.lookupClass("RemoveDuplicates"));
assertEquals(RemoveDuplicatesTokenFilterFactory.class, TokenFilterFactory.lookupClass("REMOVEDUPLICATES"));
assertEquals(RemoveDuplicatesTokenFilterFactory.class, TokenFilterFactory.lookupClass("removeduplicates"));
assertSame(RemoveDuplicatesTokenFilterFactory.class, TokenFilterFactory.lookupClass("RemoveDuplicates"));
assertSame(RemoveDuplicatesTokenFilterFactory.class, TokenFilterFactory.lookupClass("REMOVEDUPLICATES"));
assertSame(RemoveDuplicatesTokenFilterFactory.class, TokenFilterFactory.lookupClass("removeduplicates"));
}
public void testBogusLookupTokenFilterClass() {
@ -131,9 +131,9 @@ public class TestAnalysisSPILoader extends LuceneTestCase {
}
public void testLookupCharFilter() {
assertEquals(HTMLStripCharFilterFactory.class, CharFilterFactory.forName("HTMLStrip").getClass());
assertEquals(HTMLStripCharFilterFactory.class, CharFilterFactory.forName("HTMLSTRIP").getClass());
assertEquals(HTMLStripCharFilterFactory.class, CharFilterFactory.forName("htmlstrip").getClass());
assertSame(HTMLStripCharFilterFactory.class, CharFilterFactory.forName("HTMLStrip").getClass());
assertSame(HTMLStripCharFilterFactory.class, CharFilterFactory.forName("HTMLSTRIP").getClass());
assertSame(HTMLStripCharFilterFactory.class, CharFilterFactory.forName("htmlstrip").getClass());
}
public void testBogusLookupCharFilter() {
@ -153,9 +153,9 @@ public class TestAnalysisSPILoader extends LuceneTestCase {
}
public void testLookupCharFilterClass() {
assertEquals(HTMLStripCharFilterFactory.class, CharFilterFactory.lookupClass("HTMLStrip"));
assertEquals(HTMLStripCharFilterFactory.class, CharFilterFactory.lookupClass("HTMLSTRIP"));
assertEquals(HTMLStripCharFilterFactory.class, CharFilterFactory.lookupClass("htmlstrip"));
assertSame(HTMLStripCharFilterFactory.class, CharFilterFactory.lookupClass("HTMLStrip"));
assertSame(HTMLStripCharFilterFactory.class, CharFilterFactory.lookupClass("HTMLSTRIP"));
assertSame(HTMLStripCharFilterFactory.class, CharFilterFactory.lookupClass("htmlstrip"));
}
public void testBogusLookupCharFilterClass() {