Source Code Cross Referenced for TimerTaskTest.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) 


001:        /*
002:         *  Licensed to the Apache Software Foundation (ASF) under one or more
003:         *  contributor license agreements.  See the NOTICE file distributed with
004:         *  this work for additional information regarding copyright ownership.
005:         *  The ASF licenses this file to You under the Apache License, Version 2.0
006:         *  (the "License"); you may not use this file except in compliance with
007:         *  the License.  You may obtain a copy of the License at
008:         *
009:         *     http://www.apache.org/licenses/LICENSE-2.0
010:         *
011:         *  Unless required by applicable law or agreed to in writing, software
012:         *  distributed under the License is distributed on an "AS IS" BASIS,
013:         *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014:         *  See the License for the specific language governing permissions and
015:         *  limitations under the License.
016:         */
017:
018:        package org.apache.harmony.luni.tests.java.util;
019:
020:        import java.util.Timer;
021:        import java.util.TimerTask;
022:
023:        public class TimerTaskTest extends junit.framework.TestCase {
024:            Object sync = new Object(), start = new Object();
025:
026:            /**
027:             * Warning: These tests have the possibility to leave a VM hanging if the
028:             * Timer is not cancelled.
029:             */
030:            class TimerTestTask extends TimerTask {
031:                private int wasRun = 0;
032:
033:                // Set this to true to see normal tests fail (or hang possibly)
034:                // The default is false and needs to be set by some tests
035:                private boolean sleepInRun = false;
036:
037:                public void run() {
038:                    synchronized (this ) {
039:                        wasRun++;
040:                    }
041:                    synchronized (start) {
042:                        start.notify();
043:                    }
044:                    if (sleepInRun) {
045:
046:                        try {
047:                            Thread.sleep(200);
048:                        } catch (InterruptedException e) {
049:                        }
050:                    }
051:                    synchronized (sync) {
052:                        sync.notify();
053:                    }
054:                }
055:
056:                public synchronized int wasRun() {
057:                    return wasRun;
058:                }
059:
060:                public void sleepInRun(boolean value) {
061:                    sleepInRun = value;
062:                }
063:            }
064:
065:            /**
066:             * @tests java.util.TimerTask#TimerTask()
067:             */
068:            public void test_Constructor() {
069:                // Ensure the constructor does not fail
070:                new TimerTestTask();
071:            }
072:
073:            /**
074:             * @tests java.util.TimerTask#cancel()
075:             */
076:            public void test_cancel() {
077:                Timer t = null;
078:                try {
079:                    // Ensure cancel returns false if never scheduled
080:                    TimerTestTask testTask = new TimerTestTask();
081:                    assertTrue(
082:                            "Unsheduled tasks should return false for cancel()",
083:                            !testTask.cancel());
084:
085:                    // Ensure cancelled task never runs
086:                    t = new Timer();
087:                    testTask = new TimerTestTask();
088:                    t.schedule(testTask, 500);
089:                    assertTrue("TimerTask should not have run yet", testTask
090:                            .cancel());
091:                    t.cancel();
092:
093:                    // Ensure cancelling a task which has already run returns true
094:                    t = new Timer();
095:                    testTask = new TimerTestTask();
096:                    t.schedule(testTask, 50);
097:                    while (testTask.wasRun() == 0) {
098:                        try {
099:                            Thread.sleep(150);
100:                        } catch (InterruptedException e) {
101:                        }
102:                    }
103:                    assertFalse(
104:                            "TimerTask.cancel() should return false if task has run",
105:                            testTask.cancel());
106:                    assertFalse(
107:                            "TimerTask.cancel() should return false if called a second time",
108:                            testTask.cancel());
109:                    t.cancel();
110:
111:                    // Ensure cancelling a repeated execution task which has never run
112:                    // returns true
113:                    t = new Timer();
114:                    testTask = new TimerTestTask();
115:                    t.schedule(testTask, 500, 500); // should never run			
116:                    assertTrue(
117:                            "TimerTask.cancel() should return true if sheduled for repeated execution even if not run",
118:                            testTask.cancel());
119:                    t.cancel();
120:
121:                    // Ensure cancelling a repeated execution task which HAS run returns
122:                    // true
123:                    t = new Timer();
124:                    testTask = new TimerTestTask();
125:                    t.schedule(testTask, 50, 50);
126:                    while (testTask.wasRun() == 0) {
127:                        try {
128:                            Thread.sleep(100);
129:                        } catch (InterruptedException e) {
130:                        }
131:                    }
132:                    assertTrue(
133:                            "TimerTask.cancel() should return true if sheduled for repeated execution and run",
134:                            testTask.cancel());
135:                    t.cancel();
136:
137:                    // Ensure calling cancel a second returns false
138:                    t = new Timer();
139:                    testTask = new TimerTestTask();
140:                    t.schedule(testTask, 5000); // Should never run			
141:                    assertTrue(
142:                            "TimerTask.cancel() should return true if task has never run",
143:                            testTask.cancel());
144:                    assertFalse(
145:                            "TimerTask.cancel() should return false if called a second time",
146:                            testTask.cancel());
147:                    t.cancel();
148:
149:                    // Ensure cancelling a task won't cause deadlock
150:                    t = new Timer();
151:                    testTask = new TimerTestTask();
152:                    testTask.sleepInRun(true);
153:                    synchronized (start) {
154:                        t.schedule(testTask, 0);
155:                        try {
156:                            start.wait();
157:                            Thread.sleep(50);
158:                        } catch (InterruptedException e) {
159:                        }
160:                    }
161:                    assertFalse("TimerTask should have been cancelled",
162:                            testTask.cancel());
163:                    t.cancel();
164:                } finally {
165:                    if (t != null)
166:                        t.cancel();
167:                }
168:            }
169:
170:            /**
171:             * @tests java.util.TimerTask#scheduledExecutionTime()
172:             */
173:            public void test_scheduledExecutionTime() {
174:                Timer t = null;
175:                try {
176:                    // Ensure scheduledExecutionTime is roughly right
177:                    t = new Timer();
178:                    TimerTestTask testTask = new TimerTestTask();
179:                    t.schedule(testTask, 100);
180:                    long time = System.currentTimeMillis() + 100;
181:                    synchronized (sync) {
182:                        try {
183:                            sync.wait(500);
184:                        } catch (InterruptedException e) {
185:                        }
186:                    }
187:                    long scheduledExecutionTime = testTask
188:                            .scheduledExecutionTime();
189:                    assertTrue(scheduledExecutionTime <= time);
190:                    t.cancel();
191:
192:                    // Ensure scheduledExecutionTime is the last scheduled time
193:                    t = new Timer();
194:                    testTask = new TimerTestTask();
195:                    t.schedule(testTask, 100, 500);
196:                    long estNow = System.currentTimeMillis() + 100;
197:                    // Will wake in 100, and every 500 run again
198:                    // We want to try to get it after it's run at least once but not
199:                    // twice
200:                    synchronized (sync) {
201:                        try {
202:                            sync.wait(500);
203:                        } catch (InterruptedException e) {
204:                        }
205:                    }
206:                    scheduledExecutionTime = testTask.scheduledExecutionTime();
207:                    assertTrue(scheduledExecutionTime <= estNow);
208:                    t.cancel();
209:                } finally {
210:                    if (t != null)
211:                        t.cancel();
212:                }
213:
214:            }
215:
216:            /**
217:             * @tests java.util.TimerTask#run()
218:             */
219:            public void test_run() {
220:                Timer t = null;
221:                try {
222:                    // Ensure a new task is never run
223:                    TimerTestTask testTask = new TimerTestTask();
224:                    try {
225:                        Thread.sleep(200);
226:                    } catch (InterruptedException e) {
227:                    }
228:                    assertEquals(
229:                            "TimerTask.run() method should not have been called",
230:                            0, testTask.wasRun());
231:
232:                    // Ensure a task is run
233:                    t = new Timer();
234:                    testTask = new TimerTestTask();
235:                    t.schedule(testTask, 200);
236:                    while (testTask.wasRun() < 1) {
237:                        try {
238:                            Thread.sleep(400);
239:                        } catch (InterruptedException e) {
240:                        }
241:                    }
242:                    assertFalse(testTask.cancel());
243:                    t.cancel();
244:                } finally {
245:                    if (t != null)
246:                        t.cancel();
247:                }
248:
249:            }
250:        }
ww_w___.j_av_a_2s._c_o___m___ | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.