From bd8bdb08b3a3ae6b2c0bc84548b5a12891ebf4e8 Mon Sep 17 00:00:00 2001 From: Uwe Schindler Date: Mon, 9 Apr 2012 16:52:14 +0000 Subject: [PATCH] LUCENE-3969: Remove code duplication git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/branches/lucene3969@1311320 13f79535-47bb-0310-9956-ffa450edef68 --- .../analysis/core/TestRandomChains.java | 184 ++++++++---------- 1 file changed, 76 insertions(+), 108 deletions(-) diff --git a/modules/analysis/common/src/test/org/apache/lucene/analysis/core/TestRandomChains.java b/modules/analysis/common/src/test/org/apache/lucene/analysis/core/TestRandomChains.java index 31fb5f24797..d49e1c001e6 100644 --- a/modules/analysis/common/src/test/org/apache/lucene/analysis/core/TestRandomChains.java +++ b/modules/analysis/common/src/test/org/apache/lucene/analysis/core/TestRandomChains.java @@ -184,6 +184,35 @@ public class TestRandomChains extends BaseTokenStreamTestCase { return (Constructor) ctor; } + private static void getClassesForPackage(String pckgname, List> classes) throws Exception { + final ClassLoader cld = TestRandomChains.class.getClassLoader(); + final String path = pckgname.replace('.', '/'); + final Enumeration resources = cld.getResources(path); + while (resources.hasMoreElements()) { + final File directory = new File(resources.nextElement().toURI()); + if (directory.exists()) { + String[] files = directory.list(); + for (String file : files) { + if (new File(directory, file).isDirectory()) { + // recurse + String subPackage = pckgname + "." + file; + getClassesForPackage(subPackage, classes); + } + if (file.endsWith(".class")) { + String clazzName = file.substring(0, file.length() - 6); + // exclude Test classes that happen to be in these packages. + // class.ForName'ing some of them can cause trouble. + if (!clazzName.endsWith("Test") && !clazzName.startsWith("Test")) { + // Don't run static initializers, as we won't use most of them. + // Java will do that automatically once accessed/instantiated. + classes.add(Class.forName(pckgname + '.' + clazzName, false, cld)); + } + } + } + } + } + } + private static interface ArgProducer { Object create(Random random); } @@ -497,8 +526,6 @@ public class TestRandomChains extends BaseTokenStreamTestCase { Random random = new Random(seed); TokenizerSpec tokenizerspec = newTokenizer(random, reader); TokenFilterSpec filterspec = newFilterChain(random, tokenizerspec.tokenizer); - //System.out.println("seed=" + seed + ",tokenizerSpec=" + tokenizerspec.toString); - //System.out.println("seed=" + seed + ",tokenfilterSpec=" + filterspec.toString); return new TokenStreamComponents(tokenizerspec.tokenizer, filterspec.stream); } @@ -506,7 +533,6 @@ public class TestRandomChains extends BaseTokenStreamTestCase { protected Reader initReader(Reader reader) { Random random = new Random(seed); CharFilterSpec charfilterspec = newCharFilterChain(random, reader); - //System.out.println("seed=" + seed + ",charFilterSpec=" + charfilterspec.toString); return charfilterspec.reader; } @@ -530,34 +556,46 @@ public class TestRandomChains extends BaseTokenStreamTestCase { return sb.toString(); } + private T createComponent(Constructor ctor, Object[] args, StringBuilder descr) { + try { + final T instance = ctor.newInstance(args); + if (descr.length() > 0) { + descr.append(","); + } + descr.append(ctor.getDeclaringClass().getName()); + String params = Arrays.toString(args); + params = params.substring(1, params.length()-1); + descr.append("(").append(params).append(")"); + return instance; + } catch (InvocationTargetException ite) { + final Throwable cause = ite.getCause(); + if (cause instanceof IllegalArgumentException || + cause instanceof UnsupportedOperationException) { + // thats ok, ignore + if (VERBOSE) { + System.err.println("Ignoring IAE/UOE from ctor:"); + cause.printStackTrace(System.err); + } + } else { + Rethrow.rethrow(cause); + } + } catch (IllegalAccessException iae) { + Rethrow.rethrow(iae); + } catch (InstantiationException ie) { + Rethrow.rethrow(ie); + } + return null; // no success + } + // create a new random tokenizer from classpath private TokenizerSpec newTokenizer(Random random, Reader reader) { TokenizerSpec spec = new TokenizerSpec(); - boolean success = false; - while (!success) { - try { - final Constructor ctor = tokenizers.get(random.nextInt(tokenizers.size())); - final Object args[] = newTokenizerArgs(random, reader, ctor.getParameterTypes()); - spec.tokenizer = ctor.newInstance(args); - spec.toString = ctor.getDeclaringClass().getName() + ("(" + Arrays.toString(args) + ")"); - success = true; - } catch (InvocationTargetException ite) { - final Throwable cause = ite.getCause(); - if (cause instanceof IllegalArgumentException || - cause instanceof UnsupportedOperationException) { - // thats ok, ignore - if (VERBOSE) { - System.err.println("Ignoring IAE/UOE from ctor:"); - cause.printStackTrace(System.err); - } - } else { - Rethrow.rethrow(cause); - } - } catch (IllegalAccessException iae) { - Rethrow.rethrow(iae); - } catch (InstantiationException ie) { - Rethrow.rethrow(ie); - } + while (spec.tokenizer == null) { + final Constructor ctor = tokenizers.get(random.nextInt(tokenizers.size())); + final StringBuilder descr = new StringBuilder(); + final Object args[] = newTokenizerArgs(random, reader, ctor.getParameterTypes()); + spec.tokenizer = createComponent(ctor, args, descr); + spec.toString = descr.toString(); } return spec; } @@ -570,33 +608,12 @@ public class TestRandomChains extends BaseTokenStreamTestCase { for (int i = 0; i < numFilters; i++) { boolean success = false; while (!success) { - try { - final Constructor ctor = charfilters.get(random.nextInt(charfilters.size())); - final Object args[] = newCharFilterArgs(random, spec.reader, ctor.getParameterTypes()); - spec.reader = ctor.newInstance(args); - - if (descr.length() > 0) { - descr.append(","); - } - descr.append(ctor.getDeclaringClass().getName()); - descr.append("(" + Arrays.toString(args) + ")"); + final Constructor ctor = charfilters.get(random.nextInt(charfilters.size())); + final Object args[] = newCharFilterArgs(random, spec.reader, ctor.getParameterTypes()); + reader = createComponent(ctor, args, descr); + if (reader != null) { success = true; - } catch (InvocationTargetException ite) { - final Throwable cause = ite.getCause(); - if (cause instanceof IllegalArgumentException || - cause instanceof UnsupportedOperationException) { - // thats ok, ignore - if (VERBOSE) { - System.err.println("Ignoring IAE/UOE from ctor:"); - cause.printStackTrace(System.err); - } - } else { - Rethrow.rethrow(cause); - } - } catch (IllegalAccessException iae) { - Rethrow.rethrow(iae); - } catch (InstantiationException ie) { - Rethrow.rethrow(ie); + spec.reader = reader; } } } @@ -612,32 +629,12 @@ public class TestRandomChains extends BaseTokenStreamTestCase { for (int i = 0; i < numFilters; i++) { boolean success = false; while (!success) { - try { - final Constructor ctor = tokenfilters.get(random.nextInt(tokenfilters.size())); - final Object args[] = newFilterArgs(random, spec.stream, ctor.getParameterTypes()); - spec.stream = ctor.newInstance(args); - if (descr.length() > 0) { - descr.append(","); - } - descr.append(ctor.getDeclaringClass().getName()); - descr.append("(" + Arrays.toString(args) + ")"); + final Constructor ctor = tokenfilters.get(random.nextInt(tokenfilters.size())); + final Object args[] = newFilterArgs(random, spec.stream, ctor.getParameterTypes()); + final TokenFilter flt = createComponent(ctor, args, descr); + if (flt != null) { success = true; - } catch (InvocationTargetException ite) { - final Throwable cause = ite.getCause(); - if (cause instanceof IllegalArgumentException || - cause instanceof UnsupportedOperationException) { - // thats ok, ignore - if (VERBOSE) { - System.err.println("Ignoring IAE/UOE from ctor:"); - cause.printStackTrace(System.err); - } - } else { - Rethrow.rethrow(cause); - } - } catch (IllegalAccessException iae) { - Rethrow.rethrow(iae); - } catch (InstantiationException ie) { - Rethrow.rethrow(ie); + spec.stream = flt; } } } @@ -676,33 +673,4 @@ public class TestRandomChains extends BaseTokenStreamTestCase { } } } - - private static void getClassesForPackage(String pckgname, List> classes) throws Exception { - final ClassLoader cld = TestRandomChains.class.getClassLoader(); - final String path = pckgname.replace('.', '/'); - final Enumeration resources = cld.getResources(path); - while (resources.hasMoreElements()) { - final File directory = new File(resources.nextElement().toURI()); - if (directory.exists()) { - String[] files = directory.list(); - for (String file : files) { - if (new File(directory, file).isDirectory()) { - // recurse - String subPackage = pckgname + "." + file; - getClassesForPackage(subPackage, classes); - } - if (file.endsWith(".class")) { - String clazzName = file.substring(0, file.length() - 6); - // exclude Test classes that happen to be in these packages. - // class.ForName'ing some of them can cause trouble. - if (!clazzName.endsWith("Test") && !clazzName.startsWith("Test")) { - // Don't run static initializers, as we won't use most of them. - // Java will do that automatically once accessed/instantiated. - classes.add(Class.forName(pckgname + '.' + clazzName, false, cld)); - } - } - } - } - } - } }