author | sherman |
Fri, 12 Apr 2013 07:57:35 -0700 | |
changeset 16852 | 60207b2b4b42 |
parent 15665 | 2223468cd706 |
child 23010 | 6dadb192ad81 |
permissions | -rw-r--r-- |
15658 | 1 |
/* |
2 |
* Copyright (c) 2012, 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 |
|
15665
2223468cd706
8008161: Regression: j.u.TimeZone.getAvailableIDs(rawOffset) returns non-sorted list
sherman
parents:
15658
diff
changeset
|
26 |
*@bug 8007572 8008161 |
15658 | 27 |
*@summary Test whether the TimeZone generated from JSR310 tzdb is the same |
28 |
*as the one from the tz data from javazic |
|
29 |
*/ |
|
30 |
||
31 |
import java.io.File; |
|
32 |
import java.lang.reflect.*; |
|
33 |
import java.nio.file.*; |
|
34 |
import java.util.*; |
|
35 |
import java.util.regex.*; |
|
36 |
import java.time.zone.*; |
|
37 |
import java.time.ZoneId; |
|
38 |
||
39 |
public class TestZoneInfo310 { |
|
40 |
||
41 |
public static void main(String[] args) throws Throwable { |
|
42 |
||
43 |
String TESTDIR = System.getProperty("test.dir", "."); |
|
44 |
String SRCDIR = System.getProperty("test.src", "."); |
|
45 |
String tzdir = SRCDIR + File.separator + "tzdata"; |
|
46 |
String tzfiles = "africa antarctica asia australasia europe northamerica pacificnew southamerica backward etcetera systemv"; |
|
47 |
String jdk_tzdir = SRCDIR + File.separator + "tzdata_jdk"; |
|
48 |
String jdk_tzfiles = "gmt jdk11_backward"; |
|
49 |
String zidir = TESTDIR + File.separator + "zi"; |
|
50 |
File fZidir = new File(zidir); |
|
51 |
if (!fZidir.exists()) { |
|
52 |
fZidir.mkdirs(); |
|
53 |
} |
|
54 |
Matcher m = Pattern.compile("tzdata(?<ver>[0-9]{4}[A-z])") |
|
55 |
.matcher(new String(Files.readAllBytes(Paths.get(tzdir, "VERSION")), "ascii")); |
|
56 |
String ver = m.find() ? m.group("ver") : "NULL"; |
|
57 |
||
58 |
ArrayList<String> alist = new ArrayList<>(); |
|
59 |
alist.add("-V"); |
|
60 |
alist.add(ver); |
|
61 |
alist.add("-d"); |
|
62 |
alist.add(zidir); |
|
63 |
for (String f : tzfiles.split(" ")) { |
|
64 |
alist.add(tzdir + File.separator + f); |
|
65 |
} |
|
66 |
for (String f : jdk_tzfiles.split(" ")) { |
|
67 |
alist.add(jdk_tzdir + File.separator + f); |
|
68 |
} |
|
69 |
System.out.println("Compiling tz files!"); |
|
70 |
Main.main(alist.toArray(new String[alist.size()])); |
|
16852 | 71 |
|
15658 | 72 |
////////////////////////////////// |
73 |
System.out.println("testing!"); |
|
74 |
ZoneInfoFile.ziDir = zidir; |
|
75 |
long t0, t1; |
|
76 |
||
77 |
t0 = System.nanoTime(); |
|
78 |
ZoneInfoOld.getTimeZone("America/Los_Angeles"); |
|
79 |
t1 = System.nanoTime(); |
|
80 |
System.out.printf("OLD.getZoneInfoOld()[1]=%d%n", (t1 - t0) / 1000); |
|
81 |
||
82 |
t0 = System.nanoTime(); |
|
83 |
ZoneInfoOld.getTimeZone("America/New_York"); |
|
84 |
t1 = System.nanoTime(); |
|
85 |
System.out.printf("OLD.getZoneInfoOld()[2]=%d%n", (t1 - t0) / 1000); |
|
86 |
||
87 |
t0 = System.nanoTime(); |
|
88 |
ZoneInfoOld.getTimeZone("America/Denver"); |
|
89 |
t1 = System.nanoTime(); |
|
90 |
System.out.printf("OLD.getZoneInfoOld()[3]=%d%n", (t1 - t0) / 1000); |
|
91 |
||
92 |
t0 = System.nanoTime(); |
|
93 |
String[] zids_old = ZoneInfoOld.getAvailableIDs(); |
|
94 |
t1 = System.nanoTime(); |
|
95 |
System.out.printf("OLD.getAvailableIDs()=%d, total=%d%n", |
|
96 |
(t1 - t0) / 1000, zids_old.length); |
|
97 |
Arrays.sort(zids_old); |
|
98 |
||
99 |
t0 = System.nanoTime(); |
|
16852 | 100 |
String[] alias_old = ZoneInfoOld.getAliasTable() |
101 |
.keySet().toArray(new String[0]); |
|
102 |
t1 = System.nanoTime(); |
|
103 |
System.out.printf("OLD.getAliasTable()=%d, total=%d%n", |
|
104 |
(t1 - t0) / 1000, alias_old.length); |
|
105 |
Arrays.sort(alias_old); |
|
106 |
||
107 |
t0 = System.currentTimeMillis(); |
|
108 |
for (String zid : zids_old) { |
|
109 |
ZoneInfoOld.getTimeZone(zid); |
|
110 |
} |
|
111 |
t1 = System.currentTimeMillis(); |
|
112 |
System.out.printf("OLD.TotalTZ()=%d (ms)%n", t1 - t0); |
|
113 |
||
114 |
/* |
|
115 |
t0 = System.nanoTime(); |
|
15658 | 116 |
ZoneId.of("America/Los_Angeles").getRules(); |
117 |
t1 = System.nanoTime(); |
|
16852 | 118 |
System.out.printf("NEW.ZoneId.of()[1]=%d%n", (t1 - t0) / 1000); |
119 |
*/ |
|
15658 | 120 |
t0 = System.nanoTime(); |
121 |
TimeZone tz = TimeZone.getTimeZone("America/Los_Angeles"); |
|
122 |
t1 = System.nanoTime(); |
|
123 |
System.out.printf("NEW.getTimeZone()[1]=%d%n", (t1 - t0) / 1000); |
|
124 |
||
125 |
t0 = System.nanoTime(); |
|
126 |
tz = TimeZone.getTimeZone("America/New_York"); |
|
127 |
t1 = System.nanoTime(); |
|
128 |
System.out.printf("NEW.getTimeZone()[2]=%d%n", (t1 - t0) / 1000); |
|
129 |
||
130 |
t0 = System.nanoTime(); |
|
131 |
tz = TimeZone.getTimeZone("America/Denver"); |
|
132 |
t1 = System.nanoTime(); |
|
133 |
System.out.printf("NEW.getTimeZone()[3]=%d%n", (t1 - t0) / 1000); |
|
134 |
||
135 |
t0 = System.nanoTime(); |
|
136 |
String[] zids_new = TimeZone.getAvailableIDs(); |
|
137 |
t1 = System.nanoTime(); |
|
138 |
System.out.printf("NEW.getAvailableIDs()=%d, total=%d%n", |
|
139 |
(t1 - t0) / 1000, zids_new.length); |
|
140 |
Arrays.sort(zids_new); |
|
141 |
||
16852 | 142 |
t0 = System.nanoTime(); |
143 |
String[] alias_new = sun.util.calendar.ZoneInfo.getAliasTable() |
|
144 |
.keySet().toArray(new String[0]); |
|
145 |
t1 = System.nanoTime(); |
|
146 |
System.out.printf("NEW.getAliasTable()=%d, total=%d%n", |
|
147 |
(t1 - t0) / 1000, alias_new.length); |
|
148 |
Arrays.sort(alias_new); |
|
149 |
||
15658 | 150 |
t0 = System.currentTimeMillis(); |
151 |
for (String zid : zids_new) { |
|
152 |
TimeZone.getTimeZone(zid); |
|
153 |
} |
|
154 |
t1 = System.currentTimeMillis(); |
|
155 |
System.out.printf("NEW.TotalTZ()=%d (ms)%n", t1 - t0); |
|
156 |
||
157 |
if (!Arrays.equals(zids_old, zids_new)) { |
|
158 |
throw new RuntimeException(" FAILED: availableIds don't match"); |
|
159 |
} |
|
16852 | 160 |
|
161 |
if (!Arrays.equals(alias_old, alias_new)) { |
|
162 |
throw new RuntimeException(" FAILED: aliases don't match"); |
|
163 |
} |
|
164 |
||
15658 | 165 |
for (String zid : zids_new) { |
166 |
ZoneInfoOld zi = toZoneInfoOld(TimeZone.getTimeZone(zid)); |
|
167 |
ZoneInfoOld ziOLD = (ZoneInfoOld)ZoneInfoOld.getTimeZone(zid); |
|
168 |
if (! zi.equalsTo(ziOLD)) { |
|
169 |
System.out.println(zi.diffsTo(ziOLD)); |
|
170 |
throw new RuntimeException(" FAILED: " + zid); |
|
171 |
} |
|
172 |
} |
|
173 |
delete(fZidir); |
|
174 |
||
175 |
// test tzdb version |
|
176 |
if (!ver.equals(sun.util.calendar.ZoneInfoFile.getVersion())) { |
|
177 |
System.out.printf(" FAILED: ver=%s, expected=%s%n", |
|
178 |
sun.util.calendar.ZoneInfoFile.getVersion(), ver); |
|
179 |
throw new RuntimeException("Version test failed"); |
|
180 |
} |
|
15665
2223468cd706
8008161: Regression: j.u.TimeZone.getAvailableIDs(rawOffset) returns non-sorted list
sherman
parents:
15658
diff
changeset
|
181 |
|
2223468cd706
8008161: Regression: j.u.TimeZone.getAvailableIDs(rawOffset) returns non-sorted list
sherman
parents:
15658
diff
changeset
|
182 |
// test getAvailableIDs(raw); |
2223468cd706
8008161: Regression: j.u.TimeZone.getAvailableIDs(rawOffset) returns non-sorted list
sherman
parents:
15658
diff
changeset
|
183 |
zids_new = TimeZone.getAvailableIDs(-8 * 60 * 60 * 1000); |
2223468cd706
8008161: Regression: j.u.TimeZone.getAvailableIDs(rawOffset) returns non-sorted list
sherman
parents:
15658
diff
changeset
|
184 |
//Arrays.sort(zids_new); |
2223468cd706
8008161: Regression: j.u.TimeZone.getAvailableIDs(rawOffset) returns non-sorted list
sherman
parents:
15658
diff
changeset
|
185 |
zids_old = ZoneInfoOld.getAvailableIDs(-8 * 60 * 60 * 1000); |
2223468cd706
8008161: Regression: j.u.TimeZone.getAvailableIDs(rawOffset) returns non-sorted list
sherman
parents:
15658
diff
changeset
|
186 |
if (!Arrays.equals(zids_new, zids_old)) { |
2223468cd706
8008161: Regression: j.u.TimeZone.getAvailableIDs(rawOffset) returns non-sorted list
sherman
parents:
15658
diff
changeset
|
187 |
System.out.println("------------------------"); |
2223468cd706
8008161: Regression: j.u.TimeZone.getAvailableIDs(rawOffset) returns non-sorted list
sherman
parents:
15658
diff
changeset
|
188 |
System.out.println("NEW.getAvailableIDs(-8:00)"); |
2223468cd706
8008161: Regression: j.u.TimeZone.getAvailableIDs(rawOffset) returns non-sorted list
sherman
parents:
15658
diff
changeset
|
189 |
for (String zid : zids_new) { |
2223468cd706
8008161: Regression: j.u.TimeZone.getAvailableIDs(rawOffset) returns non-sorted list
sherman
parents:
15658
diff
changeset
|
190 |
System.out.println(zid); |
2223468cd706
8008161: Regression: j.u.TimeZone.getAvailableIDs(rawOffset) returns non-sorted list
sherman
parents:
15658
diff
changeset
|
191 |
} |
2223468cd706
8008161: Regression: j.u.TimeZone.getAvailableIDs(rawOffset) returns non-sorted list
sherman
parents:
15658
diff
changeset
|
192 |
System.out.println("------------------------"); |
2223468cd706
8008161: Regression: j.u.TimeZone.getAvailableIDs(rawOffset) returns non-sorted list
sherman
parents:
15658
diff
changeset
|
193 |
System.out.println("OLD.getAvailableIDs(-8:00)"); |
2223468cd706
8008161: Regression: j.u.TimeZone.getAvailableIDs(rawOffset) returns non-sorted list
sherman
parents:
15658
diff
changeset
|
194 |
for (String zid : zids_old) { |
2223468cd706
8008161: Regression: j.u.TimeZone.getAvailableIDs(rawOffset) returns non-sorted list
sherman
parents:
15658
diff
changeset
|
195 |
System.out.println(zid); |
2223468cd706
8008161: Regression: j.u.TimeZone.getAvailableIDs(rawOffset) returns non-sorted list
sherman
parents:
15658
diff
changeset
|
196 |
} |
2223468cd706
8008161: Regression: j.u.TimeZone.getAvailableIDs(rawOffset) returns non-sorted list
sherman
parents:
15658
diff
changeset
|
197 |
throw new RuntimeException(" FAILED: availableIds(offset) don't match"); |
2223468cd706
8008161: Regression: j.u.TimeZone.getAvailableIDs(rawOffset) returns non-sorted list
sherman
parents:
15658
diff
changeset
|
198 |
} |
15658 | 199 |
} |
200 |
||
201 |
private static void delete(File f) { |
|
202 |
if (f.isDirectory()) { |
|
203 |
for (File f0 : f.listFiles()) { |
|
204 |
delete(f0); |
|
205 |
} |
|
206 |
} |
|
207 |
f.delete(); |
|
208 |
} |
|
209 |
||
210 |
// to access sun.util.calendar.ZoneInfo's private fields |
|
211 |
static Class<?> ziClz; |
|
212 |
static Field rawOffset; |
|
213 |
static Field checksum; |
|
214 |
static Field dstSavings; |
|
215 |
static Field transitions; |
|
216 |
static Field offsets; |
|
217 |
static Field simpleTimeZoneParams; |
|
218 |
static Field willGMTOffsetChange; |
|
219 |
static { |
|
220 |
try { |
|
221 |
ziClz = Class.forName("sun.util.calendar.ZoneInfo"); |
|
222 |
rawOffset = ziClz.getDeclaredField("rawOffset"); |
|
223 |
checksum = ziClz.getDeclaredField("checksum"); |
|
224 |
dstSavings = ziClz.getDeclaredField("dstSavings"); |
|
225 |
transitions = ziClz.getDeclaredField("transitions"); |
|
226 |
offsets = ziClz.getDeclaredField("offsets"); |
|
227 |
simpleTimeZoneParams = ziClz.getDeclaredField("simpleTimeZoneParams"); |
|
228 |
willGMTOffsetChange = ziClz.getDeclaredField("willGMTOffsetChange"); |
|
229 |
rawOffset.setAccessible(true); |
|
230 |
checksum.setAccessible(true); |
|
231 |
dstSavings.setAccessible(true); |
|
232 |
transitions.setAccessible(true); |
|
233 |
offsets.setAccessible(true); |
|
234 |
simpleTimeZoneParams.setAccessible(true); |
|
235 |
willGMTOffsetChange.setAccessible(true); |
|
236 |
} catch (Exception x) { |
|
237 |
throw new RuntimeException(x); |
|
238 |
} |
|
239 |
} |
|
240 |
||
241 |
private static ZoneInfoOld toZoneInfoOld(TimeZone tz) throws Exception { |
|
242 |
return new ZoneInfoOld(tz.getID(), |
|
243 |
rawOffset.getInt(tz), |
|
244 |
dstSavings.getInt(tz), |
|
245 |
checksum.getInt(tz), |
|
246 |
(long[])transitions.get(tz), |
|
247 |
(int[])offsets.get(tz), |
|
248 |
(int[])simpleTimeZoneParams.get(tz), |
|
249 |
willGMTOffsetChange.getBoolean(tz)); |
|
250 |
} |
|
251 |
||
252 |
||
253 |
} |