1 /*
  2  * Copyright (c) 2007, 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  * @summary test ISO639-2 language codes
 27  * @library /java/text/testlib
 28  * @compile -encoding ascii Bug4175998Test.java
 29  * @run main Bug4175998Test
 30  * @bug 4175998 8303917
 31  */
 32 
 33 /*
 34  *
 35  *
 36  * (C) Copyright IBM Corp. 1998 - All Rights Reserved
 37  *
 38  * The original version of this source code and documentation is
 39  * copyrighted and owned by IBM. These materials are provided
 40  * under terms of a License Agreement between IBM and Sun.
 41  * This technology is protected by multiple US and International
 42  * patents. This notice and attribution to IBM may not be removed.
 43  *
 44  */
 45 
 46 import java.io.BufferedReader;
 47 import java.io.FileReader;
 48 import java.util.*;
 49 
 50 /**
 51  *  Bug4175998Test verifies that the following bug has been fixed:
 52  *  Bug 4175998 - The java.util.Locale.getISO3Language() returns wrong result for a locale with
 53  *           language code 'ta'(Tamil).
 54  */
 55 public class Bug4175998Test extends IntlTest {
 56     public static void main(String[] args) throws Exception {
 57         new Bug4175998Test().run(args);
 58         //generateTables();    //uncomment this to regenerate data tables
 59     }
 60 
 61     public void testIt() throws Exception {
 62         boolean bad = false;
 63         for (final String[] localeCodes : CODES) {
 64             final Locale l = Locale.of(localeCodes[0]);
 65             final String iso3 = l.getISO3Language();
 66             if (!iso3.equals(localeCodes[1])) {
 67                 logln("Locale(" + l + ") returned bad ISO3 language code."
 68                         + "   Got '" + iso3 + "' instead of '" + localeCodes[1] + "'");
 69                 bad = true;
 70             }
 71         }
 72         if (bad) {
 73             errln("Bad ISO3 language codes detected.");
 74         }
 75     }
 76 
 77      private static final String[][] CODES = {
 78          {"aa","aar","aar"},
 79          {"ab","abk","abk"},
 80          {"af","afr","afr"},
 81          {"ak","aka","aka"},
 82          {"sq","sqi","alb"},
 83          {"am","amh","amh"},
 84          {"ar","ara","ara"},
 85          {"an","arg","arg"},
 86          {"hy","hye","arm"},
 87          {"as","asm","asm"},
 88          {"av","ava","ava"},
 89          {"ae","ave","ave"},
 90          {"ay","aym","aym"},
 91          {"az","aze","aze"},
 92          {"ba","bak","bak"},
 93          {"bm","bam","bam"},
 94          {"eu","eus","baq"},
 95          {"be","bel","bel"},
 96          {"bn","ben","ben"},
 97          {"bh","bih","bih"},
 98          {"bi","bis","bis"},
 99          {"bs","bos","bos"},
100          {"br","bre","bre"},
101          {"bg","bul","bul"},
102          {"my","mya","bur"},
103          {"ca","cat","cat"},
104          {"ch","cha","cha"},
105          {"ce","che","che"},
106          {"zh","zho","chi"},
107          {"cu","chu","chu"},
108          {"cv","chv","chv"},
109          {"kw","cor","cor"},
110          {"co","cos","cos"},
111          {"cr","cre","cre"},
112          {"cs","ces","cze"},
113          {"da","dan","dan"},
114          {"dv","div","div"},
115          {"nl","nld","dut"},
116          {"dz","dzo","dzo"},
117          {"en","eng","eng"},
118          {"eo","epo","epo"},
119          {"et","est","est"},
120          {"ee","ewe","ewe"},
121          {"fo","fao","fao"},
122          {"fj","fij","fij"},
123          {"fi","fin","fin"},
124          {"fr","fra","fre"},
125          {"fy","fry","fry"},
126          {"ff","ful","ful"},
127          {"ka","kat","geo"},
128          {"de","deu","ger"},
129          {"gd","gla","gla"},
130          {"ga","gle","gle"},
131          {"gl","glg","glg"},
132          {"gv","glv","glv"},
133          {"el","ell","gre"},
134          {"gn","grn","grn"},
135          {"gu","guj","guj"},
136          {"ht","hat","hat"},
137          {"ha","hau","hau"},
138          {"he","heb","heb"},
139          {"hz","her","her"},
140          {"hi","hin","hin"},
141          {"ho","hmo","hmo"},
142          {"hr","hrv","hrv"},
143          {"hu","hun","hun"},
144          {"ig","ibo","ibo"},
145          {"is","isl","ice"},
146          {"io","ido","ido"},
147          {"ii","iii","iii"},
148          {"iu","iku","iku"},
149          {"ie","ile","ile"},
150          {"ia","ina","ina"},
151          {"id","ind","ind"},
152          {"ik","ipk","ipk"},
153          {"it","ita","ita"},
154          {"jv","jav","jav"},
155          {"ja","jpn","jpn"},
156          {"kl","kal","kal"},
157          {"kn","kan","kan"},
158          {"ks","kas","kas"},
159          {"kr","kau","kau"},
160          {"kk","kaz","kaz"},
161          {"km","khm","khm"},
162          {"ki","kik","kik"},
163          {"rw","kin","kin"},
164          {"ky","kir","kir"},
165          {"kv","kom","kom"},
166          {"kg","kon","kon"},
167          {"ko","kor","kor"},
168          {"kj","kua","kua"},
169          {"ku","kur","kur"},
170          {"lo","lao","lao"},
171          {"la","lat","lat"},
172          {"lv","lav","lav"},
173          {"li","lim","lim"},
174          {"ln","lin","lin"},
175          {"lt","lit","lit"},
176          {"lb","ltz","ltz"},
177          {"lu","lub","lub"},
178          {"lg","lug","lug"},
179          {"mk","mkd","mac"},
180          {"mh","mah","mah"},
181          {"ml","mal","mal"},
182          {"mi","mri","mao"},
183          {"mr","mar","mar"},
184          {"ms","msa","may"},
185          {"mg","mlg","mlg"},
186          {"mt","mlt","mlt"},
187          {"mn","mon","mon"},
188          {"na","nau","nau"},
189          {"nv","nav","nav"},
190          {"nr","nbl","nbl"},
191          {"nd","nde","nde"},
192          {"ng","ndo","ndo"},
193          {"ne","nep","nep"},
194          {"nn","nno","nno"},
195          {"nb","nob","nob"},
196          {"no","nor","nor"},
197          {"ny","nya","nya"},
198          {"oc","oci","oci"},
199          {"oj","oji","oji"},
200          {"or","ori","ori"},
201          {"om","orm","orm"},
202          {"os","oss","oss"},
203          {"pa","pan","pan"},
204          {"fa","fas","per"},
205          {"pi","pli","pli"},
206          {"pl","pol","pol"},
207          {"pt","por","por"},
208          {"ps","pus","pus"},
209          {"qu","que","que"},
210          {"rm","roh","roh"},
211          {"ro","ron","rum"},
212          {"rn","run","run"},
213          {"ru","rus","rus"},
214          {"sg","sag","sag"},
215          {"sa","san","san"},
216          {"si","sin","sin"},
217          {"sk","slk","slo"},
218          {"sl","slv","slv"},
219          {"se","sme","sme"},
220          {"sm","smo","smo"},
221          {"sn","sna","sna"},
222          {"sd","snd","snd"},
223          {"so","som","som"},
224          {"st","sot","sot"},
225          {"es","spa","spa"},
226          {"sc","srd","srd"},
227          {"sr","srp","srp"},
228          {"ss","ssw","ssw"},
229          {"su","sun","sun"},
230          {"sw","swa","swa"},
231          {"sv","swe","swe"},
232          {"ty","tah","tah"},
233          {"ta","tam","tam"},
234          {"tt","tat","tat"},
235          {"te","tel","tel"},
236          {"tg","tgk","tgk"},
237          {"tl","tgl","tgl"},
238          {"th","tha","tha"},
239          {"bo","bod","tib"},
240          {"ti","tir","tir"},
241          {"to","ton","ton"},
242          {"tn","tsn","tsn"},
243          {"ts","tso","tso"},
244          {"tk","tuk","tuk"},
245          {"tr","tur","tur"},
246          {"tw","twi","twi"},
247          {"ug","uig","uig"},
248          {"uk","ukr","ukr"},
249          {"ur","urd","urd"},
250          {"uz","uzb","uzb"},
251          {"ve","ven","ven"},
252          {"vi","vie","vie"},
253          {"vo","vol","vol"},
254          {"cy","cym","wel"},
255          {"wa","wln","wln"},
256          {"wo","wol","wol"},
257          {"xh","xho","xho"},
258          {"yi","yid","yid"},
259          {"yo","yor","yor"},
260          {"za","zha","zha"},
261          {"zu","zul","zul"},
262     };
263 
264     // The following code was used to generate the table above from the two ISO standards.
265     private static final String ISO639 = "ISO-639-2_utf-8.txt";
266     private static void generateTables() {
267         try {
268             BufferedReader ISO639File = new BufferedReader(new FileReader(ISO639));
269             for (String line = ISO639File.readLine(); line != null; line = ISO639File.readLine()) {
270                 String[] tokens= line.split("\\|");
271                 String iso639_1 = tokens[2];
272                 String iso639_2B = tokens[1];
273                 String iso639_2T = tokens[0];
274                 if (iso639_1.isEmpty()){
275                     continue; // Skip if not both a 639-1 and 639-2 code
276                 }
277                 if (iso639_2B.isEmpty()){
278                     iso639_2B = iso639_2T; // Default 639/B to 639/T if empty
279                 }
280                 System.out.printf("""
281                         {"%s","%s","%s"},
282                         """, iso639_1, iso639_2B, iso639_2T);
283             }
284         } catch (Exception e) {
285             System.out.println(e);
286         }
287     }
288 }
289 
290 // CODES generated from https://www.loc.gov/standards/iso639-2/ISO-639-2_utf-8.txt
291 // on March 9th, 2023.