Methods Summary |
---|
public static void | main(java.lang.String[] args)
junit.textui.TestRunner.run(suite());
|
public static junit.framework.Test | suite()
return new TestSuite(TimeUnitTest.class);
|
public void | testConvert()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 void | testConvertSaturate()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 void | testSerialization()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 void | testTimedJoin()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 void | testTimedSleep()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 void | testTimedWait()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 void | testTimedWait_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 void | testToMicros()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 void | testToMillis()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 void | testToNanos()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 void | testToNanosSaturate()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 void | testToSeconds()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 void | testToString()toString returns string containing common name of unit
String s = TimeUnit.SECONDS.toString();
assertTrue(s.indexOf("ECOND") >= 0);
|