• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.IOException;
12 import java.io.PrintWriter;
13 import java.io.StringWriter;
14 import java.util.Map;
15 import java.util.TreeMap;
16 
17 import org.unicode.cldr.util.CLDRConfig;
18 import org.unicode.cldr.util.CLDRFile;
19 import org.unicode.cldr.util.CLDRLocale;
20 import org.unicode.cldr.util.CLDRLocale.CLDRFormatter;
21 import org.unicode.cldr.util.CLDRLocale.FormatBehavior;
22 import org.unicode.cldr.util.CLDRPaths;
23 import org.unicode.cldr.util.Factory;
24 import org.unicode.cldr.util.FileReaders;
25 import org.unicode.cldr.util.SimpleFactory;
26 import org.unicode.cldr.util.XMLFileReader;
27 import org.unicode.cldr.util.XPathParts;
28 
29 import com.ibm.icu.dev.test.TestFmwk;
30 import com.ibm.icu.text.Transform;
31 import com.ibm.icu.util.ULocale;
32 
33 /**
34  * @author srl
35  *
36  */
37 public class TestCLDRUtils extends TestFmwk {
38 
39     /**
40      *
41      */
TestCLDRUtils()42     public TestCLDRUtils() {
43         // TODO Auto-generated constructor stub
44     }
45 
46     static Transform<String, String> SHORT_ALT_PICKER = new Transform<String, String>() {
47         public String transform(String source) {
48             return "short";
49         }
50     };
51 
TestVariantName()52     public void TestVariantName() {
53         CLDRFile english = CLDRConfig.getInstance().getEnglish();
54 
55         checkNames(english, "en_US_POSIX", "American English (Computer)",
56             "US English (Computer)", "English (United States, Computer)",
57             "English (US, Computer)");
58 
59         checkNames(english, new ULocale("en_US_POSIX").toLanguageTag(),
60             "American English (Computer)", "US English (Computer)",
61             "English (United States, Computer)", "English (US, Computer)");
62 
63         checkNames(english, "en_HK", "English (Hong Kong SAR China)",
64             "English (Hong Kong)", "English (Hong Kong SAR China)",
65             "English (Hong Kong)");
66 
67         checkNames(english, "en_GB", "British English", "UK English",
68             "English (United Kingdom)", "English (UK)");
69 
70         checkNames(english, "eo_001", "Esperanto (World)");
71 
72         checkNames(english, "el_POLYTON", "Greek (Polytonic)");
73 
74         checkNames(english, new ULocale("el__POLYTON").toLanguageTag(),
75             "Greek (Polytonic)");
76 
77         CLDRFile french = CLDRConfig.getInstance().getCldrFactory()
78             .make("fr", true);
79 
80         checkNames(french, "en_US_POSIX", "anglais américain (informatique)",
81             "anglais [É.-U.] (informatique)",
82             "anglais (États-Unis, informatique)",
83             "anglais (É.-U., informatique)");
84     }
85 
86     /**
87      *
88      * @param french
89      * @param locale
90      * @param combinedLong
91      * @param otherNames
92      *            : combinedShort, uncombinedLong, uncombinedShort
93      */
checkNames(CLDRFile french, String locale, String combinedLong, String... otherNames)94     private void checkNames(CLDRFile french, String locale,
95         String combinedLong, String... otherNames) {
96         assertEquals("Test variant formatting combinedLong " + locale,
97             combinedLong, french.getName(locale));
98         String combinedShort = otherNames.length > 0 ? otherNames[0]
99             : combinedLong;
100         String uncombinedLong = otherNames.length > 1 ? otherNames[1]
101             : combinedLong;
102         String uncombinedShort = otherNames.length > 2 ? otherNames[2]
103             : uncombinedLong;
104 
105         assertEquals("Test variant formatting combinedShort " + locale,
106             combinedShort, french.getName(locale, false, SHORT_ALT_PICKER));
107         assertEquals("Test variant formatting uncombinedLong " + locale,
108             uncombinedLong, french.getName(locale, true));
109         assertEquals("Test variant formatting uncombinedShort " + locale,
110             uncombinedShort, french.getName(locale, true, SHORT_ALT_PICKER));
111     }
112 
TestEmptyCLDRFile()113     public void TestEmptyCLDRFile() {
114         CLDRLocale aloc = CLDRLocale.getInstance("und_AQ_NONEXISTENT");
115         logln("Testing CLDRFile.make(" + aloc.toString() + ").write()");
116         CLDRFile emptyFile = SimpleFactory.makeFile(aloc.getBaseName());
117         StringWriter outStream = new StringWriter();
118         try {
119             emptyFile.write(new PrintWriter(outStream));
120         } finally {
121             logln(aloc.getBaseName()
122                 + ".xml: "
123                 + outStream.toString().replaceAll("\n", "\\\\n")
124                     .replaceAll("\t", "\\\\t"));
125         }
126         if (outStream.toString().length() == 0) {
127             errln("Error: empty CLDRFile of " + aloc
128                 + " turned into a 0-length string.");
129         }
130     }
131 
TestCLDRLocaleFormatNoFile()132     public void TestCLDRLocaleFormatNoFile() {
133         logln("Tests for CLDRLocale:");
134         CLDRLocale
135             .setDefaultFormatter(new CLDRFormatter(FormatBehavior.replace));
136         String tests_str[] = { "", "root", "el__POLYTON", "el_POLYTON",
137             "__UND", "en", "en_GB", "en_Shav", "en_Shav_GB",
138             "en_Latn_GB_POLYTON", "nnh", "sq_XK" };
139         for (String s : tests_str) {
140             CLDRLocale loc = CLDRLocale.getInstance(s);
141             String str = loc.toString();
142             ULocale uloc = loc.toULocale();
143             String fromloc = new ULocale(s).toString();
144             String format = loc.getDisplayName();
145             CLDRLocale parent = loc.getParent();
146             logln(s + ":  tostring:" + str + ", uloc:" + uloc + ", fromloc:"
147                 + fromloc + ", format: " + format + ", parent:" + parent);
148         }
149 
150         CLDRLocale.setDefaultFormatter(CLDRLocale.getSimpleFormatterFor(ULocale
151             .getDefault()));
152     }
153 
154     // Disable this test, because we now require known DtdTypes
oldTestCLDRLocaleDataDriven()155     public void oldTestCLDRLocaleDataDriven() throws IOException {
156         XMLFileReader myReader = new XMLFileReader();
157         final Factory cldrFactory = Factory
158             .make(CLDRPaths.MAIN_DIRECTORY, ".*");
159         final CLDRFile engFile = cldrFactory.make("en", true);
160         final CLDRFormatter engFormat = new CLDRFormatter(engFile);
161         final XPathParts xpp = new XPathParts(null, null);
162         final Map<String, String> attrs = new TreeMap<String, String>();
163         myReader.setHandler(new XMLFileReader.SimpleHandler() {
164             public void handlePathValue(String path, String value) {
165                 xpp.clear();
166                 xpp.initialize(path);
167                 attrs.clear();
168                 for (String k : xpp.getAttributeKeys(-1)) {
169                     attrs.put(k, xpp.getAttributeValue(-1, k));
170                 }
171                 String elem = xpp.getElement(-1);
172                 logln("* <" + elem + " " + attrs.toString() + ">" + value
173                     + "</" + elem + ">");
174                 String loc = attrs.get("locale");
175                 CLDRLocale locale = CLDRLocale.getInstance(loc);
176                 if (elem.equals("format")) {
177                     String type = attrs.get("type");
178                     String result = null;
179                     boolean combined = Boolean.parseBoolean(attrs
180                         .get("combined"));
181                     Transform<String, String> picker = attrs.get("alt")
182                         .equalsIgnoreCase("short") ? SHORT_ALT_PICKER
183                             : null;
184                     if (type.equals("region")) {
185                         result = locale.getDisplayCountry(engFormat);
186                     } else if (type.equals("all")) {
187                         result = locale.getDisplayName(engFormat, combined,
188                             picker);
189                     } else {
190                         errln("Unknown test type: " + type);
191                         return;
192                     }
193 
194                     if (result == null) {
195                         errln("Null result!");
196                         return;
197                     }
198                     logln("  result=" + result);
199                     if (!result.equals(value)) {
200                         errln("For format test " + attrs.toString()
201                             + " expected '" + value + "' got '" + result
202                             + "'");
203                     }
204                 } else if (elem.equals("echo")) {
205                     logln("*** \"" + value.trim() + "\"");
206                 } else {
207                     throw new IllegalArgumentException(
208                         "Unknown test element type " + elem);
209                 }
210             };
211             // public void handleComment(String path, String comment) {};
212             // public void handleElementDecl(String name, String model) {};
213             // public void handleAttributeDecl(String eName, String aName,
214             // String type, String mode, String value) {};
215         });
216         String fileName = "TestCLDRLocale" + ".xml";
217         logln("Reading" + fileName);
218         myReader.read(TestCLDRUtils.class.getResource("data/" + fileName)
219             .toString(), FileReaders.openFile(TestCLDRUtils.class, "data/"
220                 + fileName),
221             -1, true);
222     }
223 
TestCLDRLocaleInheritance()224     public void TestCLDRLocaleInheritance() {
225         CLDRLocale ml = CLDRLocale.getInstance("ml");
226         CLDRLocale ml_IN = CLDRLocale.getInstance("ml_IN");
227         CLDRLocale ml_Mlym = CLDRLocale.getInstance("ml_Mlym");
228         CLDRLocale ml_Mlym_IN = CLDRLocale.getInstance("ml_Mlym_IN");
229 
230         logln("Testing descendants of " + ml + " " + ml.getDisplayName());
231         CLDRLocale areSub[][] = { // isChild returns true for i!=0
232             { ml, ml_IN, ml_Mlym, ml_Mlym_IN }, { ml_Mlym, ml_Mlym_IN }, };
233         for (CLDRLocale[] row : areSub) {
234             CLDRLocale parent = row[0];
235             for (CLDRLocale child : row) {
236                 // TODO move the first checkChild here if the meaning changes.
237                 if (child == parent) {
238                     continue;
239                 }
240                 checkChild(child, parent, false);
241                 checkChild(parent, child, true);
242             }
243         }
244         CLDRLocale notSub[] = { // isChild returns false
245             CLDRLocale.getInstance("mli"), CLDRLocale.getInstance("root"),
246             CLDRLocale.ROOT };
247         for (CLDRLocale child : notSub) {
248             checkChild(ml, child, false);
249         }
250     }
251 
TestCLDRLocaleEquivalence()252     public void TestCLDRLocaleEquivalence() {
253         assertEquals("root is caseless", CLDRLocale.getInstance("root"), CLDRLocale.getInstance("RoOt"));
254         assertEquals("root = empty", CLDRLocale.getInstance("root"), CLDRLocale.getInstance(""));
255         String test = "zh-TW-u-co-pinyin";
256         assertEquals(test, test, CLDRLocale.getInstance(test).toLanguageTag());
257     }
258 
checkChild(CLDRLocale parent, CLDRLocale child, boolean expected)259     private boolean checkChild(CLDRLocale parent, CLDRLocale child,
260         boolean expected) {
261         boolean got = child.childOf(parent);
262         String message = child + ".childOf(" + parent + ") " + "["
263             + child.getDisplayName() + ", " + parent.getDisplayName()
264             + "] " + "= " + got;
265         if (got == expected) {
266             logln(message);
267         } else {
268             errln(message + ", but expected " + expected);
269         }
270         return got;
271     }
272 
273     /**
274      * @param args
275      */
main(String[] args)276     public static void main(String[] args) {
277         double deltaTime = System.currentTimeMillis();
278         new TestCLDRUtils().run(args);
279         deltaTime = System.currentTimeMillis() - deltaTime;
280         System.out.println("Seconds: " + deltaTime / 1000);
281     }
282 
283 }
284