diff options
author | Brian Carlstrom <bdc@google.com> | 2013-04-25 17:01:43 -0700 |
---|---|---|
committer | Android Git Automerger <android-git-automerger@android.com> | 2013-04-25 17:01:43 -0700 |
commit | 4e8de9ebb002963675d332ab0e07e9d73500de5a (patch) | |
tree | 761960fcdcb0793decc1b473852851d4e2fbc49b /luni | |
parent | b4f42c5d47b4574bd303dd38553264ec0944d56f (diff) | |
parent | 5ef7aa7fbd7ae4a110c4c262de36a0feaa7e48d4 (diff) | |
download | libcore-4e8de9ebb002963675d332ab0e07e9d73500de5a.zip libcore-4e8de9ebb002963675d332ab0e07e9d73500de5a.tar.gz libcore-4e8de9ebb002963675d332ab0e07e9d73500de5a.tar.bz2 |
am 5ef7aa7f: Merge "TimerTest cleanup"
* commit '5ef7aa7fbd7ae4a110c4c262de36a0feaa7e48d4':
TimerTest cleanup
Diffstat (limited to 'luni')
-rw-r--r-- | luni/src/test/java/tests/api/java/util/TimerTest.java | 486 |
1 files changed, 147 insertions, 339 deletions
diff --git a/luni/src/test/java/tests/api/java/util/TimerTest.java b/luni/src/test/java/tests/api/java/util/TimerTest.java index b09fa45..739c018 100644 --- a/luni/src/test/java/tests/api/java/util/TimerTest.java +++ b/luni/src/test/java/tests/api/java/util/TimerTest.java @@ -22,12 +22,13 @@ import java.util.Date; import java.util.Timer; import java.util.TimerTask; import java.util.concurrent.atomic.AtomicReference; +import junit.framework.TestCase; -public class TimerTest extends junit.framework.TestCase { +public class TimerTest extends TestCase { int timerCounter = 0; - Object sync = new Object(); + private final Object sync = new Object(); /** * Warning: These tests have the possibility to leave a VM hanging if the @@ -59,14 +60,17 @@ public class TimerTest extends junit.framework.TestCase { synchronized (this) { wasRun++; } - if (incrementCount) + if (incrementCount) { timerCounter++; - if (terminateCount == timerCounter && timer != null) + } + if (terminateCount == timerCounter && timer != null) { timer.cancel(); + } if (sleepInRun) { try { Thread.sleep(200); } catch (InterruptedException e) { + throw new RuntimeException(e); } } synchronized (sync) { @@ -91,19 +95,16 @@ public class TimerTest extends junit.framework.TestCase { } } - private void awaitRun(TimerTestTask task) { + private void awaitRun(TimerTestTask task) throws Exception { while (task.wasRun() == 0) { - try { - Thread.sleep(150); - } catch (InterruptedException e) { - } + Thread.sleep(150); } } /** * java.util.Timer#Timer(boolean) */ - public void test_ConstructorZ() { + public void test_ConstructorZ() throws Exception { Timer t = null; try { // Ensure a task is run @@ -122,7 +123,7 @@ public class TimerTest extends junit.framework.TestCase { /** * java.util.Timer#Timer() */ - public void test_Constructor() { + public void test_Constructor() throws Exception { Timer t = null; try { // Ensure a task is run @@ -141,7 +142,7 @@ public class TimerTest extends junit.framework.TestCase { /** * java.util.Timer#Timer(String, boolean) */ - public void test_ConstructorSZ() { + public void test_ConstructorSZ() throws Exception { Timer t = null; try { // Ensure a task is run @@ -157,23 +158,21 @@ public class TimerTest extends junit.framework.TestCase { try { new Timer(null, true); - fail("NullPointerException expected"); - } catch (NullPointerException e) { - //expected + fail(); + } catch (NullPointerException expected) { } try { new Timer(null, false); - fail("NullPointerException expected"); - } catch (NullPointerException e) { - //expected + fail(); + } catch (NullPointerException expected) { } } /** * java.util.Timer#Timer(String) */ - public void test_ConstructorS() { + public void test_ConstructorS() throws Exception { Timer t = null; try { // Ensure a task is run @@ -189,31 +188,26 @@ public class TimerTest extends junit.framework.TestCase { try { new Timer(null); - fail("NullPointerException expected"); - } catch (NullPointerException e) { - //expected + fail(); + } catch (NullPointerException expected) { } } /** * java.util.Timer#cancel() */ - public void test_cancel() { + public void test_cancel() throws Exception { Timer t = null; try { // Ensure a task throws an IllegalStateException after cancelled t = new Timer(); TimerTestTask testTask = new TimerTestTask(); t.cancel(); - boolean exception = false; try { t.schedule(testTask, 100, 200); - } catch (IllegalStateException e) { - exception = true; + fail("Scheduling a task after Timer.cancel() should throw exception"); + } catch (IllegalStateException expected) { } - assertTrue( - "Scheduling a task after Timer.cancel() should throw exception", - exception); // Ensure a task is run but not after cancel t = new Timer(); @@ -222,13 +216,10 @@ public class TimerTest extends junit.framework.TestCase { awaitRun(testTask); t.cancel(); synchronized (sync) { - try { - sync.wait(500); - } catch (InterruptedException e) { - } + sync.wait(500); } assertEquals("TimerTask.run() method should not have been called after cancel", - 1, testTask.wasRun()); + 1, testTask.wasRun()); // Ensure you can call cancel more than once t = new Timer(); @@ -239,13 +230,10 @@ public class TimerTest extends junit.framework.TestCase { t.cancel(); t.cancel(); synchronized (sync) { - try { - sync.wait(500); - } catch (InterruptedException e) { - } + sync.wait(500); } assertEquals("TimerTask.run() method should not have been called after cancel", - 1, testTask.wasRun()); + 1, testTask.wasRun()); // Ensure that a call to cancel from within a timer ensures no more // run @@ -255,23 +243,21 @@ public class TimerTest extends junit.framework.TestCase { testTask.terminateCount(5); // Terminate after 5 runs t.schedule(testTask, 100, 100); synchronized (sync) { - try { - sync.wait(200); - sync.wait(200); - sync.wait(200); - sync.wait(200); - sync.wait(200); - sync.wait(200); - } catch (InterruptedException e) { - } - } - assertTrue("TimerTask.run() method should be called 5 times not " - + testTask.wasRun(), testTask.wasRun() == 5); - t.cancel(); - try { - Thread.sleep(200); - } catch (InterruptedException e) { - } + sync.wait(200); + assertEquals(1, testTask.wasRun()); + sync.wait(200); + assertEquals(2, testTask.wasRun()); + sync.wait(200); + assertEquals(3, testTask.wasRun()); + sync.wait(200); + assertEquals(4, testTask.wasRun()); + sync.wait(200); + assertEquals(5, testTask.wasRun()); + sync.wait(200); + assertEquals(5, testTask.wasRun()); + } + t.cancel(); + Thread.sleep(200); } finally { if (t != null) t.cancel(); @@ -316,7 +302,7 @@ public class TimerTest extends junit.framework.TestCase { /** * java.util.Timer#schedule(java.util.TimerTask, java.util.Date) */ - public void test_scheduleLjava_util_TimerTaskLjava_util_Date() { + public void test_scheduleLjava_util_TimerTaskLjava_util_Date() throws Exception { Timer t = null; try { // Ensure a Timer throws an IllegalStateException after cancelled @@ -324,15 +310,11 @@ public class TimerTest extends junit.framework.TestCase { TimerTestTask testTask = new TimerTestTask(); Date d = new Date(System.currentTimeMillis() + 100); t.cancel(); - boolean exception = false; try { t.schedule(testTask, d); - } catch (IllegalStateException e) { - exception = true; + fail("Scheduling a task after Timer.cancel() should throw exception"); + } catch (IllegalStateException expected) { } - assertTrue( - "Scheduling a task after Timer.cancel() should throw exception", - exception); // Ensure a Timer throws an IllegalStateException if task already // cancelled @@ -340,15 +322,11 @@ public class TimerTest extends junit.framework.TestCase { testTask = new TimerTestTask(); d = new Date(System.currentTimeMillis() + 100); testTask.cancel(); - exception = false; try { t.schedule(testTask, d); - } catch (IllegalStateException e) { - exception = true; + fail("Scheduling a task after cancelling it should throw exception"); + } catch (IllegalStateException expected) { } - assertTrue( - "Scheduling a task after cancelling it should throw exception", - exception); t.cancel(); // Ensure a Timer throws an IllegalArgumentException if delay is @@ -356,58 +334,41 @@ public class TimerTest extends junit.framework.TestCase { t = new Timer(); testTask = new TimerTestTask(); d = new Date(-100); - exception = false; try { t.schedule(testTask, d); - } catch (IllegalArgumentException e) { - exception = true; + fail("Scheduling a task with negative date should throw IllegalArgumentException"); + } catch (IllegalArgumentException expected) { } - assertTrue( - "Scheduling a task with negative date should throw IllegalArgumentException", - exception); t.cancel(); // Ensure a Timer throws a NullPointerException if the task is null t = new Timer(); - exception = false; d = new Date(System.currentTimeMillis() + 100); try { t.schedule(null, d); - } catch (NullPointerException e) { - exception = true; + fail("Scheduling a null task should throw NullPointerException"); + } catch (NullPointerException expected) { } - assertTrue( - "Scheduling a null task should throw NullPointerException", - exception); t.cancel(); // Ensure a Timer throws a NullPointerException if the date is null t = new Timer(); testTask = new TimerTestTask(); - exception = false; try { t.schedule(testTask, null); - } catch (NullPointerException e) { - exception = true; + fail("Scheduling a null date should throw NullPointerException"); + } catch (NullPointerException expected) { } - assertTrue( - "Scheduling a null date should throw NullPointerException", - exception); t.cancel(); // Ensure proper sequence of exceptions t = new Timer(); d = new Date(-100); - exception = false; try { t.schedule(null, d); - } catch (NullPointerException e) { - } catch (IllegalArgumentException e) { - exception = true; + fail("Scheduling a null task with negative date should throw IllegalArgumentException first"); + } catch (IllegalArgumentException expected) { } - assertTrue( - "Scheduling a null task with negative date should throw IllegalArgumentException first", - exception); t.cancel(); // Ensure a task is run @@ -436,13 +397,9 @@ public class TimerTest extends junit.framework.TestCase { testTask.incrementCount(true); d = new Date(System.currentTimeMillis() + 10); t.schedule(testTask, d); - try { - Thread.sleep(400); - } catch (InterruptedException e) { - } - assertTrue( - "Multiple tasks should have incremented counter 4 times not " - + timerCounter, timerCounter == 4); + Thread.sleep(400); + assertTrue("Multiple tasks should have incremented counter 4 times not " + + timerCounter, timerCounter == 4); t.cancel(); } finally { if (t != null) @@ -453,79 +410,58 @@ public class TimerTest extends junit.framework.TestCase { /** * java.util.Timer#schedule(java.util.TimerTask, long) */ - public void test_scheduleLjava_util_TimerTaskJ() { + public void test_scheduleLjava_util_TimerTaskJ() throws Exception { Timer t = null; try { // Ensure a Timer throws an IllegalStateException after cancelled t = new Timer(); TimerTestTask testTask = new TimerTestTask(); t.cancel(); - boolean exception = false; try { t.schedule(testTask, 100); - } catch (IllegalStateException e) { - exception = true; + fail("Scheduling a task after Timer.cancel() should throw exception"); + } catch (IllegalStateException expected) { } - assertTrue( - "Scheduling a task after Timer.cancel() should throw exception", - exception); // Ensure a Timer throws an IllegalStateException if task already // cancelled t = new Timer(); testTask = new TimerTestTask(); testTask.cancel(); - exception = false; try { t.schedule(testTask, 100); - } catch (IllegalStateException e) { - exception = true; + fail("Scheduling a task after cancelling it should throw exception"); + } catch (IllegalStateException expected) { } - assertTrue( - "Scheduling a task after cancelling it should throw exception", - exception); t.cancel(); // Ensure a Timer throws an IllegalArgumentException if delay is // negative t = new Timer(); testTask = new TimerTestTask(); - exception = false; try { t.schedule(testTask, -100); - } catch (IllegalArgumentException e) { - exception = true; + fail("Scheduling a task with negative delay should throw IllegalArgumentException"); + } catch (IllegalArgumentException expected) { } - assertTrue( - "Scheduling a task with negative delay should throw IllegalArgumentException", - exception); t.cancel(); // Ensure a Timer throws a NullPointerException if the task is null t = new Timer(); - exception = false; try { t.schedule(null, 10); - } catch (NullPointerException e) { - exception = true; + fail("Scheduling a null task should throw NullPointerException"); + } catch (NullPointerException expected) { } - assertTrue( - "Scheduling a null task should throw NullPointerException", - exception); t.cancel(); // Ensure proper sequence of exceptions t = new Timer(); - exception = false; try { t.schedule(null, -10); - } catch (NullPointerException e) { - } catch (IllegalArgumentException e) { - exception = true; + fail("Scheduling a null task with negative delays should throw IllegalArgumentException first"); + } catch (IllegalArgumentException expected) { } - assertTrue( - "Scheduling a null task with negative delays should throw IllegalArgumentException first", - exception); t.cancel(); // Ensure a task is run @@ -549,13 +485,9 @@ public class TimerTest extends junit.framework.TestCase { testTask = new TimerTestTask(); testTask.incrementCount(true); t.schedule(testTask, 10); - try { - Thread.sleep(400); - } catch (InterruptedException e) { - } - assertTrue( - "Multiple tasks should have incremented counter 4 times not " - + timerCounter, timerCounter == 4); + Thread.sleep(400); + assertTrue("Multiple tasks should have incremented counter 4 times not " + + timerCounter, timerCounter == 4); t.cancel(); } finally { if (t != null) @@ -566,122 +498,89 @@ public class TimerTest extends junit.framework.TestCase { /** * java.util.Timer#schedule(java.util.TimerTask, long, long) */ - public void test_scheduleLjava_util_TimerTaskJJ() { + public void test_scheduleLjava_util_TimerTaskJJ() throws Exception { Timer t = null; try { // Ensure a Timer throws an IllegalStateException after cancelled t = new Timer(); TimerTestTask testTask = new TimerTestTask(); t.cancel(); - boolean exception = false; try { t.schedule(testTask, 100, 100); - } catch (IllegalStateException e) { - exception = true; + fail("Scheduling a task after Timer.cancel() should throw exception"); + } catch (IllegalStateException expected) { } - assertTrue( - "Scheduling a task after Timer.cancel() should throw exception", - exception); // Ensure a Timer throws an IllegalStateException if task already // cancelled t = new Timer(); testTask = new TimerTestTask(); testTask.cancel(); - exception = false; try { t.schedule(testTask, 100, 100); - } catch (IllegalStateException e) { - exception = true; + fail("Scheduling a task after cancelling it should throw exception"); + } catch (IllegalStateException expected) { } - assertTrue( - "Scheduling a task after cancelling it should throw exception", - exception); t.cancel(); // Ensure a Timer throws an IllegalArgumentException if delay is // negative t = new Timer(); testTask = new TimerTestTask(); - exception = false; try { t.schedule(testTask, -100, 100); - } catch (IllegalArgumentException e) { - exception = true; + fail("Scheduling a task with negative delay should throw IllegalArgumentException"); + } catch (IllegalArgumentException expected) { } - assertTrue( - "Scheduling a task with negative delay should throw IllegalArgumentException", - exception); t.cancel(); // Ensure a Timer throws an IllegalArgumentException if period is // negative t = new Timer(); testTask = new TimerTestTask(); - exception = false; try { t.schedule(testTask, 100, -100); - } catch (IllegalArgumentException e) { - exception = true; + fail("Scheduling a task with negative period should throw IllegalArgumentException"); + } catch (IllegalArgumentException expected) { } - assertTrue( - "Scheduling a task with negative period should throw IllegalArgumentException", - exception); t.cancel(); // Ensure a Timer throws an IllegalArgumentException if period is // zero t = new Timer(); testTask = new TimerTestTask(); - exception = false; try { t.schedule(testTask, 100, 0); - } catch (IllegalArgumentException e) { - exception = true; + fail("Scheduling a task with 0 period should throw IllegalArgumentException"); + } catch (IllegalArgumentException expected) { } - assertTrue( - "Scheduling a task with 0 period should throw IllegalArgumentException", - exception); t.cancel(); // Ensure a Timer throws a NullPointerException if the task is null t = new Timer(); - exception = false; try { t.schedule(null, 10, 10); - } catch (NullPointerException e) { - exception = true; + fail("Scheduling a null task should throw NullPointerException"); + } catch (NullPointerException expected) { } - assertTrue( - "Scheduling a null task should throw NullPointerException", - exception); t.cancel(); // Ensure proper sequence of exceptions t = new Timer(); - exception = false; try { t.schedule(null, -10, -10); - } catch (NullPointerException e) { - } catch (IllegalArgumentException e) { - exception = true; + fail("Scheduling a null task with negative delays should throw IllegalArgumentException first"); + } catch (IllegalArgumentException expected) { } - assertTrue( - "Scheduling a null task with negative delays should throw IllegalArgumentException first", - exception); t.cancel(); // Ensure a task is run at least twice t = new Timer(); testTask = new TimerTestTask(); t.schedule(testTask, 100, 100); - try { - Thread.sleep(400); - } catch (InterruptedException e) { - } - assertTrue( - "TimerTask.run() method should have been called at least twice (" - + testTask.wasRun() + ")", testTask.wasRun() >= 2); + Thread.sleep(400); + assertTrue("TimerTask.run() method should have been called at least twice (" + + testTask.wasRun() + ")", testTask.wasRun() >= 2); t.cancel(); // Ensure multiple tasks are run @@ -698,13 +597,9 @@ public class TimerTest extends junit.framework.TestCase { testTask = new TimerTestTask(); testTask.incrementCount(true); t.schedule(testTask, 100, 200); // at least 4 times - try { - Thread.sleep(1200); // Allowed more room for error - } catch (InterruptedException e) { - } - assertTrue( - "Multiple tasks should have incremented counter 24 times not " - + timerCounter, timerCounter >= 24); + Thread.sleep(1200); // Allowed more room for error + assertTrue("Multiple tasks should have incremented counter 24 times not " + + timerCounter, timerCounter >= 24); t.cancel(); } finally { if (t != null) @@ -716,7 +611,7 @@ public class TimerTest extends junit.framework.TestCase { * java.util.Timer#schedule(java.util.TimerTask, java.util.Date, * long) */ - public void test_scheduleLjava_util_TimerTaskLjava_util_DateJ() { + public void test_scheduleLjava_util_TimerTaskLjava_util_DateJ() throws Exception { Timer t = null; try { // Ensure a Timer throws an IllegalStateException after cancelled @@ -724,15 +619,11 @@ public class TimerTest extends junit.framework.TestCase { TimerTestTask testTask = new TimerTestTask(); Date d = new Date(System.currentTimeMillis() + 100); t.cancel(); - boolean exception = false; try { t.schedule(testTask, d, 100); - } catch (IllegalStateException e) { - exception = true; + fail("Scheduling a task after Timer.cancel() should throw exception"); + } catch (IllegalStateException expected) { } - assertTrue( - "Scheduling a task after Timer.cancel() should throw exception", - exception); // Ensure a Timer throws an IllegalStateException if task already // cancelled @@ -740,15 +631,11 @@ public class TimerTest extends junit.framework.TestCase { d = new Date(System.currentTimeMillis() + 100); testTask = new TimerTestTask(); testTask.cancel(); - exception = false; try { t.schedule(testTask, d, 100); - } catch (IllegalStateException e) { - exception = true; + fail("Scheduling a task after cancelling it should throw exception"); + } catch (IllegalStateException expected) { } - assertTrue( - "Scheduling a task after cancelling it should throw exception", - exception); t.cancel(); // Ensure a Timer throws an IllegalArgumentException if delay is @@ -756,15 +643,11 @@ public class TimerTest extends junit.framework.TestCase { t = new Timer(); d = new Date(-100); testTask = new TimerTestTask(); - exception = false; try { t.schedule(testTask, d, 100); - } catch (IllegalArgumentException e) { - exception = true; + fail("Scheduling a task with negative delay should throw IllegalArgumentException"); + } catch (IllegalArgumentException expected) { } - assertTrue( - "Scheduling a task with negative delay should throw IllegalArgumentException", - exception); t.cancel(); // Ensure a Timer throws an IllegalArgumentException if period is @@ -772,58 +655,41 @@ public class TimerTest extends junit.framework.TestCase { t = new Timer(); d = new Date(System.currentTimeMillis() + 100); testTask = new TimerTestTask(); - exception = false; try { t.schedule(testTask, d, -100); - } catch (IllegalArgumentException e) { - exception = true; + fail("Scheduling a task with negative period should throw IllegalArgumentException"); + } catch (IllegalArgumentException expected) { } - assertTrue( - "Scheduling a task with negative period should throw IllegalArgumentException", - exception); t.cancel(); // Ensure a Timer throws a NullPointerException if the task is null t = new Timer(); d = new Date(System.currentTimeMillis() + 100); - exception = false; try { t.schedule(null, d, 10); - } catch (NullPointerException e) { - exception = true; + fail("Scheduling a null task should throw NullPointerException"); + } catch (NullPointerException expected) { } - assertTrue( - "Scheduling a null task should throw NullPointerException", - exception); t.cancel(); // Ensure a Timer throws a NullPointerException if the date is null t = new Timer(); testTask = new TimerTestTask(); - exception = false; try { t.schedule(testTask, null, 10); - } catch (NullPointerException e) { - exception = true; + fail("Scheduling a null task should throw NullPointerException"); + } catch (NullPointerException expected) { } - assertTrue( - "Scheduling a null task should throw NullPointerException", - exception); t.cancel(); // Ensure proper sequence of exceptions t = new Timer(); d = new Date(-100); - exception = false; try { t.schedule(null, d, 10); - } catch (NullPointerException e) { - } catch (IllegalArgumentException e) { - exception = true; + fail("Scheduling a null task with negative dates should throw IllegalArgumentException first"); + } catch (IllegalArgumentException expected) { } - assertTrue( - "Scheduling a null task with negative dates should throw IllegalArgumentException first", - exception); t.cancel(); // Ensure a task is run at least twice @@ -831,13 +697,9 @@ public class TimerTest extends junit.framework.TestCase { d = new Date(System.currentTimeMillis() + 100); testTask = new TimerTestTask(); t.schedule(testTask, d, 100); - try { - Thread.sleep(800); - } catch (InterruptedException e) { - } - assertTrue( - "TimerTask.run() method should have been called at least twice (" - + testTask.wasRun() + ")", testTask.wasRun() >= 2); + Thread.sleep(800); + assertTrue("TimerTask.run() method should have been called at least twice (" + + testTask.wasRun() + ")", testTask.wasRun() >= 2); t.cancel(); // Ensure multiple tasks are run @@ -858,13 +720,9 @@ public class TimerTest extends junit.framework.TestCase { testTask.incrementCount(true); d = new Date(System.currentTimeMillis() + 100); t.schedule(testTask, d, 200); // at least 4 times - try { - Thread.sleep(3000); - } catch (InterruptedException e) { - } - assertTrue( - "Multiple tasks should have incremented counter 24 times not " - + timerCounter, timerCounter >= 24); + Thread.sleep(3000); + assertTrue("Multiple tasks should have incremented counter 24 times not " + + timerCounter, timerCounter >= 24); t.cancel(); } finally { if (t != null) @@ -876,64 +734,48 @@ public class TimerTest extends junit.framework.TestCase { * java.util.Timer#scheduleAtFixedRate(java.util.TimerTask, long, * long) */ - public void test_scheduleAtFixedRateLjava_util_TimerTaskJJ() { + public void test_scheduleAtFixedRateLjava_util_TimerTaskJJ() throws Exception { Timer t = null; try { // Ensure a Timer throws an IllegalStateException after cancelled t = new Timer(); TimerTestTask testTask = new TimerTestTask(); t.cancel(); - boolean exception = false; try { t.scheduleAtFixedRate(testTask, 100, 100); - } catch (IllegalStateException e) { - exception = true; + fail("scheduleAtFixedRate after Timer.cancel() should throw exception"); + } catch (IllegalStateException expected) { } - assertTrue( - "scheduleAtFixedRate after Timer.cancel() should throw exception", - exception); // Ensure a Timer throws an IllegalArgumentException if delay is // negative t = new Timer(); testTask = new TimerTestTask(); - exception = false; try { t.scheduleAtFixedRate(testTask, -100, 100); - } catch (IllegalArgumentException e) { - exception = true; + fail("scheduleAtFixedRate with negative delay should throw IllegalArgumentException"); + } catch (IllegalArgumentException expected) { } - assertTrue( - "scheduleAtFixedRate with negative delay should throw IllegalArgumentException", - exception); t.cancel(); // Ensure a Timer throws an IllegalArgumentException if period is // negative t = new Timer(); testTask = new TimerTestTask(); - exception = false; try { t.scheduleAtFixedRate(testTask, 100, -100); - } catch (IllegalArgumentException e) { - exception = true; + fail("scheduleAtFixedRate with negative period should throw IllegalArgumentException"); + } catch (IllegalArgumentException expected) { } - assertTrue( - "scheduleAtFixedRate with negative period should throw IllegalArgumentException", - exception); t.cancel(); // Ensure a task is run at least twice t = new Timer(); testTask = new TimerTestTask(); t.scheduleAtFixedRate(testTask, 100, 100); - try { - Thread.sleep(400); - } catch (InterruptedException e) { - } - assertTrue( - "TimerTask.run() method should have been called at least twice (" - + testTask.wasRun() + ")", testTask.wasRun() >= 2); + Thread.sleep(400); + assertTrue("TimerTask.run() method should have been called at least twice (" + + testTask.wasRun() + ")", testTask.wasRun() >= 2); t.cancel(); class SlowThenFastTask extends TimerTask { @@ -953,6 +795,7 @@ public class TimerTest extends junit.framework.TestCase { try { Thread.sleep(200); } catch (InterruptedException e) { + throw new RuntimeException(e); } } } @@ -972,13 +815,10 @@ public class TimerTest extends junit.framework.TestCase { // at least 9 times even when asleep t.scheduleAtFixedRate(slowThenFastTask, 100, 100); - try { - Thread.sleep(1000); - } catch (InterruptedException e) { - } + Thread.sleep(1000); long lastDelta = slowThenFastTask.lastDelta(); assertTrue("Fixed Rate Schedule should catch up, but is off by " - + lastDelta + " ms", slowThenFastTask.lastDelta < 300); + + lastDelta + " ms", slowThenFastTask.lastDelta < 300); t.cancel(); } finally { if (t != null) @@ -990,96 +830,70 @@ public class TimerTest extends junit.framework.TestCase { * java.util.Timer#scheduleAtFixedRate(java.util.TimerTask, * java.util.Date, long) */ - public void test_scheduleAtFixedRateLjava_util_TimerTaskLjava_util_DateJ() { + public void test_scheduleAtFixedRateLjava_util_TimerTaskLjava_util_DateJ() throws Exception { Timer t = null; try { // Ensure a Timer throws an IllegalStateException after cancelled t = new Timer(); TimerTestTask testTask = new TimerTestTask(); t.cancel(); - boolean exception = false; Date d = new Date(System.currentTimeMillis() + 100); try { t.scheduleAtFixedRate(testTask, d, 100); - } catch (IllegalStateException e) { - exception = true; + fail("scheduleAtFixedRate after Timer.cancel() should throw exception"); + } catch (IllegalStateException expected) { } - assertTrue( - "scheduleAtFixedRate after Timer.cancel() should throw exception", - exception); // Ensure a Timer throws an IllegalArgumentException if delay is // negative t = new Timer(); testTask = new TimerTestTask(); - exception = false; d = new Date(-100); try { t.scheduleAtFixedRate(testTask, d, 100); - } catch (IllegalArgumentException e) { - exception = true; + fail("scheduleAtFixedRate with negative Date should throw IllegalArgumentException"); + } catch (IllegalArgumentException expected) { } - assertTrue( - "scheduleAtFixedRate with negative Date should throw IllegalArgumentException", - exception); t.cancel(); // Ensure a Timer throws an IllegalArgumentException if period is // negative t = new Timer(); testTask = new TimerTestTask(); - exception = false; try { t.scheduleAtFixedRate(testTask, d, -100); - } catch (IllegalArgumentException e) { - exception = true; + fail("scheduleAtFixedRate with negative period should throw IllegalArgumentException"); + } catch (IllegalArgumentException expected) { } - assertTrue( - "scheduleAtFixedRate with negative period should throw IllegalArgumentException", - exception); t.cancel(); // Ensure a Timer throws an NullPointerException if date is Null t = new Timer(); testTask = new TimerTestTask(); - exception = false; try { t.scheduleAtFixedRate(testTask, null, 100); - } catch (NullPointerException e) { - exception = true; + fail("scheduleAtFixedRate with null date should throw NullPointerException"); + } catch (NullPointerException expected) { } - assertTrue( - "scheduleAtFixedRate with null date should throw NullPointerException", - exception); t.cancel(); // Ensure proper sequence of exceptions t = new Timer(); - exception = false; d = new Date(-100); try { t.scheduleAtFixedRate(null, d, 10); - } catch (NullPointerException e) { - } catch (IllegalArgumentException e) { - exception = true; + fail("Scheduling a null task with negative date should throw IllegalArgumentException first"); + } catch (IllegalArgumentException expected) { } - assertTrue( - "Scheduling a null task with negative date should throw IllegalArgumentException first", - exception); t.cancel(); // Ensure proper sequence of exceptions t = new Timer(); - exception = false; try { t.scheduleAtFixedRate(null, null, -10); - } catch (NullPointerException e) { - } catch (IllegalArgumentException e) { - exception = true; + fail("Scheduling a null task & null date & negative period should throw IllegalArgumentException first"); + } catch (IllegalArgumentException expected) { } - assertTrue( - "Scheduling a null task & null date & negative period should throw IllegalArgumentException first", - exception); t.cancel(); // Ensure a task is run at least twice @@ -1087,13 +901,9 @@ public class TimerTest extends junit.framework.TestCase { testTask = new TimerTestTask(); d = new Date(System.currentTimeMillis() + 100); t.scheduleAtFixedRate(testTask, d, 100); - try { - Thread.sleep(400); - } catch (InterruptedException e) { - } - assertTrue( - "TimerTask.run() method should have been called at least twice (" - + testTask.wasRun() + ")", testTask.wasRun() >= 2); + Thread.sleep(400); + assertTrue("TimerTask.run() method should have been called at least twice (" + + testTask.wasRun() + ")", testTask.wasRun() >= 2); t.cancel(); class SlowThenFastTask extends TimerTask { @@ -1113,6 +923,7 @@ public class TimerTest extends junit.framework.TestCase { try { Thread.sleep(200); } catch (InterruptedException e) { + throw new RuntimeException(e); } } } @@ -1133,13 +944,10 @@ public class TimerTest extends junit.framework.TestCase { // at least 9 times even when asleep t.scheduleAtFixedRate(slowThenFastTask, d, 100); - try { - Thread.sleep(1000); - } catch (InterruptedException e) { - } + Thread.sleep(1000); long lastDelta = slowThenFastTask.lastDelta(); assertTrue("Fixed Rate Schedule should catch up, but is off by " - + lastDelta + " ms", lastDelta < 300); + + lastDelta + " ms", lastDelta < 300); t.cancel(); } finally { if (t != null) @@ -1152,7 +960,7 @@ public class TimerTest extends junit.framework.TestCase { * let those exceptions bubble up, where they will both notify the thread's * uncaught exception handler and terminate the timer's thread. */ - public void testThrowingTaskKillsTimerThread() throws InterruptedException { + public void testThrowingTaskKillsTimerThread() throws Exception { final AtomicReference<Thread> threadRef = new AtomicReference<Thread>(); new Timer().schedule(new TimerTask() { @Override public void run() { |