1 /*
   2  * Copyright (c) 2008, 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 /*
  27  * See OldMappingTest.sh
  28  */
  29 
  30 import java.lang.reflect.*;
  31 import java.util.*;
  32 
  33 public class OldIDMappingTest {
  34     private static final String MAPPING_PROPERTY_NAME = "sun.timezone.ids.oldmapping";
  35     private static final Map<String, String> newmap = new HashMap<String, String>();
  36     static {
  37         // Add known new mappings
  38         newmap.put("EST", "EST");
  39         newmap.put("MST", "MST");
  40         newmap.put("HST", "HST");
  41     }
  42 
  43     public static void main(String[] args) {
  44         boolean useOldMapping = true;
  45         String arg = args[0];
  46         if (arg.equals("-new")) {
  47             useOldMapping = false;
  48         } else if (arg.equals("-old")) {
  49             useOldMapping = true;
  50         } else {
  51             throw new RuntimeException("-old or -new must be specified; got " + arg);
  52         }
  53 
  54         // Get a Field for TzIDOldMapping in sun.util.calendar.
  55         Map<String, String> oldmap = null;
  56         try {
  57             Class<?> oldmapClass = Class.forName("sun.util.calendar.TzIDOldMapping");
  58             Field map = oldmapClass.getDeclaredField("MAP");
  59             map.setAccessible(true);
  60             oldmap = (Map<String, String>) map.get(null);
  61         } catch (Exception e) {
  62             throw new RuntimeException("can't get TzIDOldMapping.MAP", e);
  63         }
  64 
  65         String prop = System.getProperty(MAPPING_PROPERTY_NAME);
  66         System.out.println(MAPPING_PROPERTY_NAME + "=" + prop);
  67 
  68         // Try the test multiple times with modifying TimeZones to
  69         // make sure TimeZone instances for the old mapping are
  70         // properly copied (defensive copy).
  71         for (int count = 0; count < 3; count++) {
  72             for (String id : oldmap.keySet()) {
  73                 TimeZone tzAlias = TimeZone.getTimeZone(id);
  74                 TimeZone tz = TimeZone.getTimeZone(oldmap.get(id));
  75                 if (useOldMapping) {
  76                     if (!tzAlias.hasSameRules(tz)) {
  77                         throw new RuntimeException("OLDMAP: " + MAPPING_PROPERTY_NAME + "=" + prop + ": "
  78                                                    + id + " isn't an alias of " + oldmap.get(id));
  79                     }
  80                     if (count == 0) {
  81                         System.out.println("    " + id + " => " + oldmap.get(id));
  82                     }
  83                     tzAlias.setRawOffset(tzAlias.getRawOffset() * count);
  84                 } else {
  85                     if (!newmap.containsKey(id)) {
  86                         // ignore ids not contained in the new map
  87                         if (count == 0) {
  88                             System.out.println("    " + id + " => " + oldmap.get(id));
  89                         }
  90                         tzAlias.setRawOffset(tzAlias.getRawOffset() * count);
  91                         continue;
  92                     }
  93                     if (tzAlias.hasSameRules(tz)) {
  94                         throw new RuntimeException("NEWMAP: " + MAPPING_PROPERTY_NAME + "=" + prop + ": "
  95                                                    + id + " is an alias of " + oldmap.get(id));
  96                     }
  97                     tz = TimeZone.getTimeZone(newmap.get(id));
  98                     if (!tzAlias.hasSameRules(tz)) {
  99                         throw new RuntimeException("NEWMAP: " + MAPPING_PROPERTY_NAME + "=" + prop + ": "
 100                                                    + id + " isn't an alias of " + newmap.get(id));
 101                     }
 102                     if (count == 0) {
 103                         System.out.println("    " + id + " => " + newmap.get(id));
 104                     }
 105                     tzAlias.setRawOffset(tzAlias.getRawOffset() * count);
 106                 }
 107             }
 108         }
 109     }
 110 }