You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by us...@apache.org on 2012/04/09 18:52:15 UTC

svn commit: r1311320 - /lucene/dev/branches/lucene3969/modules/analysis/common/src/test/org/apache/lucene/analysis/core/TestRandomChains.java

Author: uschindler
Date: Mon Apr  9 16:52:14 2012
New Revision: 1311320

URL: http://svn.apache.org/viewvc?rev=1311320&view=rev
Log:
LUCENE-3969: Remove code duplication

Modified:
    lucene/dev/branches/lucene3969/modules/analysis/common/src/test/org/apache/lucene/analysis/core/TestRandomChains.java

Modified: lucene/dev/branches/lucene3969/modules/analysis/common/src/test/org/apache/lucene/analysis/core/TestRandomChains.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/lucene3969/modules/analysis/common/src/test/org/apache/lucene/analysis/core/TestRandomChains.java?rev=1311320&r1=1311319&r2=1311320&view=diff
==============================================================================
--- lucene/dev/branches/lucene3969/modules/analysis/common/src/test/org/apache/lucene/analysis/core/TestRandomChains.java (original)
+++ lucene/dev/branches/lucene3969/modules/analysis/common/src/test/org/apache/lucene/analysis/core/TestRandomChains.java Mon Apr  9 16:52:14 2012
@@ -184,6 +184,35 @@ public class TestRandomChains extends Ba
     return (Constructor<? extends T>) ctor;
   }
   
+  private static void getClassesForPackage(String pckgname, List<Class<?>> classes) throws Exception {
+    final ClassLoader cld = TestRandomChains.class.getClassLoader();
+    final String path = pckgname.replace('.', '/');
+    final Enumeration<URL> 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 Ba
       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 Ba
     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 Ba
       return sb.toString();
     }
     
+    private <T> T createComponent(Constructor<? extends T> 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<? extends Tokenizer> 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<? extends Tokenizer> 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 Ba
       for (int i = 0; i < numFilters; i++) {
         boolean success = false;
         while (!success) {
-          try {
-            final Constructor<? extends CharStream> 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<? extends CharStream> 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 Ba
       for (int i = 0; i < numFilters; i++) {
         boolean success = false;
         while (!success) {
-          try {
-            final Constructor<? extends TokenFilter> 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<? extends TokenFilter> 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 Ba
       }
     }
   }
-  
-  private static void getClassesForPackage(String pckgname, List<Class<?>> classes) throws Exception {
-    final ClassLoader cld = TestRandomChains.class.getClassLoader();
-    final String path = pckgname.replace('.', '/');
-    final Enumeration<URL> 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));
-            }
-          }
-        }
-      }
-    }
-  }
 }