1 /* 2 * Copyright (c) 2005, 2016, 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 8141452 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.time.temporal.ChronoUnit; 45 import java.util.Arrays; 46 import java.util.concurrent.TimeUnit; 47 48 public class Basic { 49 private static void realMain(String[] args) throws Throwable { 50 51 for (TimeUnit u : TimeUnit.values()) { 52 System.out.println(u); 53 check(u instanceof TimeUnit); 54 equal(42L, u.convert(42, u)); 55 for (TimeUnit v : TimeUnit.values()) 56 if (u.convert(42, v) >= 42) 57 equal(42L, v.convert(u.convert(42, v), u)); 58 check(readObject(serializedForm(u)) == u); 59 } 60 61 equal( 24L, HOURS.convert (1, DAYS)); 62 equal( 60L, MINUTES.convert (1, HOURS)); 63 equal( 60L, SECONDS.convert (1, MINUTES)); 64 equal(1000L, MILLISECONDS.convert(1, SECONDS)); 65 equal(1000L, MICROSECONDS.convert(1, MILLISECONDS)); 66 equal(1000L, NANOSECONDS.convert (1, MICROSECONDS)); 67 68 equal( 24L, DAYS.toHours(1)); 69 equal( 60L, HOURS.toMinutes(1)); 70 equal( 60L, MINUTES.toSeconds(1)); 71 equal(1000L, SECONDS.toMillis(1)); 72 equal(1000L, MILLISECONDS.toMicros(1)); 73 equal(1000L, MICROSECONDS.toNanos(1)); 74 75 long t0 = System.nanoTime(); 76 MILLISECONDS.sleep(3); /* See windows bug 6313903, might not sleep */ 77 long elapsedMillis = (System.nanoTime() - t0)/(1000L * 1000L); 78 System.out.printf("elapsed=%d%n", elapsedMillis); 79 check(elapsedMillis >= 0); 80 /* Might not sleep on windows: check(elapsedMillis >= 3); */ 81 check(elapsedMillis < 1000); 82 83 //---------------------------------------------------------------- 84 // Tests for serialized form compatibility with previous release 85 //---------------------------------------------------------------- 86 byte[] serializedForm = /* Generated using tiger */ 87 {-84, -19, 0, 5, '~', 'r', 0, 29, 'j', 'a', 'v', 'a', '.', 88 'u', 't', 'i', 'l', '.', 'c', 'o', 'n', 'c', 'u', 'r', 'r', 'e', 89 'n', 't', '.', 'T', 'i', 'm', 'e', 'U', 'n', 'i', 't', 0, 0, 90 0, 0, 0, 0, 0, 0, 18, 0, 0, 'x', 'r', 0, 14, 91 'j', 'a', 'v', 'a', '.', 'l', 'a', 'n', 'g', '.', 'E', 'n', 'u', 92 'm', 0, 0, 0, 0, 0, 0, 0, 0, 18, 0, 0, 'x', 93 'p', 't', 0, 7, 'S', 'E', 'C', 'O', 'N', 'D', 'S', }; 94 check(Arrays.equals(serializedForm(SECONDS), serializedForm)); 95 } 96 97 //--------------------- Infrastructure --------------------------- 98 static volatile int passed = 0, failed = 0; 99 static void pass() {passed++;} 100 static void fail() {failed++; Thread.dumpStack();} 101 static void fail(String msg) {System.out.println(msg); fail();} 102 static void unexpected(Throwable t) {failed++; t.printStackTrace();} 103 static void check(boolean cond) {if (cond) pass(); else fail();} 104 static void equal(Object x, Object y) { 105 if (x == null ? y == null : x.equals(y)) pass(); 106 else fail(x + " not equal to " + y);} 107 public static void main(String[] args) throws Throwable { 108 try {realMain(args);} catch (Throwable t) {unexpected(t);} 109 System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed); 110 if (failed > 0) throw new AssertionError("Some tests failed");} 111 static byte[] serializedForm(Object obj) throws IOException { 112 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 113 new ObjectOutputStream(baos).writeObject(obj); 114 return baos.toByteArray(); 115 } 116 static Object readObject(byte[] bytes) 117 throws IOException, ClassNotFoundException { 118 InputStream is = new ByteArrayInputStream(bytes); 119 return new ObjectInputStream(is).readObject(); 120 } 121 122 //----------------------------------------------------------------------- 123 // toChronoUnit(), of() 124 //-----------------------------------------------------------------------` 125 @DataProvider(name = "convertedUnit") 126 Object[][] data_convertedUnit() { 127 return new Object[][] { 128 {NANOSECONDS, ChronoUnit.NANOS, true}, 129 {MICROSECONDS, ChronoUnit.MICROS, true}, 130 {MILLISECONDS, ChronoUnit.MILLIS, true}, 131 {SECONDS, ChronoUnit.SECONDS, true}, 132 {MINUTES, ChronoUnit.MINUTES, true}, 133 {HOURS, ChronoUnit.HOURS, true}, 134 {DAYS, ChronoUnit.DAYS, true}, 135 136 {NANOSECONDS, ChronoUnit.MINUTES, false}, 137 {MICROSECONDS, ChronoUnit.SECONDS, false}, 138 {MILLISECONDS, ChronoUnit.HOURS, false}, 139 {SECONDS, ChronoUnit.MICROS, false}, 140 {MINUTES, ChronoUnit.MILLIS, false}, 141 {HOURS, ChronoUnit.DAYS, false}, 142 {DAYS, ChronoUnit.NANOS, false}, 143 }; 144 } 145 146 @Test(dataProvider = "convertedUnit") 147 public void test_toChronoUnit(TimeUnit timeUnit, ChronoUnit chronoUnit, boolean expected) { 148 assertEquals(TimeUnit.toChronoUnit(timeUnit).equals(chronoUnit), expected); 149 } 150 151 @Test(expectedExceptions=NullPointerException.class) 152 public void test_toChronoUnit_null() { 153 TimeUnit.toChronoUnit(null); 154 } 155 156 @Test(dataProvider = "convertedUnit") 157 public void test_of(TimeUnit timeUnit, ChronoUnit chronoUnit, boolean expected) { 158 assertEquals(TimeUnit.of(chronoUnit).equals(timeUnit), expected); 159 160 } 161 162 @Test(expectedExceptions=NullPointerException.class) 163 public void test_of_null() { 164 TimeUnit.of(null); 165 } 166 167 @Test(expectedExceptions=IllegalArgumentException.class) 168 public void test_of_illegalArg() { 169 TimeUnit.of(ChronoUnit.ERAS); 170 171 } 172 }