lucene-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dwe...@apache.org
Subject svn commit: r1297940 - in /lucene/dev/trunk: lucene/test-framework/src/java/org/apache/lucene/util/ solr/test-framework/src/java/org/apache/solr/
Date Wed, 07 Mar 2012 11:20:16 GMT
Author: dweiss
Date: Wed Mar  7 11:20:16 2012
New Revision: 1297940

URL: http://svn.apache.org/viewvc?rev=1297940&view=rev
Log:
Class rule to store the currently executing test class. Removed Solr's code that pulled this
from the stack frames (which wasn't working because rules wrap the actual test code).

Added:
    lucene/dev/trunk/lucene/test-framework/src/java/org/apache/lucene/util/StoreClassNameRule.java
Modified:
    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/LuceneTestCaseRunner.java
    lucene/dev/trunk/solr/test-framework/src/java/org/apache/solr/SolrTestCaseJ4.java

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=1297940&r1=1297939&r2=1297940&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
Wed Mar  7 11:20:16 2012
@@ -265,9 +265,7 @@ public abstract class LuceneTestCase ext
 
   protected static Map<MockDirectoryWrapper,StackTraceElement[]> stores;
 
-  /** @deprecated (4.0) until we fix no-fork problems in solr tests */
-  @Deprecated
-  static List<String> testClassesRun = new ArrayList<String>();
+  private static List<String> testClassesRun = new ArrayList<String>();
 
   private static void initRandom() {
     assert !random.initialized;
@@ -279,11 +277,20 @@ public abstract class LuceneTestCase ext
   @Deprecated
   private static boolean icuTested = false;
 
+  /**
+   * Stores the currently class under test.
+   */
+  private static final StoreClassNameRule classNameRule = new StoreClassNameRule(); 
+  
   @ClassRule
-  public static TestRule classRules = RuleChain.outerRule(new SystemPropertiesInvariantRule());
+  public static TestRule classRules = RuleChain
+    .outerRule(new SystemPropertiesInvariantRule())
+    .around(classNameRule);
 
   @BeforeClass
   public static void beforeClassLuceneTestCaseJ4() {
+    testClassesRun.add(getTestClass().getSimpleName());
+
     initRandom();
     tempDirs.clear();
     stores = Collections.synchronizedMap(new IdentityHashMap<MockDirectoryWrapper,StackTraceElement[]>());
@@ -1564,6 +1571,13 @@ public abstract class LuceneTestCase ext
     }
     return context;
   }
+
+  /**
+   * Return the current class being tested.
+   */
+  public static Class<?> getTestClass() {
+    return classNameRule.getTestClass();
+  }
   
   // initialized by the TestRunner
   static boolean useNoMemoryExpensiveCodec;

Modified: lucene/dev/trunk/lucene/test-framework/src/java/org/apache/lucene/util/LuceneTestCaseRunner.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/test-framework/src/java/org/apache/lucene/util/LuceneTestCaseRunner.java?rev=1297940&r1=1297939&r2=1297940&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/test-framework/src/java/org/apache/lucene/util/LuceneTestCaseRunner.java
(original)
+++ lucene/dev/trunk/lucene/test-framework/src/java/org/apache/lucene/util/LuceneTestCaseRunner.java
Wed Mar  7 11:20:16 2012
@@ -68,7 +68,6 @@ public class LuceneTestCaseRunner extend
     
     Random r = new Random(runnerSeed);
     
-    LuceneTestCase.testClassesRun.add(getTestClass().getJavaClass().getSimpleName());
     testMethods = new ArrayList<FrameworkMethod>();
     for (Method m : getTestClass().getJavaClass().getMethods()) {
       // check if the current test's class has methods annotated with @Ignore

Added: lucene/dev/trunk/lucene/test-framework/src/java/org/apache/lucene/util/StoreClassNameRule.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/test-framework/src/java/org/apache/lucene/util/StoreClassNameRule.java?rev=1297940&view=auto
==============================================================================
--- lucene/dev/trunk/lucene/test-framework/src/java/org/apache/lucene/util/StoreClassNameRule.java
(added)
+++ lucene/dev/trunk/lucene/test-framework/src/java/org/apache/lucene/util/StoreClassNameRule.java
Wed Mar  7 11:20:16 2012
@@ -0,0 +1,39 @@
+package org.apache.lucene.util;
+
+import org.junit.rules.TestRule;
+import org.junit.runner.Description;
+import org.junit.runners.model.Statement;
+
+public class StoreClassNameRule implements TestRule {
+  private volatile Class<?> testClass;
+
+  @Override
+  public Statement apply(final Statement s, final Description d) {
+    if (!d.isSuite()) {
+      throw new IllegalArgumentException("This is a @ClassRule (applies to suites only).");
+    }
+
+    return new Statement() {
+      @Override
+      public void evaluate() throws Throwable {
+        try {
+          testClass = d.getTestClass();
+          s.evaluate();
+        } finally {
+          testClass = null;
+        }
+      }
+    };
+  }
+  
+  /**
+   * Returns the test class currently executing in this rule.
+   */
+  public Class<?> getTestClass() {
+    Class<?> clz = testClass;
+    if (clz == null) {
+      throw new RuntimeException("The rule is not currently executing.");
+    }
+    return clz;
+  }
+}

Modified: lucene/dev/trunk/solr/test-framework/src/java/org/apache/solr/SolrTestCaseJ4.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/solr/test-framework/src/java/org/apache/solr/SolrTestCaseJ4.java?rev=1297940&r1=1297939&r2=1297940&view=diff
==============================================================================
--- lucene/dev/trunk/solr/test-framework/src/java/org/apache/solr/SolrTestCaseJ4.java (original)
+++ lucene/dev/trunk/solr/test-framework/src/java/org/apache/solr/SolrTestCaseJ4.java Wed
Mar  7 11:20:16 2012
@@ -241,20 +241,11 @@ public abstract class SolrTestCaseJ4 ext
   }
 
   protected static String getClassName() {
-    StackTraceElement[] stack = new RuntimeException("WhoAmI").fillInStackTrace().getStackTrace();
-    for (int i = stack.length-1; i>=0; i--) {
-      StackTraceElement ste = stack[i];
-      String cname = ste.getClassName();
-      if (cname.indexOf(".lucene.")>=0 || cname.indexOf(".solr.")>=0) {
-        return cname;
-      }
-    }
-    return SolrTestCaseJ4.class.getName();
+    return getTestClass().getName();
   }
 
   protected static String getSimpleClassName() {
-    String cname = getClassName();
-    return cname.substring(cname.lastIndexOf('.')+1);
+    return getTestClass().getSimpleName();
   }
 
   protected static String configString;



Mime
View raw message