Wait for the thread to fail in beforeclass.

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1298760 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Dawid Weiss 2012-03-09 09:12:23 +00:00
parent ed93626ffc
commit 1557e02f47
1 changed files with 36 additions and 12 deletions

View File

@ -1,32 +1,40 @@
package org.apache.lucene.util.junitcompat; package org.apache.lucene.util.junitcompat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import junit.framework.Assert; import junit.framework.Assert;
import org.apache.lucene.util.LuceneTestCase;
import org.junit.Before; import org.junit.Before;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.JUnitCore; import org.junit.runner.JUnitCore;
import org.junit.runner.Result; import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
public class TestExceptionInBeforeClassHooks extends WithNestedTests { public class TestExceptionInBeforeClassHooks extends WithNestedTests {
public TestExceptionInBeforeClassHooks() { public TestExceptionInBeforeClassHooks() {
super(true); super(false);
} }
public static class Nested1 extends WithNestedTests.AbstractNestedTest { public static class Nested1 extends WithNestedTests.AbstractNestedTest {
@BeforeClass @BeforeClass
public static void beforeClass() { public static void beforeClass() throws Exception {
new Thread() { Thread t = new Thread() {
public void run() { public void run() {
throw new RuntimeException("foobar"); throw new RuntimeException("foobar");
} }
}.start(); };
t.start();
t.join();
} }
public void test() {} public void test() {}
} }
public static class Nested2 extends WithNestedTests.AbstractNestedTest { public static class Nested2 extends LuceneTestCase {
public void test1() throws Exception { public void test1() throws Exception {
Thread t = new Thread() { Thread t = new Thread() {
public void run() { public void run() {
@ -46,6 +54,16 @@ public class TestExceptionInBeforeClassHooks extends WithNestedTests {
t.start(); t.start();
t.join(); 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 { public static class Nested3 extends WithNestedTests.AbstractNestedTest {
@ -75,14 +93,20 @@ public class TestExceptionInBeforeClassHooks extends WithNestedTests {
@Test @Test
public void testExceptionWithinTestFailsTheTest() { public void testExceptionWithinTestFailsTheTest() {
Result runClasses = JUnitCore.runClasses(Nested2.class); Result runClasses = JUnitCore.runClasses(Nested2.class);
Assert.assertEquals(2, runClasses.getFailureCount()); Assert.assertEquals(3, runClasses.getFailureCount());
Assert.assertEquals(2, runClasses.getRunCount()); Assert.assertEquals(3, runClasses.getRunCount());
String m1 = runClasses.getFailures().get(0).getTrace(); ArrayList<String> foobars = new ArrayList<String>();
String m2 = runClasses.getFailures().get(1).getTrace(); for (Failure f : runClasses.getFailures()) {
Assert.assertTrue( Matcher m = Pattern.compile("foobar[0-9]+").matcher(f.getTrace());
(m1.contains("foobar1") && m2.contains("foobar2")) || while (m.find()) {
(m1.contains("foobar2") && m2.contains("foobar1"))); foobars.add(m.group());
}
}
Collections.sort(foobars);
Assert.assertEquals("[foobar1, foobar2, foobar3]",
Arrays.toString(foobars.toArray()));
} }
@Test @Test