You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by rm...@apache.org on 2011/05/17 22:30:10 UTC

svn commit: r1104527 - in /lucene/dev/branches/branch_3x: ./ lucene/ lucene/backwards/ lucene/contrib/ant/src/test/org/apache/lucene/ant/ lucene/contrib/queryparser/src/test/org/apache/lucene/queryParser/precedence/ lucene/src/test-framework/org/apache...

Author: rmuir
Date: Tue May 17 20:30:09 2011
New Revision: 1104527

URL: http://svn.apache.org/viewvc?rev=1104527&view=rev
Log:
LUCENE-3111: more intense checking from LuceneTestCase for setUp/tearDown bugs in tests

Modified:
    lucene/dev/branches/branch_3x/   (props changed)
    lucene/dev/branches/branch_3x/lucene/   (props changed)
    lucene/dev/branches/branch_3x/lucene/backwards/   (props changed)
    lucene/dev/branches/branch_3x/lucene/contrib/ant/src/test/org/apache/lucene/ant/HtmlDocumentTest.java
    lucene/dev/branches/branch_3x/lucene/contrib/ant/src/test/org/apache/lucene/ant/TextDocumentTest.java
    lucene/dev/branches/branch_3x/lucene/contrib/queryparser/src/test/org/apache/lucene/queryParser/precedence/TestPrecedenceQueryParser.java
    lucene/dev/branches/branch_3x/lucene/src/test-framework/org/apache/lucene/util/LuceneTestCase.java
    lucene/dev/branches/branch_3x/lucene/src/test/org/apache/lucene/search/TestTermScorer.java
    lucene/dev/branches/branch_3x/solr/   (props changed)

Modified: lucene/dev/branches/branch_3x/lucene/contrib/ant/src/test/org/apache/lucene/ant/HtmlDocumentTest.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_3x/lucene/contrib/ant/src/test/org/apache/lucene/ant/HtmlDocumentTest.java?rev=1104527&r1=1104526&r2=1104527&view=diff
==============================================================================
--- lucene/dev/branches/branch_3x/lucene/contrib/ant/src/test/org/apache/lucene/ant/HtmlDocumentTest.java (original)
+++ lucene/dev/branches/branch_3x/lucene/contrib/ant/src/test/org/apache/lucene/ant/HtmlDocumentTest.java Tue May 17 20:30:09 2011
@@ -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 Do
     HtmlDocument 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 Do
     }
     
     @Override
-    public void tearDown() {
+    public void tearDown() throws Exception {
         doc = null;
+        super.tearDown();
     }
 }
 

Modified: lucene/dev/branches/branch_3x/lucene/contrib/ant/src/test/org/apache/lucene/ant/TextDocumentTest.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_3x/lucene/contrib/ant/src/test/org/apache/lucene/ant/TextDocumentTest.java?rev=1104527&r1=1104526&r2=1104527&view=diff
==============================================================================
--- lucene/dev/branches/branch_3x/lucene/contrib/ant/src/test/org/apache/lucene/ant/TextDocumentTest.java (original)
+++ lucene/dev/branches/branch_3x/lucene/contrib/ant/src/test/org/apache/lucene/ant/TextDocumentTest.java Tue May 17 20:30:09 2011
@@ -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 Do
     TextDocument 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 Do
     }
     
     @Override
-    public void tearDown() {
+    public void tearDown() throws Exception {
         doc = null;
+        super.tearDown();
     }
 }
 

Modified: lucene/dev/branches/branch_3x/lucene/contrib/queryparser/src/test/org/apache/lucene/queryParser/precedence/TestPrecedenceQueryParser.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_3x/lucene/contrib/queryparser/src/test/org/apache/lucene/queryParser/precedence/TestPrecedenceQueryParser.java?rev=1104527&r1=1104526&r2=1104527&view=diff
==============================================================================
--- lucene/dev/branches/branch_3x/lucene/contrib/queryparser/src/test/org/apache/lucene/queryParser/precedence/TestPrecedenceQueryParser.java (original)
+++ lucene/dev/branches/branch_3x/lucene/contrib/queryparser/src/test/org/apache/lucene/queryParser/precedence/TestPrecedenceQueryParser.java Tue May 17 20:30:09 2011
@@ -647,8 +647,9 @@ public class TestPrecedenceQueryParser e
   }
 
   @Override
-  public void tearDown() {
+  public void tearDown() throws Exception {
     BooleanQuery.setMaxClauseCount(originalMaxClauses);
+    super.tearDown();
   }
 
 }

Modified: lucene/dev/branches/branch_3x/lucene/src/test-framework/org/apache/lucene/util/LuceneTestCase.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_3x/lucene/src/test-framework/org/apache/lucene/util/LuceneTestCase.java?rev=1104527&r1=1104526&r2=1104527&view=diff
==============================================================================
--- lucene/dev/branches/branch_3x/lucene/src/test-framework/org/apache/lucene/util/LuceneTestCase.java (original)
+++ lucene/dev/branches/branch_3x/lucene/src/test-framework/org/apache/lucene/util/LuceneTestCase.java Tue May 17 20:30:09 2011
@@ -168,8 +168,15 @@ public abstract class LuceneTestCase ext
   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()
+  };
+  
   private static class UncaughtExceptionEntry {
     public final Thread thread;
     public final Throwable exception;
@@ -215,6 +222,7 @@ public abstract class LuceneTestCase ext
   
   @BeforeClass
   public static void beforeClassLuceneTestCaseJ4() {
+    state = State.INITIAL;
     staticSeed = "random".equals(TEST_SEED) ? seedRand.nextLong() : TwoLongs.fromString(TEST_SEED).l1;
     random.setSeed(staticSeed);
     tempDirs.clear();
@@ -236,6 +244,11 @@ public abstract class LuceneTestCase ext
   
   @AfterClass
   public 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) {
@@ -332,17 +345,22 @@ public abstract class LuceneTestCase ext
     public void starting(FrameworkMethod method) {
       // set current method name for logging
       LuceneTestCase.this.name = method.getName();
+      if (!testsFailed) {
+        assertTrue("ensure your setUp() calls super.setUp()!!!", state == State.SETUP);
+      }
+      state = State.RANTEST;
       super.starting(method);
     }
-    
   };
 
   @Before
   public 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) {
@@ -399,8 +417,12 @@ public abstract class LuceneTestCase ext
 
   @After
   public 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() + "'");

Modified: lucene/dev/branches/branch_3x/lucene/src/test/org/apache/lucene/search/TestTermScorer.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_3x/lucene/src/test/org/apache/lucene/search/TestTermScorer.java?rev=1104527&r1=1104526&r2=1104527&view=diff
==============================================================================
--- lucene/dev/branches/branch_3x/lucene/src/test/org/apache/lucene/search/TestTermScorer.java (original)
+++ lucene/dev/branches/branch_3x/lucene/src/test/org/apache/lucene/search/TestTermScorer.java Tue May 17 20:30:09 2011
@@ -63,6 +63,7 @@ public class TestTermScorer extends Luce
     indexSearcher.close();
     indexReader.close();
     directory.close();
+    super.tearDown();
   }
 
   public void test() throws IOException {