1 /* 2 ********************************************************************** 3 * Copyright (c) 2010, International Business Machines 4 * Corporation and others. All Rights Reserved. 5 ********************************************************************** 6 * Author: Steven R. Loomis 7 ********************************************************************** 8 */ 9 package org.unicode.cldr.unittest; 10 11 import java.io.PrintWriter; 12 import java.io.StringWriter; 13 14 import org.unicode.cldr.util.CLDRConfig; 15 import org.unicode.cldr.util.CLDRFile; 16 import org.unicode.cldr.util.CLDRLocale; 17 import org.unicode.cldr.util.CLDRLocale.CLDRFormatter; 18 import org.unicode.cldr.util.CLDRLocale.FormatBehavior; 19 import org.unicode.cldr.util.SimpleFactory; 20 21 import com.ibm.icu.dev.test.TestFmwk; 22 import com.ibm.icu.text.Transform; 23 import com.ibm.icu.util.ULocale; 24 25 /** 26 * @author srl 27 * 28 */ 29 public class TestCLDRUtils extends TestFmwk { 30 31 static Transform<String, String> SHORT_ALT_PICKER = new Transform<String, String>() { 32 public String transform(@SuppressWarnings("unused") String source) { 33 return "short"; 34 } 35 }; 36 TestVariantName()37 public void TestVariantName() { 38 CLDRFile english = CLDRConfig.getInstance().getEnglish(); 39 40 checkNames(english, "en_US_POSIX", 41 "American English (Computer)", 42 "US English (Computer)", 43 "English (United States, Computer)", 44 "English (US, Computer)"); 45 46 checkNames(english, new ULocale("en_US_POSIX").toLanguageTag(), 47 "American English (POSIX Compliant Locale)", 48 "US English (POSIX Compliant Locale)", 49 "English (United States, POSIX Compliant Locale)", 50 "English (US, POSIX Compliant Locale)"); 51 52 checkNames(english, "en_HK", "English (Hong Kong SAR China)", 53 "English (Hong Kong)", "English (Hong Kong SAR China)", 54 "English (Hong Kong)"); 55 56 checkNames(english, "en_GB", "British English", "UK English", 57 "English (United Kingdom)", "English (UK)"); 58 59 checkNames(english, "eo_001", "Esperanto (World)"); 60 61 checkNames(english, "el_POLYTON", "Greek (Polytonic)"); 62 63 checkNames(english, new ULocale("el__POLYTON").toLanguageTag(), 64 "Greek (Polytonic)"); 65 66 CLDRFile french = CLDRConfig.getInstance().getCldrFactory() 67 .make("fr", true); 68 69 checkNames(french, "en_US_POSIX", "anglais américain (informatique)", 70 "anglais [É.-U.] (informatique)", 71 "anglais (États-Unis, informatique)", 72 "anglais (É.-U., informatique)"); 73 } 74 75 /** 76 * 77 * @param french 78 * @param locale 79 * @param combinedLong 80 * @param otherNames 81 * : combinedShort, uncombinedLong, uncombinedShort 82 */ checkNames(CLDRFile french, String locale, String combinedLong, String... otherNames)83 private void checkNames(CLDRFile french, String locale, 84 String combinedLong, String... otherNames) { 85 assertEquals("Test variant formatting combinedLong " + locale, 86 combinedLong, french.getName(locale)); 87 String combinedShort = otherNames.length > 0 ? otherNames[0] 88 : combinedLong; 89 String uncombinedLong = otherNames.length > 1 ? otherNames[1] 90 : combinedLong; 91 String uncombinedShort = otherNames.length > 2 ? otherNames[2] 92 : uncombinedLong; 93 94 assertEquals("Test variant formatting combinedShort " + locale, 95 combinedShort, french.getName(locale, false, SHORT_ALT_PICKER)); 96 assertEquals("Test variant formatting uncombinedLong " + locale, 97 uncombinedLong, french.getName(locale, true)); 98 assertEquals("Test variant formatting uncombinedShort " + locale, 99 uncombinedShort, french.getName(locale, true, SHORT_ALT_PICKER)); 100 } 101 TestEmptyCLDRFile()102 public void TestEmptyCLDRFile() { 103 CLDRLocale aloc = CLDRLocale.getInstance("und_AQ_NONEXISTENT"); 104 logln("Testing CLDRFile.make(" + aloc.toString() + ").write()"); 105 CLDRFile emptyFile = SimpleFactory.makeFile(aloc.getBaseName()); 106 StringWriter outStream = new StringWriter(); 107 try { 108 emptyFile.write(new PrintWriter(outStream)); 109 } finally { 110 logln(aloc.getBaseName() 111 + ".xml: " 112 + outStream.toString().replaceAll("\n", "\\\\n") 113 .replaceAll("\t", "\\\\t")); 114 } 115 if (outStream.toString().length() == 0) { 116 errln("Error: empty CLDRFile of " + aloc 117 + " turned into a 0-length string."); 118 } 119 } 120 TestCLDRLocaleFormatNoFile()121 public void TestCLDRLocaleFormatNoFile() { 122 logln("Tests for CLDRLocale:"); 123 CLDRLocale 124 .setDefaultFormatter(new CLDRFormatter(FormatBehavior.replace)); 125 String tests_str[] = { "", "root", "el__POLYTON", "el_POLYTON", 126 "__UND", "en", "en_GB", "en_Shav", "en_Shav_GB", 127 "en_Latn_GB_POLYTON", "nnh", "sq_XK" }; 128 for (String s : tests_str) { 129 CLDRLocale loc = CLDRLocale.getInstance(s); 130 String str = loc.toString(); 131 ULocale uloc = loc.toULocale(); 132 String fromloc = new ULocale(s).toString(); 133 String format = loc.getDisplayName(); 134 CLDRLocale parent = loc.getParent(); 135 logln(s + ": tostring:" + str + ", uloc:" + uloc + ", fromloc:" 136 + fromloc + ", format: " + format + ", parent:" + parent); 137 } 138 139 CLDRLocale.setDefaultFormatter(CLDRLocale.getSimpleFormatterFor(ULocale 140 .getDefault())); 141 } 142 TestCLDRLocaleInheritance()143 public void TestCLDRLocaleInheritance() { 144 CLDRLocale ml = CLDRLocale.getInstance("ml"); 145 CLDRLocale ml_IN = CLDRLocale.getInstance("ml_IN"); 146 CLDRLocale ml_Mlym = CLDRLocale.getInstance("ml_Mlym"); 147 CLDRLocale ml_Mlym_IN = CLDRLocale.getInstance("ml_Mlym_IN"); 148 149 logln("Testing descendants of " + ml + " " + ml.getDisplayName()); 150 CLDRLocale areSub[][] = { // isChild returns true for i!=0 151 { ml, ml_IN, ml_Mlym, ml_Mlym_IN }, { ml_Mlym, ml_Mlym_IN }, }; 152 for (CLDRLocale[] row : areSub) { 153 CLDRLocale parent = row[0]; 154 for (CLDRLocale child : row) { 155 // TODO move the first checkChild here if the meaning changes. 156 if (child == parent) { 157 continue; 158 } 159 checkChild(child, parent, false); 160 checkChild(parent, child, true); 161 } 162 } 163 CLDRLocale notSub[] = { // isChild returns false 164 CLDRLocale.getInstance("mli"), CLDRLocale.getInstance("root"), 165 CLDRLocale.ROOT }; 166 for (CLDRLocale child : notSub) { 167 checkChild(ml, child, false); 168 } 169 } 170 TestCLDRLocaleEquivalence()171 public void TestCLDRLocaleEquivalence() { 172 assertSame("root is caseless", CLDRLocale.getInstance("root"), CLDRLocale.getInstance("RoOt")); 173 assertSame("root = empty", CLDRLocale.getInstance("root"), CLDRLocale.getInstance("")); 174 assertEquals("ROOT.basename = root", "root", CLDRLocale.ROOT.getBaseName()); 175 assertSame("instance of root = ROOT", CLDRLocale.getInstance("root"), CLDRLocale.ROOT); 176 String test = "zh-TW-u-co-pinyin"; 177 assertEquals(test, test, CLDRLocale.getInstance(test).toLanguageTag()); 178 } 179 checkChild(CLDRLocale parent, CLDRLocale child, boolean expected)180 private boolean checkChild(CLDRLocale parent, CLDRLocale child, 181 boolean expected) { 182 boolean got = child.childOf(parent); 183 String message = child + ".childOf(" + parent + ") " + "[" 184 + child.getDisplayName() + ", " + parent.getDisplayName() 185 + "] " + "= " + got; 186 if (got == expected) { 187 logln(message); 188 } else { 189 errln(message + ", but expected " + expected); 190 } 191 return got; 192 } 193 194 /** 195 * @param args 196 */ main(String[] args)197 public static void main(String[] args) { 198 double deltaTime = System.currentTimeMillis(); 199 new TestCLDRUtils().run(args); 200 deltaTime = System.currentTimeMillis() - deltaTime; 201 System.out.println("Seconds: " + deltaTime / 1000); 202 } 203 204 } 205