3964N/A/*
3964N/A * Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved.
3964N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3964N/A *
3964N/A * This code is free software; you can redistribute it and/or modify it
3964N/A * under the terms of the GNU General Public License version 2 only, as
3964N/A * published by the Free Software Foundation.
3964N/A *
3964N/A * This code is distributed in the hope that it will be useful, but WITHOUT
3964N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3964N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
3964N/A * version 2 for more details (a copy is included in the LICENSE file that
3964N/A * accompanied this code).
3964N/A *
3964N/A * You should have received a copy of the GNU General Public License version
3964N/A * 2 along with this work; if not, write to the Free Software Foundation,
3964N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
3964N/A *
3964N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
3964N/A * or visit www.oracle.com if you need additional information or have any
3964N/A * questions.
3964N/A */
3964N/A
3964N/A/* @test
3964N/A * @bug 5057341 6363898
3964N/A * @summary Basic tests for TimeUnit
3964N/A * @author Martin Buchholz
3964N/A */
3964N/A
3964N/Aimport java.io.*;
3964N/Aimport java.util.*;
3964N/Aimport java.util.concurrent.*;
3964N/Aimport static java.util.concurrent.TimeUnit.*;
3964N/A
3964N/Apublic class Basic {
3964N/A private static void realMain(String[] args) throws Throwable {
3964N/A
3964N/A for (TimeUnit u : TimeUnit.values()) {
3964N/A System.out.println(u);
3964N/A check(u instanceof TimeUnit);
3964N/A equal(42L, u.convert(42, u));
3964N/A for (TimeUnit v : TimeUnit.values())
3964N/A if (u.convert(42, v) >= 42)
3964N/A equal(42L, v.convert(u.convert(42, v), u));
3964N/A check(readObject(serializedForm(u)) == u);
3964N/A }
3964N/A
3964N/A equal( 24L, HOURS.convert (1, DAYS));
3964N/A equal( 60L, MINUTES.convert (1, HOURS));
3964N/A equal( 60L, SECONDS.convert (1, MINUTES));
3964N/A equal(1000L, MILLISECONDS.convert(1, SECONDS));
3964N/A equal(1000L, MICROSECONDS.convert(1, MILLISECONDS));
3964N/A equal(1000L, NANOSECONDS.convert (1, MICROSECONDS));
3964N/A
3964N/A equal( 24L, DAYS.toHours(1));
3964N/A equal( 60L, HOURS.toMinutes(1));
3964N/A equal( 60L, MINUTES.toSeconds(1));
3964N/A equal(1000L, SECONDS.toMillis(1));
3964N/A equal(1000L, MILLISECONDS.toMicros(1));
3964N/A equal(1000L, MICROSECONDS.toNanos(1));
3964N/A
3964N/A long t0 = System.nanoTime();
3964N/A MILLISECONDS.sleep(3); /* See windows bug 6313903, might not sleep */
3964N/A long elapsedMillis = (System.nanoTime() - t0)/(1000L * 1000L);
3964N/A System.out.printf("elapsed=%d%n", elapsedMillis);
3964N/A check(elapsedMillis >= 0);
3964N/A /* Might not sleep on windows: check(elapsedMillis >= 3); */
3964N/A check(elapsedMillis < 1000);
3964N/A
3964N/A //----------------------------------------------------------------
3964N/A // Tests for serialized form compatibility with previous release
3964N/A //----------------------------------------------------------------
3964N/A byte[] serializedForm = /* Generated using tiger */
3964N/A {-84, -19, 0, 5, '~', 'r', 0, 29, 'j', 'a', 'v', 'a', '.',
3964N/A 'u', 't', 'i', 'l', '.', 'c', 'o', 'n', 'c', 'u', 'r', 'r', 'e',
3964N/A 'n', 't', '.', 'T', 'i', 'm', 'e', 'U', 'n', 'i', 't', 0, 0,
4039N/A 0, 0, 0, 0, 0, 0, 18, 0, 0, 'x', 'r', 0, 14,
3964N/A 'j', 'a', 'v', 'a', '.', 'l', 'a', 'n', 'g', '.', 'E', 'n', 'u',
3964N/A 'm', 0, 0, 0, 0, 0, 0, 0, 0, 18, 0, 0, 'x',
3964N/A 'p', 't', 0, 7, 'S', 'E', 'C', 'O', 'N', 'D', 'S', };
3964N/A check(Arrays.equals(serializedForm(SECONDS), serializedForm));
3964N/A }
3964N/A
3964N/A //--------------------- Infrastructure ---------------------------
3964N/A static volatile int passed = 0, failed = 0;
3964N/A static void pass() {passed++;}
3964N/A static void fail() {failed++; Thread.dumpStack();}
3964N/A static void fail(String msg) {System.out.println(msg); fail();}
3964N/A static void unexpected(Throwable t) {failed++; t.printStackTrace();}
4039N/A static void check(boolean cond) {if (cond) pass(); else fail();}
3964N/A static void equal(Object x, Object y) {
3964N/A if (x == null ? y == null : x.equals(y)) pass();
3964N/A else fail(x + " not equal to " + y);}
3964N/A public static void main(String[] args) throws Throwable {
3964N/A try {realMain(args);} catch (Throwable t) {unexpected(t);}
3964N/A System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
3964N/A if (failed > 0) throw new AssertionError("Some tests failed");}
3964N/A static byte[] serializedForm(Object obj) throws IOException {
3964N/A ByteArrayOutputStream baos = new ByteArrayOutputStream();
4039N/A new ObjectOutputStream(baos).writeObject(obj);
3964N/A return baos.toByteArray();
3964N/A }
3964N/A static Object readObject(byte[] bytes)
3964N/A throws IOException, ClassNotFoundException {
3964N/A InputStream is = new ByteArrayInputStream(bytes);
3964N/A return new ObjectInputStream(is).readObject();
4039N/A }
3964N/A}
3964N/A