Source Code Cross Referenced for TimerTest.java in  » Apache-Harmony-Java-SE » org-package » org » apache » harmony » luni » tests » java » util » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Apache Harmony Java SE » org package » org.apache.harmony.luni.tests.java.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


0001:        /*
0002:         *  Licensed to the Apache Software Foundation (ASF) under one or more
0003:         *  contributor license agreements.  See the NOTICE file distributed with
0004:         *  this work for additional information regarding copyright ownership.
0005:         *  The ASF licenses this file to You under the Apache License, Version 2.0
0006:         *  (the "License"); you may not use this file except in compliance with
0007:         *  the License.  You may obtain a copy of the License at
0008:         *
0009:         *     http://www.apache.org/licenses/LICENSE-2.0
0010:         *
0011:         *  Unless required by applicable law or agreed to in writing, software
0012:         *  distributed under the License is distributed on an "AS IS" BASIS,
0013:         *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0014:         *  See the License for the specific language governing permissions and
0015:         *  limitations under the License.
0016:         */
0017:
0018:        package org.apache.harmony.luni.tests.java.util;
0019:
0020:        import java.util.Date;
0021:        import java.util.Timer;
0022:        import java.util.TimerTask;
0023:
0024:        public class TimerTest extends junit.framework.TestCase {
0025:
0026:            int timerCounter = 0;
0027:
0028:            Object sync = new Object();
0029:
0030:            /**
0031:             * Warning: These tests have the possibility to leave a VM hanging if the
0032:             * Timer is not cancelled.
0033:             */
0034:            class TimerTestTask extends TimerTask {
0035:                int wasRun = 0;
0036:
0037:                // Should we sleep for 200 ms each run()?
0038:                boolean sleepInRun = false;
0039:
0040:                // Should we increment the timerCounter?
0041:                boolean incrementCount = false;
0042:
0043:                // Should we terminate the timer at a specific timerCounter?
0044:                int terminateCount = -1;
0045:
0046:                // The timer we belong to
0047:                Timer timer = null;
0048:
0049:                public TimerTestTask() {
0050:                }
0051:
0052:                public TimerTestTask(Timer t) {
0053:                    timer = t;
0054:                }
0055:
0056:                public void run() {
0057:                    synchronized (this ) {
0058:                        wasRun++;
0059:                    }
0060:                    if (incrementCount)
0061:                        timerCounter++;
0062:                    if (terminateCount == timerCounter && timer != null)
0063:                        timer.cancel();
0064:                    if (sleepInRun) {
0065:                        try {
0066:                            Thread.sleep(200);
0067:                        } catch (InterruptedException e) {
0068:                        }
0069:                    }
0070:                    synchronized (sync) {
0071:                        sync.notify();
0072:                    }
0073:                }
0074:
0075:                public synchronized int wasRun() {
0076:                    return wasRun;
0077:                }
0078:
0079:                public void sleepInRun(boolean sleepInRun) {
0080:                    this .sleepInRun = sleepInRun;
0081:                }
0082:
0083:                public void incrementCount(boolean incrementCount) {
0084:                    this .incrementCount = incrementCount;
0085:                }
0086:
0087:                public void terminateCount(int terminateCount) {
0088:                    this .terminateCount = terminateCount;
0089:                }
0090:            }
0091:
0092:            /**
0093:             * @tests java.util.Timer#Timer(boolean)
0094:             */
0095:            public void test_ConstructorZ() {
0096:                Timer t = null;
0097:                try {
0098:                    // Ensure a task is run
0099:                    t = new Timer(true);
0100:                    TimerTestTask testTask = new TimerTestTask();
0101:                    t.schedule(testTask, 200);
0102:                    synchronized (sync) {
0103:                        try {
0104:                            sync.wait(1000);
0105:                        } catch (InterruptedException e) {
0106:                        }
0107:                    }
0108:                    assertEquals(
0109:                            "TimerTask.run() method not called after 200ms", 1,
0110:                            testTask.wasRun());
0111:                    t.cancel();
0112:                } finally {
0113:                    if (t != null)
0114:                        t.cancel();
0115:                }
0116:
0117:            }
0118:
0119:            /**
0120:             * @tests java.util.Timer#Timer()
0121:             */
0122:            public void test_Constructor() {
0123:                Timer t = null;
0124:                try {
0125:                    // Ensure a task is run
0126:                    t = new Timer();
0127:                    TimerTestTask testTask = new TimerTestTask();
0128:                    t.schedule(testTask, 200);
0129:                    synchronized (sync) {
0130:                        try {
0131:                            sync.wait(1000);
0132:                        } catch (InterruptedException e) {
0133:                        }
0134:                    }
0135:                    assertEquals(
0136:                            "TimerTask.run() method not called after 200ms", 1,
0137:                            testTask.wasRun());
0138:                    t.cancel();
0139:                } finally {
0140:                    if (t != null)
0141:                        t.cancel();
0142:                }
0143:
0144:            }
0145:
0146:            /**
0147:             * @tests java.util.Timer#Timer(String, boolean)
0148:             */
0149:            public void test_ConstructorSZ() {
0150:                Timer t = null;
0151:                try {
0152:                    // Ensure a task is run
0153:                    t = new Timer("test_ConstructorSZThread", true);
0154:                    TimerTestTask testTask = new TimerTestTask();
0155:                    t.schedule(testTask, 200);
0156:                    synchronized (sync) {
0157:                        try {
0158:                            sync.wait(1000);
0159:                        } catch (InterruptedException e) {
0160:                        }
0161:                    }
0162:                    assertEquals(
0163:                            "TimerTask.run() method not called after 200ms", 1,
0164:                            testTask.wasRun());
0165:                    t.cancel();
0166:                } finally {
0167:                    if (t != null)
0168:                        t.cancel();
0169:                }
0170:            }
0171:
0172:            /**
0173:             * @tests java.util.Timer#Timer(String)
0174:             */
0175:            public void test_ConstructorS() {
0176:                Timer t = null;
0177:                try {
0178:                    // Ensure a task is run
0179:                    t = new Timer("test_ConstructorSThread");
0180:                    TimerTestTask testTask = new TimerTestTask();
0181:                    t.schedule(testTask, 200);
0182:                    synchronized (sync) {
0183:                        try {
0184:                            sync.wait(1000);
0185:                        } catch (InterruptedException e) {
0186:                        }
0187:                    }
0188:                    assertEquals(
0189:                            "TimerTask.run() method not called after 200ms", 1,
0190:                            testTask.wasRun());
0191:                    t.cancel();
0192:                } finally {
0193:                    if (t != null)
0194:                        t.cancel();
0195:                }
0196:            }
0197:
0198:            /**
0199:             * @tests java.util.Timer#cancel()
0200:             */
0201:            public void test_cancel() {
0202:                Timer t = null;
0203:                try {
0204:                    // Ensure a task throws an IllegalStateException after cancelled
0205:                    t = new Timer();
0206:                    TimerTestTask testTask = new TimerTestTask();
0207:                    t.cancel();
0208:                    boolean exception = false;
0209:                    try {
0210:                        t.schedule(testTask, 100, 200);
0211:                    } catch (IllegalStateException e) {
0212:                        exception = true;
0213:                    }
0214:                    assertTrue(
0215:                            "Scheduling a task after Timer.cancel() should throw exception",
0216:                            exception);
0217:
0218:                    // Ensure a task is run but not after cancel
0219:                    t = new Timer();
0220:                    testTask = new TimerTestTask();
0221:                    t.schedule(testTask, 100, 500);
0222:                    synchronized (sync) {
0223:                        try {
0224:                            sync.wait(1000);
0225:                        } catch (InterruptedException e) {
0226:                        }
0227:                    }
0228:                    assertEquals(
0229:                            "TimerTask.run() method not called after 200ms", 1,
0230:                            testTask.wasRun());
0231:                    t.cancel();
0232:                    synchronized (sync) {
0233:                        try {
0234:                            sync.wait(500);
0235:                        } catch (InterruptedException e) {
0236:                        }
0237:                    }
0238:                    assertEquals(
0239:                            "TimerTask.run() method should not have been called after cancel",
0240:                            1, testTask.wasRun());
0241:
0242:                    // Ensure you can call cancel more than once
0243:                    t = new Timer();
0244:                    testTask = new TimerTestTask();
0245:                    t.schedule(testTask, 100, 500);
0246:                    synchronized (sync) {
0247:                        try {
0248:                            sync.wait(500);
0249:                        } catch (InterruptedException e) {
0250:                        }
0251:                    }
0252:                    assertEquals(
0253:                            "TimerTask.run() method not called after 200ms", 1,
0254:                            testTask.wasRun());
0255:                    t.cancel();
0256:                    t.cancel();
0257:                    t.cancel();
0258:                    synchronized (sync) {
0259:                        try {
0260:                            sync.wait(500);
0261:                        } catch (InterruptedException e) {
0262:                        }
0263:                    }
0264:                    assertEquals(
0265:                            "TimerTask.run() method should not have been called after cancel",
0266:                            1, testTask.wasRun());
0267:
0268:                    // Ensure that a call to cancel from within a timer ensures no more
0269:                    // run
0270:                    t = new Timer();
0271:                    testTask = new TimerTestTask(t);
0272:                    testTask.incrementCount(true);
0273:                    testTask.terminateCount(5); // Terminate after 5 runs
0274:                    t.schedule(testTask, 100, 100);
0275:                    synchronized (sync) {
0276:                        try {
0277:                            sync.wait(200);
0278:                            sync.wait(200);
0279:                            sync.wait(200);
0280:                            sync.wait(200);
0281:                            sync.wait(200);
0282:                            sync.wait(200);
0283:                        } catch (InterruptedException e) {
0284:                        }
0285:                    }
0286:                    assertTrue(
0287:                            "TimerTask.run() method should be called 5 times not "
0288:                                    + testTask.wasRun(), testTask.wasRun() == 5);
0289:                    t.cancel();
0290:                    try {
0291:                        Thread.sleep(200);
0292:                    } catch (InterruptedException e) {
0293:                    }
0294:                } finally {
0295:                    if (t != null)
0296:                        t.cancel();
0297:                }
0298:
0299:            }
0300:
0301:            /**
0302:             * @tests java.util.Timer#purge()
0303:             */
0304:            public void test_purge() throws Exception {
0305:                Timer t = null;
0306:                try {
0307:                    t = new Timer();
0308:                    assertEquals(0, t.purge());
0309:
0310:                    TimerTestTask[] tasks = new TimerTestTask[100];
0311:                    int[] delayTime = { 50, 80, 20, 70, 40, 10, 90, 30, 60 };
0312:
0313:                    int j = 0;
0314:                    for (int i = 0; i < 100; i++) {
0315:                        tasks[i] = new TimerTestTask();
0316:                        t.schedule(tasks[i], delayTime[j++], 200);
0317:                        if (j == 9) {
0318:                            j = 0;
0319:                        }
0320:                    }
0321:
0322:                    for (int i = 0; i < 50; i++) {
0323:                        tasks[i].cancel();
0324:                    }
0325:
0326:                    assertTrue(t.purge() <= 50);
0327:                    assertEquals(0, t.purge());
0328:                } finally {
0329:                    if (t != null) {
0330:                        t.cancel();
0331:                    }
0332:                }
0333:            }
0334:
0335:            /**
0336:             * @tests java.util.Timer#schedule(java.util.TimerTask, java.util.Date)
0337:             */
0338:            public void test_scheduleLjava_util_TimerTaskLjava_util_Date() {
0339:                Timer t = null;
0340:                try {
0341:                    // Ensure a Timer throws an IllegalStateException after cancelled
0342:                    t = new Timer();
0343:                    TimerTestTask testTask = new TimerTestTask();
0344:                    Date d = new Date(System.currentTimeMillis() + 100);
0345:                    t.cancel();
0346:                    boolean exception = false;
0347:                    try {
0348:                        t.schedule(testTask, d);
0349:                    } catch (IllegalStateException e) {
0350:                        exception = true;
0351:                    }
0352:                    assertTrue(
0353:                            "Scheduling a task after Timer.cancel() should throw exception",
0354:                            exception);
0355:
0356:                    // Ensure a Timer throws an IllegalStateException if task already
0357:                    // cancelled
0358:                    t = new Timer();
0359:                    testTask = new TimerTestTask();
0360:                    d = new Date(System.currentTimeMillis() + 100);
0361:                    testTask.cancel();
0362:                    exception = false;
0363:                    try {
0364:                        t.schedule(testTask, d);
0365:                    } catch (IllegalStateException e) {
0366:                        exception = true;
0367:                    }
0368:                    assertTrue(
0369:                            "Scheduling a task after cancelling it should throw exception",
0370:                            exception);
0371:                    t.cancel();
0372:
0373:                    // Ensure a Timer throws an IllegalArgumentException if delay is
0374:                    // negative
0375:                    t = new Timer();
0376:                    testTask = new TimerTestTask();
0377:                    d = new Date(-100);
0378:                    exception = false;
0379:                    try {
0380:                        t.schedule(testTask, d);
0381:                    } catch (IllegalArgumentException e) {
0382:                        exception = true;
0383:                    }
0384:                    assertTrue(
0385:                            "Scheduling a task with negative date should throw IllegalArgumentException",
0386:                            exception);
0387:                    t.cancel();
0388:
0389:                    // Ensure a Timer throws a NullPointerException if the task is null
0390:                    t = new Timer();
0391:                    exception = false;
0392:                    d = new Date(System.currentTimeMillis() + 100);
0393:                    try {
0394:                        t.schedule(null, d);
0395:                    } catch (NullPointerException e) {
0396:                        exception = true;
0397:                    }
0398:                    assertTrue(
0399:                            "Scheduling a null task should throw NullPointerException",
0400:                            exception);
0401:                    t.cancel();
0402:
0403:                    // Ensure a Timer throws a NullPointerException if the date is null
0404:                    t = new Timer();
0405:                    testTask = new TimerTestTask();
0406:                    exception = false;
0407:                    try {
0408:                        t.schedule(testTask, null);
0409:                    } catch (NullPointerException e) {
0410:                        exception = true;
0411:                    }
0412:                    assertTrue(
0413:                            "Scheduling a null date should throw NullPointerException",
0414:                            exception);
0415:                    t.cancel();
0416:
0417:                    // Ensure proper sequence of exceptions
0418:                    t = new Timer();
0419:                    d = new Date(-100);
0420:                    exception = false;
0421:                    try {
0422:                        t.schedule(null, d);
0423:                    } catch (NullPointerException e) {
0424:                    } catch (IllegalArgumentException e) {
0425:                        exception = true;
0426:                    }
0427:                    assertTrue(
0428:                            "Scheduling a null task with negative date should throw IllegalArgumentException first",
0429:                            exception);
0430:                    t.cancel();
0431:
0432:                    // Ensure a task is run
0433:                    t = new Timer();
0434:                    testTask = new TimerTestTask();
0435:                    d = new Date(System.currentTimeMillis() + 200);
0436:                    t.schedule(testTask, d);
0437:                    try {
0438:                        Thread.sleep(400);
0439:                    } catch (InterruptedException e) {
0440:                    }
0441:                    assertEquals(
0442:                            "TimerTask.run() method not called after 200ms", 1,
0443:                            testTask.wasRun());
0444:                    t.cancel();
0445:
0446:                    // Ensure multiple tasks are run
0447:                    t = new Timer();
0448:                    testTask = new TimerTestTask();
0449:                    testTask.incrementCount(true);
0450:                    d = new Date(System.currentTimeMillis() + 100);
0451:                    t.schedule(testTask, d);
0452:                    testTask = new TimerTestTask();
0453:                    testTask.incrementCount(true);
0454:                    d = new Date(System.currentTimeMillis() + 150);
0455:                    t.schedule(testTask, d);
0456:                    testTask = new TimerTestTask();
0457:                    testTask.incrementCount(true);
0458:                    d = new Date(System.currentTimeMillis() + 70);
0459:                    t.schedule(testTask, d);
0460:                    testTask = new TimerTestTask();
0461:                    testTask.incrementCount(true);
0462:                    d = new Date(System.currentTimeMillis() + 10);
0463:                    t.schedule(testTask, d);
0464:                    try {
0465:                        Thread.sleep(400);
0466:                    } catch (InterruptedException e) {
0467:                    }
0468:                    assertTrue(
0469:                            "Multiple tasks should have incremented counter 4 times not "
0470:                                    + timerCounter, timerCounter == 4);
0471:                    t.cancel();
0472:                } finally {
0473:                    if (t != null)
0474:                        t.cancel();
0475:                }
0476:            }
0477:
0478:            /**
0479:             * @tests java.util.Timer#schedule(java.util.TimerTask, long)
0480:             */
0481:            public void test_scheduleLjava_util_TimerTaskJ() {
0482:                Timer t = null;
0483:                try {
0484:                    // Ensure a Timer throws an IllegalStateException after cancelled
0485:                    t = new Timer();
0486:                    TimerTestTask testTask = new TimerTestTask();
0487:                    t.cancel();
0488:                    boolean exception = false;
0489:                    try {
0490:                        t.schedule(testTask, 100);
0491:                    } catch (IllegalStateException e) {
0492:                        exception = true;
0493:                    }
0494:                    assertTrue(
0495:                            "Scheduling a task after Timer.cancel() should throw exception",
0496:                            exception);
0497:
0498:                    // Ensure a Timer throws an IllegalStateException if task already
0499:                    // cancelled
0500:                    t = new Timer();
0501:                    testTask = new TimerTestTask();
0502:                    testTask.cancel();
0503:                    exception = false;
0504:                    try {
0505:                        t.schedule(testTask, 100);
0506:                    } catch (IllegalStateException e) {
0507:                        exception = true;
0508:                    }
0509:                    assertTrue(
0510:                            "Scheduling a task after cancelling it should throw exception",
0511:                            exception);
0512:                    t.cancel();
0513:
0514:                    // Ensure a Timer throws an IllegalArgumentException if delay is
0515:                    // negative
0516:                    t = new Timer();
0517:                    testTask = new TimerTestTask();
0518:                    exception = false;
0519:                    try {
0520:                        t.schedule(testTask, -100);
0521:                    } catch (IllegalArgumentException e) {
0522:                        exception = true;
0523:                    }
0524:                    assertTrue(
0525:                            "Scheduling a task with negative delay should throw IllegalArgumentException",
0526:                            exception);
0527:                    t.cancel();
0528:
0529:                    // Ensure a Timer throws a NullPointerException if the task is null
0530:                    t = new Timer();
0531:                    exception = false;
0532:                    try {
0533:                        t.schedule(null, 10);
0534:                    } catch (NullPointerException e) {
0535:                        exception = true;
0536:                    }
0537:                    assertTrue(
0538:                            "Scheduling a null task should throw NullPointerException",
0539:                            exception);
0540:                    t.cancel();
0541:
0542:                    // Ensure proper sequence of exceptions
0543:                    t = new Timer();
0544:                    exception = false;
0545:                    try {
0546:                        t.schedule(null, -10);
0547:                    } catch (NullPointerException e) {
0548:                    } catch (IllegalArgumentException e) {
0549:                        exception = true;
0550:                    }
0551:                    assertTrue(
0552:                            "Scheduling a null task with negative delays should throw IllegalArgumentException first",
0553:                            exception);
0554:                    t.cancel();
0555:
0556:                    // Ensure a task is run
0557:                    t = new Timer();
0558:                    testTask = new TimerTestTask();
0559:                    t.schedule(testTask, 200);
0560:                    try {
0561:                        Thread.sleep(400);
0562:                    } catch (InterruptedException e) {
0563:                    }
0564:                    assertEquals(
0565:                            "TimerTask.run() method not called after 200ms", 1,
0566:                            testTask.wasRun());
0567:                    t.cancel();
0568:
0569:                    // Ensure multiple tasks are run
0570:                    t = new Timer();
0571:                    testTask = new TimerTestTask();
0572:                    testTask.incrementCount(true);
0573:                    t.schedule(testTask, 100);
0574:                    testTask = new TimerTestTask();
0575:                    testTask.incrementCount(true);
0576:                    t.schedule(testTask, 150);
0577:                    testTask = new TimerTestTask();
0578:                    testTask.incrementCount(true);
0579:                    t.schedule(testTask, 70);
0580:                    testTask = new TimerTestTask();
0581:                    testTask.incrementCount(true);
0582:                    t.schedule(testTask, 10);
0583:                    try {
0584:                        Thread.sleep(400);
0585:                    } catch (InterruptedException e) {
0586:                    }
0587:                    assertTrue(
0588:                            "Multiple tasks should have incremented counter 4 times not "
0589:                                    + timerCounter, timerCounter == 4);
0590:                    t.cancel();
0591:                } finally {
0592:                    if (t != null)
0593:                        t.cancel();
0594:                }
0595:            }
0596:
0597:            /**
0598:             * @tests java.util.Timer#schedule(java.util.TimerTask, long, long)
0599:             */
0600:            public void test_scheduleLjava_util_TimerTaskJJ() {
0601:                Timer t = null;
0602:                try {
0603:                    // Ensure a Timer throws an IllegalStateException after cancelled
0604:                    t = new Timer();
0605:                    TimerTestTask testTask = new TimerTestTask();
0606:                    t.cancel();
0607:                    boolean exception = false;
0608:                    try {
0609:                        t.schedule(testTask, 100, 100);
0610:                    } catch (IllegalStateException e) {
0611:                        exception = true;
0612:                    }
0613:                    assertTrue(
0614:                            "Scheduling a task after Timer.cancel() should throw exception",
0615:                            exception);
0616:
0617:                    // Ensure a Timer throws an IllegalStateException if task already
0618:                    // cancelled
0619:                    t = new Timer();
0620:                    testTask = new TimerTestTask();
0621:                    testTask.cancel();
0622:                    exception = false;
0623:                    try {
0624:                        t.schedule(testTask, 100, 100);
0625:                    } catch (IllegalStateException e) {
0626:                        exception = true;
0627:                    }
0628:                    assertTrue(
0629:                            "Scheduling a task after cancelling it should throw exception",
0630:                            exception);
0631:                    t.cancel();
0632:
0633:                    // Ensure a Timer throws an IllegalArgumentException if delay is
0634:                    // negative
0635:                    t = new Timer();
0636:                    testTask = new TimerTestTask();
0637:                    exception = false;
0638:                    try {
0639:                        t.schedule(testTask, -100, 100);
0640:                    } catch (IllegalArgumentException e) {
0641:                        exception = true;
0642:                    }
0643:                    assertTrue(
0644:                            "Scheduling a task with negative delay should throw IllegalArgumentException",
0645:                            exception);
0646:                    t.cancel();
0647:
0648:                    // Ensure a Timer throws an IllegalArgumentException if period is
0649:                    // negative
0650:                    t = new Timer();
0651:                    testTask = new TimerTestTask();
0652:                    exception = false;
0653:                    try {
0654:                        t.schedule(testTask, 100, -100);
0655:                    } catch (IllegalArgumentException e) {
0656:                        exception = true;
0657:                    }
0658:                    assertTrue(
0659:                            "Scheduling a task with negative period should throw IllegalArgumentException",
0660:                            exception);
0661:                    t.cancel();
0662:
0663:                    // Ensure a Timer throws an IllegalArgumentException if period is
0664:                    // zero
0665:                    t = new Timer();
0666:                    testTask = new TimerTestTask();
0667:                    exception = false;
0668:                    try {
0669:                        t.schedule(testTask, 100, 0);
0670:                    } catch (IllegalArgumentException e) {
0671:                        exception = true;
0672:                    }
0673:                    assertTrue(
0674:                            "Scheduling a task with 0 period should throw IllegalArgumentException",
0675:                            exception);
0676:                    t.cancel();
0677:
0678:                    // Ensure a Timer throws a NullPointerException if the task is null
0679:                    t = new Timer();
0680:                    exception = false;
0681:                    try {
0682:                        t.schedule(null, 10, 10);
0683:                    } catch (NullPointerException e) {
0684:                        exception = true;
0685:                    }
0686:                    assertTrue(
0687:                            "Scheduling a null task should throw NullPointerException",
0688:                            exception);
0689:                    t.cancel();
0690:
0691:                    // Ensure proper sequence of exceptions
0692:                    t = new Timer();
0693:                    exception = false;
0694:                    try {
0695:                        t.schedule(null, -10, -10);
0696:                    } catch (NullPointerException e) {
0697:                    } catch (IllegalArgumentException e) {
0698:                        exception = true;
0699:                    }
0700:                    assertTrue(
0701:                            "Scheduling a null task with negative delays should throw IllegalArgumentException first",
0702:                            exception);
0703:                    t.cancel();
0704:
0705:                    // Ensure a task is run at least twice
0706:                    t = new Timer();
0707:                    testTask = new TimerTestTask();
0708:                    t.schedule(testTask, 100, 100);
0709:                    try {
0710:                        Thread.sleep(400);
0711:                    } catch (InterruptedException e) {
0712:                    }
0713:                    assertTrue(
0714:                            "TimerTask.run() method should have been called at least twice ("
0715:                                    + testTask.wasRun() + ")", testTask
0716:                                    .wasRun() >= 2);
0717:                    t.cancel();
0718:
0719:                    // Ensure multiple tasks are run
0720:                    t = new Timer();
0721:                    testTask = new TimerTestTask();
0722:                    testTask.incrementCount(true);
0723:                    t.schedule(testTask, 100, 100); // at least 9 times
0724:                    testTask = new TimerTestTask();
0725:                    testTask.incrementCount(true);
0726:                    t.schedule(testTask, 200, 100); // at least 7 times
0727:                    testTask = new TimerTestTask();
0728:                    testTask.incrementCount(true);
0729:                    t.schedule(testTask, 300, 200); // at least 4 times
0730:                    testTask = new TimerTestTask();
0731:                    testTask.incrementCount(true);
0732:                    t.schedule(testTask, 100, 200); // at least 4 times
0733:                    try {
0734:                        Thread.sleep(1200); // Allowed more room for error
0735:                    } catch (InterruptedException e) {
0736:                    }
0737:                    assertTrue(
0738:                            "Multiple tasks should have incremented counter 24 times not "
0739:                                    + timerCounter, timerCounter >= 24);
0740:                    t.cancel();
0741:                } finally {
0742:                    if (t != null)
0743:                        t.cancel();
0744:                }
0745:            }
0746:
0747:            /**
0748:             * @tests java.util.Timer#schedule(java.util.TimerTask, java.util.Date,
0749:             *        long)
0750:             */
0751:            public void test_scheduleLjava_util_TimerTaskLjava_util_DateJ() {
0752:                Timer t = null;
0753:                try {
0754:                    // Ensure a Timer throws an IllegalStateException after cancelled
0755:                    t = new Timer();
0756:                    TimerTestTask testTask = new TimerTestTask();
0757:                    Date d = new Date(System.currentTimeMillis() + 100);
0758:                    t.cancel();
0759:                    boolean exception = false;
0760:                    try {
0761:                        t.schedule(testTask, d, 100);
0762:                    } catch (IllegalStateException e) {
0763:                        exception = true;
0764:                    }
0765:                    assertTrue(
0766:                            "Scheduling a task after Timer.cancel() should throw exception",
0767:                            exception);
0768:
0769:                    // Ensure a Timer throws an IllegalStateException if task already
0770:                    // cancelled
0771:                    t = new Timer();
0772:                    d = new Date(System.currentTimeMillis() + 100);
0773:                    testTask = new TimerTestTask();
0774:                    testTask.cancel();
0775:                    exception = false;
0776:                    try {
0777:                        t.schedule(testTask, d, 100);
0778:                    } catch (IllegalStateException e) {
0779:                        exception = true;
0780:                    }
0781:                    assertTrue(
0782:                            "Scheduling a task after cancelling it should throw exception",
0783:                            exception);
0784:                    t.cancel();
0785:
0786:                    // Ensure a Timer throws an IllegalArgumentException if delay is
0787:                    // negative
0788:                    t = new Timer();
0789:                    d = new Date(-100);
0790:                    testTask = new TimerTestTask();
0791:                    exception = false;
0792:                    try {
0793:                        t.schedule(testTask, d, 100);
0794:                    } catch (IllegalArgumentException e) {
0795:                        exception = true;
0796:                    }
0797:                    assertTrue(
0798:                            "Scheduling a task with negative delay should throw IllegalArgumentException",
0799:                            exception);
0800:                    t.cancel();
0801:
0802:                    // Ensure a Timer throws an IllegalArgumentException if period is
0803:                    // negative
0804:                    t = new Timer();
0805:                    d = new Date(System.currentTimeMillis() + 100);
0806:                    testTask = new TimerTestTask();
0807:                    exception = false;
0808:                    try {
0809:                        t.schedule(testTask, d, -100);
0810:                    } catch (IllegalArgumentException e) {
0811:                        exception = true;
0812:                    }
0813:                    assertTrue(
0814:                            "Scheduling a task with negative period should throw IllegalArgumentException",
0815:                            exception);
0816:                    t.cancel();
0817:
0818:                    // Ensure a Timer throws a NullPointerException if the task is null
0819:                    t = new Timer();
0820:                    d = new Date(System.currentTimeMillis() + 100);
0821:                    exception = false;
0822:                    try {
0823:                        t.schedule(null, d, 10);
0824:                    } catch (NullPointerException e) {
0825:                        exception = true;
0826:                    }
0827:                    assertTrue(
0828:                            "Scheduling a null task should throw NullPointerException",
0829:                            exception);
0830:                    t.cancel();
0831:
0832:                    // Ensure a Timer throws a NullPointerException if the date is null
0833:                    t = new Timer();
0834:                    testTask = new TimerTestTask();
0835:                    exception = false;
0836:                    try {
0837:                        t.schedule(testTask, null, 10);
0838:                    } catch (NullPointerException e) {
0839:                        exception = true;
0840:                    }
0841:                    assertTrue(
0842:                            "Scheduling a null task should throw NullPointerException",
0843:                            exception);
0844:                    t.cancel();
0845:
0846:                    // Ensure proper sequence of exceptions
0847:                    t = new Timer();
0848:                    d = new Date(-100);
0849:                    exception = false;
0850:                    try {
0851:                        t.schedule(null, d, 10);
0852:                    } catch (NullPointerException e) {
0853:                    } catch (IllegalArgumentException e) {
0854:                        exception = true;
0855:                    }
0856:                    assertTrue(
0857:                            "Scheduling a null task with negative dates should throw IllegalArgumentException first",
0858:                            exception);
0859:                    t.cancel();
0860:
0861:                    // Ensure a task is run at least twice
0862:                    t = new Timer();
0863:                    d = new Date(System.currentTimeMillis() + 100);
0864:                    testTask = new TimerTestTask();
0865:                    t.schedule(testTask, d, 100);
0866:                    try {
0867:                        Thread.sleep(800);
0868:                    } catch (InterruptedException e) {
0869:                    }
0870:                    assertTrue(
0871:                            "TimerTask.run() method should have been called at least twice ("
0872:                                    + testTask.wasRun() + ")", testTask
0873:                                    .wasRun() >= 2);
0874:                    t.cancel();
0875:
0876:                    // Ensure multiple tasks are run
0877:                    t = new Timer();
0878:                    testTask = new TimerTestTask();
0879:                    testTask.incrementCount(true);
0880:                    d = new Date(System.currentTimeMillis() + 100);
0881:                    t.schedule(testTask, d, 100); // at least 9 times
0882:                    testTask = new TimerTestTask();
0883:                    testTask.incrementCount(true);
0884:                    d = new Date(System.currentTimeMillis() + 200);
0885:                    t.schedule(testTask, d, 100); // at least 7 times
0886:                    testTask = new TimerTestTask();
0887:                    testTask.incrementCount(true);
0888:                    d = new Date(System.currentTimeMillis() + 300);
0889:                    t.schedule(testTask, d, 200); // at least 4 times
0890:                    testTask = new TimerTestTask();
0891:                    testTask.incrementCount(true);
0892:                    d = new Date(System.currentTimeMillis() + 100);
0893:                    t.schedule(testTask, d, 200); // at least 4 times
0894:                    try {
0895:                        Thread.sleep(3000);
0896:                    } catch (InterruptedException e) {
0897:                    }
0898:                    assertTrue(
0899:                            "Multiple tasks should have incremented counter 24 times not "
0900:                                    + timerCounter, timerCounter >= 24);
0901:                    t.cancel();
0902:                } finally {
0903:                    if (t != null)
0904:                        t.cancel();
0905:                }
0906:            }
0907:
0908:            /**
0909:             * @tests java.util.Timer#scheduleAtFixedRate(java.util.TimerTask, long,
0910:             *        long)
0911:             */
0912:            public void test_scheduleAtFixedRateLjava_util_TimerTaskJJ() {
0913:                Timer t = null;
0914:                try {
0915:                    // Ensure a Timer throws an IllegalStateException after cancelled
0916:                    t = new Timer();
0917:                    TimerTestTask testTask = new TimerTestTask();
0918:                    t.cancel();
0919:                    boolean exception = false;
0920:                    try {
0921:                        t.scheduleAtFixedRate(testTask, 100, 100);
0922:                    } catch (IllegalStateException e) {
0923:                        exception = true;
0924:                    }
0925:                    assertTrue(
0926:                            "scheduleAtFixedRate after Timer.cancel() should throw exception",
0927:                            exception);
0928:
0929:                    // Ensure a Timer throws an IllegalArgumentException if delay is
0930:                    // negative
0931:                    t = new Timer();
0932:                    testTask = new TimerTestTask();
0933:                    exception = false;
0934:                    try {
0935:                        t.scheduleAtFixedRate(testTask, -100, 100);
0936:                    } catch (IllegalArgumentException e) {
0937:                        exception = true;
0938:                    }
0939:                    assertTrue(
0940:                            "scheduleAtFixedRate with negative delay should throw IllegalArgumentException",
0941:                            exception);
0942:                    t.cancel();
0943:
0944:                    // Ensure a Timer throws an IllegalArgumentException if period is
0945:                    // negative
0946:                    t = new Timer();
0947:                    testTask = new TimerTestTask();
0948:                    exception = false;
0949:                    try {
0950:                        t.scheduleAtFixedRate(testTask, 100, -100);
0951:                    } catch (IllegalArgumentException e) {
0952:                        exception = true;
0953:                    }
0954:                    assertTrue(
0955:                            "scheduleAtFixedRate with negative period should throw IllegalArgumentException",
0956:                            exception);
0957:                    t.cancel();
0958:
0959:                    // Ensure a task is run at least twice
0960:                    t = new Timer();
0961:                    testTask = new TimerTestTask();
0962:                    t.scheduleAtFixedRate(testTask, 100, 100);
0963:                    try {
0964:                        Thread.sleep(400);
0965:                    } catch (InterruptedException e) {
0966:                    }
0967:                    assertTrue(
0968:                            "TimerTask.run() method should have been called at least twice ("
0969:                                    + testTask.wasRun() + ")", testTask
0970:                                    .wasRun() >= 2);
0971:                    t.cancel();
0972:
0973:                    class SlowThenFastTask extends TimerTask {
0974:                        int wasRun = 0;
0975:
0976:                        long startedAt;
0977:
0978:                        long lastDelta;
0979:
0980:                        public void run() {
0981:                            if (wasRun == 0)
0982:                                startedAt = System.currentTimeMillis();
0983:                            lastDelta = System.currentTimeMillis()
0984:                                    - (startedAt + (100 * wasRun));
0985:                            wasRun++;
0986:                            if (wasRun == 2) {
0987:                                try {
0988:                                    Thread.sleep(200);
0989:                                } catch (InterruptedException e) {
0990:                                }
0991:                            }
0992:                        }
0993:
0994:                        public long lastDelta() {
0995:                            return lastDelta;
0996:                        }
0997:
0998:                        public int wasRun() {
0999:                            return wasRun;
1000:                        }
1001:                    }
1002:
1003:                    // Ensure multiple tasks are run
1004:                    t = new Timer();
1005:                    SlowThenFastTask slowThenFastTask = new SlowThenFastTask();
1006:
1007:                    // at least 9 times even when asleep
1008:                    t.scheduleAtFixedRate(slowThenFastTask, 100, 100);
1009:                    try {
1010:                        Thread.sleep(1000);
1011:                    } catch (InterruptedException e) {
1012:                    }
1013:                    long lastDelta = slowThenFastTask.lastDelta();
1014:                    assertTrue(
1015:                            "Fixed Rate Schedule should catch up, but is off by "
1016:                                    + lastDelta + " ms",
1017:                            slowThenFastTask.lastDelta < 300);
1018:                    t.cancel();
1019:                } finally {
1020:                    if (t != null)
1021:                        t.cancel();
1022:                }
1023:            }
1024:
1025:            /**
1026:             * @tests java.util.Timer#scheduleAtFixedRate(java.util.TimerTask,
1027:             *        java.util.Date, long)
1028:             */
1029:            public void test_scheduleAtFixedRateLjava_util_TimerTaskLjava_util_DateJ() {
1030:                Timer t = null;
1031:                try {
1032:                    // Ensure a Timer throws an IllegalStateException after cancelled
1033:                    t = new Timer();
1034:                    TimerTestTask testTask = new TimerTestTask();
1035:                    t.cancel();
1036:                    boolean exception = false;
1037:                    Date d = new Date(System.currentTimeMillis() + 100);
1038:                    try {
1039:                        t.scheduleAtFixedRate(testTask, d, 100);
1040:                    } catch (IllegalStateException e) {
1041:                        exception = true;
1042:                    }
1043:                    assertTrue(
1044:                            "scheduleAtFixedRate after Timer.cancel() should throw exception",
1045:                            exception);
1046:
1047:                    // Ensure a Timer throws an IllegalArgumentException if delay is
1048:                    // negative
1049:                    t = new Timer();
1050:                    testTask = new TimerTestTask();
1051:                    exception = false;
1052:                    d = new Date(-100);
1053:                    try {
1054:                        t.scheduleAtFixedRate(testTask, d, 100);
1055:                    } catch (IllegalArgumentException e) {
1056:                        exception = true;
1057:                    }
1058:                    assertTrue(
1059:                            "scheduleAtFixedRate with negative Date should throw IllegalArgumentException",
1060:                            exception);
1061:                    t.cancel();
1062:
1063:                    // Ensure a Timer throws an IllegalArgumentException if period is
1064:                    // negative
1065:                    t = new Timer();
1066:                    testTask = new TimerTestTask();
1067:                    exception = false;
1068:                    try {
1069:                        t.scheduleAtFixedRate(testTask, d, -100);
1070:                    } catch (IllegalArgumentException e) {
1071:                        exception = true;
1072:                    }
1073:                    assertTrue(
1074:                            "scheduleAtFixedRate with negative period should throw IllegalArgumentException",
1075:                            exception);
1076:                    t.cancel();
1077:
1078:                    // Ensure a Timer throws an NullPointerException if date is Null
1079:                    t = new Timer();
1080:                    testTask = new TimerTestTask();
1081:                    exception = false;
1082:                    try {
1083:                        t.scheduleAtFixedRate(testTask, null, 100);
1084:                    } catch (NullPointerException e) {
1085:                        exception = true;
1086:                    }
1087:                    assertTrue(
1088:                            "scheduleAtFixedRate with null date should throw NullPointerException",
1089:                            exception);
1090:                    t.cancel();
1091:
1092:                    // Ensure proper sequence of exceptions
1093:                    t = new Timer();
1094:                    exception = false;
1095:                    d = new Date(-100);
1096:                    try {
1097:                        t.scheduleAtFixedRate(null, d, 10);
1098:                    } catch (NullPointerException e) {
1099:                    } catch (IllegalArgumentException e) {
1100:                        exception = true;
1101:                    }
1102:                    assertTrue(
1103:                            "Scheduling a null task with negative date should throw IllegalArgumentException first",
1104:                            exception);
1105:                    t.cancel();
1106:
1107:                    // Ensure proper sequence of exceptions
1108:                    t = new Timer();
1109:                    exception = false;
1110:                    try {
1111:                        t.scheduleAtFixedRate(null, null, -10);
1112:                    } catch (NullPointerException e) {
1113:                    } catch (IllegalArgumentException e) {
1114:                        exception = true;
1115:                    }
1116:                    assertTrue(
1117:                            "Scheduling a null task & null date & negative period should throw IllegalArgumentException first",
1118:                            exception);
1119:                    t.cancel();
1120:
1121:                    // Ensure a task is run at least twice
1122:                    t = new Timer();
1123:                    testTask = new TimerTestTask();
1124:                    d = new Date(System.currentTimeMillis() + 100);
1125:                    t.scheduleAtFixedRate(testTask, d, 100);
1126:                    try {
1127:                        Thread.sleep(400);
1128:                    } catch (InterruptedException e) {
1129:                    }
1130:                    assertTrue(
1131:                            "TimerTask.run() method should have been called at least twice ("
1132:                                    + testTask.wasRun() + ")", testTask
1133:                                    .wasRun() >= 2);
1134:                    t.cancel();
1135:
1136:                    class SlowThenFastTask extends TimerTask {
1137:                        int wasRun = 0;
1138:
1139:                        long startedAt;
1140:
1141:                        long lastDelta;
1142:
1143:                        public void run() {
1144:                            if (wasRun == 0)
1145:                                startedAt = System.currentTimeMillis();
1146:                            lastDelta = System.currentTimeMillis()
1147:                                    - (startedAt + (100 * wasRun));
1148:                            wasRun++;
1149:                            if (wasRun == 2) {
1150:                                try {
1151:                                    Thread.sleep(200);
1152:                                } catch (InterruptedException e) {
1153:                                }
1154:                            }
1155:                        }
1156:
1157:                        public long lastDelta() {
1158:                            return lastDelta;
1159:                        }
1160:
1161:                        public int wasRun() {
1162:                            return wasRun;
1163:                        }
1164:                    }
1165:
1166:                    // Ensure multiple tasks are run
1167:                    t = new Timer();
1168:                    SlowThenFastTask slowThenFastTask = new SlowThenFastTask();
1169:                    d = new Date(System.currentTimeMillis() + 100);
1170:
1171:                    // at least 9 times even when asleep
1172:                    t.scheduleAtFixedRate(slowThenFastTask, d, 100);
1173:                    try {
1174:                        Thread.sleep(1000);
1175:                    } catch (InterruptedException e) {
1176:                    }
1177:                    long lastDelta = slowThenFastTask.lastDelta();
1178:                    assertTrue(
1179:                            "Fixed Rate Schedule should catch up, but is off by "
1180:                                    + lastDelta + " ms", lastDelta < 300);
1181:                    t.cancel();
1182:                } finally {
1183:                    if (t != null)
1184:                        t.cancel();
1185:                }
1186:            }
1187:
1188:            protected void setUp() {
1189:                timerCounter = 0;
1190:            }
1191:
1192:            protected void tearDown() {
1193:            }
1194:        }
ww__w___.___j_a_v_a2_s.___co__m___ | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.