Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/util/calendar/zi/TestZoneInfo310.java
38855 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 822456026* @summary Test whether the TimeZone generated from JSR310 tzdb is the same27* as the one from the tz data from javazic28* @build BackEnd Checksum DayOfWeek Gen GenDoc Main Mappings Month29* Rule RuleDay RuleRec Simple TestZoneInfo310 Time Timezone30* TzIDOldMapping Zone ZoneInfoFile ZoneInfoOld ZoneRec Zoneinfo31* @run main TestZoneInfo31032*/3334import java.io.File;35import java.lang.reflect.*;36import java.nio.file.*;37import java.util.*;38import java.util.regex.*;39import java.time.zone.*;40import java.time.ZoneId;4142public class TestZoneInfo310 {4344public static void main(String[] args) throws Throwable {4546String TESTDIR = System.getProperty("test.dir", ".");47String SRCDIR = System.getProperty("test.src", ".");48String tzdir = SRCDIR + File.separator + "tzdata";49String tzfiles = "africa antarctica asia australasia europe northamerica southamerica backward etcetera";50String jdk_tzdir = SRCDIR + File.separator + "tzdata_jdk";51String jdk_tzfiles = "gmt jdk11_backward";52String zidir = TESTDIR + File.separator + "zi";53File fZidir = new File(zidir);54if (!fZidir.exists()) {55fZidir.mkdirs();56}57Matcher m = Pattern.compile("tzdata(?<ver>[0-9]{4}[A-z])")58.matcher(new String(Files.readAllBytes(Paths.get(tzdir, "VERSION")), "ascii"));59String ver = m.find() ? m.group("ver") : "NULL";6061ArrayList<String> alist = new ArrayList<>();62alist.add("-V");63alist.add(ver);64alist.add("-d");65alist.add(zidir);66for (String f : tzfiles.split(" ")) {67alist.add(tzdir + File.separator + f);68}69for (String f : jdk_tzfiles.split(" ")) {70alist.add(jdk_tzdir + File.separator + f);71}72System.out.println("Compiling tz files!");73Main.main(alist.toArray(new String[alist.size()]));7475//////////////////////////////////76System.out.println("testing!");77ZoneInfoFile.ziDir = zidir;78long t0, t1;7980t0 = System.nanoTime();81ZoneInfoOld.getTimeZone("America/Los_Angeles");82t1 = System.nanoTime();83System.out.printf("OLD.getZoneInfoOld()[1]=%d%n", (t1 - t0) / 1000);8485t0 = System.nanoTime();86ZoneInfoOld.getTimeZone("America/New_York");87t1 = System.nanoTime();88System.out.printf("OLD.getZoneInfoOld()[2]=%d%n", (t1 - t0) / 1000);8990t0 = System.nanoTime();91ZoneInfoOld.getTimeZone("America/Denver");92t1 = System.nanoTime();93System.out.printf("OLD.getZoneInfoOld()[3]=%d%n", (t1 - t0) / 1000);9495t0 = System.nanoTime();96String[] zids_old = ZoneInfoOld.getAvailableIDs();97t1 = System.nanoTime();98System.out.printf("OLD.getAvailableIDs()=%d, total=%d%n",99(t1 - t0) / 1000, zids_old.length);100Arrays.sort(zids_old);101102t0 = System.nanoTime();103String[] alias_old = ZoneInfoOld.getAliasTable()104.keySet().toArray(new String[0]);105t1 = System.nanoTime();106System.out.printf("OLD.getAliasTable()=%d, total=%d%n",107(t1 - t0) / 1000, alias_old.length);108Arrays.sort(alias_old);109110t0 = System.currentTimeMillis();111for (String zid : zids_old) {112ZoneInfoOld.getTimeZone(zid);113}114t1 = System.currentTimeMillis();115System.out.printf("OLD.TotalTZ()=%d (ms)%n", t1 - t0);116117/*118t0 = System.nanoTime();119ZoneId.of("America/Los_Angeles").getRules();120t1 = System.nanoTime();121System.out.printf("NEW.ZoneId.of()[1]=%d%n", (t1 - t0) / 1000);122*/123t0 = System.nanoTime();124TimeZone tz = TimeZone.getTimeZone("America/Los_Angeles");125t1 = System.nanoTime();126System.out.printf("NEW.getTimeZone()[1]=%d%n", (t1 - t0) / 1000);127128t0 = System.nanoTime();129tz = TimeZone.getTimeZone("America/New_York");130t1 = System.nanoTime();131System.out.printf("NEW.getTimeZone()[2]=%d%n", (t1 - t0) / 1000);132133t0 = System.nanoTime();134tz = TimeZone.getTimeZone("America/Denver");135t1 = System.nanoTime();136System.out.printf("NEW.getTimeZone()[3]=%d%n", (t1 - t0) / 1000);137138t0 = System.nanoTime();139String[] zids_new = TimeZone.getAvailableIDs();140t1 = System.nanoTime();141System.out.printf("NEW.getAvailableIDs()=%d, total=%d%n",142(t1 - t0) / 1000, zids_new.length);143Arrays.sort(zids_new);144145t0 = System.nanoTime();146String[] alias_new = sun.util.calendar.ZoneInfo.getAliasTable()147.keySet().toArray(new String[0]);148t1 = System.nanoTime();149System.out.printf("NEW.getAliasTable()=%d, total=%d%n",150(t1 - t0) / 1000, alias_new.length);151Arrays.sort(alias_new);152153t0 = System.currentTimeMillis();154for (String zid : zids_new) {155TimeZone.getTimeZone(zid);156}157t1 = System.currentTimeMillis();158System.out.printf("NEW.TotalTZ()=%d (ms)%n", t1 - t0);159160if (!Arrays.equals(zids_old, zids_new)) {161throw new RuntimeException(" FAILED: availableIds don't match");162}163164if (!Arrays.equals(alias_old, alias_new)) {165throw new RuntimeException(" FAILED: aliases don't match");166}167168for (String zid : zids_new) {169ZoneInfoOld zi = toZoneInfoOld(TimeZone.getTimeZone(zid));170ZoneInfoOld ziOLD = (ZoneInfoOld)ZoneInfoOld.getTimeZone(zid);171/*172* Temporary ignoring the failing TimeZones which are having zone173* rules defined till year 2037 and/or above and have negative DST174* save time in IANA tzdata. This bug is tracked via JDK-8223388.175*/176if (zid.equals("Africa/Casablanca") || zid.equals("Africa/El_Aaiun")177|| zid.equals("Asia/Tehran") || zid.equals("Iran")) {178continue;179}180if (! zi.equalsTo(ziOLD)) {181System.out.println(zi.diffsTo(ziOLD));182throw new RuntimeException(" FAILED: " + zid);183}184}185delete(fZidir);186187// test tzdb version188if (!ver.equals(sun.util.calendar.ZoneInfoFile.getVersion())) {189System.out.printf(" FAILED: ver=%s, expected=%s%n",190sun.util.calendar.ZoneInfoFile.getVersion(), ver);191throw new RuntimeException("Version test failed");192}193194// test getAvailableIDs(raw);195zids_new = TimeZone.getAvailableIDs(-8 * 60 * 60 * 1000);196Arrays.sort(zids_new);197zids_old = ZoneInfoOld.getAvailableIDs(-8 * 60 * 60 * 1000);198Arrays.sort(zids_old);199if (!Arrays.equals(zids_new, zids_old)) {200System.out.println("------------------------");201System.out.println("NEW.getAvailableIDs(-8:00)");202for (String zid : zids_new) {203System.out.println(zid);204}205System.out.println("------------------------");206System.out.println("OLD.getAvailableIDs(-8:00)");207for (String zid : zids_old) {208System.out.println(zid);209}210throw new RuntimeException(" FAILED: availableIds(offset) don't match");211}212}213214private static void delete(File f) {215if (f.isDirectory()) {216for (File f0 : f.listFiles()) {217delete(f0);218}219}220f.delete();221}222223// to access sun.util.calendar.ZoneInfo's private fields224static Class<?> ziClz;225static Field rawOffset;226static Field checksum;227static Field dstSavings;228static Field transitions;229static Field offsets;230static Field simpleTimeZoneParams;231static Field willGMTOffsetChange;232static {233try {234ziClz = Class.forName("sun.util.calendar.ZoneInfo");235rawOffset = ziClz.getDeclaredField("rawOffset");236checksum = ziClz.getDeclaredField("checksum");237dstSavings = ziClz.getDeclaredField("dstSavings");238transitions = ziClz.getDeclaredField("transitions");239offsets = ziClz.getDeclaredField("offsets");240simpleTimeZoneParams = ziClz.getDeclaredField("simpleTimeZoneParams");241willGMTOffsetChange = ziClz.getDeclaredField("willGMTOffsetChange");242rawOffset.setAccessible(true);243checksum.setAccessible(true);244dstSavings.setAccessible(true);245transitions.setAccessible(true);246offsets.setAccessible(true);247simpleTimeZoneParams.setAccessible(true);248willGMTOffsetChange.setAccessible(true);249} catch (Exception x) {250throw new RuntimeException(x);251}252}253254private static ZoneInfoOld toZoneInfoOld(TimeZone tz) throws Exception {255return new ZoneInfoOld(tz.getID(),256rawOffset.getInt(tz),257dstSavings.getInt(tz),258checksum.getInt(tz),259(long[])transitions.get(tz),260(int[])offsets.get(tz),261(int[])simpleTimeZoneParams.get(tz),262willGMTOffsetChange.getBoolean(tz));263}264265266}267268269