1 /*
  2  * Copyright (c) 2005, 2023, 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 6329116 6756569 6757131 6758988 6764308 6796489 6834474 6609737 6507067
 27  *      7039469 7090843 7103108 7103405 7158483 8008577 8059206 8064560 8072042
 28  *      8077685 8151876 8166875 8169191 8170316 8176044
 29  * @summary Make sure that timezone short display names are idenical to Olson's data.
 30  * @run junit/othervm -Djava.locale.providers=COMPAT,SPI Bug6329116
 31  */
 32 
 33 import java.io.*;
 34 import java.text.*;
 35 import java.util.*;
 36 
 37 import org.junit.jupiter.api.Test;
 38 
 39 import static org.junit.jupiter.api.Assertions.fail;
 40 
 41 public class Bug6329116 {
 42 
 43     static Locale[] locales = Locale.getAvailableLocales();
 44     static String[] timezones = TimeZone.getAvailableIDs();
 45 
 46     @Test
 47     public void bug6329116() throws IOException {
 48         boolean err = false;
 49 
 50         HashMap<String, String> aliasTable = new HashMap<>();
 51         HashSet<String> timezoneTable = new HashSet<>();
 52         for (String t : timezones) {
 53             timezoneTable.add(t);
 54         }
 55 
 56         String line, key, value;
 57         StringTokenizer st;
 58 
 59         try (TextFileReader in = new TextFileReader("aliases.txt")) {
 60             while ((line = in.readLine()) != null) {
 61                 st = new StringTokenizer(line);
 62                 st.nextToken();
 63                 key = st.nextToken();
 64                 value = st.nextToken();
 65 
 66                 if (!value.equals("ROC")) {
 67                     if (aliasTable.containsKey(key)) {
 68                         aliasTable.put(key, aliasTable.get(key) + " " + value);
 69                     } else {
 70                         aliasTable.put(key, value);
 71                     }
 72                 }
 73             }
 74         }
 75 
 76         try (TextFileReader in = new TextFileReader("displaynames.txt")) {
 77             String timezoneID, expected, expected_DST, got;
 78             String[] aliases, tzs;
 79             TimeZone tz;
 80             while ((line = in.readLine()) != null) {
 81                 st = new StringTokenizer(line);
 82                 timezoneID = st.nextToken();
 83                 expected = st.nextToken();
 84                 if (st.hasMoreTokens()) {
 85                     expected_DST = st.nextToken();
 86                 } else {
 87                     expected_DST = null;
 88                 }
 89 
 90                 if (aliasTable.containsKey(timezoneID)) {
 91                     aliases = aliasTable.get(timezoneID).split(" ");
 92                     tzs = new String[1 + aliases.length];
 93                     System.arraycopy(aliases, 0, tzs, 1, aliases.length);
 94                     aliasTable.remove(timezoneID);
 95                 } else {
 96                     tzs = new String[1];
 97                 }
 98                 tzs[0] = timezoneID;
 99 
100                 for (int j = 0; j < tzs.length; j++) {
101                     tz = TimeZone.getTimeZone(tzs[j]);
102 
103                     if (!tzs[j].equals(tz.getID())) {
104                         System.err.println(tzs[j] + " may not be a valid Timezone ID and \"" + tz.getID() + "\" was returned. Please check it.");
105                         err = true;
106                     }
107 
108                     timezoneTable.remove(tzs[j]);
109 
110                     for (int i = 0; i < locales.length; i++) {
111                         got = tz.getDisplayName(false, TimeZone.SHORT, locales[i]);
112                         if (!expected.equals(got) &&
113                             !expected.startsWith(got + "/") &&
114                             !expected.endsWith("/" + got)) {
115                             if (useLocalzedShortDisplayName(tz, locales[i], got, false)) {
116 /*
117                                 System.out.println(tzs[j] +
118                                                    ((j > 0) ? "(Alias of \"" + tzs[0] + "\")" : "") +
119                                                    " seems to use a localized short display name" +
120                                                    ": original: " + expected +
121                                                    ": got: " + got + " for non-DST in " +
122                                                    locales[i] + " locale.");
123 */
124                             } else {
125                                 System.err.println(tzs[j] +
126                                                    ((j > 0) ? "(Alias of \"" + tzs[0] + "\")" : "") +
127                                                    ": expected: " + expected +
128                                                    ": got: " + got + " for non-DST in " +
129                                                    locales[i] + " locale.");
130                                 err = true;
131                             }
132                         }
133 
134                         got = tz.getDisplayName(true, TimeZone.SHORT, locales[i]);
135                         if (expected_DST != null) {
136                             if (!expected_DST.equals(got) &&
137                                 !expected_DST.startsWith(got + "/") &&
138                                 !expected_DST.endsWith("/" + got)) {
139                                 if (tzs[j].equals("Europe/London") &&
140                                     locales[i].equals(new Locale("en", "IE"))) {
141                                     continue;
142                                 } else if (useLocalzedShortDisplayName(tz, locales[i], got, true)) {
143 /*
144                                 System.out.println(tzs[j] +
145                                     ((j > 0) ? "(Alias of \"" + tzs[0] + "\")" : "") +
146                                     " seems to use a localized short display name" +
147                                     ": original: " + expected_DST +
148                                     ": got: " + got + " for DST in " +
149                                     locales[i] + " locale.");
150 */
151                                     continue;
152                                 }
153                                 System.err.println(tzs[j] +
154                                                    ((j > 0) ? "(Alias of \"" + tzs[0] + "\")" : "") +
155                                                    ": expected: " + expected_DST +
156                                                    ": got: " + got + " for DST in " +
157                                                    locales[i] + " locale.");
158                                 err = true;
159                             }
160                         } else {
161                             // Some timezones don't have DST display names in Olson's data,
162                             // and we created them ourselves based on non-DST display names
163                             // to prepare potential use in the future.
164                             // Because there's no expected name, we don't judge if these
165                             // DST display names are correct but just compare them with
166                             // non-DST diplay names for checking with our eyes .
167                             if (!expected.equals(got) &&
168                                 !expected.startsWith(got + "/") &&
169                                 !expected.endsWith("/" + got)) {
170 /*
171                                 System.out.println("## " + tzs[j] +
172                                                    ((j > 0) ? "(Alias of \"" + tzs[0] + "\")" : "") +
173                                                    ": expected: " + expected +
174                                                    ": got: " + got + " for DST in " +
175                                                    locales[i] + " locale.");
176 */
177                             }
178                         }
179                     }
180                 }
181             }
182         }
183 
184         if (!timezoneTable.isEmpty()) {
185             System.out.println("# Timezone(s) valid in JRE but untested in this test program:");
186             Iterator<String> it = timezoneTable.iterator();
187             while (it.hasNext()) {
188                 System.out.println(it.next());
189             }
190             System.out.println();
191         }
192 
193         if (!aliasTable.isEmpty()) {
194             System.out.println("# Timezone(s) exists in Olson's data as Link but unused in JRE:");
195             for (Map.Entry<String, String> entry : aliasTable.entrySet()) {
196                 System.out.println(entry);
197             }
198         }
199 
200         if (err) {
201             fail("At least one timezone display name is incorrect.");
202         }
203     }
204 
205     static boolean useLocalzedShortDisplayName(TimeZone tz,
206                                                Locale locale,
207                                                String got,
208                                                boolean inDST) {
209         if (locale.getLanguage().equals("de")) {
210             String name = tz.getDisplayName(inDST, TimeZone.LONG, locale);
211             if (inDST) {
212                 if (("Mitteleurop\u00e4ische Sommerzeit".equals(name) && "MESZ".equals(got)) ||
213                     ("Osteurop\u00e4ische Sommerzeit".equals(name) && "OESZ".equals(got)) ||
214                     ("Westeurop\u00e4ische Sommerzeit".equals(name) && "WESZ".equals(got))) {
215                     return true;
216                 }
217             } else {
218                 if (("Mitteleurop\u00e4ische Zeit".equals(name) && "MEZ".equals(got)) ||
219                     ("Osteurop\u00e4ische Zeit".equals(name) && "OEZ".equals(got)) ||
220                     ("Westeurop\u00e4ische Zeit".equals(name) && "WEZ".equals(got))) {
221                     return true;
222                 }
223             }
224         } else if (locale.getLanguage().equals("zh") &&
225             (locale.getCountry().equals("TW") || locale.getCountry().equals("HK"))) {
226             String name = tz.getDisplayName(inDST, TimeZone.LONG, locale);
227             if (inDST) {
228                 if (("\u53f0\u7063\u590f\u4ee4\u6642\u9593".equals(name) && "TDT".equals(got))) {
229                     return true;
230                 }
231             } else {
232                 if (("\u53f0\u7063\u6a19\u6e96\u6642\u9593".equals(name) && "TST".equals(got))) {
233                     return true;
234                 }
235             }
236         }
237         // If we get a TimeZone with GMT+hh:mm format, we can ignore the offset value
238         if (tz.getDisplayName(Locale.ENGLISH).startsWith("GMT+") || tz.getDisplayName(Locale.ENGLISH).startsWith("GMT-")) {
239             return tz.getDisplayName().substring(0, 3).equals(got.substring(0, 3));
240         }
241 
242         return false;
243     }
244 
245 }