Path: blob/master/test/jdk/sun/util/calendar/zi/TestZoneInfo310.java
41153 views
/*1* Copyright (c) 2012, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 8007572 8008161 8157792 8212970 822456026* @summary Test whether the TimeZone generated from JSR310 tzdb is the same27* as the one from the tz data from javazic28* @modules java.base/sun.util.calendar:+open29* @build BackEnd Checksum DayOfWeek Gen GenDoc Main Mappings Month30* Rule RuleDay RuleRec Simple TestZoneInfo310 Time Timezone31* TzIDOldMapping Zone ZoneInfoFile ZoneInfoOld ZoneRec Zoneinfo32* @run main TestZoneInfo31033*/3435import java.io.File;36import java.lang.reflect.*;37import java.nio.file.*;38import java.util.*;39import java.util.regex.*;40import java.time.zone.*;41import java.time.ZoneId;4243public class TestZoneInfo310 {4445public static void main(String[] args) throws Throwable {4647String TESTDIR = System.getProperty("test.dir", ".");48Path tzdir = Paths.get(System.getProperty("test.root"),49"..", "..", "make", "data", "tzdata");50String tzfiles = "africa antarctica asia australasia europe northamerica southamerica backward etcetera gmt";51Path jdk_tzdir = Paths.get(System.getProperty("test.src"), "tzdata_jdk");52String jdk_tzfiles = "jdk11_backward";53String zidir = TESTDIR + File.separator + "zi";54File fZidir = new File(zidir);55if (!fZidir.exists()) {56fZidir.mkdirs();57}58Matcher m = Pattern.compile("tzdata(?<ver>[0-9]{4}[A-z])")59.matcher(new String(Files.readAllBytes(tzdir.resolve("VERSION")), "ascii"));60String ver = m.find() ? m.group("ver") : "NULL";6162ArrayList<String> alist = new ArrayList<>();63alist.add("-V");64alist.add(ver);65alist.add("-d");66alist.add(zidir);67for (String f : tzfiles.split(" ")) {68alist.add(tzdir.resolve(f).toString());69}70for (String f : jdk_tzfiles.split(" ")) {71alist.add(jdk_tzdir.resolve(f).toString());72}73System.out.println("Compiling tz files!");74Main.main(alist.toArray(new String[alist.size()]));7576//////////////////////////////////77System.out.println("testing!");78ZoneInfoFile.ziDir = zidir;79long t0, t1;8081t0 = System.nanoTime();82ZoneInfoOld.getTimeZone("America/Los_Angeles");83t1 = System.nanoTime();84System.out.printf("OLD.getZoneInfoOld()[1]=%d%n", (t1 - t0) / 1000);8586t0 = System.nanoTime();87ZoneInfoOld.getTimeZone("America/New_York");88t1 = System.nanoTime();89System.out.printf("OLD.getZoneInfoOld()[2]=%d%n", (t1 - t0) / 1000);9091t0 = System.nanoTime();92ZoneInfoOld.getTimeZone("America/Denver");93t1 = System.nanoTime();94System.out.printf("OLD.getZoneInfoOld()[3]=%d%n", (t1 - t0) / 1000);9596t0 = System.nanoTime();97String[] zids_old = ZoneInfoOld.getAvailableIDs();98t1 = System.nanoTime();99System.out.printf("OLD.getAvailableIDs()=%d, total=%d%n",100(t1 - t0) / 1000, zids_old.length);101Arrays.sort(zids_old);102103t0 = System.nanoTime();104String[] alias_old = ZoneInfoOld.getAliasTable()105.keySet().toArray(new String[0]);106t1 = System.nanoTime();107System.out.printf("OLD.getAliasTable()=%d, total=%d%n",108(t1 - t0) / 1000, alias_old.length);109Arrays.sort(alias_old);110111t0 = System.currentTimeMillis();112for (String zid : zids_old) {113ZoneInfoOld.getTimeZone(zid);114}115t1 = System.currentTimeMillis();116System.out.printf("OLD.TotalTZ()=%d (ms)%n", t1 - t0);117118/*119t0 = System.nanoTime();120ZoneId.of("America/Los_Angeles").getRules();121t1 = System.nanoTime();122System.out.printf("NEW.ZoneId.of()[1]=%d%n", (t1 - t0) / 1000);123*/124t0 = System.nanoTime();125TimeZone tz = TimeZone.getTimeZone("America/Los_Angeles");126t1 = System.nanoTime();127System.out.printf("NEW.getTimeZone()[1]=%d%n", (t1 - t0) / 1000);128129t0 = System.nanoTime();130tz = TimeZone.getTimeZone("America/New_York");131t1 = System.nanoTime();132System.out.printf("NEW.getTimeZone()[2]=%d%n", (t1 - t0) / 1000);133134t0 = System.nanoTime();135tz = TimeZone.getTimeZone("America/Denver");136t1 = System.nanoTime();137System.out.printf("NEW.getTimeZone()[3]=%d%n", (t1 - t0) / 1000);138139t0 = System.nanoTime();140String[] zids_new = TimeZone.getAvailableIDs();141t1 = System.nanoTime();142System.out.printf("NEW.getAvailableIDs()=%d, total=%d%n",143(t1 - t0) / 1000, zids_new.length);144Arrays.sort(zids_new);145146t0 = System.nanoTime();147String[] alias_new = sun.util.calendar.ZoneInfo.getAliasTable()148.keySet().toArray(new String[0]);149t1 = System.nanoTime();150System.out.printf("NEW.getAliasTable()=%d, total=%d%n",151(t1 - t0) / 1000, alias_new.length);152Arrays.sort(alias_new);153154t0 = System.currentTimeMillis();155for (String zid : zids_new) {156TimeZone.getTimeZone(zid);157}158t1 = System.currentTimeMillis();159System.out.printf("NEW.TotalTZ()=%d (ms)%n", t1 - t0);160161if (!Arrays.equals(zids_old, zids_new)) {162throw new RuntimeException(" FAILED: availableIds don't match");163}164165if (!Arrays.equals(alias_old, alias_new)) {166throw new RuntimeException(" FAILED: aliases don't match");167}168169for (String zid : zids_new) {170ZoneInfoOld zi = toZoneInfoOld(TimeZone.getTimeZone(zid));171ZoneInfoOld ziOLD = (ZoneInfoOld)ZoneInfoOld.getTimeZone(zid);172/*173* Temporary ignoring the failing TimeZones which are having zone174* rules defined till year 2037 and/or above and have negative DST175* save time in IANA tzdata. This bug is tracked via JDK-8223388.176*177* These are the zones/rules that employ negative DST in vanguard178* format (as of 2019a):179*180* - Rule "Eire"181* - Rule "Morocco"182* - Rule "Namibia"183* - Zone "Europe/Prague"184*185* Tehran/Iran rule has rules beyond 2037, in which javazic assumes186* to be the last year. Thus javazic's rule is based on year 2037187* (Mar 20th/Sep 20th are the cutover dates), while the real rule188* has year 2087 where Mar 21st/Sep 21st are the cutover dates.189*/190if (zid.equals("Africa/Casablanca") || // uses "Morocco" rule191zid.equals("Africa/El_Aaiun") || // uses "Morocco" rule192zid.equals("Africa/Windhoek") || // uses "Namibia" rule193zid.equals("Eire") ||194zid.equals("Europe/Bratislava") || // link to "Europe/Prague"195zid.equals("Europe/Dublin") || // uses "Eire" rule196zid.equals("Europe/Prague") ||197zid.equals("Asia/Tehran") || // last rule mismatch198zid.equals("Iran")) { // last rule mismatch199continue;200}201if (! zi.equalsTo(ziOLD)) {202System.out.println(zi.diffsTo(ziOLD));203throw new RuntimeException(" FAILED: " + zid);204}205}206delete(fZidir);207208// test tzdb version209if (!ver.equals(sun.util.calendar.ZoneInfoFile.getVersion())) {210System.out.printf(" FAILED: ver=%s, expected=%s%n",211sun.util.calendar.ZoneInfoFile.getVersion(), ver);212throw new RuntimeException("Version test failed");213}214215// test getAvailableIDs(raw);216zids_new = TimeZone.getAvailableIDs(-8 * 60 * 60 * 1000);217Arrays.sort(zids_new);218zids_old = ZoneInfoOld.getAvailableIDs(-8 * 60 * 60 * 1000);219Arrays.sort(zids_old);220if (!Arrays.equals(zids_new, zids_old)) {221System.out.println("------------------------");222System.out.println("NEW.getAvailableIDs(-8:00)");223for (String zid : zids_new) {224System.out.println(zid);225}226System.out.println("------------------------");227System.out.println("OLD.getAvailableIDs(-8:00)");228for (String zid : zids_old) {229System.out.println(zid);230}231throw new RuntimeException(" FAILED: availableIds(offset) don't match");232}233}234235private static void delete(File f) {236if (f.isDirectory()) {237for (File f0 : f.listFiles()) {238delete(f0);239}240}241f.delete();242}243244// to access sun.util.calendar.ZoneInfo's private fields245static Class<?> ziClz;246static Field rawOffset;247static Field checksum;248static Field dstSavings;249static Field transitions;250static Field offsets;251static Field simpleTimeZoneParams;252static Field willGMTOffsetChange;253static {254try {255ziClz = Class.forName("sun.util.calendar.ZoneInfo");256rawOffset = ziClz.getDeclaredField("rawOffset");257checksum = ziClz.getDeclaredField("checksum");258dstSavings = ziClz.getDeclaredField("dstSavings");259transitions = ziClz.getDeclaredField("transitions");260offsets = ziClz.getDeclaredField("offsets");261simpleTimeZoneParams = ziClz.getDeclaredField("simpleTimeZoneParams");262willGMTOffsetChange = ziClz.getDeclaredField("willGMTOffsetChange");263rawOffset.setAccessible(true);264checksum.setAccessible(true);265dstSavings.setAccessible(true);266transitions.setAccessible(true);267offsets.setAccessible(true);268simpleTimeZoneParams.setAccessible(true);269willGMTOffsetChange.setAccessible(true);270} catch (Exception x) {271throw new RuntimeException(x);272}273}274275private static ZoneInfoOld toZoneInfoOld(TimeZone tz) throws Exception {276return new ZoneInfoOld(tz.getID(),277rawOffset.getInt(tz),278dstSavings.getInt(tz),279checksum.getInt(tz),280(long[])transitions.get(tz),281(int[])offsets.get(tz),282(int[])simpleTimeZoneParams.get(tz),283willGMTOffsetChange.getBoolean(tz));284}285286287}288289290