test/jdk/java/util/Calendar/JavatimeTest.java
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 42159 jdk/test/java/util/Calendar/JavatimeTest.java@9ab10842acf7
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
     1
/*
42159
9ab10842acf7 8165296: update existing i18n test cases of test/java/util
nishjain
parents: 30046
diff changeset
     2
 * Copyright (c) 2013, 2016, Oracle and/or its affiliates. All rights reserved.
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
     4
 *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
     7
 * published by the Free Software Foundation.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
     8
 *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    13
 * accompanied this code).
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    14
 *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    18
 *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    21
 * questions.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    22
 */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    23
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    24
/*
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    25
 *@test
15999
3ff9cd94b52e 8008254: j.u.Calendar.JavatimeTest failed at TL b78 pit testing
sherman
parents: 15658
diff changeset
    26
 *@bug 8007520 8008254
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    27
 *@summary Test those bridge methods to/from java.time date/time classes
30046
cf2c86e1819e 8078334: Mark regression tests using randomness
darcy
parents: 21286
diff changeset
    28
 * @key randomness
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    29
 */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    30
42159
9ab10842acf7 8165296: update existing i18n test cases of test/java/util
nishjain
parents: 30046
diff changeset
    31
import java.time.Instant;
9ab10842acf7 8165296: update existing i18n test cases of test/java/util
nishjain
parents: 30046
diff changeset
    32
import java.time.LocalDateTime;
9ab10842acf7 8165296: update existing i18n test cases of test/java/util
nishjain
parents: 30046
diff changeset
    33
import java.time.ZonedDateTime;
9ab10842acf7 8165296: update existing i18n test cases of test/java/util
nishjain
parents: 30046
diff changeset
    34
import java.time.ZoneId;
9ab10842acf7 8165296: update existing i18n test cases of test/java/util
nishjain
parents: 30046
diff changeset
    35
