2 ******************************************************************************************
3 * Copyright (C) 2009-2010, Google, Inc.; International Business Machines Corporation and *
4 * others. All Rights Reserved. *
5 ******************************************************************************************
8 package com.ibm.icu.dev.test.util;
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;
17 * Test the LanguageMatcher.
21 public class LocaleMatcherTest extends TestFmwk {
23 public static void main(String[] args) throws Exception {
24 new LocaleMatcherTest().run(args);
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());
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));
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"));
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"));
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"));
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());
71 LanguageMatcherData data = new LanguageMatcherData();
73 LanguageMatcherData clone = data.cloneAsThawed();
75 if (clone.equals(data)) {
76 errln("Error cloneAsThawed() is equal.");
79 if (data.isFrozen()) {
80 errln("Error LanguageMatcherData is frozen!");
84 private void assertEquals(Object expected, Object string) {
85 assertEquals("", expected, string);