From 06d319b97d4931ffa8c9cc79ca203854de11975b Mon Sep 17 00:00:00 2001 From: Robert Muir Date: Sun, 24 Jul 2011 14:37:03 +0000 Subject: [PATCH] SOLR-2673: randomize test methods in LuceneTestCase to flush out test-ordering dependencies git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1150384 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/lucene/util/LuceneTestCase.java | 24 ++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/lucene/src/test-framework/org/apache/lucene/util/LuceneTestCase.java b/lucene/src/test-framework/org/apache/lucene/util/LuceneTestCase.java index 59effca02b3..2c7aa15f206 100644 --- a/lucene/src/test-framework/org/apache/lucene/util/LuceneTestCase.java +++ b/lucene/src/test-framework/org/apache/lucene/util/LuceneTestCase.java @@ -347,12 +347,16 @@ public abstract class LuceneTestCase extends Assert { @Deprecated private static List testClassesRun = new ArrayList(); - @BeforeClass - public static void beforeClassLuceneTestCaseJ4() { - state = State.INITIAL; + private static void initRandom() { + assert !random.initialized; staticSeed = "random".equals(TEST_SEED) ? seedRand.nextLong() : TwoLongs.fromString(TEST_SEED).l1; random.setSeed(staticSeed); random.initialized = true; + } + + @BeforeClass + public static void beforeClassLuceneTestCaseJ4() { + state = State.INITIAL; tempDirs.clear(); stores = Collections.synchronizedMap(new IdentityHashMap()); @@ -1408,6 +1412,10 @@ public abstract class LuceneTestCase extends Assert { protected List computeTestMethods() { if (testMethods != null) return testMethods; + + initRandom(); + Random r = new Random(random.nextLong()); + testClassesRun.add(getTestClass().getJavaClass().getSimpleName()); testMethods = new ArrayList(); for (Method m : getTestClass().getJavaClass().getMethods()) { @@ -1457,6 +1465,15 @@ public abstract class LuceneTestCase extends Assert { } catch (Exception e) { throw new RuntimeException(e); } } } + // sort the test methods first before shuffling them, so that the shuffle is consistent + // across different implementations that might order the methods different originally. + Collections.sort(testMethods, new Comparator() { + @Override + public int compare(FrameworkMethod f1, FrameworkMethod f2) { + return f1.getName().compareTo(f2.getName()); + } + }); + Collections.shuffle(testMethods, r); return testMethods; } @@ -1494,6 +1511,7 @@ public abstract class LuceneTestCase extends Assert { public LuceneTestCaseRunner(Class clazz) throws InitializationError { super(clazz); + // evil we cannot init our random here, because super() calls computeTestMethods!!!!; Filter f = new Filter() { @Override