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).

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1297940 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Dawid Weiss 2012-03-07 11:20:16 +00:00
parent ad453aeeda
commit 8041c826a7
4 changed files with 59 additions and 16 deletions

View File

@ -265,9 +265,7 @@ public abstract class LuceneTestCase extends Assert {
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 extends Assert {
@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 extends Assert {
}
return context;
}
/**
* Return the current class being tested.
*/
public static Class<?> getTestClass() {
return classNameRule.getTestClass();
}
// initialized by the TestRunner
static boolean useNoMemoryExpensiveCodec;

View File

@ -68,7 +68,6 @@ public class LuceneTestCaseRunner extends BlockJUnit4ClassRunner {
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

View File

@ -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;
}
}

View File

@ -241,20 +241,11 @@ public abstract class SolrTestCaseJ4 extends LuceneTestCase {
}
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;