xref: /aosp_15_r20/external/cldr/tools/cldr-code/src/test/java/org/unicode/cldr/unittest/TestIdentity.java (revision 912701f9769bb47905792267661f0baf2b85bed5)
1 package org.unicode.cldr.unittest;
2 
3 import com.ibm.icu.dev.test.TestFmwk;
4 import java.util.ArrayList;
5 import java.util.HashSet;
6 import java.util.List;
7 import java.util.Set;
8 import org.unicode.cldr.util.CLDRConfig;
9 import org.unicode.cldr.util.CLDRFile;
10 import org.unicode.cldr.util.Factory;
11 import org.unicode.cldr.util.LanguageTagCanonicalizer;
12 import org.unicode.cldr.util.LanguageTagParser;
13 
14 public class TestIdentity extends TestFmwk {
15     static CLDRConfig testInfo = CLDRConfig.getInstance();
16 
main(String[] args)17     public static void main(String[] args) {
18         new TestIdentity().run(args);
19     }
20 
TestIdentityVsFilename()21     public void TestIdentityVsFilename() {
22 
23         LanguageTagParser ltp = new LanguageTagParser();
24         LanguageTagCanonicalizer ltc = new LanguageTagCanonicalizer();
25 
26         List<Factory> factories = new ArrayList<Factory>();
27         factories.add(testInfo.getFullCldrFactory());
28         factories.add(testInfo.getExemplarsFactory());
29         factories.add(testInfo.getCollationFactory());
30         factories.add(testInfo.getRBNFFactory());
31         factories.add(testInfo.getAnnotationsFactory());
32         for (Factory factory : factories) {
33             for (String locale : factory.getAvailable()) {
34                 String canonicalLocaleID = ltc.transform(locale);
35                 ltp.set(locale);
36                 String fLanguage = ltp.getLanguage();
37                 String fScript = ltp.getScript().length() > 0 ? ltp.getScript() : "<missing>";
38                 String fTerritory = ltp.getRegion().length() > 0 ? ltp.getRegion() : "<missing>";
39                 Set<String> fVariants = new HashSet<String>(ltp.getVariants());
40                 CLDRFile localeData;
41                 if (factory.equals(testInfo.getFullCldrFactory())) {
42                     localeData = testInfo.getCLDRFile(locale, false);
43                 } else {
44                     localeData = factory.make(locale, false);
45                 }
46                 String identity = localeData.getLocaleIDFromIdentity();
47                 ltp.set(identity);
48                 String iLanguage = ltp.getLanguage();
49                 if (!fLanguage.equals(iLanguage)) {
50                     errln(
51                             "Language code for locale \""
52                                     + locale
53                                     + "\" does not match the identity section."
54                                     + "\n\tLocated in : "
55                                     + factory.getSourceDirectoryForLocale(locale).getPath()
56                                     + "\n\tValue in file name is: "
57                                     + fLanguage
58                                     + "\n\tValue in identity section is: "
59                                     + iLanguage);
60                 }
61                 String iScript = ltp.getScript().length() > 0 ? ltp.getScript() : "<missing>";
62                 if (!fScript.equals(iScript)) {
63                     errln(
64                             "Script code for locale \""
65                                     + locale
66                                     + "\" does not match the identity section."
67                                     + "\n\tLocated in : "
68                                     + factory.getSourceDirectoryForLocale(locale).getPath()
69                                     + "\n\tValue in file name is: "
70                                     + fScript
71                                     + "\n\tValue in identity section is: "
72                                     + iScript);
73                 }
74                 String iTerritory = ltp.getRegion().length() > 0 ? ltp.getRegion() : "<missing>";
75                 if (!fTerritory.equals(iTerritory)) {
76                     errln(
77                             "Territory code for locale \""
78                                     + locale
79                                     + "\" does not match the identity section."
80                                     + "\n\tLocated in : "
81                                     + factory.getSourceDirectoryForLocale(locale).getPath()
82                                     + "\n\tValue in file name is: "
83                                     + fTerritory
84                                     + "\n\tValue in identity section is: "
85                                     + iTerritory);
86                 }
87                 Set<String> iVariants = new HashSet<String>(ltp.getVariants());
88                 if (!fVariants.equals(iVariants)) {
89                     errln(
90                             "Variants for locale \""
91                                     + locale
92                                     + "\" do not match the identity section."
93                                     + "\n\tLocated in : "
94                                     + factory.getSourceDirectoryForLocale(locale).getPath()
95                                     + "\n\tValue in file name is: "
96                                     + fVariants.toString()
97                                     + "\n\tValue in identity section is: "
98                                     + iVariants.toString());
99                 }
100                 if (canonicalLocaleID != null) {
101                     ltp.set(canonicalLocaleID);
102                     String canonicalLanguage = ltp.getLanguage();
103                     if (!fLanguage.equals(canonicalLanguage)) {
104                         errln(
105                                 "Locale \""
106                                         + locale
107                                         + "\" uses a non-canonical language tag: "
108                                         + fLanguage
109                                         + "\n\tLocated in : "
110                                         + factory.getSourceDirectoryForLocale(locale).getPath()
111                                         + "\n\tCanonical form would be : "
112                                         + canonicalLanguage);
113                     }
114                 }
115             }
116         }
117     }
118 }
119