Fix test failure caused in Maven, because test-framework classes are packaged in JAR.

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1372218 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Uwe Schindler 2012-08-13 00:01:59 +00:00
parent d4e264648a
commit e1295dc3d8
3 changed files with 23 additions and 8 deletions

View File

@ -100,8 +100,7 @@ public class TestAllAnalyzersHaveFactories extends LuceneTestCase {
private static final ResourceLoader loader = new StringMockResourceLoader(""); private static final ResourceLoader loader = new StringMockResourceLoader("");
public void test() throws Exception { public void test() throws Exception {
List<Class<?>> analysisClasses = new ArrayList<Class<?>>(); List<Class<?>> analysisClasses = TestRandomChains.getClassesForPackage("org.apache.lucene.analysis");
TestRandomChains.getClassesForPackage("org.apache.lucene.analysis", analysisClasses);
for (final Class<?> c : analysisClasses) { for (final Class<?> c : analysisClasses) {
final int modifiers = c.getModifiers(); final int modifiers = c.getModifiers();

View File

@ -25,6 +25,7 @@ import java.io.StringReader;
import java.lang.reflect.Constructor; import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Modifier; import java.lang.reflect.Modifier;
import java.net.URI;
import java.net.URL; import java.net.URL;
import java.nio.CharBuffer; import java.nio.CharBuffer;
import java.util.ArrayList; import java.util.ArrayList;
@ -165,8 +166,7 @@ public class TestRandomChains extends BaseTokenStreamTestCase {
@BeforeClass @BeforeClass
public static void beforeClass() throws Exception { public static void beforeClass() throws Exception {
List<Class<?>> analysisClasses = new ArrayList<Class<?>>(); List<Class<?>> analysisClasses = getClassesForPackage("org.apache.lucene.analysis");
getClassesForPackage("org.apache.lucene.analysis", analysisClasses);
tokenizers = new ArrayList<Constructor<? extends Tokenizer>>(); tokenizers = new ArrayList<Constructor<? extends Tokenizer>>();
tokenfilters = new ArrayList<Constructor<? extends TokenFilter>>(); tokenfilters = new ArrayList<Constructor<? extends TokenFilter>>();
charfilters = new ArrayList<Constructor<? extends CharFilter>>(); charfilters = new ArrayList<Constructor<? extends CharFilter>>();
@ -235,19 +235,30 @@ public class TestRandomChains extends BaseTokenStreamTestCase {
private static <T> Constructor<T> castConstructor(Class<T> instanceClazz, Constructor<?> ctor) { private static <T> Constructor<T> castConstructor(Class<T> instanceClazz, Constructor<?> ctor) {
return (Constructor<T>) ctor; return (Constructor<T>) ctor;
} }
static void getClassesForPackage(String pckgname, List<Class<?>> classes) throws Exception {
public static List<Class<?>> getClassesForPackage(String pckgname) throws Exception {
final List<Class<?>> classes = new ArrayList<Class<?>>();
collectClassesForPackage(pckgname, classes);
assertFalse("No classes found in package '"+pckgname+"'; maybe your test classes are packaged as JAR file?", classes.isEmpty());
return classes;
}
private static void collectClassesForPackage(String pckgname, List<Class<?>> classes) throws Exception {
final ClassLoader cld = TestRandomChains.class.getClassLoader(); final ClassLoader cld = TestRandomChains.class.getClassLoader();
final String path = pckgname.replace('.', '/'); final String path = pckgname.replace('.', '/');
final Enumeration<URL> resources = cld.getResources(path); final Enumeration<URL> resources = cld.getResources(path);
while (resources.hasMoreElements()) { while (resources.hasMoreElements()) {
final File directory = new File(resources.nextElement().toURI()); final URI uri = resources.nextElement().toURI();
if (!"file".equalsIgnoreCase(uri.getScheme()))
continue;
final File directory = new File(uri);
if (directory.exists()) { if (directory.exists()) {
String[] files = directory.list(); String[] files = directory.list();
for (String file : files) { for (String file : files) {
if (new File(directory, file).isDirectory()) { if (new File(directory, file).isDirectory()) {
// recurse // recurse
String subPackage = pckgname + "." + file; String subPackage = pckgname + "." + file;
getClassesForPackage(subPackage, classes); collectClassesForPackage(subPackage, classes);
} }
if (file.endsWith(".class")) { if (file.endsWith(".class")) {
String clazzName = file.substring(0, file.length() - 6); String clazzName = file.substring(0, file.length() - 6);

View File

@ -25,6 +25,7 @@ import org.apache.solr.highlight.DefaultSolrHighlighter;
import org.apache.solr.search.LRUCache; import org.apache.solr.search.LRUCache;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import java.io.File; import java.io.File;
import java.net.URI;
import java.net.URL; import java.net.URL;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Enumeration; import java.util.Enumeration;
@ -94,7 +95,10 @@ public class SolrInfoMBeanTest extends SolrTestCaseJ4
String path = pckgname.replace('.', '/'); String path = pckgname.replace('.', '/');
Enumeration<URL> resources = cld.getResources(path); Enumeration<URL> resources = cld.getResources(path);
while (resources.hasMoreElements()) { while (resources.hasMoreElements()) {
final File f = new File(resources.nextElement().toURI()); final URI uri = resources.nextElement().toURI();
if (!"file".equalsIgnoreCase(uri.getScheme()))
continue;
final File f = new File(uri);
directories.add(f); directories.add(f);
} }
@ -114,6 +118,7 @@ public class SolrInfoMBeanTest extends SolrTestCaseJ4
} }
} }
} }
assertFalse("No classes found in package '"+pckgname+"'; maybe your test classes are packaged as JAR file?", classes.isEmpty());
return classes; return classes;
} }
} }