< prev index next >

test/java/util/concurrent/TimeUnit/Basic.java

Print this page


   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));


 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 }
   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));


 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 }
< prev index next >