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