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.
   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  * See OldMappingTest.sh
  26  */
  27 
  28 import java.lang.reflect.*;
  29 import java.util.*;
  30 
  31 public class OldIDMappingTest {
  32     private static final String MAPPING_PROPERTY_NAME = "sun.timezone.ids.oldmapping";
  33     private static final Map<String, String> newmap = new HashMap<String, String>();
  34     static {
  35         // Add known new mappings
  36         newmap.put("EST", "EST");
  37         newmap.put("MST", "MST");
  38         newmap.put("HST", "HST");
  39     }
  40 
  41     public static void main(String[] args) {
  42         boolean useOldMapping = true;
  43         String arg = args[0];
  44         if (arg.equals("-new")) {
  45             useOldMapping = false;
  46         } else if (arg.equals("-old")) {
  47             useOldMapping = true;
  48         } else {
  49             throw new RuntimeException("-old or -new must be specified; got " + arg);
  50         }
  51 
  52         Map<String, String> oldmap = TzIDOldMapping.MAP;
  53         String prop = System.getProperty(MAPPING_PROPERTY_NAME);
  54         System.out.println(MAPPING_PROPERTY_NAME + "=" + prop);
  55 
  56         // Try the test multiple times with modifying TimeZones to
  57         // make sure TimeZone instances for the old mapping are
  58         // properly copied (defensive copy).
  59         for (int count = 0; count < 3; count++) {
  60             for (String id : oldmap.keySet()) {
  61                 TimeZone tzAlias = TimeZone.getTimeZone(id);
  62                 TimeZone tz = TimeZone.getTimeZone(oldmap.get(id));
  63                 if (useOldMapping) {
  64                     if (!tzAlias.hasSameRules(tz)) {
  65                         throw new RuntimeException("OLDMAP: " + MAPPING_PROPERTY_NAME + "=" + prop + ": "
  66                                                    + id + " isn't an alias of " + oldmap.get(id));
  67                     }
  68                     if (count == 0) {
  69                         System.out.println("    " + id + " => " + oldmap.get(id));
  70                     }
  71                     tzAlias.setRawOffset(tzAlias.getRawOffset() * count);
  72                 } else {
  73                     if (!newmap.containsKey(id)) {
  74                         // ignore ids not contained in the new map
  75                         if (count == 0) {
  76                             System.out.println("    " + id + " => " + oldmap.get(id));
  77                         }
  78                         tzAlias.setRawOffset(tzAlias.getRawOffset() * count);
  79                         continue;
  80                     }
  81                     if (tzAlias.hasSameRules(tz)) {
  82                         throw new RuntimeException("NEWMAP: " + MAPPING_PROPERTY_NAME + "=" + prop + ": "
  83                                                    + id + " is an alias of " + oldmap.get(id));
  84                     }
  85                     tz = TimeZone.getTimeZone(newmap.get(id));
  86                     if (!tzAlias.hasSameRules(tz)) {
  87                         throw new RuntimeException("NEWMAP: " + MAPPING_PROPERTY_NAME + "=" + prop + ": "
  88                                                    + id + " isn't an alias of " + newmap.get(id));
  89                     }
  90                     if (count == 0) {
  91                         System.out.println("    " + id + " => " + newmap.get(id));
  92                     }
  93                     tzAlias.setRawOffset(tzAlias.getRawOffset() * count);
  94                 }
  95             }
  96         }
  97     }
  98 }