FileDocCategorySizeDatePackage
TimeUnitTest.javaAPI DocAndroid 1.5 API11631Wed May 06 22:41:02 BST 2009tests.api.java.util.concurrent

TimeUnitTest

public class TimeUnitTest extends JSR166TestCase

Fields Summary
Constructors Summary
Methods Summary
public static voidmain(java.lang.String[] args)

        junit.textui.TestRunner.run(suite());        
    
public static junit.framework.Testsuite()

        return new TestSuite(TimeUnitTest.class);
    
public voidtestConvert()
convert correctly converts sample values across the four units

        for (long t = 0; t < 10; ++t) {
            assertEquals(t, 
                         TimeUnit.SECONDS.convert(t, 
                                                  TimeUnit.SECONDS));
            assertEquals(t, 
                         TimeUnit.SECONDS.convert(1000 * t, 
                                                  TimeUnit.MILLISECONDS));
            assertEquals(t, 
                         TimeUnit.SECONDS.convert(1000000 * t, 
                                                  TimeUnit.MICROSECONDS));
            assertEquals(t, 
                         TimeUnit.SECONDS.convert(1000000000 * t, 
                                                  TimeUnit.NANOSECONDS));
            assertEquals(1000 * t, 
                         TimeUnit.MILLISECONDS.convert(t, 
                                                  TimeUnit.SECONDS));
            assertEquals(t, 
                         TimeUnit.MILLISECONDS.convert(t, 
                                                  TimeUnit.MILLISECONDS));
            assertEquals(t, 
                         TimeUnit.MILLISECONDS.convert(1000 * t, 
                                                  TimeUnit.MICROSECONDS));
            assertEquals(t, 
                         TimeUnit.MILLISECONDS.convert(1000000 * t, 
                                                  TimeUnit.NANOSECONDS));
            assertEquals(1000000 * t, 
                         TimeUnit.MICROSECONDS.convert(t, 
                                                  TimeUnit.SECONDS));
            assertEquals(1000 * t, 
                         TimeUnit.MICROSECONDS.convert(t, 
                                                  TimeUnit.MILLISECONDS));
            assertEquals(t, 
                         TimeUnit.MICROSECONDS.convert(t, 
                                                  TimeUnit.MICROSECONDS));
            assertEquals(t, 
                         TimeUnit.MICROSECONDS.convert(1000 * t, 
                                                  TimeUnit.NANOSECONDS));
            assertEquals(1000000000 * t, 
                         TimeUnit.NANOSECONDS.convert(t, 
                                                  TimeUnit.SECONDS));
            assertEquals(1000000 * t, 
                         TimeUnit.NANOSECONDS.convert(t, 
                                                  TimeUnit.MILLISECONDS));
            assertEquals(1000 * t, 
                         TimeUnit.NANOSECONDS.convert(t, 
                                                  TimeUnit.MICROSECONDS));
            assertEquals(t, 
                         TimeUnit.NANOSECONDS.convert(t, 
                                                  TimeUnit.NANOSECONDS));
        }
    
public voidtestConvertSaturate()
convert saturates positive too-large values to Long.MAX_VALUE and negative to LONG.MIN_VALUE

        assertEquals(Long.MAX_VALUE,
                     TimeUnit.NANOSECONDS.convert(Long.MAX_VALUE / 2,
                                                  TimeUnit.SECONDS));
        assertEquals(Long.MIN_VALUE,
                     TimeUnit.NANOSECONDS.convert(-Long.MAX_VALUE / 4,
                                                  TimeUnit.SECONDS));
    
public voidtestSerialization()
a deserialized serialized unit is equal

        TimeUnit q = TimeUnit.MILLISECONDS;

        try {
            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
            out.writeObject(q);
            out.close();

            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
            TimeUnit r = (TimeUnit)in.readObject();
            
            assertEquals(q.toString(), r.toString());
        } catch(Exception e){
            e.printStackTrace();
            unexpectedException();
        }
    
public voidtestTimedJoin()
timedJoin throws InterruptedException when interrupted

        Thread t = new Thread(new Runnable() {
                public void run() {
                    TimeUnit tu = TimeUnit.MILLISECONDS;        
                    try {
                        Thread s = new Thread(new Runnable() {
                                public void run() {
                                    try {
                                        Thread.sleep(MEDIUM_DELAY_MS);
                                    } catch(InterruptedException success){}
                                }
                            });
                        s.start();
                        tu.timedJoin(s,MEDIUM_DELAY_MS);
                        threadShouldThrow();
                    }
                    catch(Exception e) {}
                }
            });
        t.start();
        try {
            Thread.sleep(SHORT_DELAY_MS);
            t.interrupt();
            t.join();
        } catch(Exception e) {
            unexpectedException();
        }
    
