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 junit.framework.*;
034: import java.util.*;
035:
036: /**
037: * @author Taras Puchko
038: */
039: public class TimerTask_TestCase extends TestCase {
040:
041: class MyTimerTask extends TimerTask {
042: public volatile long lastTime;
043: public volatile int count;
044:
045: public synchronized void run() {
046: lastTime = scheduledExecutionTime();
047: count++;
048: notifyAll();
049: }
050: }
051:
052: public void testCancel_1() throws Exception {
053: Timer timer = new Timer();
054: MyTimerTask task = new MyTimerTask();
055: assertFalse(task.cancel());
056: try {
057: timer.schedule(task, 0);
058: fail();
059: } catch (IllegalStateException e) {
060: //ok
061: }
062: Thread.sleep(50);
063: assertEquals(0, task.count);
064: }
065:
066: public void testCancel_2() throws Exception {
067: Timer timer = new Timer();
068: MyTimerTask task = new MyTimerTask();
069: timer.schedule(task, 200);
070: assertTrue(task.cancel());
071: Thread.sleep(200);
072: assertEquals(0, task.count);
073: }
074:
075: public void testCancel_3() throws Exception {
076: Timer timer = new Timer();
077: MyTimerTask task = new MyTimerTask();
078: timer.schedule(task, 0);
079: Thread.sleep(100);
080: assertEquals(1, task.count);
081: assertFalse(task.cancel());
082: }
083:
084: public void testCancel_4() throws Exception {
085: Timer timer = new Timer();
086: MyTimerTask task = new MyTimerTask();
087: timer.schedule(task, 600, 600);
088: Thread.sleep(1500);
089: assertEquals(2, task.count);
090: assertTrue(task.cancel());
091: assertFalse(task.cancel());
092: Thread.sleep(900);
093: assertEquals(2, task.count);
094: }
095:
096: public void testScheduledExecutionTime_1() throws Exception {
097: Timer timer = new Timer();
098: MyTimerTask task = new MyTimerTask();
099: long delay = 200;
100: long period = 300;
101: long time = System.currentTimeMillis();
102: timer.scheduleAtFixedRate(task, delay, period);
103: synchronized (task) {
104: task.wait();
105: }
106: long lastTime = task.lastTime;
107: assertTime(time + delay, lastTime);
108: Thread.sleep(50);
109: assertEquals(lastTime, task.scheduledExecutionTime());
110: synchronized (task) {
111: task.wait();
112: }
113: lastTime = task.lastTime;
114: assertTime(time + delay + period, lastTime);
115: Thread.sleep(50);
116: assertEquals(lastTime, task.scheduledExecutionTime());
117: synchronized (task) {
118: task.wait();
119: }
120: lastTime = task.lastTime;
121: assertTime(time + delay + 2 * period, lastTime);
122: timer.cancel();
123: }
124:
125: public void testScheduledExecutionTime_2() throws Exception {
126: Timer timer = new Timer();
127: timer.schedule(new TimerTask() {
128: public void run() {
129: try {
130: Thread.sleep(500);
131: } catch (InterruptedException e) {
132: throw new ThreadDeath();
133: }
134: }
135: }, 0);
136: MyTimerTask task = new MyTimerTask();
137: long delay = 200;
138: long time = System.currentTimeMillis();
139: synchronized (task) {
140: timer.scheduleAtFixedRate(task, delay, 1000);
141: task.wait();
142: }
143: assertTime(time + delay, task.lastTime);
144: timer.cancel();
145: }
146:
147: public void testScheduledExecutionTime_3() throws Exception {
148: Timer timer = new Timer();
149: MyTimerTask task = new MyTimerTask();
150: long delay = 200;
151: long time = System.currentTimeMillis();
152: timer.schedule(task, delay);
153: synchronized (task) {
154: task.wait();
155: }
156: assertTime(time + delay, task.lastTime);
157: timer.cancel();
158: }
159:
160: private void assertTime(long expected, long found) {
161: long delta = Math.abs(expected - found);
162: if (delta > 75) {
163: throw new AssertionFailedError("Expected: " + expected
164: + ", but found: " + found + ", delta: " + delta
165: + ".");
166: }
167: }
168:
169: }
|