import java.time.ZoneOffset;
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    36
import java.util.Calendar;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    37
import java.util.Date;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    38
import java.util.GregorianCalendar;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    39
import java.util.Random;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    40
import java.util.TimeZone;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    41
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    42
public class JavatimeTest {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    43
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    44
    static final int NANOS_PER_SECOND = 1000_000_000;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    45
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    46
    public static void main(String[] args) throws Throwable {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    47
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    48
        int N = 10000;
42159
9ab10842acf7 8165296: update existing i18n test cases of test/java/util
nishjain
parents: 30046
diff changeset
    49
        @SuppressWarnings("deprecation")
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    50
        long t1970 = new java.util.Date(70, 0, 01).getTime();
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    51
        Random r = new Random();
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    52
        for (int i = 0; i < N; i++) {
42159
9ab10842acf7 8165296: update existing i18n test cases of test/java/util
nishjain
parents: 30046
diff changeset
    53
            int days = r.nextInt(50) * 365 + r.nextInt(365);
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    54
            long secs = t1970 + days * 86400 + r.nextInt(86400);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    55
            int nanos = r.nextInt(NANOS_PER_SECOND);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    56
            int nanos_ms = nanos / 1000000 * 1000000; // millis precision
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    57
            long millis = secs * 1000 + r.nextInt(1000);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    58
            LocalDateTime ldt = LocalDateTime.ofEpochSecond(secs, nanos, ZoneOffset.UTC);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    59
            LocalDateTime ldt_ms = LocalDateTime.ofEpochSecond(secs, nanos_ms, ZoneOffset.UTC);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    60
            Instant inst = Instant.ofEpochSecond(secs, nanos);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    61
            Instant inst_ms = Instant.ofEpochSecond(secs, nanos_ms);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    62
            ///////////// java.util.Date /////////////////////////
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    63
            Date jud = new java.util.Date(millis);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    64
            Instant inst0 = jud.toInstant();
42159
9ab10842acf7 8165296: update existing i18n test cases of test/java/util
nishjain
parents: 30046
diff changeset
    65
            if (jud.getTime() != inst0.toEpochMilli()
9ab10842acf7 8165296: update existing i18n test cases of test/java/util
nishjain
parents: 30046
diff changeset
    66
                    || !jud.equals(Date.from(inst0))) {
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    67
                System.out.printf("ms: %16d  ns: %10d  ldt:[%s]%n", millis, nanos, ldt);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    68
                throw new RuntimeException("FAILED: j.u.d -> instant -> j.u.d");
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    69
            }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    70
            // roundtrip only with millis precision
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    71
            Date jud0 = Date.from(inst_ms);
42159
9ab10842acf7 8165296: update existing i18n test cases of test/java/util
nishjain
parents: 30046
diff changeset
    72
            if (jud0.getTime() != inst_ms.toEpochMilli()
9ab10842acf7 8165296: update existing i18n test cases of test/java/util
nishjain
parents: 30046
diff changeset
    73
                    || !inst_ms.equals(jud0.toInstant())) {
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    74
                System.out.printf("ms: %16d  ns: %10d  ldt:[%s]%n", millis, nanos, ldt);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    75
                throw new RuntimeException("FAILED: instant -> j.u.d -> instant");
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    76
            }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    77
            //////////// java.util.GregorianCalendar /////////////
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    78
            GregorianCalendar cal = new GregorianCalendar();
15999
3ff9cd94b52e 8008254: j.u.Calendar.JavatimeTest failed at TL b78 pit testing
sherman
parents: 15658
diff changeset
    79
            // non-roundtrip of tz name between j.u.tz and j.t.zid
3ff9cd94b52e 8008254: j.u.Calendar.JavatimeTest failed at TL b78 pit testing
sherman
parents: 15658
diff changeset
    80
            cal.setTimeZone(TimeZone.getTimeZone(ZoneId.systemDefault()));
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    81
            cal.setGregorianChange(new java.util.Date(Long.MIN_VALUE));
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    82
            cal.setFirstDayOfWeek(Calendar.MONDAY);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    83
            cal.setMinimalDaysInFirstWeek(4);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    84
            cal.setTimeInMillis(millis);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    85
            ZonedDateTime zdt0 = cal.toZonedDateTime();
42159
9ab10842acf7 8165296: update existing i18n test cases of test/java/util
nishjain
parents: 30046
diff changeset
    86
            if (cal.getTimeInMillis() != zdt0.toInstant().toEpochMilli()
9ab10842acf7 8165296: update existing i18n test cases of test/java/util
nishjain
parents: 30046
diff changeset
    87
                    || !cal.equals(GregorianCalendar.from(zdt0))) {
15999
3ff9cd94b52e 8008254: j.u.Calendar.JavatimeTest failed at TL b78 pit testing
sherman
parents: 15658
diff changeset
    88
                System.out.println("cal:" + cal);
3ff9cd94b52e 8008254: j.u.Calendar.JavatimeTest failed at TL b78 pit testing
sherman
parents: 15658
diff changeset
    89
                System.out.println("zdt:" + zdt0);
3ff9cd94b52e 8008254: j.u.Calendar.JavatimeTest failed at TL b78 pit testing
sherman
parents: 15658
diff changeset
    90
                System.out.println("calNew:" + GregorianCalendar.from(zdt0));
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    91
                System.out.printf("ms: %16d  ns: %10d  ldt:[%s]%n", millis, nanos, ldt);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    92
                throw new RuntimeException("FAILED: gcal -> zdt -> gcal");
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    93
            }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    94
            inst0 = cal.toInstant();
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    95
            if (cal.getTimeInMillis() != inst0.toEpochMilli()) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    96
                System.out.printf("ms: %16d  ns: %10d  ldt:[%s]%n", millis, nanos, ldt);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    97
                throw new RuntimeException("FAILED: gcal -> zdt");
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    98
            }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
    99
            ZonedDateTime zdt = ZonedDateTime.of(ldt_ms, ZoneId.systemDefault());
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   100
            GregorianCalendar cal0 = GregorianCalendar.from(zdt);
42159
9ab10842acf7 8165296: update existing i18n test cases of test/java/util
nishjain
parents: 30046
diff changeset
   101
            if (zdt.toInstant().toEpochMilli() != cal0.getTimeInMillis()
9ab10842acf7 8165296: update existing i18n test cases of test/java/util
nishjain
parents: 30046
diff changeset
   102
                    || !zdt.equals(GregorianCalendar.from(zdt).toZonedDateTime())) {
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   103
                System.out.printf("ms: %16d  ns: %10d  ldt:[%s]%n", millis, nanos, ldt);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   104
                throw new RuntimeException("FAILED: zdt -> gcal -> zdt");
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   105
            }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   106
        }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   107
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   108
        ///////////// java.util.TimeZone /////////////////////////
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   109
        for (String zidStr : TimeZone.getAvailableIDs()) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   110
            // TBD: tzdt intergration
42159
9ab10842acf7 8165296: update existing i18n test cases of test/java/util
nishjain
parents: 30046
diff changeset
   111
            if (zidStr.startsWith("SystemV")
9ab10842acf7 8165296: update existing i18n test cases of test/java/util
nishjain
parents: 30046
diff changeset
   112
                    || zidStr.contains("Riyadh8")
9ab10842acf7 8165296: update existing i18n test cases of test/java/util
nishjain
parents: 30046
diff changeset
   113
                    || zidStr.equals("US/Pacific-New")
9ab10842acf7 8165296: update existing i18n test cases of test/java/util
nishjain
parents: 30046
diff changeset
   114
                    || zidStr.equals("EST")
9ab10842acf7 8165296: update existing i18n test cases of test/java/util
nishjain
parents: 30046
diff changeset
   115
                    || zidStr.equals("HST")
9ab10842acf7 8165296: update existing i18n test cases of test/java/util
nishjain
parents: 30046
diff changeset
   116
                    || zidStr.equals("MST")) {
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   117
                continue;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   118
            }
21286
0641a36bb610 8025971: Remove ZoneId.OLD_SHORT_IDS
sherman
parents: 16852
diff changeset
   119
            ZoneId zid = ZoneId.of(zidStr, ZoneId.SHORT_IDS);
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   120
            if (!zid.equals(TimeZone.getTimeZone(zid).toZoneId())) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   121
                throw new RuntimeException("FAILED: zid -> tz -> zid :" + zidStr);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   122
            }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   123
            TimeZone tz = TimeZone.getTimeZone(zidStr);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   124
            // no round-trip for alias and "GMT"
42159
9ab10842acf7 8165296: update existing i18n test cases of test/java/util
nishjain
parents: 30046
diff changeset
   125
            if (!tz.equals(TimeZone.getTimeZone(tz.toZoneId()))
9ab10842acf7 8165296: update existing i18n test cases of test/java/util
nishjain
parents: 30046
diff changeset
   126
                    && !ZoneId.SHORT_IDS.containsKey(zidStr)
9ab10842acf7 8165296: update existing i18n test cases of test/java/util
nishjain
parents: 30046
diff changeset
   127
                    && !zidStr.startsWith("GMT")) {
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   128
                throw new RuntimeException("FAILED: tz -> zid -> tz :" + zidStr);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   129
            }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   130
        }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   131
        System.out.println("Passed!");
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   132
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents:
diff changeset
   133
}