2 *******************************************************************************
\r
3 * Copyright (C) 2008, International Business Machines Corporation and *
\r
4 * others. All Rights Reserved. *
\r
5 *******************************************************************************
\r
7 package com.ibm.icu.dev.test.localespi;
\r
9 import java.text.CollationKey;
\r
10 import java.text.Collator;
\r
11 import java.util.Locale;
\r
13 import com.ibm.icu.dev.test.TestFmwk;
\r
15 public class CollatorTest extends TestFmwk {
\r
16 public static void main(String[] args) throws Exception {
\r
17 new CollatorTest().run(args);
\r
21 * Check if getInstance returns the ICU implementation.
\r
23 public void TestGetInstance() {
\r
24 for (Locale loc : Collator.getAvailableLocales()) {
\r
25 if (TestUtil.isProblematicIBMLocale(loc)) {
\r
26 logln("Skipped " + loc);
\r
30 Collator coll = Collator.getInstance(loc);
\r
32 boolean isIcuImpl = (coll instanceof com.ibm.icu.impl.jdkadapter.CollatorICU);
\r
34 if (TestUtil.isICUExtendedLocale(loc)) {
\r
36 errln("FAIL: getInstance returned JDK Collator for locale " + loc);
\r
40 logln("INFO: getInstance returned ICU Collator for locale " + loc);
\r
42 Locale iculoc = TestUtil.toICUExtendedLocale(loc);
\r
43 Collator collIcu = Collator.getInstance(iculoc);
\r
45 if (!coll.equals(collIcu)) {
\r
46 errln("FAIL: getInstance returned ICU Collator for locale " + loc
\r
47 + ", but different from the one for locale " + iculoc);
\r
50 if (!(collIcu instanceof com.ibm.icu.impl.jdkadapter.CollatorICU)) {
\r
51 errln("FAIL: getInstance returned JDK Collator for locale " + iculoc);
\r
59 * Testing the behavior of text collation between ICU instance and its
\r
60 * equivalent created via the Locale SPI framework.
\r
62 public void TestICUEquivalent() {
\r
63 Locale[] TEST_LOCALES = {
\r
64 new Locale("en", "US"),
\r
65 new Locale("de", "DE"),
\r
66 new Locale("ja", "JP"),
\r
69 String[] TEST_DATA = {
\r
75 "\u304b\u3075\u3047",
\r
76 "\u304c\u3075\u3047",
\r
77 "\u304b\u3075\u3048",
\r
78 "\u30ab\u30d5\u30a7",
\r
79 "\uff76\uff8c\uff6a",
\r
82 for (Locale loc : TEST_LOCALES) {
\r
83 Locale iculoc = TestUtil.toICUExtendedLocale(loc);
\r
84 Collator jdkColl = Collator.getInstance(iculoc);
\r
85 com.ibm.icu.text.Collator icuColl = com.ibm.icu.text.Collator.getInstance(loc);
\r
87 // Default strength = TERITIARY
\r
88 checkCollation(jdkColl, icuColl, TEST_DATA, "TERITIARY", loc);
\r
91 jdkColl.setStrength(Collator.PRIMARY);
\r
92 icuColl.setStrength(com.ibm.icu.text.Collator.PRIMARY);
\r
93 checkCollation(jdkColl, icuColl, TEST_DATA, "PRIMARY", loc);
\r
96 jdkColl.setStrength(Collator.SECONDARY);
\r
97 icuColl.setStrength(com.ibm.icu.text.Collator.SECONDARY);
\r
98 checkCollation(jdkColl, icuColl, TEST_DATA, "SECONDARY", loc);
\r
102 private void checkCollation(Collator jdkColl, com.ibm.icu.text.Collator icuColl,
\r
103 String[] data, String strength, Locale loc) {
\r
104 for (String text1 : data) {
\r
105 for (String text2 : data) {
\r
106 int jdkRes = jdkColl.compare(text1, text2);
\r
107 int icuRes = icuColl.compare(text1, text2);
\r
109 if (jdkRes != icuRes) {
\r
110 errln("FAIL: Different results for [text1=" + text1 + ",text2=" + text2 + ") for locale "
\r
111 + loc + " with strength " + strength + " - Result (jdk=" + jdkRes + ",icu=" + icuRes + ")");
\r
114 // Evaluate collationKey
\r
115 CollationKey jdkKey1 = jdkColl.getCollationKey(text1);
\r
116 CollationKey jdkKey2 = jdkColl.getCollationKey(text2);
\r
118 com.ibm.icu.text.CollationKey icuKey1 = icuColl.getCollationKey(text1);
\r
119 com.ibm.icu.text.CollationKey icuKey2 = icuColl.getCollationKey(text2);
\r
121 int jdkKeyRes = jdkKey1.compareTo(jdkKey2);
\r
122 int icuKeyRes = icuKey1.compareTo(icuKey2);
\r
124 if (jdkKeyRes != icuKeyRes) {
\r
125 errln("FAIL: Different collationKey comparison results for [text1=" + text1 + ",text2=" + text2
\r
126 + ") for locale " + loc + " with strength " + strength
\r
127 + " - Result (jdk=" + jdkRes + ",icu=" + icuRes + ")");
\r