You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by dw...@apache.org on 2012/06/09 20:33:48 UTC

svn commit: r1348475 - in /lucene/dev/trunk/lucene/test-framework/src/java/org/apache/lucene/util: AbstractBeforeAfterRule.java LuceneTestCase.java TestRuleSetupAndRestoreClassEnv.java TestRuleSetupAndRestoreInstanceEnv.java

Author: dweiss
Date: Sat Jun  9 18:33:48 2012
New Revision: 1348475

URL: http://svn.apache.org/viewvc?rev=1348475&view=rev
Log:
LUCENE-4102: refactored support for SuppressCodec annotation (assumption thrown at suite level rather than method level.

Modified:
    lucene/dev/trunk/lucene/test-framework/src/java/org/apache/lucene/util/AbstractBeforeAfterRule.java
    lucene/dev/trunk/lucene/test-framework/src/java/org/apache/lucene/util/LuceneTestCase.java
    lucene/dev/trunk/lucene/test-framework/src/java/org/apache/lucene/util/TestRuleSetupAndRestoreClassEnv.java
    lucene/dev/trunk/lucene/test-framework/src/java/org/apache/lucene/util/TestRuleSetupAndRestoreInstanceEnv.java

Modified: lucene/dev/trunk/lucene/test-framework/src/java/org/apache/lucene/util/AbstractBeforeAfterRule.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/test-framework/src/java/org/apache/lucene/util/AbstractBeforeAfterRule.java?rev=1348475&r1=1348474&r2=1348475&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/test-framework/src/java/org/apache/lucene/util/AbstractBeforeAfterRule.java (original)
+++ lucene/dev/trunk/lucene/test-framework/src/java/org/apache/lucene/util/AbstractBeforeAfterRule.java Sat Jun  9 18:33:48 2012
@@ -38,10 +38,10 @@ abstract class AbstractBeforeAfterRule i
   public Statement apply(final Statement s, final Description d) {
     return new Statement() {
       public void evaluate() throws Throwable {
-        before();
-        
         final ArrayList<Throwable> errors = new ArrayList<Throwable>();
+
         try {
+          before();
           s.evaluate();
         } catch (Throwable t) {
           errors.add(t);

Modified: lucene/dev/trunk/lucene/test-framework/src/java/org/apache/lucene/util/LuceneTestCase.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/test-framework/src/java/org/apache/lucene/util/LuceneTestCase.java?rev=1348475&r1=1348474&r2=1348475&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/test-framework/src/java/org/apache/lucene/util/LuceneTestCase.java (original)
+++ lucene/dev/trunk/lucene/test-framework/src/java/org/apache/lucene/util/LuceneTestCase.java Sat Jun  9 18:33:48 2012
@@ -1101,11 +1101,4 @@ public abstract class LuceneTestCase ext
       throw new IOException("Cannot find resource: " + name);
     }
   }
-
-  /**
-   * @see SuppressCodecs 
-   */
-  static boolean shouldAvoidCodec(String codec) {
-    return classEnvRule.shouldAvoidCodec(codec);
-  }
 }

Modified: lucene/dev/trunk/lucene/test-framework/src/java/org/apache/lucene/util/TestRuleSetupAndRestoreClassEnv.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/test-framework/src/java/org/apache/lucene/util/TestRuleSetupAndRestoreClassEnv.java?rev=1348475&r1=1348474&r2=1348475&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/test-framework/src/java/org/apache/lucene/util/TestRuleSetupAndRestoreClassEnv.java (original)
+++ lucene/dev/trunk/lucene/test-framework/src/java/org/apache/lucene/util/TestRuleSetupAndRestoreClassEnv.java Sat Jun  9 18:33:48 2012
@@ -37,6 +37,8 @@ import org.apache.lucene.search.RandomSi
 import org.apache.lucene.search.similarities.DefaultSimilarity;
 import org.apache.lucene.search.similarities.Similarity;
 import org.apache.lucene.util.LuceneTestCase.SuppressCodecs;
+import org.junit.internal.AssumptionViolatedException;
+
 import com.carrotsearch.randomizedtesting.RandomizedContext;
 
 import static org.apache.lucene.util.LuceneTestCase.*;
@@ -78,7 +80,11 @@ final class TestRuleSetupAndRestoreClass
     if (System.getProperty("solr.directoryFactory") == null) {
       System.setProperty("solr.directoryFactory", "org.apache.solr.core.MockDirectoryFactory");
     }
-    
+
+    // Restore more Solr properties. 
+    restoreProperties.put("solr.solr.home", System.getProperty("solr.solr.home"));
+    restoreProperties.put("solr.data.dir", System.getProperty("solr.data.dir"));
+
     // enable the Lucene 3.x PreflexRW codec explicitly, to work around bugs in IBM J9 / Harmony ServiceLoader:
     try {
       final java.lang.reflect.Field spiLoaderField = Codec.class.getDeclaredField("loader");
@@ -106,7 +112,7 @@ final class TestRuleSetupAndRestoreClass
       throw new RuntimeException("Cannot access internals of Codec and NamedSPILoader classes", e);
     }
     
-    // if verbose: print some debugging stuff about which codecs are loaded
+    // if verbose: print some debugging stuff about which codecs are loaded.
     if (VERBOSE) {
       Set<String> codecs = Codec.availableCodecs();
       for (String codec : codecs) {
@@ -129,7 +135,7 @@ final class TestRuleSetupAndRestoreClass
             final String name;
             if (Thread.currentThread().getName().startsWith("TEST-")) {
               // The name of the main thread is way too
-              // long when looking at IW verbose output...:
+              // long when looking at IW verbose output...
               name = "main";
             } else {
               name = Thread.currentThread().getName();
@@ -146,8 +152,6 @@ final class TestRuleSetupAndRestoreClass
     if (targetClass.isAnnotationPresent(SuppressCodecs.class)) {
       SuppressCodecs a = targetClass.getAnnotation(SuppressCodecs.class);
       avoidCodecs.addAll(Arrays.asList(a.value()));
-      System.err.println("NOTE: Suppressing codecs " + Arrays.toString(a.value()) 
-          + " for " + targetClass.getSimpleName() + ".");
     }
     
     PREFLEX_IMPERSONATION_IS_ACTIVE = false;
@@ -206,7 +210,40 @@ final class TestRuleSetupAndRestoreClass
     TimeZone randomTimeZone = randomTimeZone(random());
     timeZone = testTimeZone.equals("random") ? randomTimeZone : TimeZone.getTimeZone(testTimeZone);
     TimeZone.setDefault(timeZone);
-    similarity = random().nextBoolean() ? new DefaultSimilarity() : new RandomSimilarityProvider(random());    
+    similarity = random().nextBoolean() ? new DefaultSimilarity() : new RandomSimilarityProvider(random());
+
+    // Check codec restrictions once at class level.
+    try {
+      checkCodecRestrictions(codec);
+    } catch (AssumptionViolatedException e) {
+      System.err.println("NOTE: " + e.getMessage() + " Suppressed codecs: " + 
+          Arrays.toString(avoidCodecs.toArray()));
+      throw e;
+    }
+  }
+
+  /**
+   * Check codec restrictions.
+   * 
+   * @throws AssumptionViolatedException if the class does not work with a given codec.
+   */
+  private void checkCodecRestrictions(Codec codec) {
+    assumeFalse("Class not allowed to use codec: " + codec.getName() + ".",
+        shouldAvoidCodec(codec.getName()));
+
+    if (codec instanceof RandomCodec && !avoidCodecs.isEmpty()) {
+      for (String name : ((RandomCodec)codec).formatNames) {
+        assumeFalse("Class not allowed to use postings format: " + name + ".",
+            shouldAvoidCodec(name));
+      }
+    }
+
+    PostingsFormat pf = codec.postingsFormat();
+    assumeFalse("Class not allowed to use postings format: " + pf.getName() + ".",
+        shouldAvoidCodec(pf.getName()));
+
+    assumeFalse("Class not allowed to use postings format: " + LuceneTestCase.TEST_POSTINGSFORMAT + ".", 
+        shouldAvoidCodec(LuceneTestCase.TEST_POSTINGSFORMAT));
   }
 
   /**
@@ -225,17 +262,14 @@ final class TestRuleSetupAndRestoreClass
 
     Codec.setDefault(savedCodec);
     InfoStream.setDefault(savedInfoStream);
-    Locale.setDefault(savedLocale);
-    TimeZone.setDefault(savedTimeZone);
-
-    System.clearProperty("solr.solr.home");
-    System.clearProperty("solr.data.dir");
+    if (savedLocale != null) Locale.setDefault(savedLocale);
+    if (savedTimeZone != null) TimeZone.setDefault(savedTimeZone);
   }
 
   /**
    * Should a given codec be avoided for the currently executing suite?
    */
-  public boolean shouldAvoidCodec(String codec) {
+  private boolean shouldAvoidCodec(String codec) {
     return !avoidCodecs.isEmpty() && avoidCodecs.contains(codec);
   }
 }

Modified: lucene/dev/trunk/lucene/test-framework/src/java/org/apache/lucene/util/TestRuleSetupAndRestoreInstanceEnv.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/test-framework/src/java/org/apache/lucene/util/TestRuleSetupAndRestoreInstanceEnv.java?rev=1348475&r1=1348474&r2=1348475&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/test-framework/src/java/org/apache/lucene/util/TestRuleSetupAndRestoreInstanceEnv.java (original)
+++ lucene/dev/trunk/lucene/test-framework/src/java/org/apache/lucene/util/TestRuleSetupAndRestoreInstanceEnv.java Sat Jun  9 18:33:48 2012
@@ -1,10 +1,6 @@
 package org.apache.lucene.util;
 
-import org.apache.lucene.codecs.Codec;
-import org.apache.lucene.codecs.PostingsFormat;
-import org.apache.lucene.index.RandomCodec;
 import org.apache.lucene.search.BooleanQuery;
-import org.junit.internal.AssumptionViolatedException;
 
 /**
  * Licensed to the Apache Software Foundation (ASF) under one or more
@@ -32,27 +28,6 @@ final class TestRuleSetupAndRestoreInsta
 
   protected void before() {
     savedBoolMaxClauseCount = BooleanQuery.getMaxClauseCount();
-
-    Codec codec = Codec.getDefault();
-    if (LuceneTestCase.shouldAvoidCodec(codec.getName())) {
-      throw new AssumptionViolatedException(
-          "Method not allowed to use codec: " + codec.getName() + ".");
-    }
-    // TODO: make this more efficient
-    if (codec instanceof RandomCodec) {
-      for (String name : ((RandomCodec)codec).formatNames) {
-        if (LuceneTestCase.shouldAvoidCodec(name)) {
-          throw new AssumptionViolatedException(
-              "Method not allowed to use postings format: " + name + ".");
-        }
-      }
-    }
-    PostingsFormat pf = codec.postingsFormat();
-    if (LuceneTestCase.shouldAvoidCodec(pf.getName())) {
-      throw new AssumptionViolatedException(
-          "Method not allowed to use postings format: " + pf.getName() + ".");
-    }
-    
   }
 
   protected void after() {