ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/TimeUnitTest.java
Revision: 1.2
Committed: Sun Sep 7 20:39:11 2003 UTC (20 years, 8 months ago) by dl
Branch: MAIN
Changes since 1.1: +22 -0 lines
Log Message:
Added serialization and lock tests

File Contents

# User Rev Content
1 dl 1.1 /*
2     * Written by members of JCP JSR-166 Expert Group and released to the
3     * public domain. Use, modify, and redistribute this code in any way
4     * without acknowledgement. Other contributors include Andrew Wright,
5     * Jeffrey Hayes, Pat Fischer, Mike Judd.
6     */
7    
8    
9     import junit.framework.*;
10     import java.util.concurrent.*;
11 dl 1.2 import java.io.*;
12 dl 1.1
13     public class TimeUnitTest extends TestCase {
14     static public boolean DEBUG = false;
15    
16    
17     public static void main(String[] args) {
18     junit.textui.TestRunner.run(suite());
19     }
20    
21     public static Test suite() {
22     return new TestSuite(TimeUnitTest.class);
23     }
24    
25     public void testConvert() {
26     for (long t = 0; t < 10; ++t) {
27     assertEquals(t,
28     TimeUnit.SECONDS.convert(t,
29     TimeUnit.SECONDS));
30     assertEquals(t,
31     TimeUnit.SECONDS.convert(1000 * t,
32     TimeUnit.MILLISECONDS));
33     assertEquals(t,
34     TimeUnit.SECONDS.convert(1000000 * t,
35     TimeUnit.MICROSECONDS));
36     assertEquals(t,
37     TimeUnit.SECONDS.convert(1000000000 * t,
38     TimeUnit.NANOSECONDS));
39     assertEquals(1000 * t,
40     TimeUnit.MILLISECONDS.convert(t,
41     TimeUnit.SECONDS));
42     assertEquals(t,
43     TimeUnit.MILLISECONDS.convert(t,
44     TimeUnit.MILLISECONDS));
45     assertEquals(t,
46     TimeUnit.MILLISECONDS.convert(1000 * t,
47     TimeUnit.MICROSECONDS));
48     assertEquals(t,
49     TimeUnit.MILLISECONDS.convert(1000000 * t,
50     TimeUnit.NANOSECONDS));
51     assertEquals(1000000 * t,
52     TimeUnit.MICROSECONDS.convert(t,
53     TimeUnit.SECONDS));
54     assertEquals(1000 * t,
55     TimeUnit.MICROSECONDS.convert(t,
56     TimeUnit.MILLISECONDS));
57     assertEquals(t,
58     TimeUnit.MICROSECONDS.convert(t,
59     TimeUnit.MICROSECONDS));
60     assertEquals(t,
61     TimeUnit.MICROSECONDS.convert(1000 * t,
62     TimeUnit.NANOSECONDS));
63     assertEquals(1000000000 * t,
64     TimeUnit.NANOSECONDS.convert(t,
65     TimeUnit.SECONDS));
66     assertEquals(1000000 * t,
67     TimeUnit.NANOSECONDS.convert(t,
68     TimeUnit.MILLISECONDS));
69     assertEquals(1000 * t,
70     TimeUnit.NANOSECONDS.convert(t,
71     TimeUnit.MICROSECONDS));
72     assertEquals(t,
73     TimeUnit.NANOSECONDS.convert(t,
74     TimeUnit.NANOSECONDS));
75     }
76     }
77    
78     public void testToNanos() {
79     for (long t = 0; t < 10; ++t) {
80     assertEquals(1000000000 * t,
81     TimeUnit.SECONDS.toNanos(t));
82    
83     assertEquals(1000000 * t,
84     TimeUnit.MILLISECONDS.toNanos(t));
85     assertEquals(1000 * t,
86     TimeUnit.MICROSECONDS.toNanos(t));
87     assertEquals(t,
88     TimeUnit.SECONDS.NANOSECONDS.toNanos(t));
89     }
90     }
91    
92    
93     public void testConvertSaturate() {
94     assertEquals(Long.MAX_VALUE,
95     TimeUnit.NANOSECONDS.convert(Long.MAX_VALUE / 2,
96     TimeUnit.SECONDS));
97     assertEquals(Long.MIN_VALUE,
98     TimeUnit.NANOSECONDS.convert(-Long.MAX_VALUE / 4,
99     TimeUnit.SECONDS));
100    
101     }
102    
103     public void testToNanosSaturate() {
104     assertEquals(Long.MAX_VALUE,
105     TimeUnit.MILLISECONDS.toNanos(Long.MAX_VALUE / 2));
106     assertEquals(Long.MIN_VALUE,
107     TimeUnit.MILLISECONDS.toNanos(-Long.MAX_VALUE / 3));
108    
109     }
110    
111    
112     public void testToString() {
113     String s = TimeUnit.SECONDS.toString();
114     assertTrue(s.indexOf("econd") >= 0);
115     }
116    
117     // Exception tests
118    
119     /**
120     * This test specifically to catch the unreported exception
121     */
122     public void testTimedWaitForUnreportedIllegalMonitorException() {
123     //created a new thread with anonymous runnable
124    
125     Thread t = new Thread(new Runnable() {
126     public void run() {
127     Object o = new Object();
128     TimeUnit tu = TimeUnit.MILLISECONDS;
129     try {
130     tu.timedWait(o,40000);
131     fail("should throw");
132     }
133     catch (InterruptedException ie) {
134     fail("should not throw IE here");
135     }
136     catch(IllegalMonitorStateException success) {
137     }
138    
139     }
140     });
141     t.start();
142     try {
143     Thread.sleep(100);
144     t.interrupt();
145     t.join();
146     } catch(Exception e) {
147     fail("Unexpected exception");
148     }
149     }
150    
151     /**
152     * Test to verify that timedWait will throw InterruptedException.
153     * Thread t waits on timedWait while the main thread interrupts it.
154     * Note: This does not throw IllegalMonitorException since timeWait
155     * is synchronized on o
156     */
157     public void testTimedWait() {
158     Thread t = new Thread(new Runnable() {
159     public void run() {
160     Object o = new Object();
161    
162     TimeUnit tu = TimeUnit.MILLISECONDS;
163     try {
164     synchronized(o) {
165     tu.timedWait(o,1000);
166     }
167     fail("should throw");
168     }
169     catch(InterruptedException success) {}
170     catch(IllegalMonitorStateException failure) {
171     fail("should not throw");
172     }
173     }
174     });
175     t.start();
176     try {
177     Thread.sleep(100);
178     t.interrupt();
179     t.join();
180     } catch(Exception e) {
181     fail("Unexpected exception");
182     }
183     }
184    
185    
186     /**
187     * Test to verify that timedJoin will throw InterruptedException.
188     * Thread t waits on timedJoin while the main thread interrupts it.
189     */
190     public void testTimedJoin() {
191     Thread t = new Thread(new Runnable() {
192     public void run() {
193     TimeUnit tu = TimeUnit.MILLISECONDS;
194     try {
195     Thread s = new Thread(new Runnable() {
196     public void run() {
197     try{
198     Thread.sleep(1000);
199     }catch(InterruptedException success){}
200     }
201     });
202     s.start();
203     tu.timedJoin(s,1000);
204     fail("should throw");
205     }
206     catch(Exception e) {}
207     }
208     });
209     t.start();
210     try {
211     Thread.sleep(100);
212     t.interrupt();
213     t.join();
214     } catch(Exception e) {
215     fail("Unexpected exception");
216     }
217     }
218    
219     /**
220     * Test to verify that timedSleep will throw InterruptedException.
221     * Thread t waits on timedSleep while the main thread interrupts it.
222     */
223     public void testTimedSleep() {
224     //created a new thread with anonymous runnable
225    
226     Thread t = new Thread(new Runnable() {
227     public void run() {
228     TimeUnit tu = TimeUnit.MILLISECONDS;
229     try {
230     tu.sleep(1000);
231     fail("should throw");
232     }
233     catch(InterruptedException success) {}
234     }
235     });
236     t.start();
237     try {
238     Thread.sleep(100);
239     t.interrupt();
240     t.join();
241     } catch(Exception e) {
242     fail("Unexpected exception");
243     }
244     }
245 dl 1.2
246     public void testSerialization() {
247     TimeUnit q = TimeUnit.MILLISECONDS;
248    
249     try {
250     ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
251     ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
252     out.writeObject(q);
253     out.close();
254    
255     ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
256     ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
257     TimeUnit r = (TimeUnit)in.readObject();
258    
259     assertEquals(q.toString(), r.toString());
260     } catch(Exception e){
261     e.printStackTrace();
262     fail("unexpected exception");
263     }
264     }
265    
266 dl 1.1 }