|
1 /* |
|
2 * Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. |
|
8 * |
|
9 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 * version 2 for more details (a copy is included in the LICENSE file that |
|
13 * accompanied this code). |
|
14 * |
|
15 * You should have received a copy of the GNU General Public License version |
|
16 * 2 along with this work; if not, write to the Free Software Foundation, |
|
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 * |
|
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
20 * or visit www.oracle.com if you need additional information or have any |
|
21 * questions. |
|
22 */ |
|
23 |
|
24 /* @test |
|
25 * @bug 5057341 6363898 |
|
26 * @summary Basic tests for TimeUnit |
|
27 * @author Martin Buchholz |
|
28 */ |
|
29 |
|
30 import static java.util.concurrent.TimeUnit.DAYS; |
|
31 import static java.util.concurrent.TimeUnit.HOURS; |
|
32 import static java.util.concurrent.TimeUnit.MICROSECONDS; |
|
33 import static java.util.concurrent.TimeUnit.MILLISECONDS; |
|
34 import static java.util.concurrent.TimeUnit.MINUTES; |
|
35 import static java.util.concurrent.TimeUnit.NANOSECONDS; |
|
36 import static java.util.concurrent.TimeUnit.SECONDS; |
|
37 |
|
38 import java.io.ByteArrayInputStream; |
|
39 import java.io.ByteArrayOutputStream; |
|
40 import java.io.IOException; |
|
41 import java.io.InputStream; |
|
42 import java.io.ObjectOutputStream; |
|
43 import java.io.ObjectInputStream; |
|
44 import java.util.Arrays; |
|
45 import java.util.concurrent.TimeUnit; |
|
46 |
|
47 public class Basic { |
|
48 private static void realMain(String[] args) throws Throwable { |
|
49 |
|
50 for (TimeUnit u : TimeUnit.values()) { |
|
51 System.out.println(u); |
|
52 check(u instanceof TimeUnit); |
|
53 equal(42L, u.convert(42, u)); |
|
54 for (TimeUnit v : TimeUnit.values()) |
|
55 if (u.convert(42, v) >= 42) |
|
56 equal(42L, v.convert(u.convert(42, v), u)); |
|
57 check(readObject(serializedForm(u)) == u); |
|
58 } |
|
59 |
|
60 equal( 24L, HOURS.convert (1, DAYS)); |
|
61 equal( 60L, MINUTES.convert (1, HOURS)); |
|
62 equal( 60L, SECONDS.convert (1, MINUTES)); |
|
63 equal(1000L, MILLISECONDS.convert(1, SECONDS)); |
|
64 equal(1000L, MICROSECONDS.convert(1, MILLISECONDS)); |
|
65 equal(1000L, NANOSECONDS.convert (1, MICROSECONDS)); |
|
66 |
|
67 equal( 24L, DAYS.toHours(1)); |
|
68 equal( 60L, HOURS.toMinutes(1)); |
|
69 equal( 60L, MINUTES.toSeconds(1)); |
|
70 equal(1000L, SECONDS.toMillis(1)); |
|
71 equal(1000L, MILLISECONDS.toMicros(1)); |
|
72 equal(1000L, MICROSECONDS.toNanos(1)); |
|
73 |
|
74 long t0 = System.nanoTime(); |
|
75 MILLISECONDS.sleep(3); /* See windows bug 6313903, might not sleep */ |
|
76 long elapsedMillis = (System.nanoTime() - t0)/(1000L * 1000L); |
|
77 System.out.printf("elapsed=%d%n", elapsedMillis); |
|
78 check(elapsedMillis >= 0); |
|
79 /* Might not sleep on windows: check(elapsedMillis >= 3); */ |
|
80 check(elapsedMillis < 1000); |
|
81 |
|
82 //---------------------------------------------------------------- |
|
83 // Tests for serialized form compatibility with previous release |
|
84 //---------------------------------------------------------------- |
|
85 byte[] serializedForm = /* Generated using tiger */ |
|
86 {-84, -19, 0, 5, '~', 'r', 0, 29, 'j', 'a', 'v', 'a', '.', |
|
87 'u', 't', 'i', 'l', '.', 'c', 'o', 'n', 'c', 'u', 'r', 'r', 'e', |
|
88 'n', 't', '.', 'T', 'i', 'm', 'e', 'U', 'n', 'i', 't', 0, 0, |
|
89 0, 0, 0, 0, 0, 0, 18, 0, 0, 'x', 'r', 0, 14, |
|
90 'j', 'a', 'v', 'a', '.', 'l', 'a', 'n', 'g', '.', 'E', 'n', 'u', |
|
91 'm', 0, 0, 0, 0, 0, 0, 0, 0, 18, 0, 0, 'x', |
|
92 'p', 't', 0, 7, 'S', 'E', 'C', 'O', 'N', 'D', 'S', }; |
|
93 check(Arrays.equals(serializedForm(SECONDS), serializedForm)); |
|
94 } |
|
95 |
|
96 //--------------------- Infrastructure --------------------------- |
|
97 static volatile int passed = 0, failed = 0; |
|
98 static void pass() {passed++;} |
|
99 static void fail() {failed++; Thread.dumpStack();} |
|
100 static void fail(String msg) {System.out.println(msg); fail();} |
|
101 static void unexpected(Throwable t) {failed++; t.printStackTrace();} |
|
102 static void check(boolean cond) {if (cond) pass(); else fail();} |
|
103 static void equal(Object x, Object y) { |
|
104 if (x == null ? y == null : x.equals(y)) pass(); |
|
105 else fail(x + " not equal to " + y);} |
|
106 public static void main(String[] args) throws Throwable { |
|
107 try {realMain(args);} catch (Throwable t) {unexpected(t);} |
|
108 System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed); |
|
109 if (failed > 0) throw new AssertionError("Some tests failed");} |
|
110 static byte[] serializedForm(Object obj) throws IOException { |
|
111 ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
|
112 new ObjectOutputStream(baos).writeObject(obj); |
|
113 return baos.toByteArray(); |
|
114 } |
|
115 static Object readObject(byte[] bytes) |
|
116 throws IOException, ClassNotFoundException { |
|
117 InputStream is = new ByteArrayInputStream(bytes); |
|
118 return new ObjectInputStream(is).readObject(); |
|
119 } |
|
120 } |