001: /***
002: * Retrotranslator: a Java bytecode transformer that translates Java classes
003: * compiled with JDK 5.0 into classes that can be run on JVM 1.4.
004: *
005: * Copyright (c) 2005 - 2008 Taras Puchko
006: * All rights reserved.
007: *
008: * Redistribution and use in source and binary forms, with or without
009: * modification, are permitted provided that the following conditions
010: * are met:
011: * 1. Redistributions of source code must retain the above copyright
012: * notice, this list of conditions and the following disclaimer.
013: * 2. Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in the
015: * documentation and/or other materials provided with the distribution.
016: * 3. Neither the name of the copyright holders nor the names of its
017: * contributors may be used to endorse or promote products derived from
018: * this software without specific prior written permission.
019: *
020: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
021: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
022: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
023: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
024: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
025: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
026: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
027: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
028: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
029: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
030: * THE POSSIBILITY OF SUCH DAMAGE.
031: */package net.sf.retrotranslator.runtime.java.util;
032:
033: import net.sf.retrotranslator.tests.TestCaseBase;
034:
035: import java.lang.ref.WeakReference;
036: import java.util.*;
037: import java.util.concurrent.Callable;
038: import java.util.concurrent.atomic.AtomicBoolean;
039:
040: /**
041: * @author Taras Puchko
042: */
043: public class Timer_TestCase extends TestCaseBase {
044:
045: class MyTimerTask extends TimerTask {
046: public volatile int count;
047: public volatile boolean error;
048:
049: public void run() {
050: count++;
051: if (error) {
052: throw new RuntimeException();
053: }
054: }
055: }
056:
057: class HalfSecondTimerTask extends TimerTask {
058: public void run() {
059: try {
060: Thread.sleep(500);
061: } catch (InterruptedException e) {
062: // ignore
063: }
064: }
065: }
066:
067: public void testTimer() throws Exception {
068: new Timer().cancel();
069: new Timer(true).cancel();
070: Timer firstTimer = new Timer("FirstTimer");
071: Thread firstThread = getThread("FirstTimer");
072: assertTrue(firstThread.isAlive());
073: assertFalse(firstThread.isDaemon());
074: firstTimer.cancel();
075: Timer secondTimer = new Timer("SecondTimer", true);
076: Thread secondThread = getThread("SecondTimer");
077: assertTrue(secondThread.isAlive());
078: assertTrue(secondThread.isDaemon());
079: secondTimer.cancel();
080: }
081:
082: public void testCancel_1() throws Exception {
083: Timer timer = new Timer();
084: MyTimerTask task = new MyTimerTask();
085: timer.schedule(task, 100, 100);
086: timer.cancel();
087: Thread.sleep(300);
088: assertEquals(0, task.count);
089: assertTrue(task.cancel());
090: timer.cancel();
091: }
092:
093: public void testCancel_2() throws Exception {
094: Timer timer = new Timer();
095: MyTimerTask task = new MyTimerTask();
096: timer.schedule(task, 200, 200);
097: Thread.sleep(500);
098: assertEquals(2, task.count);
099: timer.cancel();
100: Thread.sleep(500);
101: assertEquals(2, task.count);
102: }
103:
104: public void testPurge() throws Exception {
105: Timer timer = new Timer();
106: MyTimerTask firstTask = new MyTimerTask();
107: MyTimerTask secondTask = new MyTimerTask();
108: MyTimerTask thirdTask = new MyTimerTask();
109: timer.schedule(firstTask, 0, 10);
110: timer.schedule(secondTask, 200, 200);
111: timer.schedule(thirdTask, 0, 200);
112: firstTask.cancel();
113: final WeakReference<TimerTask> firstReference = new WeakReference<TimerTask>(
114: firstTask);
115: firstTask = null;
116: this .gc(new Callable<Boolean>() {
117: public Boolean call() throws Exception {
118: return firstReference.get() != null;
119: }
120: });
121: assertNull(firstReference.get());
122: secondTask.cancel();
123: thirdTask.cancel();
124: assertTrue(timer.purge() <= 2);
125: }
126:
127: public void testSchedule_Once_Delay_1() throws Exception {
128: Timer timer = new Timer();
129: MyTimerTask task = new MyTimerTask();
130: timer.schedule(task, 200);
131: Thread.sleep(100);
132: assertEquals(0, task.count);
133: Thread.sleep(300);
134: assertEquals(1, task.count);
135: assertFalse(task.cancel());
136: }
137:
138: public void testSchedule_Once_Delay_2() throws Exception {
139: Timer timer = new Timer();
140: try {
141: timer.schedule(new MyTimerTask(), -100);
142: fail();
143: } catch (IllegalArgumentException e) {
144: //ok
145: }
146: try {
147: timer.schedule(new MyTimerTask(), Long.MAX_VALUE);
148: fail();
149: } catch (IllegalArgumentException e) {
150: //ok
151: }
152: MyTimerTask task = new MyTimerTask();
153: timer.schedule(task, 0);
154: try {
155: timer.schedule(task, 0);
156: fail();
157: } catch (IllegalStateException e) {
158: //ok
159: }
160: task = new MyTimerTask();
161: task.cancel();
162: try {
163: timer.schedule(task, 0);
164: fail();
165: } catch (IllegalStateException e) {
166: //ok
167: }
168: timer.cancel();
169: try {
170: timer.schedule(new MyTimerTask(), 0);
171: fail();
172: } catch (IllegalStateException e) {
173: //ok
174: }
175: }
176:
177: public void testSchedule_Once_Date_1() throws Exception {
178: Timer timer = new Timer();
179: MyTimerTask task = new MyTimerTask();
180: timer
181: .schedule(task, new Date(
182: System.currentTimeMillis() + 200));
183: Thread.sleep(100);
184: assertEquals(0, task.count);
185: Thread.sleep(200);
186: assertEquals(1, task.count);
187: assertFalse(task.cancel());
188: }
189:
190: public void testSchedule_Once_Date_2() throws Exception {
191: Timer timer = new Timer();
192: MyTimerTask task = new MyTimerTask();
193: timer
194: .schedule(task, new Date(
195: System.currentTimeMillis() - 200));
196: Thread.sleep(100);
197: assertEquals(1, task.count);
198: try {
199: timer.schedule(new MyTimerTask(), new Date(-200));
200: fail();
201: } catch (IllegalArgumentException e) {
202: //ok
203: }
204: try {
205: timer.schedule(task, new Date());
206: fail();
207: } catch (IllegalStateException e) {
208: //ok
209: }
210: task = new MyTimerTask();
211: task.cancel();
212: try {
213: timer.schedule(task, new Date());
214: fail();
215: } catch (IllegalStateException e) {
216: //ok
217: }
218: timer.cancel();
219: try {
220: timer.schedule(new MyTimerTask(), new Date());
221: fail();
222: } catch (IllegalStateException e) {
223: //ok
224: }
225: timer = new Timer();
226: timer.schedule(new TimerTask() {
227: public void run() {
228: throw new ThreadDeath();
229: }
230: }, 0);
231: Thread.sleep(50);
232: try {
233: timer.schedule(new MyTimerTask(), new Date());
234: fail();
235: } catch (IllegalStateException e) {
236: //ok
237: }
238: }
239:
240: public void testSchedule_WithFixedDelay_Delay_1() throws Exception {
241: Timer timer = new Timer();
242: MyTimerTask task = new MyTimerTask();
243: timer.schedule(new HalfSecondTimerTask(), 0);
244: timer.schedule(task, 200, 200);
245: Thread.sleep(450);
246: assertEquals(0, task.count);
247: Thread.sleep(200);
248: assertEquals(1, task.count);
249: Thread.sleep(200);
250: assertEquals(2, task.count);
251: assertTrue(task.cancel());
252: }
253:
254: public void testSchedule_WithFixedDelay_Delay_2() throws Exception {
255: Timer timer = new Timer();
256: MyTimerTask errorTask = new MyTimerTask();
257: errorTask.error = true;
258: timer.schedule(errorTask, 100, 100);
259: Thread.sleep(50);
260: assertEquals(0, errorTask.count);
261: Thread.sleep(100);
262: assertEquals(1, errorTask.count);
263: Thread.sleep(200);
264: assertEquals(1, errorTask.count);
265: }
266:
267: public void testSchedule_WithFixedDelay_Delay_3() throws Exception {
268: Timer timer = new Timer();
269: try {
270: timer.schedule(new MyTimerTask(), 200, 0);
271: fail();
272: } catch (IllegalArgumentException e) {
273: //ok
274: }
275: try {
276: timer.schedule(new MyTimerTask(), 200, -100);
277: fail();
278: } catch (IllegalArgumentException e) {
279: //ok
280: }
281: try {
282: timer.schedule(new MyTimerTask(), -200, 100);
283: fail();
284: } catch (IllegalArgumentException e) {
285: //ok
286: }
287: try {
288: timer.schedule(new MyTimerTask(), Long.MAX_VALUE, 100);
289: fail();
290: } catch (IllegalArgumentException e) {
291: //ok
292: }
293: TimerTask task = new MyTimerTask();
294: timer.schedule(task, 10, 10);
295: try {
296: timer.schedule(task, 10, 10);
297: fail();
298: } catch (IllegalStateException e) {
299: //ok
300: }
301: task = new MyTimerTask();
302: task.cancel();
303: try {
304: timer.schedule(task, 10, 10);
305: fail();
306: } catch (IllegalStateException e) {
307: //ok
308: }
309: timer.cancel();
310: try {
311: timer.schedule(new MyTimerTask(), 10, 10);
312: fail();
313: } catch (IllegalStateException e) {
314: //ok
315: }
316: timer = new Timer();
317: timer.schedule(new TimerTask() {
318: public void run() {
319: throw new ThreadDeath();
320: }
321: }, 0);
322: Thread.sleep(50);
323: try {
324: timer.schedule(new MyTimerTask(), 10, 10);
325: fail();
326: } catch (IllegalStateException e) {
327: //ok
328: }
329: }
330:
331: public void testSchedule_WithFixedDelay_Date_1() throws Exception {
332: Timer timer = new Timer();
333: MyTimerTask task = new MyTimerTask();
334: timer.schedule(new HalfSecondTimerTask(), 0);
335: timer.schedule(task,
336: new Date(System.currentTimeMillis() + 200), 200);
337: Thread.sleep(400);
338: assertEquals(0, task.count);
339: Thread.sleep(200);
340: assertEquals(1, task.count);
341: Thread.sleep(200);
342: assertEquals(2, task.count);
343: assertTrue(task.cancel());
344: }
345:
346: public void testSchedule_WithFixedDelay_Date_2() throws Exception {
347: Timer timer = new Timer();
348: MyTimerTask task = new MyTimerTask();
349: timer.schedule(task,
350: new Date(System.currentTimeMillis() - 400), 200);
351: Thread.sleep(100);
352: assertEquals(1, task.count);
353: Thread.sleep(200);
354: assertEquals(2, task.count);
355: Thread.sleep(200);
356: assertEquals(3, task.count);
357: assertTrue(task.cancel());
358: }
359:
360: public void testSchedule_WithFixedDelay_Date_3() throws Exception {
361: Timer timer = new Timer();
362: try {
363: timer.schedule(new MyTimerTask(), new Date(), 0);
364: fail();
365: } catch (IllegalArgumentException e) {
366: //ok
367: }
368: try {
369: timer.schedule(new MyTimerTask(), new Date(), -100);
370: fail();
371: } catch (IllegalArgumentException e) {
372: //ok
373: }
374: try {
375: timer.schedule(new MyTimerTask(), new Date(-200), 100);
376: fail();
377: } catch (IllegalArgumentException e) {
378: //ok
379: }
380: timer.schedule(new MyTimerTask(), new Date(System
381: .currentTimeMillis() - 200), 100);
382: timer
383: .schedule(new MyTimerTask(), new Date(Long.MAX_VALUE),
384: 100);
385: TimerTask task = new MyTimerTask();
386: timer.schedule(task, new Date(), 10);
387: try {
388: timer.schedule(task, new Date(), 10);
389: fail();
390: } catch (IllegalStateException e) {
391: //ok
392: }
393: task = new MyTimerTask();
394: task.cancel();
395: try {
396: timer.schedule(task, new Date(), 10);
397: fail();
398: } catch (IllegalStateException e) {
399: //ok
400: }
401: timer.cancel();
402: try {
403: timer.schedule(new MyTimerTask(), new Date(), 10);
404: fail();
405: } catch (IllegalStateException e) {
406: //ok
407: }
408: timer = new Timer();
409: timer.schedule(new TimerTask() {
410: public void run() {
411: throw new ThreadDeath();
412: }
413: }, new Date(), 10);
414: Thread.sleep(50);
415: try {
416: timer.schedule(new MyTimerTask(), new Date(), 10);
417: fail();
418: } catch (IllegalStateException e) {
419: //ok
420: }
421: }
422:
423: public void testScheduleAtFixedRate_Delay_1() throws Exception {
424: Timer timer = new Timer();
425: MyTimerTask task = new MyTimerTask();
426: timer.schedule(new HalfSecondTimerTask(), 0);
427: timer.scheduleAtFixedRate(task, 200, 200);
428: Thread.sleep(450);
429: assertEquals(0, task.count);
430: Thread.sleep(200);
431: assertEquals(3, task.count);
432: Thread.sleep(200);
433: assertEquals(4, task.count);
434: assertTrue(task.cancel());
435: }
436:
437: public void testScheduleAtFixedRate_Delay_2() throws Exception {
438: Timer timer = new Timer();
439: try {
440: timer.scheduleAtFixedRate(new MyTimerTask(), 200, 0);
441: fail();
442: } catch (IllegalArgumentException e) {
443: //ok
444: }
445: try {
446: timer.scheduleAtFixedRate(new MyTimerTask(), 200, -100);
447: fail();
448: } catch (IllegalArgumentException e) {
449: //ok
450: }
451: try {
452: timer.scheduleAtFixedRate(new MyTimerTask(), -200, 100);
453: fail();
454: } catch (IllegalArgumentException e) {
455: //ok
456: }
457: try {
458: timer.scheduleAtFixedRate(new MyTimerTask(),
459: Long.MAX_VALUE, 100);
460: fail();
461: } catch (IllegalArgumentException e) {
462: //ok
463: }
464: TimerTask task = new MyTimerTask();
465: timer.scheduleAtFixedRate(task, 10, 10);
466: try {
467: timer.scheduleAtFixedRate(task, 10, 10);
468: fail();
469: } catch (IllegalStateException e) {
470: //ok
471: }
472: task = new MyTimerTask();
473: task.cancel();
474: try {
475: timer.scheduleAtFixedRate(task, 10, 10);
476: fail();
477: } catch (IllegalStateException e) {
478: //ok
479: }
480: timer.cancel();
481: try {
482: timer.scheduleAtFixedRate(new MyTimerTask(), 10, 10);
483: fail();
484: } catch (IllegalStateException e) {
485: //ok
486: }
487: timer = new Timer();
488: final AtomicBoolean flag = new AtomicBoolean(true);
489: timer.scheduleAtFixedRate(new TimerTask() {
490: public void run() {
491: flag.set(false);
492: throw new ThreadDeath();
493: }
494: }, 0, 10);
495: while (flag.get()) {
496: Thread.sleep(50);
497: }
498: try {
499: timer.scheduleAtFixedRate(new MyTimerTask(), 10, 10);
500: fail();
501: } catch (IllegalStateException e) {
502: //ok
503: }
504: }
505:
506: public void testScheduleAtFixedRate_Date_1() throws Exception {
507: Timer timer = new Timer();
508: MyTimerTask task = new MyTimerTask();
509: timer.schedule(new HalfSecondTimerTask(), 0);
510: timer.scheduleAtFixedRate(task, new Date(System
511: .currentTimeMillis() + 250), 250);
512: Thread.sleep(350);
513: assertEquals(0, task.count);
514: Thread.sleep(250);
515: assertEquals(2, task.count);
516: Thread.sleep(250);
517: assertEquals(3, task.count);
518: assertTrue(task.cancel());
519: }
520:
521: public void testScheduleAtFixedRate_Date_3() throws Exception {
522: Timer timer = new Timer();
523: try {
524: timer.scheduleAtFixedRate(new MyTimerTask(), new Date(), 0);
525: fail();
526: } catch (IllegalArgumentException e) {
527: //ok
528: }
529: try {
530: timer.scheduleAtFixedRate(new MyTimerTask(), new Date(),
531: -100);
532: fail();
533: } catch (IllegalArgumentException e) {
534: //ok
535: }
536: try {
537: timer.scheduleAtFixedRate(new MyTimerTask(),
538: new Date(-200), 100);
539: fail();
540: } catch (IllegalArgumentException e) {
541: //ok
542: }
543: timer.scheduleAtFixedRate(new MyTimerTask(), new Date(System
544: .currentTimeMillis() - 200), 100);
545: timer.scheduleAtFixedRate(new MyTimerTask(), new Date(
546: Long.MAX_VALUE), 100);
547: TimerTask task = new MyTimerTask();
548: timer.scheduleAtFixedRate(task, new Date(), 10);
549: try {
550: timer.scheduleAtFixedRate(task, new Date(), 10);
551: fail();
552: } catch (IllegalStateException e) {
553: //ok
554: }
555: task = new MyTimerTask();
556: task.cancel();
557: try {
558: timer.scheduleAtFixedRate(task, new Date(), 10);
559: fail();
560: } catch (IllegalStateException e) {
561: //ok
562: }
563: timer.cancel();
564: try {
565: timer
566: .scheduleAtFixedRate(new MyTimerTask(), new Date(),
567: 10);
568: fail();
569: } catch (IllegalStateException e) {
570: //ok
571: }
572: timer = new Timer();
573: timer.scheduleAtFixedRate(new TimerTask() {
574: public void run() {
575: throw new ThreadDeath();
576: }
577: }, new Date(), 10);
578: Thread.sleep(50);
579: try {
580: timer
581: .scheduleAtFixedRate(new MyTimerTask(), new Date(),
582: 10);
583: fail();
584: } catch (IllegalStateException e) {
585: //ok
586: }
587: }
588:
589: public void testFinalize() throws Exception {
590: MyTimerTask task = new MyTimerTask();
591: new Timer("MyTimer").schedule(task, 400, 400);
592: final Thread thread = getThread("MyTimer");
593: assertTrue(thread.isAlive());
594: Thread.sleep(1000);
595: assertTrue(thread.isAlive());
596: assertTrue(task.count > 0);
597: task.cancel();
598: for (int i = 0; thread.isAlive() && i < 10; i++) {
599: gc(new Callable<Boolean>() {
600: public Boolean call() throws Exception {
601: return thread.isAlive();
602: }
603: });
604: }
605: assertFalse(thread.isAlive());
606: }
607:
608: private Thread getThread(String name) {
609: for (Thread thread : Thread.getAllStackTraces().keySet()) {
610: if (thread.getName().equals(name)) {
611: return thread;
612: }
613: }
614: throw new IllegalArgumentException("Thread not found: " + name);
615: }
616:
617: }
|