ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/TimeUnitTest.java
(Generate patch)

Comparing jsr166/src/test/tck/TimeUnitTest.java (file contents):
Revision 1.12 by jsr166, Mon Nov 16 05:30:08 2009 UTC vs.
Revision 1.20 by jsr166, Fri May 27 19:12:09 2011 UTC

# Line 1 | Line 1
1   /*
2   * Written by Doug Lea with assistance from members of JCP JSR-166
3   * Expert Group and released to the public domain, as explained at
4 < * http://creativecommons.org/licenses/publicdomain
4 > * http://creativecommons.org/publicdomain/zero/1.0/
5   * Other contributors include Andrew Wright, Jeffrey Hayes,
6   * Pat Fisher, Mike Judd.
7   */
8  
9
9   import junit.framework.*;
10   import java.util.concurrent.*;
11   import java.io.*;
12  
13   public class TimeUnitTest extends JSR166TestCase {
14      public static void main(String[] args) {
15 <        junit.textui.TestRunner.run(suite());
15 >        junit.textui.TestRunner.run(suite());
16      }
17  
18      public static Test suite() {
19 <        return new TestSuite(TimeUnitTest.class);
19 >        return new TestSuite(TimeUnitTest.class);
20      }
21  
22      // (loops to 88888 check increments at all time divisions.)
# Line 49 | Line 48 | public class TimeUnitTest extends JSR166
48                           TimeUnit.SECONDS.convert(1000000000L*t,
49                                                    TimeUnit.NANOSECONDS));
50  
52
51              assertEquals(1000L*t*60*60*24,
52                           TimeUnit.MILLISECONDS.convert(t,
53                                                    TimeUnit.DAYS));
# Line 279 | Line 277 | public class TimeUnitTest extends JSR166
277          }
278      }
279  
282
280      /**
281       * convert saturates positive too-large values to Long.MAX_VALUE
282       * and negative to LONG.MIN_VALUE
# Line 309 | Line 306 | public class TimeUnitTest extends JSR166
306          assertEquals(Long.MIN_VALUE,
307                       TimeUnit.NANOSECONDS.convert(-Long.MAX_VALUE / 4,
308                                                    TimeUnit.DAYS));
312
309      }
310  
311      /**
# Line 317 | Line 313 | public class TimeUnitTest extends JSR166
313       * and negative to LONG.MIN_VALUE
314       */
315      public void testToNanosSaturate() {
316 <            assertEquals(Long.MAX_VALUE,
317 <                         TimeUnit.MILLISECONDS.toNanos(Long.MAX_VALUE / 2));
318 <            assertEquals(Long.MIN_VALUE,
319 <                         TimeUnit.MILLISECONDS.toNanos(-Long.MAX_VALUE / 3));
316 >        assertEquals(Long.MAX_VALUE,
317 >                     TimeUnit.MILLISECONDS.toNanos(Long.MAX_VALUE / 2));
318 >        assertEquals(Long.MIN_VALUE,
319 >                     TimeUnit.MILLISECONDS.toNanos(-Long.MAX_VALUE / 3));
320      }
321  
326
322      /**
323 <     * toString returns string containing common name of unit
323 >     * toString returns name of unit
324       */
325      public void testToString() {
326 <        String s = TimeUnit.SECONDS.toString();
332 <        assertTrue(s.indexOf("ECOND") >= 0);
326 >        assertEquals("SECONDS", TimeUnit.SECONDS.toString());
327      }
328  
329 +    /**
330 +     * name returns name of unit
331 +     */
332 +    public void testName() {
333 +        assertEquals("SECONDS", TimeUnit.SECONDS.name());
334 +    }
335  
336      /**
337 <     *  Timed wait without holding lock throws
338 <     *  IllegalMonitorStateException
337 >     * Timed wait without holding lock throws
338 >     * IllegalMonitorStateException
339       */
340 <    public void testTimedWait_IllegalMonitorException() {
341 <        //created a new thread with anonymous runnable
342 <
343 <        Thread t = new Thread(new Runnable() {
344 <                public void run() {
345 <                    Object o = new Object();
346 <                    TimeUnit tu = TimeUnit.MILLISECONDS;
347 <                    try {
348 <                        tu.timedWait(o,LONG_DELAY_MS);
349 <                        threadShouldThrow();
350 <                    }
351 <                    catch (InterruptedException ie) {
352 <                        threadUnexpectedException();
353 <                    }
354 <                    catch (IllegalMonitorStateException success) {
355 <                    }
340 >    public void testTimedWait_IllegalMonitorException() throws Exception {
341 >        Thread t = new Thread(new CheckedRunnable() {
342 >            public void realRun() throws InterruptedException {
343 >                Object o = new Object();
344 >                TimeUnit tu = TimeUnit.MILLISECONDS;
345 >                try {
346 >                    tu.timedWait(o,LONG_DELAY_MS);
347 >                    threadShouldThrow();
348 >                } catch (IllegalMonitorStateException success) {}}});
349  
357                }
358            });
350          t.start();
351 <        try {
352 <            Thread.sleep(SHORT_DELAY_MS);
353 <            t.interrupt();
363 <            t.join();
364 <        } catch (Exception e) {
365 <            unexpectedException();
366 <        }
351 >        Thread.sleep(SHORT_DELAY_MS);
352 >        t.interrupt();
353 >        t.join();
354      }
355  
356      /**
357       * timedWait throws InterruptedException when interrupted
358       */
359 <    public void testTimedWait() {
360 <        Thread t = new Thread(new Runnable() {
361 <                public void run() {
362 <                    Object o = new Object();
363 <
364 <                    TimeUnit tu = TimeUnit.MILLISECONDS;
365 <                    try {
366 <                        synchronized(o) {
367 <                            tu.timedWait(o,MEDIUM_DELAY_MS);
368 <                        }
369 <                        threadShouldThrow();
370 <                    }
371 <                    catch (InterruptedException success) {}
372 <                    catch (IllegalMonitorStateException failure) {
386 <                        threadUnexpectedException();
387 <                    }
388 <                }
389 <            });
390 <        t.start();
391 <        try {
392 <            Thread.sleep(SHORT_DELAY_MS);
393 <            t.interrupt();
394 <            t.join();
395 <        } catch (Exception e) {
396 <            unexpectedException();
397 <        }
359 >    public void testTimedWait() throws InterruptedException {
360 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
361 >            public void realRun() throws InterruptedException {
362 >                Object o = new Object();
363 >
364 >                TimeUnit tu = TimeUnit.MILLISECONDS;
365 >                synchronized (o) {
366 >                    tu.timedWait(o,MEDIUM_DELAY_MS);
367 >                }
368 >            }});
369 >        t.start();
370 >        Thread.sleep(SHORT_DELAY_MS);
371 >        t.interrupt();
372 >        t.join();
373      }
374  
400
375      /**
376       * timedJoin throws InterruptedException when interrupted
377       */
378 <    public void testTimedJoin() {
379 <        Thread t = new Thread(new Runnable() {
380 <                public void run() {
381 <                    TimeUnit tu = TimeUnit.MILLISECONDS;
382 <                    try {
383 <                        Thread s = new Thread(new Runnable() {
384 <                                public void run() {
385 <                                    try {
386 <                                        Thread.sleep(MEDIUM_DELAY_MS);
387 <                                    } catch (InterruptedException success) {}
388 <                                }
389 <                            });
390 <                        s.start();
391 <                        tu.timedJoin(s,MEDIUM_DELAY_MS);
392 <                        threadShouldThrow();
393 <                    }
394 <                    catch (Exception e) {}
421 <                }
422 <            });
423 <        t.start();
424 <        try {
425 <            Thread.sleep(SHORT_DELAY_MS);
426 <            t.interrupt();
427 <            t.join();
428 <        } catch (Exception e) {
429 <            unexpectedException();
430 <        }
378 >    public void testTimedJoin() throws InterruptedException {
379 >        final Thread s = new Thread(new CheckedInterruptedRunnable() {
380 >            public void realRun() throws InterruptedException {
381 >                Thread.sleep(MEDIUM_DELAY_MS);
382 >            }});
383 >        final Thread t = new Thread(new CheckedInterruptedRunnable() {
384 >            public void realRun() throws InterruptedException {
385 >                TimeUnit tu = TimeUnit.MILLISECONDS;
386 >                tu.timedJoin(s, MEDIUM_DELAY_MS);
387 >            }});
388 >        s.start();
389 >        t.start();
390 >        Thread.sleep(SHORT_DELAY_MS);
391 >        t.interrupt();
392 >        t.join();
393 >        s.interrupt();
394 >        s.join();
395      }
396  
397      /**
398 <     *  timedSleep throws InterruptedException when interrupted
399 <     */
400 <    public void testTimedSleep() {
401 <        //created a new thread with anonymous runnable
398 >     * timedSleep throws InterruptedException when interrupted
399 >     */
400 >    public void testTimedSleep() throws InterruptedException {
401 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
402 >            public void realRun() throws InterruptedException {
403 >                TimeUnit tu = TimeUnit.MILLISECONDS;
404 >                tu.sleep(MEDIUM_DELAY_MS);
405 >            }});
406  
407 <        Thread t = new Thread(new Runnable() {
408 <                public void run() {
409 <                    TimeUnit tu = TimeUnit.MILLISECONDS;
410 <                    try {
443 <                        tu.sleep(MEDIUM_DELAY_MS);
444 <                        threadShouldThrow();
445 <                    }
446 <                    catch (InterruptedException success) {}
447 <                }
448 <            });
449 <        t.start();
450 <        try {
451 <            Thread.sleep(SHORT_DELAY_MS);
452 <            t.interrupt();
453 <            t.join();
454 <        } catch (Exception e) {
455 <            unexpectedException();
456 <        }
407 >        t.start();
408 >        Thread.sleep(SHORT_DELAY_MS);
409 >        t.interrupt();
410 >        t.join();
411      }
412  
413      /**
414 <     * a deserialized serialized unit is equal
414 >     * a deserialized serialized unit is the same instance
415       */
416 <    public void testSerialization() {
416 >    public void testSerialization() throws Exception {
417          TimeUnit q = TimeUnit.MILLISECONDS;
418  
419 <        try {
420 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
421 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
422 <            out.writeObject(q);
423 <            out.close();
424 <
425 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
426 <            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
427 <            TimeUnit r = (TimeUnit)in.readObject();
474 <
475 <            assertEquals(q.toString(), r.toString());
476 <        } catch (Exception e) {
477 <            e.printStackTrace();
478 <            unexpectedException();
479 <        }
419 >        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
420 >        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
421 >        out.writeObject(q);
422 >        out.close();
423 >
424 >        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
425 >        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
426 >        TimeUnit r = (TimeUnit)in.readObject();
427 >        assertSame(q, r);
428      }
429  
430   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines