1 /*
   2  * Copyright (c) 2008, 2018, 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  * @test
  26  * @bug 6466476
  27  * @summary Compatibility test for the old JDK ID mapping and Olson IDs
  28  * @comment Expecting the new (Olson compatible) mapping (default)
  29  * @run main/othervm -Dsun.timezone.ids.oldmapping=null OldIDMappingTest -new
  30  * @run main/othervm -Dsun.timezone.ids.oldmapping="" OldIDMappingTest -new
  31  * @run main/othervm -Dsun.timezone.ids.oldmapping=no OldIDMappingTest -new
  32  * @run main/othervm -Dsun.timezone.ids.oldmapping=No OldIDMappingTest -new
  33  * @run main/othervm -Dsun.timezone.ids.oldmapping=NO OldIDMappingTest -new
  34  * @run main/othervm -Dsun.timezone.ids.oldmapping=false OldIDMappingTest -new
  35  * @run main/othervm -Dsun.timezone.ids.oldmapping=False OldIDMappingTest -new
  36  * @run main/othervm -Dsun.timezone.ids.oldmapping=FALSE OldIDMappingTest -new
  37  * @run main/othervm -Dsun.timezone.ids.oldmapping=Hello OldIDMappingTest -new
  38  * @comment Expecting the old mapping
  39  * @run main/othervm -Dsun.timezone.ids.oldmapping=true OldIDMappingTest -old
  40  * @run main/othervm -Dsun.timezone.ids.oldmapping=True OldIDMappingTest -old
  41  * @run main/othervm -Dsun.timezone.ids.oldmapping=TRUE OldIDMappingTest -old
  42  * @run main/othervm -Dsun.timezone.ids.oldmapping=yes OldIDMappingTest -old
  43  * @run main/othervm -Dsun.timezone.ids.oldmapping=Yes OldIDMappingTest -old
  44  * @run main/othervm -Dsun.timezone.ids.oldmapping=YES OldIDMappingTest -old
  45  */
  46 
  47 import java.util.HashMap;
  48 import java.util.Map;
  49 import java.util.TimeZone;
  50 
  51 public class OldIDMappingTest {
  52     private static final String MAPPING_PROPERTY_NAME = "sun.timezone.ids.oldmapping";
  53     private static final Map<String, String> newmap = new HashMap<String, String>();
  54     static {
  55         // Add known new mappings
  56         newmap.put("EST", "EST");
  57         newmap.put("MST", "MST");
  58         newmap.put("HST", "HST");
  59     }
  60 
  61     public static void main(String[] args) {
  62         boolean useOldMapping = true;
  63         String arg = args[0];
  64         if (arg.equals("-new")) {
  65             useOldMapping = false;
  66         } else if (arg.equals("-old")) {
  67             useOldMapping = true;
  68         } else {
  69             throw new RuntimeException("-old or -new must be specified; got " + arg);
  70         }
  71 
  72         Map<String, String> oldmap = TzIDOldMapping.MAP;
  73         String prop = System.getProperty(MAPPING_PROPERTY_NAME);
  74         System.out.println(MAPPING_PROPERTY_NAME + "=" + prop);
  75 
  76         // Try the test multiple times with modifying TimeZones to
  77         // make sure TimeZone instances for the old mapping are
  78         // properly copied (defensive copy).
  79         for (int count = 0; count < 3; count++) {
  80             for (String id : oldmap.keySet()) {
  81                 TimeZone tzAlias = TimeZone.getTimeZone(id);
  82                 TimeZone tz = TimeZone.getTimeZone(oldmap.get(id));
  83                 if (useOldMapping) {
  84                     if (!tzAlias.hasSameRules(tz)) {
  85                         throw new RuntimeException("OLDMAP: " + MAPPING_PROPERTY_NAME
  86                                 + "=" + prop + ": " + id
  87                                 + " isn't an alias of " + oldmap.get(id));
  88                     }
  89                     if (count == 0) {
  90                         System.out.println("    " + id + " => " + oldmap.get(id));
  91                     }
  92                     tzAlias.setRawOffset(tzAlias.getRawOffset() * count);
  93                 } else {
  94                     if (!newmap.containsKey(id)) {
  95                         // ignore ids not contained in the new map
  96                         if (count == 0) {
  97                             System.out.println("    " + id + " => " + oldmap.get(id));
  98                         }
  99                         tzAlias.setRawOffset(tzAlias.getRawOffset() * count);
 100                         continue;
 101                     }
 102                     if (tzAlias.hasSameRules(tz)) {
 103                         throw new RuntimeException("NEWMAP: " + MAPPING_PROPERTY_NAME
 104                                 + "=" + prop + ": " + id
 105                                 + " is an alias of " + oldmap.get(id));
 106                     }
 107                     tz = TimeZone.getTimeZone(newmap.get(id));
 108                     if (!tzAlias.hasSameRules(tz)) {
 109                         throw new RuntimeException("NEWMAP: " + MAPPING_PROPERTY_NAME
 110                                 + "=" + prop + ": " + id
 111                                 + " isn't an alias of " + newmap.get(id));
 112                     }
 113                     if (count == 0) {
 114                         System.out.println("    " + id + " => " + newmap.get(id));
 115                     }
 116                     tzAlias.setRawOffset(tzAlias.getRawOffset() * count);
 117                 }
 118             }
 119         }
 120     }
 121 }