|
1 /* |
|
2 * Copyright (c) 2013, 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 /* |
|
25 *@test |
|
26 *@bug 8007520 |
|
27 *@summary Test those bridge methods to/from java.time date/time classes |
|
28 * @key randomness |
|
29 */ |
|
30 |
|
31 import java.util.Random; |
|
32 import java.sql.Date; |
|
33 import java.sql.Time; |
|
34 import java.sql.Timestamp; |
|
35 import java.time.Instant; |
|
36 import java.time.LocalDateTime; |
|
37 import java.time.LocalDate; |
|
38 import java.time.LocalTime; |
|
39 import java.time.ZoneId; |
|
40 import java.time.ZoneOffset; |
|
41 import java.time.ZonedDateTime; |
|
42 |
|
43 public class JavatimeTest { |
|
44 |
|
45 static final int NANOS_PER_SECOND = 1000000000; |
|
46 |
|
47 public static void main(String[] args) throws Throwable { |
|
48 int N = 10000; |
|
49 long t1970 = new java.util.Date(70, 0, 01).getTime(); |
|
50 Random r = new Random(); |
|
51 for (int i = 0; i < N; i++) { |
|
52 int days = r.nextInt(50) * 365 + r.nextInt(365); |
|
53 long secs = t1970 + days * 86400 + r.nextInt(86400); |
|
54 int nanos = r.nextInt(NANOS_PER_SECOND); |
|
55 int nanos_ms = nanos / 1000000 * 1000000; // millis precision |
|
56 long millis = secs * 1000 + r.nextInt(1000); |
|
57 |
|
58 LocalDateTime ldt = LocalDateTime.ofEpochSecond(secs, nanos, ZoneOffset.UTC); |
|
59 LocalDateTime ldt_ms = LocalDateTime.ofEpochSecond(secs, nanos_ms, ZoneOffset.UTC); |
|
60 Instant inst = Instant.ofEpochSecond(secs, nanos); |
|
61 Instant inst_ms = Instant.ofEpochSecond(secs, nanos_ms); |
|
62 //System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); |
|
63 |
|
64 /////////// Timestamp //////////////////////////////// |
|
65 Timestamp ta = new Timestamp(millis); |
|
66 ta.setNanos(nanos); |
|
67 if (!isEqual(ta.toLocalDateTime(), ta)) { |
|
68 System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); |
|
69 print(ta.toLocalDateTime(), ta); |
|
70 throw new RuntimeException("FAILED: j.s.ts -> ldt"); |
|
71 } |
|
72 if (!isEqual(ldt, Timestamp.valueOf(ldt))) { |
|
73 System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); |
|
74 print(ldt, Timestamp.valueOf(ldt)); |
|
75 throw new RuntimeException("FAILED: ldt -> j.s.ts"); |
|
76 } |
|
77 Instant inst0 = ta.toInstant(); |
|
78 if (ta.getTime() != inst0.toEpochMilli() || |
|
79 ta.getNanos() != inst0.getNano() || |
|
80 !ta.equals(Timestamp.from(inst0))) { |
|
81 System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); |
|
82 throw new RuntimeException("FAILED: j.s.ts -> instant -> j.s.ts"); |
|
83 } |
|
84 inst = Instant.ofEpochSecond(secs, nanos); |
|
85 Timestamp ta0 = Timestamp.from(inst); |
|
86 if (ta0.getTime() != inst.toEpochMilli() || |
|
87 ta0.getNanos() != inst.getNano() || |
|
88 !inst.equals(ta0.toInstant())) { |
|
89 System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); |
|
90 throw new RuntimeException("FAILED: instant -> timestamp -> instant"); |
|
91 } |
|
92 |
|
93 ////////// java.sql.Date ///////////////////////////// |
|
94 // j.s.d/t uses j.u.d.equals() !!!!!!!! |
|
95 java.sql.Date jsd = new java.sql.Date(millis); |
|
96 if (!isEqual(jsd.toLocalDate(), jsd)) { |
|
97 System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); |
|
98 print(jsd.toLocalDate(), jsd); |
|
99 throw new RuntimeException("FAILED: j.s.d -> ld"); |
|
100 } |
|
101 LocalDate ld = ldt.toLocalDate(); |
|
102 if (!isEqual(ld, java.sql.Date.valueOf(ld))) { |
|
103 System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); |
|
104 print(ld, java.sql.Date.valueOf(ld)); |
|
105 throw new RuntimeException("FAILED: ld -> j.s.d"); |
|
106 } |
|
107 ////////// java.sql.Time ///////////////////////////// |
|
108 java.sql.Time jst = new java.sql.Time(millis); |
|
109 if (!isEqual(jst.toLocalTime(), jst)) { |
|
110 System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); |
|
111 print(jst.toLocalTime(), jst); |
|
112 throw new RuntimeException("FAILED: j.s.t -> lt"); |
|
113 } |
|
114 // millis precision |
|
115 LocalTime lt = ldt_ms.toLocalTime(); |
|
116 if (!isEqual(lt, java.sql.Time.valueOf(lt))) { |
|
117 System.out.printf("ms: %16d ns: %10d ldt:[%s]%n", millis, nanos, ldt); |
|
118 print(lt, java.sql.Time.valueOf(lt)); |
|
119 throw new RuntimeException("FAILED: lt -> j.s.t"); |
|
120 } |
|
121 } |
|
122 System.out.println("Passed!"); |
|
123 } |
|
124 |
|
125 private static boolean isEqual(LocalDateTime ldt, Timestamp ts) { |
|
126 ZonedDateTime zdt = ZonedDateTime.of(ldt, ZoneId.systemDefault()); |
|
127 return zdt.getYear() == ts.getYear() + 1900 && |
|
128 zdt.getMonthValue() == ts.getMonth() + 1 && |
|
129 zdt.getDayOfMonth() == ts.getDate() && |
|
130 zdt.getHour() == ts.getHours() && |
|
131 zdt.getMinute() == ts.getMinutes() && |
|
132 zdt.getSecond() == ts.getSeconds() && |
|
133 zdt.getNano() == ts.getNanos(); |
|
134 } |
|
135 |
|
136 private static void print(LocalDateTime ldt, Timestamp ts) { |
|
137 ZonedDateTime zdt = ZonedDateTime.of(ldt, ZoneId.systemDefault()); |
|
138 System.out.printf("ldt:ts %d/%d, %d/%d, %d/%d, %d/%d, %d/%d, %d/%d, nano:[%d/%d]%n", |
|
139 zdt.getYear(), ts.getYear() + 1900, |
|
140 zdt.getMonthValue(), ts.getMonth() + 1, |
|
141 zdt.getDayOfMonth(), ts.getDate(), |
|
142 zdt.getHour(), ts.getHours(), |
|
143 zdt.getMinute(), ts.getMinutes(), |
|
144 zdt.getSecond(), ts.getSeconds(), |
|
145 zdt.getNano(), ts.getNanos()); |
|
146 } |
|
147 |
|
148 private static boolean isEqual(LocalDate ld, java.sql.Date d) { |
|
149 return ld.getYear() == d.getYear() + 1900 && |
|
150 ld.getMonthValue() == d.getMonth() + 1 && |
|
151 ld.getDayOfMonth() == d.getDate(); |
|
152 } |
|
153 |
|
154 private static void print(LocalDate ld, java.sql.Date d) { |
|
155 System.out.printf("%d/%d, %d/%d, %d/%d%n", |
|
156 ld.getYear(), d.getYear() + 1900, |
|
157 ld.getMonthValue(), d.getMonth() + 1, |
|
158 ld.getDayOfMonth(), d.getDate()); |
|
159 } |
|
160 |
|
161 private static boolean isEqual(LocalTime lt, java.sql.Time t) { |
|
162 return lt.getHour() == t.getHours() && |
|
163 lt.getMinute() == t.getMinutes() && |
|
164 lt.getSecond() == t.getSeconds(); |
|
165 } |
|
166 |
|
167 private static void print(LocalTime lt, java.sql.Time t) { |
|
168 System.out.printf("%d/%d, %d/%d, %d/%d%n", |
|
169 lt.getHour(), t.getHours(), |
|
170 lt.getMinute(), t.getMinutes(), |
|
171 lt.getSecond(), t.getSeconds()); |
|
172 } |
|
173 } |