1 /*
2  *******************************************************************************
3  * Copyright (C) 2008-2015, International Business Machines Corporation and    *
4  * others. All Rights Reserved.                                                *
5  *******************************************************************************
6  */
7 package com.ibm.icu.dev.test.localespi;
8 
9 import java.text.BreakIterator;
10 import java.util.Locale;
11 
12 import com.ibm.icu.dev.test.TestFmwk;
13 
14 public class BreakIteratorTest extends TestFmwk {
main(String[] args)15     public static void main(String[] args) throws Exception {
16         new BreakIteratorTest().run(args);
17     }
18 
19     private static final int CHARACTER_BRK = 0;
20     private static final int WORD_BRK = 1;
21     private static final int LINE_BRK = 2;
22     private static final int SENTENCE_BRK = 3;
23 
24     /*
25      * Check if getInstance returns the ICU implementation.
26      */
TestGetInstance()27     public void TestGetInstance() {
28         for (Locale loc : BreakIterator.getAvailableLocales()) {
29             if (TestUtil.isExcluded(loc)) {
30                 logln("Skipped " + loc);
31                 continue;
32             }
33             checkGetInstance(CHARACTER_BRK, loc);
34             checkGetInstance(WORD_BRK, loc);
35             checkGetInstance(LINE_BRK, loc);
36             checkGetInstance(SENTENCE_BRK, loc);
37         }
38     }
39 
checkGetInstance(int type, Locale loc)40     private void checkGetInstance(int type, Locale loc) {
41         BreakIterator brkitr = null;
42         String method = null;
43         switch (type) {
44         case CHARACTER_BRK:
45             brkitr = BreakIterator.getCharacterInstance(loc);
46             method = "getCharacterInstance";
47             break;
48         case WORD_BRK:
49             brkitr = BreakIterator.getWordInstance(loc);
50             method = "getWordInstance";
51             break;
52         case LINE_BRK:
53             brkitr = BreakIterator.getLineInstance(loc);
54             method = "getLineInstance";
55             break;
56         case SENTENCE_BRK:
57             brkitr = BreakIterator.getSentenceInstance(loc);
58             method = "getSentenceInstance";
59             break;
60         default:
61             errln("FAIL: Unknown break iterator type");
62             return;
63         }
64 
65         boolean isIcuImpl = (brkitr instanceof com.ibm.icu.impl.jdkadapter.BreakIteratorICU);
66 
67         if (TestUtil.isICUExtendedLocale(loc)) {
68             if (!isIcuImpl) {
69                 errln("FAIL: " + method + " returned JDK BreakIterator for locale " + loc);
70             }
71         } else {
72             if (isIcuImpl) {
73                 logln("INFO: " + method + " returned ICU BreakIterator for locale " + loc);
74             }
75             BreakIterator brkitrIcu = null;
76             Locale iculoc = TestUtil.toICUExtendedLocale(loc);
77             switch (type) {
78             case CHARACTER_BRK:
79                 brkitrIcu = BreakIterator.getCharacterInstance(iculoc);
80                 break;
81             case WORD_BRK:
82                 brkitrIcu = BreakIterator.getWordInstance(iculoc);
83                 break;
84             case LINE_BRK:
85                 brkitrIcu = BreakIterator.getLineInstance(iculoc);
86                 break;
87             case SENTENCE_BRK:
88                 brkitrIcu = BreakIterator.getSentenceInstance(iculoc);
89                 break;
90             }
91             if (isIcuImpl) {
92                 if (!brkitr.equals(brkitrIcu)) {
93                     // BreakIterator.getXXXInstance returns a cached BreakIterator instance.
94                     // BreakIterator does not override Object#equals, so the result may not be
95                     // consistent.
96 //                        logln("INFO: " + method + " returned ICU BreakIterator for locale " + loc
97 //                                + ", but different from the one for locale " + iculoc);
98                 }
99             } else {
100                 if (!(brkitrIcu instanceof com.ibm.icu.impl.jdkadapter.BreakIteratorICU)) {
101                     errln("FAIL: " + method + " returned JDK BreakIterator for locale " + iculoc);
102                 }
103             }
104         }
105     }
106 
107     /*
108      * Testing the behavior of text break between ICU instance and its
109      * equivalent created via the Locale SPI framework.
110      */
TestICUEquivalent()111     public void TestICUEquivalent() {
112         Locale[] TEST_LOCALES = {
113                 new Locale("en", "US"),
114                 new Locale("fr", "FR"),
115                 new Locale("th", "TH"),
116                 new Locale("zh", "CN"),
117         };
118 
119         String[] TEST_DATA = {
120                 "International Components for Unicode (ICU) is an open source project of mature "
121                 + "C/C++ and Java libraries for Unicode support, software internationalization and "
122                 + "software globalization. ICU is widely portable to many operating systems and "
123                 + "environments. It gives applications the same results on all platforms and between "
124                 + "C/C++ and Java software. The ICU project is an open source development project "
125                 + "that is sponsored, supported and used by IBM and many other companies.",
126 
127                 "L'International Components for Unicode (ICU) est un projet open source qui fourni "
128                 + "des biblioth\u00e8ques pour les langages informatique C/C++ et Java pour supporter "
129                 + "Unicode, l'internationalisation et la mondialisation des logiciels. ICU est largement "
130                 + "portable vers beaucoup de syst\u00e8mes d'exploitations et d'environnements. Il "
131                 + "donne aux applications les m\u00eames comportements et r\u00e9sultats sur toutes "
132                 + "les plateformes et entre les logiciels C/C++ et Java. Le projet ICU est un projet "
133                 + "dont les code sources sont disponibles qui est sponsoris\u00e9, support\u00e9 et "
134                 + "utilis\u00e9 par IBM et beaucoup d'autres entreprises.",
135 
136                 "\u5728IBM\u7b49\u4f01\u696d\u4e2d\uff0c\u56fd\u9645\u5316\u7ecf\u5e38\u7b80\u5199"
137                 + "\u4e3aI18N (\u6216i18n\u6216I18n)\uff0c\u5176\u4e2d18\u4ee3\u8868\u4e86\u4e2d\u95f4"
138                 + "\u7701\u7565\u768418\u4e2a\u5b57\u6bcd\uff1b\u800c\u201c\u672c\u5730\u5316\u201d"
139                 + "\u540c\u53ef\u7b80\u5199\u4e3al10n\u3002\u9019\u4e24\u4e2a\u6982\u5ff5\u6709\u65f6"
140                 + "\u5408\u79f0\u5168\u7403\u5316\uff08g11n\uff09\uff0c\u4f46\u662f\u5168\u7403\u5316"
141                 + "\u7684\u6db5\u4e49\u66f4\u4e3a\u4e00\u822c\u5316\u3002\u53e6\u5916\u5076\u5c14\u4f1a"
142                 + "\u51fa\u73b0\u201cp13n\u201d\uff0c\u4ee3\u8868\u4e2a\u4eba\u5316\uff08personalization"
143                 + "\uff09\u3002",
144 
145                 "\u0e01\u0e23\u0e38\u0e07\u0e40\u0e17\u0e1e\u0e21\u0e2b\u0e32\u0e19\u0e04\u0e23"
146                 + "\u0e43\u0e19\u0e1b\u0e31\u0e08\u0e08\u0e38\u0e1a\u0e31\u0e19\u0e40\u0e1b\u0e47"
147                 + "\u0e19\u0e28\u0e39\u0e19\u0e22\u0e4c\u0e01\u0e25\u0e32\u0e07\u0e01\u0e32\u0e23"
148                 + "\u0e1b\u0e01\u0e04\u0e23\u0e2d\u0e07 \u0e01\u0e32\u0e23\u0e28\u0e36\u0e01\u0e29"
149                 + "\u0e32 \u0e01\u0e32\u0e23\u0e04\u0e21\u0e19\u0e32\u0e04\u0e21\u0e02\u0e19\u0e2a"
150                 + "\u0e48\u0e07 \u0e01\u0e32\u0e23\u0e40\u0e07\u0e34\u0e19\u0e01\u0e32\u0e23\u0e18"
151                 + "\u0e19\u0e32\u0e04\u0e32\u0e23 \u0e01\u0e32\u0e23\u0e1e\u0e32\u0e13\u0e34\u0e0a"
152                 + "\u0e22\u0e4c \u0e01\u0e32\u0e23\u0e2a\u0e37\u0e48\u0e2d\u0e2a\u0e32\u0e23 \u0e2f"
153                 + "\u0e25\u0e2f \u0e42\u0e14\u0e22\u0e21\u0e35\u0e1e\u0e37\u0e49\u0e19\u0e17\u0e35"
154                 + "\u0e48\u0e17\u0e31\u0e49\u0e07\u0e2b\u0e21\u0e14 1,562.2 \u0e15\u0e32\u0e23\u0e32"
155                 + "\u0e07\u0e01\u0e34\u0e42\u0e25\u0e40\u0e21\u0e15\u0e23 \u0e1e\u0e34\u0e01\u0e31"
156                 + "\u0e14\u0e17\u0e32\u0e07\u0e20\u0e39\u0e21\u0e34\u0e28\u0e32\u0e2a\u0e15\u0e23"
157                 + "\u0e4c\u0e04\u0e37\u0e2d \u0e25\u0e30\u0e15\u0e34\u0e08\u0e39\u0e14 13\u00b0 45"
158                 + "\u2019 \u0e40\u0e2b\u0e19\u0e37\u0e2d \u0e25\u0e2d\u0e07\u0e08\u0e34\u0e08\u0e39"
159                 + "\u0e14 100\u00b0 31\u2019 \u0e15\u0e30\u0e27\u0e31\u0e19\u0e2d\u0e2d\u0e01"
160         };
161 
162         BreakIterator[] jdkBrkItrs = new BreakIterator[4];
163         com.ibm.icu.text.BreakIterator[] icuBrkItrs = new com.ibm.icu.text.BreakIterator[4];
164 
165         for (Locale loc : TEST_LOCALES) {
166             Locale iculoc = TestUtil.toICUExtendedLocale(loc);
167 
168             jdkBrkItrs[0] = BreakIterator.getCharacterInstance(iculoc);
169             jdkBrkItrs[1] = BreakIterator.getWordInstance(iculoc);
170             jdkBrkItrs[2] = BreakIterator.getLineInstance(iculoc);
171             jdkBrkItrs[3] = BreakIterator.getSentenceInstance(iculoc);
172 
173             icuBrkItrs[0] = com.ibm.icu.text.BreakIterator.getCharacterInstance(iculoc);
174             icuBrkItrs[1] = com.ibm.icu.text.BreakIterator.getWordInstance(iculoc);
175             icuBrkItrs[2] = com.ibm.icu.text.BreakIterator.getLineInstance(iculoc);
176             icuBrkItrs[3] = com.ibm.icu.text.BreakIterator.getSentenceInstance(iculoc);
177 
178             for (String text : TEST_DATA) {
179                 for (int i = 0; i < 4; i++) {
180                     compareBreaks(text, jdkBrkItrs[i], icuBrkItrs[i]);
181                 }
182             }
183         }
184     }
185 
compareBreaks(String text, BreakIterator jdkBrk, com.ibm.icu.text.BreakIterator icuBrk)186     private void compareBreaks(String text, BreakIterator jdkBrk, com.ibm.icu.text.BreakIterator icuBrk) {
187         jdkBrk.setText(text);
188         icuBrk.setText(text);
189 
190         // Forward
191         int jidx = jdkBrk.first();
192         int iidx = icuBrk.first();
193         if (jidx != iidx) {
194             errln("FAIL: Different first boundaries (jdk=" + jidx + ",icu=" + iidx + ") for text:\n" + text);
195         }
196         while (true) {
197             jidx = jdkBrk.next();
198             iidx = icuBrk.next();
199             if (jidx != iidx) {
200                 errln("FAIL: Different boundaries (jdk=" + jidx + ",icu=" + iidx + "direction=forward) for text:\n" + text);
201             }
202             if (jidx == BreakIterator.DONE) {
203                 break;
204             }
205         }
206 
207         // Backward
208         jidx = jdkBrk.last();
209         iidx = jdkBrk.last();
210         if (jidx != iidx) {
211             errln("FAIL: Different last boundaries (jdk=" + jidx + ",icu=" + iidx + ") for text:\n" + text);
212         }
213         while (true) {
214             jidx = jdkBrk.previous();
215             iidx = icuBrk.previous();
216             if (jidx != iidx) {
217                 errln("FAIL: Different boundaries (jdk=" + jidx + ",icu=" + iidx + "direction=backward) for text:\n" + text);
218             }
219             if (jidx == BreakIterator.DONE) {
220                 break;
221             }
222         }
223     }
224 }
225