Apache Lucene open-source search software
LUCENE-3111: more intense checking from LuceneTestCase for setUp/tearDown bugs in tests
Robert Muirfc85939864a4
diff --git a/lucene/contrib/ant/src/test/org/apache/lucene/ant/HtmlDocumentTest.java b/lucene/contrib/ant/src/test/org/apache/lucene/ant/HtmlDocumentTest.javaindex c622cf38f6a..149cc0d9d7e 100644--- a/lucene/contrib/ant/src/test/org/apache/lucene/ant/HtmlDocumentTest.java+++ b/lucene/contrib/ant/src/test/org/apache/lucene/ant/HtmlDocumentTest.java@@ -17,8 +17,6 @@ package org.apache.lucene.ant;* limitations under the License.*/-import java.io.IOException;-import org.apache.lucene.ant.DocumentTestCase;import org.apache.lucene.ant.HtmlDocument;@@ -27,7 +25,8 @@ public class HtmlDocumentTest extends DocumentTestCaseHtmlDocument doc;@Override- public void setUp() throws IOException {+ public void setUp() throws Exception {+ super.setUp();doc = new HtmlDocument(getFile("test.html"));}@@ -37,8 +36,9 @@ public class HtmlDocumentTest extends DocumentTestCase}@Override- public void tearDown() {+ public void tearDown() throws Exception {doc = null;+ super.tearDown();}}diff --git a/lucene/contrib/ant/src/test/org/apache/lucene/ant/TextDocumentTest.java b/lucene/contrib/ant/src/test/org/apache/lucene/ant/TextDocumentTest.javaindex 5a44c68b42b..b3f6b9db850 100644--- a/lucene/contrib/ant/src/test/org/apache/lucene/ant/TextDocumentTest.java+++ b/lucene/contrib/ant/src/test/org/apache/lucene/ant/TextDocumentTest.java@@ -17,8 +17,6 @@ package org.apache.lucene.ant;* limitations under the License.*/-import java.io.IOException;-import org.apache.lucene.ant.DocumentTestCase;import org.apache.lucene.ant.TextDocument;@@ -27,7 +25,8 @@ public class TextDocumentTest extends DocumentTestCaseTextDocument doc;@Override- public void setUp() throws IOException {+ public void setUp() throws Exception {+ super.setUp();doc = new TextDocument(getFile("test.txt"));}@@ -36,8 +35,9 @@ public class TextDocumentTest extends DocumentTestCase}@Override- public void tearDown() {+ public void tearDown() throws Exception {doc = null;+ super.tearDown();}}diff --git a/lucene/contrib/queryparser/src/test/org/apache/lucene/queryParser/precedence/TestPrecedenceQueryParser.java b/lucene/contrib/queryparser/src/test/org/apache/lucene/queryParser/precedence/TestPrecedenceQueryParser.javaindex cd719791f35..7f267b9f616 100644--- a/lucene/contrib/queryparser/src/test/org/apache/lucene/queryParser/precedence/TestPrecedenceQueryParser.java+++ b/lucene/contrib/queryparser/src/test/org/apache/lucene/queryParser/precedence/TestPrecedenceQueryParser.java@@ -631,8 +631,9 @@ public class TestPrecedenceQueryParser extends LuceneTestCase {}@Override- public void tearDown() {+ public void tearDown() throws Exception {BooleanQuery.setMaxClauseCount(originalMaxClauses);+ super.tearDown();}}diff --git a/lucene/src/test-framework/org/apache/lucene/util/LuceneTestCase.java b/lucene/src/test-framework/org/apache/lucene/util/LuceneTestCase.javaindex 7e95cffacd9..59608db452a 100644--- a/lucene/src/test-framework/org/apache/lucene/util/LuceneTestCase.java+++ b/lucene/src/test-framework/org/apache/lucene/util/LuceneTestCase.java@@ -171,8 +171,15 @@ public abstract class LuceneTestCase extends Assert {private volatile Thread.UncaughtExceptionHandler savedUncaughtExceptionHandler = null;/** Used to track if setUp and tearDown are called correctly from subclasses */- private boolean setup;+ private static State state = State.INITIAL;+ private static enum State {+ INITIAL, // no tests ran yet+ SETUP, // test has called setUp()+ RANTEST, // test is running+ TEARDOWN // test has called tearDown()+ };+/*** Some tests expect the directory to contain a single segment, and want to do tests on that segment's reader.* This is an utility method to help them.@@ -326,6 +333,7 @@ public abstract class LuceneTestCase extends Assert {@BeforeClasspublic static void beforeClassLuceneTestCaseJ4() {+ state = State.INITIAL;staticSeed = "random".equals(TEST_SEED) ? seedRand.nextLong() : TwoLongs.fromString(TEST_SEED).l1;random.setSeed(staticSeed);tempDirs.clear();@@ -375,6 +383,11 @@ public abstract class LuceneTestCase extends Assert {@AfterClasspublic static void afterClassLuceneTestCaseJ4() {+ if (!testsFailed) {+ assertTrue("ensure your setUp() calls super.setUp() and your tearDown() calls super.tearDown()!!!",+ state == State.INITIAL || state == State.TEARDOWN);+ }+ state = State.INITIAL;if (! "false".equals(TEST_CLEAN_THREADS)) {int rogueThreads = threadCleanup("test class");if (rogueThreads > 0) {@@ -483,17 +496,22 @@ public abstract class LuceneTestCase extends Assert {public void starting(FrameworkMethod method) {// set current method name for loggingLuceneTestCase.this.name = method.getName();+ if (!testsFailed) {+ assertTrue("ensure your setUp() calls super.setUp()!!!", state == State.SETUP);+ }+ state = State.RANTEST;super.starting(method);}-};@Beforepublic void setUp() throws Exception {seed = "random".equals(TEST_SEED) ? seedRand.nextLong() : TwoLongs.fromString(TEST_SEED).l2;random.setSeed(seed);- assertFalse("ensure your tearDown() calls super.tearDown()!!!", setup);- setup = true;+ if (!testsFailed) {+ assertTrue("ensure your tearDown() calls super.tearDown()!!!", (state == State.INITIAL || state == State.TEARDOWN));+ }+ state = State.SETUP;savedUncaughtExceptionHandler = Thread.getDefaultUncaughtExceptionHandler();Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {public void uncaughtException(Thread t, Throwable e) {@@ -529,8 +547,12 @@ public abstract class LuceneTestCase extends Assert {@Afterpublic void tearDown() throws Exception {- assertTrue("ensure your setUp() calls super.setUp()!!!", setup);- setup = false;+ if (!testsFailed) {+ // Note: we allow a test to go straight from SETUP -> TEARDOWN (without ever entering the RANTEST state)+ // because if you assume() inside setUp(), it skips the test and the TestWatchman has no way to know...+ assertTrue("ensure your setUp() calls super.setUp()!!!", state == State.RANTEST || state == State.SETUP);+ }+ state = State.TEARDOWN;BooleanQuery.setMaxClauseCount(savedBoolMaxClauseCount);if ("perMethod".equals(TEST_CLEAN_THREADS)) {int rogueThreads = threadCleanup("test method: '" + getName() + "'");diff --git a/lucene/src/test/org/apache/lucene/search/TestTermScorer.java b/lucene/src/test/org/apache/lucene/search/TestTermScorer.javaindex d770640263c..d4f919856b4 100644--- a/lucene/src/test/org/apache/lucene/search/TestTermScorer.java+++ b/lucene/src/test/org/apache/lucene/search/TestTermScorer.java@@ -65,6 +65,7 @@ public class TestTermScorer extends LuceneTestCase {indexSearcher.close();indexReader.close();directory.close();+ super.tearDown();}public void test() throws IOException {diff --git a/lucene/src/test/org/apache/lucene/util/automaton/fst/TestFSTs.java b/lucene/src/test/org/apache/lucene/util/automaton/fst/TestFSTs.javaindex af2444b35c1..ac809b58f77 100644--- a/lucene/src/test/org/apache/lucene/util/automaton/fst/TestFSTs.java+++ b/lucene/src/test/org/apache/lucene/util/automaton/fst/TestFSTs.java@@ -54,14 +54,16 @@ public class TestFSTs extends LuceneTestCase {private MockDirectoryWrapper dir;@Override- public void setUp() throws IOException {+ public void setUp() throws Exception {+ super.setUp();dir = newDirectory();dir.setPreventDoubleWrite(false);}@Override- public void tearDown() throws IOException {+ public void tearDown() throws Exception {dir.close();+ super.tearDown();}private static BytesRef toBytesRef(IntsRef ir) {