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