Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/util/TimeZone/IDTest.java
38821 views
/*1* Copyright (c) 2002, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 4509255 5055567 6176318 709084426* @summary Tests consistencies of time zone IDs.27*/2829import java.util.Arrays;30import java.util.HashSet;31import java.util.Map;32import java.util.Set;33import java.util.TimeZone;34import java.util.TreeMap;3536public class IDTest {37public static void main(String[] args) {38Set<String> ids = new HashSet<>();39Map<Integer, Set<String>> tree = new TreeMap<>();4041String[] tzs = TimeZone.getAvailableIDs();42String[] tzs2 = TimeZone.getAvailableIDs();43if (tzs.length != tzs2.length) {44throw new RuntimeException("tzs.length(" + tzs.length45+ ") != tzs2.length(" + tzs2.length + ")");46}47for (int i = 0; i < tzs.length; i++) {48if (tzs[i] != tzs2[i]) {49throw new RuntimeException(i + ": " + tzs[i] + " != " + tzs2[i]);50}51}5253System.out.println("Total: " + tzs.length + " time zone IDs");54for (String id : tzs) {55ids.add(id);56TimeZone tz = TimeZone.getTimeZone(id);57Integer offset = tz.getRawOffset();58Set<String> s = tree.get(offset);59if (s == null) {60s = new HashSet<>();61tree.put(offset, s);62}63s.add(id);64}6566for (Integer key : tree.keySet()) {67Set<String> s1 = tree.get(key);6869// Make sure no duplicates in the other sets70for (Integer k : tree.keySet()) {71if (k.equals(key)) {72continue;73}74Set<String> s2 = new HashSet<>(tree.get(k));75s2.retainAll(s1);76if (!s2.isEmpty()) {77throw new RuntimeException("s1 included in the subset for " + (k.intValue()/60000) +78" (" + s2 + " shouldn't be in s1)");79}80}8182// Check the getAvailableIDs(int) call to return the same83// set of IDs84int offset = key.intValue();85tzs = TimeZone.getAvailableIDs(offset);86tzs2 = TimeZone.getAvailableIDs(offset);87if (!Arrays.equals(tzs, tzs2)) {88throw new RuntimeException("inconsistent tzs from getAvailableIDs("+offset+")");89}90Set<String> s2 = new HashSet<>();91s2.addAll(Arrays.asList(tzs));92if (!s1.equals(s2)) {93throw new RuntimeException("s1 != s2 for " + offset/60000 +94" (diff=" + getDiff(s1, s2) + ")");95}96if (!ids.containsAll(s2)) {97throw new RuntimeException("s2 isn't a subset of ids (" + getDiff(s2, ids) +98" not in ids)");99}100}101102for (Integer key : tree.keySet()) {103Set<String> s1 = tree.get(key);104ids.removeAll(s1);105}106if (!ids.isEmpty()) {107throw new RuntimeException("ids didn't become empty. (" + ids + ")");108}109}110111private static String getDiff(Set<String> set1, Set<String> set2) {112Set<String> s1 = new HashSet<>(set1);113s1.removeAll(set2);114115Set<String> s2 = new HashSet<>(set2);116s2.removeAll(set1);117s2.addAll(s1);118return s2.toString();119}120}121122123