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