1 /*
   2  * Copyright (c) 2002, 2016, 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 4509255 5055567 6176318 7090844
  27  * @summary Tests consistencies of time zone IDs.
  28  */
  29 
  30 import java.util.Arrays;
  31 import java.util.HashSet;
  32 import java.util.Map;
  33 import java.util.Set;
  34 import java.util.TimeZone;
  35 import java.util.TreeMap;
  36 
  37 public class IDTest {
  38     public static void main(String[] args) {
  39         Set<String> ids = new HashSet<>();
  40         Map<Integer, Set<String>> tree = new TreeMap<>();
  41 
  42         String[] tzs = TimeZone.getAvailableIDs();
  43         String[] tzs2 = TimeZone.getAvailableIDs();
  44         if (tzs.length != tzs2.length) {
  45             throw new RuntimeException("tzs.length(" + tzs.length
  46                                        + ") != tzs2.length(" + tzs2.length + ")");
  47         }
  48         for (int i = 0; i < tzs.length; i++) {
  49             if (tzs[i] != tzs2[i]) {
  50                 throw new RuntimeException(i + ": " + tzs[i] + " != " + tzs2[i]);
  51             }
  52         }
  53 
  54         System.out.println("Total: " + tzs.length + " time zone IDs");
  55         for (String id : tzs) {
  56             ids.add(id);
  57             TimeZone tz = TimeZone.getTimeZone(id);
  58             Integer offset = tz.getRawOffset();
  59             Set<String> s = tree.get(offset);
  60             if (s == null) {
  61                 s = new HashSet<>();
  62                 tree.put(offset, s);
  63             }
  64             s.add(id);
  65         }
  66 
  67         for (Integer key : tree.keySet()) {
  68             Set<String> s1 = tree.get(key);
  69 
  70             // Make sure no duplicates in the other sets
  71             for (Integer k : tree.keySet()) {
  72                 if (k.equals(key)) {
  73                     continue;
  74                 }
  75                 Set<String> s2 = new HashSet<>(tree.get(k));
  76                 s2.retainAll(s1);
  77                 if (!s2.isEmpty()) {
  78                     throw new RuntimeException("s1 included in the subset for " + (k.intValue()/60000) +
  79                                                " (" + s2 + " shouldn't be in s1)");
  80                 }
  81             }
  82 
  83             // Check the getAvailableIDs(int) call to return the same
  84             // set of IDs
  85             int offset = key.intValue();
  86             tzs = TimeZone.getAvailableIDs(offset);
  87             tzs2 = TimeZone.getAvailableIDs(offset);
  88             if (!Arrays.equals(tzs, tzs2)) {
  89                 throw new RuntimeException("inconsistent tzs from getAvailableIDs("+offset+")");
  90             }
  91             Set<String> s2 = new HashSet<>();
  92             s2.addAll(Arrays.asList(tzs));
  93             if (!s1.equals(s2)) {
  94                 throw new RuntimeException("s1 != s2 for " + offset/60000 +
  95                                            " (diff=" + getDiff(s1, s2) + ")");
  96             }
  97             if (!ids.containsAll(s2)) {
  98                 throw new RuntimeException("s2 isn't a subset of ids (" + getDiff(s2, ids) +
  99                                            " not in ids)");
 100             }
 101         }
 102 
 103         for (Integer key : tree.keySet()) {
 104             Set<String> s1 = tree.get(key);
 105             ids.removeAll(s1);
 106         }
 107         if (!ids.isEmpty()) {
 108             throw new RuntimeException("ids didn't become empty. (" + ids + ")");
 109         }
 110     }
 111 
 112     private static String getDiff(Set<String> set1, Set<String> set2) {
 113         Set<String> s1 = new HashSet<>(set1);
 114         s1.removeAll(set2);
 115 
 116         Set<String> s2 = new HashSet<>(set2);
 117         s2.removeAll(set1);
 118         s2.addAll(s1);
 119         return s2.toString();
 120     }
 121 }