1 /************************************************************************* 2 * 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * Copyright 2000, 2010 Oracle and/or its affiliates. 6 * 7 * OpenOffice.org - a multi-platform office productivity suite 8 * 9 * This file is part of OpenOffice.org. 10 * 11 * OpenOffice.org is free software: you can redistribute it and/or modify 12 * it under the terms of the GNU Lesser General Public License version 3 13 * only, as published by the Free Software Foundation. 14 * 15 * OpenOffice.org is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU Lesser General Public License version 3 for more details 19 * (a copy is included in the LICENSE file that accompanied this code). 20 * 21 * You should have received a copy of the GNU Lesser General Public License 22 * version 3 along with OpenOffice.org. If not, see 23 * <http://www.openoffice.org/license.html> 24 * for a copy of the LGPLv3 License. 25 * 26 ************************************************************************/ 27 28 package ifc.i18n; 29 30 import java.text.Collator; 31 32 import lib.MultiMethodTest; 33 34 import com.sun.star.i18n.CollatorOptions; 35 import com.sun.star.i18n.XCollator; 36 import com.sun.star.lang.Locale; 37 38 /** 39 * Testing <code>com.sun.star.i18n.XCollator</code> 40 * interface methods : 41 * <ul> 42 * <li><code> compareSubstring()</code></li> 43 * <li><code> compareString()</code></li> 44 * <li><code> loadDefaultCollator()</code></li> 45 * <li><code> loadCollatorAlgorithm()</code></li> 46 * <li><code> listCollatorAlgorithms()</code></li> 47 * <li><code> loadCollatorAlgorithmWithEndUserOption()</code></li> 48 * <li><code> listCollatorOptions()</code></li> 49 * </ul> <p> 50 * Test is <b> NOT </b> multithread compilant. <p> 51 * @see com.sun.star.i18n.XCollator 52 */ 53 public class _XCollator extends MultiMethodTest { 54 public XCollator oObj = null; 55 private String[] alg = null ; 56 private int[] opt = null ; 57 Locale loc = new Locale("en", "EN", ""); 58 59 /** 60 * Just retrieves a list of algorithms. <p> 61 * Has <b>OK</b> status if non-zero length array returned. 62 */ 63 public void _listCollatorAlgorithms() { 64 alg = oObj.listCollatorAlgorithms(loc) ; 65 log.println("Collator algorithms :"); 66 if (alg != null) { 67 for (int i = 0; i < alg.length; i++) { 68 log.println(" '" + alg[i] + "'") ; 69 } 70 tRes.tested("listCollatorAlgorithms()", alg.length > 0) ; 71 } else { 72 tRes.tested("listCollatorAlgorithms()", false) ; 73 } 74 } 75 76 /** 77 * Just gets a list of options for some collator. <p> 78 * Has <b>OK</b> status if not null value returned.<p> 79 * The following method tests are to be completed successfully before : 80 * <ul> 81 * <li> <code> listCollatorAlgorithms </code> : to have some 82 * algorithm name. </li> 83 * </ul> 84 */ 85 public void _listCollatorOptions() { 86 requiredMethod("listCollatorAlgorithms()") ; 87 opt = oObj.listCollatorOptions(alg[0]) ; 88 log.println("Collator '" + alg[0] + "' options :"); 89 if (opt != null) { 90 for (int i = 0; i < opt.length; i++) { 91 log.println(" " + opt[i]) ; 92 } 93 tRes.tested("listCollatorOptions()", true) ; 94 } else { 95 tRes.tested("listCollatorOptions()", false) ; 96 } 97 } 98 99 /** 100 * Calls the method with no options and with options(IGNORE_CASE), 101 * compares strings.<p> 102 * Has <b>OK</b> status if compareString() returned correct values. 103 */ 104 public void _loadDefaultCollator() { 105 oObj.loadDefaultCollator(loc, 0); 106 boolean res = oObj.compareString("A", "a") != 0; 107 oObj.loadDefaultCollator(loc, 108 CollatorOptions.CollatorOptions_IGNORE_CASE); 109 res &= oObj.compareString("a", "A") == 0; 110 tRes.tested("loadDefaultCollator()", res) ; 111 } 112 113 /** 114 * Calls the method with no options and with options(IGNORE_CASE), 115 * compares strings.<p> 116 * Has <b>OK</b> status if compareString() returned correct values. 117 * The following method tests are to be completed successfully before : 118 * <ul> 119 * <li> <code> listCollatorAlgorithms </code> : to have some 120 * algorithm name. </li> 121 * </ul> 122 */ 123 public void _loadCollatorAlgorithm() { 124 requiredMethod("listCollatorAlgorithms()"); 125 oObj.loadCollatorAlgorithm(alg[0], loc, 126 CollatorOptions.CollatorOptions_IGNORE_CASE); 127 boolean res = oObj.compareString("A", "a") == 0; 128 oObj.loadCollatorAlgorithm(alg[0], loc, 0); 129 res &= oObj.compareString("a", "A") != 0; 130 tRes.tested("loadCollatorAlgorithm()", res); 131 } 132 133 /** 134 * Calls the method with no options and with options(IGNORE_CASE), 135 * compares strings.<p> 136 * Has <b>OK</b> status if compareString() returned correct values. 137 * The following method tests are to be completed successfully before : 138 * <ul> 139 * <li> <code> listCollatorAlgorithms </code> : to have some 140 * algorithm name. </li> 141 * </ul> 142 */ 143 public void _loadCollatorAlgorithmWithEndUserOption() { 144 requiredMethod("listCollatorAlgorithms()"); 145 oObj.loadCollatorAlgorithmWithEndUserOption(alg[0], loc, 146 new int[] {0}); 147 boolean res = oObj.compareString("A", "a") != 0; 148 oObj.loadCollatorAlgorithmWithEndUserOption(alg[0], loc, 149 new int[] {CollatorOptions.CollatorOptions_IGNORE_CASE}); 150 res = oObj.compareString("A", "a") == 0; 151 tRes.tested("loadCollatorAlgorithmWithEndUserOption()", res); 152 } 153 154 /** 155 * Test is performed for locales : en, ru, ja, zh, ko. 156 * Default collator is loaded for each locale. Then collation 157 * is performed for different combination of symbols from range of 158 * this locale.<p> 159 * Has <b>OK</b> status if comparing of different strings 160 * returns not 0 value, then comparing in the opposite 161 * order returns value with opposite sign, and comparing 162 * of two equal strings returns 0. The such comparing is performed 163 * for one character strings. 164 */ 165 public void _compareSubstring() { 166 boolean result = true ; 167 char[] chars = new char[2] ; 168 Collator col = null ; 169 170 log.println(" #### Testing English locale ####") ; 171 oObj.loadDefaultCollator(loc, 0) ; 172 col = Collator.getInstance(new java.util.Locale("en", "EN")) ; 173 for (char ch = 0x0020; ch < 0x007F; ch ++) { 174 chars[0] = ch ; chars[1] = (char) (ch + 1) ; 175 result &= testCompareSubstring(chars, col) ; 176 } 177 178 log.println(" #### Testing Russian locale ####") ; 179 oObj.loadDefaultCollator( 180 new com.sun.star.lang.Locale("ru", "RU", ""), 0) ; 181 col = Collator.getInstance(new java.util.Locale("ru", "RU")) ; 182 for (char ch = 0x0410; ch < 0x0450; ch ++) { 183 chars[0] = ch ; chars[1] = (char) (ch + 1) ; 184 result &= testCompareSubstring(chars, col) ; 185 } 186 187 log.println(" #### Testing Japan locale ####") ; 188 oObj.loadDefaultCollator( 189 new com.sun.star.lang.Locale("ja", "JP", ""), 0) ; 190 col = Collator.getInstance(new java.util.Locale("ja", "JP")) ; 191 for (char ch = 0x4E00; ch < 0x4EFD; ch ++) { 192 chars[0] = ch ; chars[1] = (char) (ch + 1) ; 193 result &= testCompareSubstring(chars, col) ; 194 } 195 196 log.println(" #### Testing China locale ####") ; 197 oObj.loadDefaultCollator(new Locale("zh", "CN", ""), 0) ; 198 col = Collator.getInstance(new java.util.Locale("zh", "CN")) ; 199 for (char ch = 0x4E00; ch < 0x4EFD; ch ++) { 200 chars[0] = ch ; chars[1] = (char) (ch + 1) ; 201 result &= testCompareSubstring(chars, col) ; 202 } 203 204 log.println(" #### Testing Korean locale ####") ; 205 oObj.loadDefaultCollator(new Locale("ko", "KR", ""), 0) ; 206 col = Collator.getInstance(new java.util.Locale("ko", "KR")) ; 207 for (char ch = 0x4E00; ch < 0x4EFD; ch ++) { 208 chars[0] = ch ; chars[1] = (char) (ch + 1) ; 209 result &= testCompareSubstring(chars, col) ; 210 } 211 212 tRes.tested("compareSubstring()", result) ; 213 } 214 215 /** 216 * Test is performed for locales : en, ru, ja, zh, ko. 217 * Default collator is loaded for each locale. Then collation 218 * is performed for different combination of symbols from range of 219 * this locale.<p> 220 * Has <b>OK</b> status if comparing of different strings 221 * returns not 0 value, then comparing in the opposite 222 * order returns value with opposite sign, and comparing 223 * of two equal strings returns 0. The such comparing is performed 224 * for one character strings. 225 */ 226 public void _compareString() { 227 boolean result = true ; 228 char[] chars = new char[2] ; 229 Collator col = null ; 230 log.println(" #### Testing English locale ####") ; 231 oObj.loadDefaultCollator( 232 new com.sun.star.lang.Locale("en", "EN", ""), 0) ; 233 col = Collator.getInstance(new java.util.Locale("en", "EN")) ; 234 for (char ch = 0x0020; ch < 0x007F; ch ++) { 235 chars[0] = ch ; chars[1] = (char) (ch + 1) ; 236 result &= testCompareString(chars, col) ; 237 } 238 239 log.println(" #### Testing Russian locale ####") ; 240 oObj.loadDefaultCollator( 241 new com.sun.star.lang.Locale("ru", "RU", ""), 0) ; 242 col = Collator.getInstance(new java.util.Locale("ru", "RU")) ; 243 for (char ch = 0x0410; ch < 0x0450; ch ++) { 244 chars[0] = ch ; chars[1] = (char) (ch + 1) ; 245 result &= testCompareString(chars, col) ; 246 } 247 248 log.println(" #### Testing Japan locale ####") ; 249 oObj.loadDefaultCollator( 250 new com.sun.star.lang.Locale("ja", "JP", ""), 0) ; 251 col = Collator.getInstance(new java.util.Locale("ja", "JP")) ; 252 for (char ch = 0x4E00; ch < 0x4EFD; ch ++) { 253 chars[0] = ch ; chars[1] = (char) (ch + 1) ; 254 result &= testCompareString(chars, col) ; 255 } 256 257 log.println(" #### Testing China locale ####") ; 258 oObj.loadDefaultCollator(new Locale("zh", "CN", ""), 0) ; 259 col = Collator.getInstance(new java.util.Locale("zh", "CN")) ; 260 for (char ch = 0x4E00; ch < 0x4EFD; ch ++) { 261 chars[0] = ch ; chars[1] = (char) (ch + 1) ; 262 result &= testCompareString(chars, col) ; 263 } 264 265 log.println(" #### Testing Korean locale ####") ; 266 oObj.loadDefaultCollator(new Locale("ko", "KR", ""), 0) ; 267 col = Collator.getInstance(new java.util.Locale("ko", "KR")) ; 268 for (char ch = 0x4E00; ch < 0x4EFD; ch ++) { 269 chars[0] = ch ; chars[1] = (char) (ch + 1) ; 270 result &= testCompareString(chars, col) ; 271 } 272 273 tRes.tested("compareString()", result) ; 274 } 275 276 277 /** 278 * Testing compareString() method. At first method is testing single chars 279 * comparing, then strings comparing. 280 * @param locChar sequence of at list two characters of a given locale 281 * to be used in comparing. 282 * @param col Collator for a given locale 283 * @return true if: 284 * <ol> 285 * <li> if comparing of two identical characters returns zero</li> 286 * <li> if comparing of two different characters returns non zero</li> 287 * <li> if comparing of two identical strings, composed of given chars 288 * returns zero</li> 289 * <li> if comparing of two different strings, composed of given chars 290 * returns non zero</li> 291 * </ol> 292 */ 293 public boolean testCompareString(char[] locChar, Collator col) { 294 boolean result = true; 295 int res; 296 String msg = ""; 297 298 String char0 = "_"+new String(new char[] {locChar[0]}); 299 String char1 = "_"+new String(new char[] {locChar[1]}); 300 res = oObj.compareString(char0 , char0) ; 301 if (res != 0) { 302 msg += " Testing collation of single equal characters (" 303 + toUnicode(char0) + ") ... FAILED\n" ; 304 } 305 result &= res == 0 ; 306 res = oObj.compareString(char0, char1) ; 307 if (res == 0) { 308 msg += " Testing collation of single different" + 309 " characters (" + toUnicode(char0+char1) + 310 ") ... FAILED (0 returned)\n" ; 311 msg += " Java collator returned " + 312 col.compare(char0, char1) + "\n" ; 313 result = false ; 314 } else { // opposite order - sum of results must be 0 315 res += oObj.compareString(char1, char0) ; 316 if (res != 0) { 317 msg += " Testing collation of single different" + 318 " characters (" + toUnicode(char0+char1) + 319 ") ... FAILED\n" ; 320 } 321 result &= res == 0 ; 322 } 323 324 String str1 = new String(new char[] {locChar[0], locChar[0], 325 locChar[1], locChar[1], locChar[1]}) ; 326 String str2 = new String(new char[] {locChar[0], locChar[0], 327 locChar[0], locChar[1], locChar[1]}) ; 328 329 res = oObj.compareString(str1 , str1) ; 330 if (res != 0) { 331 msg += " Testing collation of equal strings (" + 332 toUnicode(str1) + ") ... FAILED\n" ; 333 } 334 result &= res == 0 ; 335 res = oObj.compareString(str1, str2) ; 336 if (res == 0) { 337 msg += " Testing collation of different strings ((" + 338 toUnicode(str1) + "),(" + toUnicode(str2) + 339 ")) ... FAILED (0 returned)\n" ; 340 msg += " Java collator returned " + 341 col.compare(str1, str2) + "\n" ; 342 result = false ; 343 } else { // opposite order - sum of results must be 344 res += oObj.compareString(str2, str1) ; 345 if (res != 0) { 346 msg += " Testing collation of different strings ((" + 347 toUnicode(str1) + "),(" + toUnicode(str2) + 348 ")) ... FAILED\n" ; 349 } 350 result &= res == 0 ; 351 } 352 353 if (!result) { 354 log.println(msg) ; 355 } 356 return result ; 357 } 358 359 360 /** 361 * Testing compareSubstring() method. Method is testing substrings comparing. 362 * @param locChar sequence of at list two characters of a given locale 363 * to be used in comparing. 364 * @param col Collator for a given locale 365 * @return true if: 366 * <ol> 367 * <li> if comparing of two identical substrings of strings, composed 368 * of given chars returns zero</li> 369 * <li> if comparing of two different substrings of strings, composed 370 * of given chars returns non zero</li> 371 * </ol> 372 */ 373 public boolean testCompareSubstring(char[] locChar, Collator col) { 374 boolean result = true ; 375 int res ; 376 String msg = "" ; 377 378 String str1 = new String(new char[] {locChar[0], locChar[0], 379 locChar[1], locChar[1], locChar[1]}) ; 380 String str2 = new String(new char[] {locChar[0], locChar[0], 381 locChar[0], locChar[1], locChar[1]}) ; 382 383 res = oObj.compareSubstring(str1, 1, 2 , str2, 2, 2) ; 384 if (res != 0) { 385 msg += " Testing collation of equal substrings (" + 386 toUnicode(str1) + ") ... FAILED\n" ; 387 } 388 result &= res == 0 ; 389 res = oObj.compareSubstring(str1, 1, 2, str2, 1, 2) ; 390 if (res == 0) { 391 msg += " Testing collation of different strings ((" + 392 toUnicode(str1.substring(1, 3)) + "),(" + 393 toUnicode(str2.substring(1, 3)) 394 + ")) ... FAILED (0 returned)\n" ; 395 msg += " Java collator returned " + col.compare 396 (str1.substring(1, 3), str2.substring(1, 3)) + "\n" ; 397 result = false ; 398 } else { // opposite order - sum of results must be 399 res += oObj.compareSubstring(str2, 1, 2, str1, 1, 2) ; 400 if (res != 0) { 401 msg += " Testing collation of different strings ((" + 402 toUnicode(str1) + "),(" + toUnicode(str2) + 403 ")) ... FAILED\n" ; 404 } 405 result &= res == 0 ; 406 } 407 408 if (!result) { 409 log.println(msg) ; 410 } 411 return result ; 412 } 413 414 415 /** 416 * Transforms string to unicode hex codes. 417 * @param str String to be transformed 418 */ 419 public String toUnicode(String str) { 420 char[] chars = str.toCharArray() ; 421 String res = "" ; 422 for (int i = 0; i < chars.length; i++) { 423 if (i != 0) res += "," ; 424 res += Integer.toHexString(chars[i]) ; 425 } 426 return res ; 427 } 428 429 } 430 431