You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by dw...@apache.org on 2012/03/09 10:14:33 UTC

svn commit: r1298762 - /lucene/dev/branches/branch_3x/lucene/core/src/test/org/apache/lucene/util/junitcompat/TestExceptionInBeforeClassHooks.java

Author: dweiss
Date: Fri Mar  9 09:14:33 2012
New Revision: 1298762

URL: http://svn.apache.org/viewvc?rev=1298762&view=rev
Log:
Wait for the thread to fail in beforeclass.

Modified:
    lucene/dev/branches/branch_3x/lucene/core/src/test/org/apache/lucene/util/junitcompat/TestExceptionInBeforeClassHooks.java

Modified: lucene/dev/branches/branch_3x/lucene/core/src/test/org/apache/lucene/util/junitcompat/TestExceptionInBeforeClassHooks.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_3x/lucene/core/src/test/org/apache/lucene/util/junitcompat/TestExceptionInBeforeClassHooks.java?rev=1298762&r1=1298761&r2=1298762&view=diff
==============================================================================
--- lucene/dev/branches/branch_3x/lucene/core/src/test/org/apache/lucene/util/junitcompat/TestExceptionInBeforeClassHooks.java (original)
+++ lucene/dev/branches/branch_3x/lucene/core/src/test/org/apache/lucene/util/junitcompat/TestExceptionInBeforeClassHooks.java Fri Mar  9 09:14:33 2012
@@ -1,32 +1,40 @@
 package org.apache.lucene.util.junitcompat;
 
+import java.util.*;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
 import junit.framework.Assert;
 
+import org.apache.lucene.util.LuceneTestCase;
 import org.junit.Before;
 import org.junit.BeforeClass;
 import org.junit.Test;
 import org.junit.runner.JUnitCore;
 import org.junit.runner.Result;
+import org.junit.runner.notification.Failure;
 
 public class TestExceptionInBeforeClassHooks extends WithNestedTests {
   public TestExceptionInBeforeClassHooks() {
-    super(true);
+    super(false);
   }
 
   public static class Nested1 extends WithNestedTests.AbstractNestedTest {
     @BeforeClass
-    public static void beforeClass() {
-      new Thread() {
+    public static void beforeClass() throws Exception {
+      Thread t = new Thread() {
         public void run() {
           throw new RuntimeException("foobar");
         }
-      }.start();
+      };
+      t.start();
+      t.join();
     }
 
     public void test() {}
   }
 
-  public static class Nested2 extends WithNestedTests.AbstractNestedTest {
+  public static class Nested2 extends LuceneTestCase {
     public void test1() throws Exception {
       Thread t = new Thread() {
         public void run() {
@@ -46,6 +54,16 @@ public class TestExceptionInBeforeClassH
       t.start();
       t.join();
     }
+    
+    public void test3() throws Exception {
+      Thread t = new Thread() {
+        public void run() {
+          throw new RuntimeException("foobar3");
+        }
+      };
+      t.start();
+      t.join();
+    }    
   }
 
   public static class Nested3 extends WithNestedTests.AbstractNestedTest {
@@ -75,14 +93,20 @@ public class TestExceptionInBeforeClassH
   @Test
   public void testExceptionWithinTestFailsTheTest() {
     Result runClasses = JUnitCore.runClasses(Nested2.class);
-    Assert.assertEquals(2, runClasses.getFailureCount());
-    Assert.assertEquals(2, runClasses.getRunCount());
+    Assert.assertEquals(3, runClasses.getFailureCount());
+    Assert.assertEquals(3, runClasses.getRunCount());
     
-    String m1 = runClasses.getFailures().get(0).getTrace();
-    String m2 = runClasses.getFailures().get(1).getTrace();
-    Assert.assertTrue(
-        (m1.contains("foobar1") && m2.contains("foobar2")) ||
-        (m1.contains("foobar2") && m2.contains("foobar1")));
+    ArrayList<String> foobars = new ArrayList<String>();
+    for (Failure f : runClasses.getFailures()) {
+      Matcher m = Pattern.compile("foobar[0-9]+").matcher(f.getTrace());
+      while (m.find()) {
+        foobars.add(m.group());
+      }
+    }
+
+    Collections.sort(foobars);
+    Assert.assertEquals("[foobar1, foobar2, foobar3]", 
+        Arrays.toString(foobars.toArray()));
   }
   
   @Test