public voidtestTimedSleep()
timedSleep throws InterruptedException when interrupted

        //created a new thread with anonymous runnable

        Thread t = new Thread(new Runnable() {
                public void run() {
                    TimeUnit tu = TimeUnit.MILLISECONDS;
                    try {
                        tu.sleep(MEDIUM_DELAY_MS);
                        threadShouldThrow();
                    }
                    catch(InterruptedException success) {} 
                }
            });
        t.start();
        try {
            Thread.sleep(SHORT_DELAY_MS);
            t.interrupt();
            t.join();
        } catch(Exception e) {
            unexpectedException();
        }
    
public voidtestTimedWait()
timedWait throws InterruptedException when interrupted

        Thread t = new Thread(new Runnable() {
                public void run() {
                    Object o = new Object();
                    
                    TimeUnit tu = TimeUnit.MILLISECONDS;
                    try {
                        synchronized(o) {
                            tu.timedWait(o,MEDIUM_DELAY_MS);
                        }
                        threadShouldThrow();
                    }
                    catch(InterruptedException success) {} 
                    catch(IllegalMonitorStateException failure) {
                        threadUnexpectedException();
                    }
                }
            });
        t.start();
        try {
            Thread.sleep(SHORT_DELAY_MS);
            t.interrupt();
            t.join();
        } catch(Exception e) {
            unexpectedException();
        }
    
public voidtestTimedWait_IllegalMonitorException()
Timed wait without holding lock throws IllegalMonitorStateException

        //created a new thread with anonymous runnable

        Thread t = new Thread(new Runnable() {
                public void run() {
                    Object o = new Object();
                    TimeUnit tu = TimeUnit.MILLISECONDS;
                    try {
                        tu.timedWait(o,LONG_DELAY_MS);
                        threadShouldThrow();
                    }
                    catch (InterruptedException ie) {
                        threadUnexpectedException();
                    } 
                    catch(IllegalMonitorStateException success) {
                    }
                    
                }
            });
        t.start();
        try {
            Thread.sleep(SHORT_DELAY_MS);
            t.interrupt();
            t.join();
        } catch(Exception e) {
            unexpectedException();
        }
    
public voidtestToMicros()
toMicros correctly converts sample values in different units to microseconds

        for (long t = 0; t < 10; ++t) {
            assertEquals(1000000 * t, 
                         TimeUnit.SECONDS.toMicros(t));

            assertEquals(1000 * t, 
                         TimeUnit.MILLISECONDS.toMicros(t));
            assertEquals(t, 
                         TimeUnit.MICROSECONDS.toMicros(t));
            assertEquals(t, 
                         TimeUnit.NANOSECONDS.toMicros(t * 1000));
        }
    
public voidtestToMillis()
toMillis correctly converts sample values in different units to milliseconds

        for (long t = 0; t < 10; ++t) {
            assertEquals(1000 * t, 
                         TimeUnit.SECONDS.toMillis(t));

            assertEquals(t, 
                         TimeUnit.MILLISECONDS.toMillis(t));
            assertEquals(t, 
                         TimeUnit.MICROSECONDS.toMillis(t * 1000));
            assertEquals(t, 
                         TimeUnit.NANOSECONDS.toMillis(t * 1000000));
        }
    
public voidtestToNanos()
toNanos correctly converts sample values in different units to nanoseconds

        for (long t = 0; t < 10; ++t) {
            assertEquals(1000000000 * t, 
                         TimeUnit.SECONDS.toNanos(t));

            assertEquals(1000000 * t, 
                         TimeUnit.MILLISECONDS.toNanos(t));
            assertEquals(1000 * t, 
                         TimeUnit.MICROSECONDS.toNanos(t));
            assertEquals(t, 
                         TimeUnit.NANOSECONDS.toNanos(t));
        }
    
public voidtestToNanosSaturate()
toNanos saturates positive too-large values to Long.MAX_VALUE and negative to LONG.MIN_VALUE

            assertEquals(Long.MAX_VALUE,
                         TimeUnit.MILLISECONDS.toNanos(Long.MAX_VALUE / 2));
            assertEquals(Long.MIN_VALUE,
                         TimeUnit.MILLISECONDS.toNanos(-Long.MAX_VALUE / 3));
    
public voidtestToSeconds()
toSeconds correctly converts sample values in different units to seconds

        for (long t = 0; t < 10; ++t) {
            assertEquals(t, 
                         TimeUnit.SECONDS.toSeconds(t));

            assertEquals(t, 
                         TimeUnit.MILLISECONDS.toSeconds(t * 1000));
            assertEquals(t, 
                         TimeUnit.MICROSECONDS.toSeconds(t * 1000000));
            assertEquals(t, 
                         TimeUnit.NANOSECONDS.toSeconds(t * 1000000000));
        }
    
public voidtestToString()
toString returns string containing common name of unit

        String s = TimeUnit.SECONDS.toString();
        assertTrue(s.indexOf("ECOND") >= 0);