]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-52_1/main/tests/core/src/com/ibm/icu/dev/test/util/LocaleMatcherTest.java
Clean up imports.
[Dictionary.git] / jars / icu4j-52_1 / main / tests / core / src / com / ibm / icu / dev / test / util / LocaleMatcherTest.java
1 /*
2  ******************************************************************************************
3  * Copyright (C) 2009-2010, Google, Inc.; International Business Machines Corporation and *
4  * others. All Rights Reserved.                                                           *
5  ******************************************************************************************
6  */
7
8 package com.ibm.icu.dev.test.util;
9
10 import com.ibm.icu.dev.test.TestFmwk;
11 import com.ibm.icu.util.LocaleMatcher;
12 import com.ibm.icu.util.LocaleMatcher.LanguageMatcherData;
13 import com.ibm.icu.util.LocalePriorityList;
14 import com.ibm.icu.util.ULocale;
15
16 /**
17  * Test the LanguageMatcher.
18  * 
19  * @author markdavis
20  */
21 public class LocaleMatcherTest extends TestFmwk {
22
23     public static void main(String[] args) throws Exception {
24         new LocaleMatcherTest().run(args);
25     }
26
27     public void testBasics() {
28         final LocaleMatcher matcher = new LocaleMatcher(LocalePriorityList.add(ULocale.FRENCH).add(ULocale.UK)
29                 .add(ULocale.ENGLISH).build());
30         logln(matcher.toString());
31
32         assertEquals(ULocale.UK, matcher.getBestMatch(ULocale.UK));
33         assertEquals(ULocale.ENGLISH, matcher.getBestMatch(ULocale.US));
34         assertEquals(ULocale.FRENCH, matcher.getBestMatch(ULocale.FRANCE));
35         assertEquals(ULocale.FRENCH, matcher.getBestMatch(ULocale.JAPAN));
36     }
37
38     public void testFallback() {
39         // check that script fallbacks are handled right
40         final LocaleMatcher matcher = new LocaleMatcher("zh_CN, zh_TW, iw");
41         assertEquals(new ULocale("zh_TW"), matcher.getBestMatch("zh_Hant"));
42         assertEquals(new ULocale("zh_CN"), matcher.getBestMatch("zh"));
43         assertEquals(new ULocale("zh_CN"), matcher.getBestMatch("zh_Hans_CN"));
44         assertEquals(new ULocale("zh_TW"), matcher.getBestMatch("zh_Hant_HK"));
45         assertEquals(new ULocale("he"), matcher.getBestMatch("iw_IT"));
46     }
47
48     public void testSpecials() {
49         // check that nearby languages are handled
50         final LocaleMatcher matcher = new LocaleMatcher("en, fil, ro, nn");
51         assertEquals(new ULocale("fil"), matcher.getBestMatch("tl"));
52         assertEquals(new ULocale("ro"), matcher.getBestMatch("mo"));
53         assertEquals(new ULocale("nn"), matcher.getBestMatch("nb"));
54         // make sure default works
55         assertEquals(new ULocale("en"), matcher.getBestMatch("ja"));
56     }
57
58     public void testRegionalSpecials() {
59         // verify that en_AU is closer to en_GB than to en (which is en_US)
60         final LocaleMatcher matcher = new LocaleMatcher("en, en_GB, es, es_419");
61         assertEquals("en_AU in {en, en_GB, es, es_419}", new ULocale("en_GB"), matcher.getBestMatch("en_AU"));
62         assertEquals("es_MX in {en, en_GB, es, es_419}", new ULocale("es_419"), matcher.getBestMatch("es_MX"));
63         assertEquals("es_ES in {en, en_GB, es, es_419}", new ULocale("es"), matcher.getBestMatch("es_ES"));
64     }
65
66     public void TestLocaleMatcherCoverage() {
67         // Add tests for better code coverage
68         LocaleMatcher matcher = new LocaleMatcher(LocalePriorityList.add(null, 0).build(), null);
69         logln(matcher.toString());
70
71         LanguageMatcherData data = new LanguageMatcherData();
72
73         LanguageMatcherData clone = data.cloneAsThawed();
74
75         if (clone.equals(data)) {
76             errln("Error cloneAsThawed() is equal.");
77         }
78
79         if (data.isFrozen()) {
80             errln("Error LanguageMatcherData is frozen!");
81         }
82     }
83
84     private void assertEquals(Object expected, Object string) {
85         assertEquals("", expected, string);
86     }
87
88 }