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         Map<String, String> oldmap = TzIDOldMapping.MAP;
  55         String prop = System.getProperty(MAPPING_PROPERTY_NAME);
  56         System.out.println(MAPPING_PROPERTY_NAME + "=" + prop);
  57 
  58         // Try the test multiple times with modifying TimeZones to
  59         // make sure TimeZone instances for the old mapping are
  60         // properly copied (defensive copy).
  61         for (int count = 0; count < 3; count++) {
  62             for (String id : oldmap.keySet()) {
  63                 TimeZone tzAlias = TimeZone.getTimeZone(id);
  64                 TimeZone tz = TimeZone.getTimeZone(oldmap.get(id));
  65                 if (useOldMapping) {
  66                     if (!tzAlias.hasSameRules(tz)) {
  67                         throw new RuntimeException("OLDMAP: " + MAPPING_PROPERTY_NAME + "=" + prop + ": "
  68                                                    + id + " isn't an alias of " + oldmap.get(id));
  69                     }
  70                     if (count == 0) {
  71                         System.out.println("    " + id + " => " + oldmap.get(id));
  72                     }
  73                     tzAlias.setRawOffset(tzAlias.getRawOffset() * count);
  74                 } else {
  75                     if (!newmap.containsKey(id)) {
  76                         // ignore ids not contained in the new map
  77                         if (count == 0) {
  78                             System.out.println("    " + id + " => " + oldmap.get(id));
  79                         }
  80                         tzAlias.setRawOffset(tzAlias.getRawOffset() * count);
  81                         continue;
  82                     }
  83                     if (tzAlias.hasSameRules(tz)) {
  84                         throw new RuntimeException("NEWMAP: " + MAPPING_PROPERTY_NAME + "=" + prop + ": "
  85                                                    + id + " is an alias of " + oldmap.get(id));
  86                     }
  87                     tz = TimeZone.getTimeZone(newmap.get(id));
  88                     if (!tzAlias.hasSameRules(tz)) {
  89                         throw new RuntimeException("NEWMAP: " + MAPPING_PROPERTY_NAME + "=" + prop + ": "
  90                                                    + id + " isn't an alias of " + newmap.get(id));
  91                     }
  92                     if (count == 0) {
  93                         System.out.println("    " + id + " => " + newmap.get(id));
  94                     }
  95                     tzAlias.setRawOffset(tzAlias.getRawOffset() * count);
  96                 }
  97             }
  98         }
  99     }
 100 }