|
| 1 | +package org.junit.tests.running.classes; |
| 2 | + |
| 3 | +import org.junit.AfterClass; |
| 4 | +import org.junit.Test; |
| 5 | +import org.junit.internal.runners.ErrorReportingRunner; |
| 6 | +import org.junit.runner.Description; |
| 7 | +import org.junit.runner.JUnitCore; |
| 8 | +import org.junit.runner.Request; |
| 9 | +import org.junit.runner.Result; |
| 10 | +import org.junit.runner.RunWith; |
| 11 | +import org.junit.runner.Runner; |
| 12 | +import org.junit.runner.notification.RunListener; |
| 13 | +import org.junit.runners.BlockJUnit4ClassRunner; |
| 14 | +import org.junit.runners.model.InitializationError; |
| 15 | + |
| 16 | +import static org.junit.Assert.assertEquals; |
| 17 | + |
| 18 | +public class ThreadsTest { |
| 19 | + private String log = ""; |
| 20 | + |
| 21 | + public static class TestWithInterrupt { |
| 22 | + |
| 23 | + @Test |
| 24 | + public void interruptCurrentThread() { |
| 25 | + Thread.currentThread().interrupt(); |
| 26 | + } |
| 27 | + |
| 28 | + @Test |
| 29 | + public void otherTestCaseInterruptingCurrentThread() { |
| 30 | + Thread.currentThread().interrupt(); |
| 31 | + } |
| 32 | + |
| 33 | + } |
| 34 | + |
| 35 | + @Test |
| 36 | + public void currentThreadInterruptedStatusIsClearedAfterEachTestExecution() { |
| 37 | + log = ""; |
| 38 | + JUnitCore jUnitCore = new JUnitCore(); |
| 39 | + jUnitCore.addListener(new RunListener() { |
| 40 | + @Override |
| 41 | + public void testFinished(Description description) { |
| 42 | + log += Thread.currentThread().isInterrupted() + " "; |
| 43 | + } |
| 44 | + }); |
| 45 | + |
| 46 | + Result result = jUnitCore.run(TestWithInterrupt.class); |
| 47 | + assertEquals(0, result.getFailureCount()); |
| 48 | + assertEquals("false false ", log); |
| 49 | + } |
| 50 | + |
| 51 | + @RunWith(BlockJUnit4ClassRunner.class) |
| 52 | + public static class TestWithInterruptFromAfterClass { |
| 53 | + @AfterClass |
| 54 | + public static void interruptCurrentThread() { |
| 55 | + Thread.currentThread().interrupt(); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + public void test() { |
| 60 | + // no-op |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + public void currentThreadInterruptStatusIsClearedAfterSuiteExecution() { |
| 66 | + log = ""; |
| 67 | + JUnitCore jUnitCore = new JUnitCore(); |
| 68 | + jUnitCore.addListener(new RunListener() { |
| 69 | + @Override |
| 70 | + public void testSuiteFinished(Description description) throws Exception { |
| 71 | + log += Thread.currentThread().isInterrupted(); |
| 72 | + } |
| 73 | + }); |
| 74 | + |
| 75 | + Request request = new Request() { |
| 76 | + @Override |
| 77 | + public Runner getRunner() { |
| 78 | + try { |
| 79 | + return new BlockJUnit4ClassRunner(TestWithInterruptFromAfterClass.class) { |
| 80 | + }; |
| 81 | + } catch (InitializationError e) { |
| 82 | + return new ErrorReportingRunner(TestWithInterruptFromAfterClass.class, e); |
| 83 | + } |
| 84 | + } |
| 85 | + }; |
| 86 | + |
| 87 | + Result result = jUnitCore.run(request); |
| 88 | + assertEquals(0, result.getFailureCount()); |
| 89 | + assertEquals("false", log); |
| 90 | + } |
| 91 | +} |
0 commit comments