1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 23 24 package api.i18n; 25 26 import com.sun.star.i18n.TransliterationModules; 27 import com.sun.star.i18n.TransliterationModulesNew; 28 import com.sun.star.i18n.TransliterationType; 29 import com.sun.star.i18n.XTransliteration; 30 import com.sun.star.lang.Locale; 31 import com.sun.star.uno.UnoRuntime; 32 import com.sun.star.uno.XComponentContext; 33 import org.junit.After; 34 import org.junit.AfterClass; 35 import org.junit.Before; 36 import org.junit.BeforeClass; 37 import org.junit.Assert; 38 import org.junit.Test; 39 import org.openoffice.test.uno.UnoApp; 40 41 /** 42 * Testing <code>com.sun.star.i18n.XTransliteration</code> 43 * interface methods : 44 * <ul> 45 * <li><code> getName()</code></li> 46 * <li><code> getType()</code></li> 47 * <li><code> loadModule()</code></li> 48 * <li><code> loadModuleNew()</code></li> 49 * <li><code> loadModuleByImplName()</code></li> 50 * <li><code> loadModulesByImplNames()</code></li> 51 * <li><code> getAvailableModules()</code></li> 52 * <li><code> transliterate()</code></li> 53 * <li><code> folding()</code></li> 54 * <li><code> equals()</code></li> 55 * <li><code> transliterateRange()</code></li> 56 * </ul> <p> 57 * Test is <b> NOT </b> multithread compliant. <p> 58 * @see com.sun.star.i18n.XTransliteration 59 */ 60 public class XTransliterationTest { 61 private static final UnoApp app = new UnoApp(); 62 63 private XComponentContext xContext = null; 64 public XTransliteration oObj = null; 65 private String[] mod = null ; 66 private Locale loc = new Locale("en", "EN", "") ; 67 68 69 // setup and close connections 70 @BeforeClass setUpConnection()71 public static void setUpConnection() throws Exception 72 { 73 app.start(); 74 } 75 76 @AfterClass tearDownConnection()77 public static void tearDownConnection() throws InterruptedException, com.sun.star.uno.Exception 78 { 79 app.close(); 80 } 81 82 @Before before()83 public void before() throws Exception { 84 xContext = app.getComponentContext(); 85 oObj = UnoRuntime.queryInterface( 86 XTransliteration.class, 87 xContext.getServiceManager().createInstanceWithContext("com.sun.star.i18n.Transliteration", xContext) 88 ); 89 } 90 91 /** 92 * Gets all available transliteration modules. <p> 93 * Has <b>OK</b> status if array returned has at least 94 * one module name. 95 */ 96 @Test _getAvailableModules()97 public void _getAvailableModules() { 98 mod = oObj.getAvailableModules(loc, TransliterationType.ONE_TO_ONE); 99 100 if (mod != null) { 101 System.out.println("Available modules :") ; 102 for (int i = 0; i < mod.length; i++) { 103 System.out.println(" '" + mod[i] + "'") ; 104 } 105 } else { 106 System.out.println("!!! NULL returned !!!") ; 107 } 108 109 Assert.assertTrue("getAvailableModules()", mod != null && mod.length > 0) ; 110 } 111 112 /** 113 * Calls the method for load IGNORE_CASE module and checks the name returned 114 * by the method <code>getName</code>. <p> 115 * Has <b>OK</b> status if the method <code>getName</code> returns the 116 * string "case ignore (generic)". 117 */ 118 @Test _loadModule()119 public void _loadModule() { 120 System.out.println("Load module IGNORE_CASE"); 121 oObj.loadModule(TransliterationModules.IGNORE_CASE, loc); 122 123 String name = oObj.getName(); 124 boolean res = name.equals("case ignore (generic)"); 125 System.out.println("getName return: " + name); 126 127 Assert.assertTrue("loadModule()", res ); 128 } 129 130 /** 131 * Loads <code>LOWERCASE_UPPERCASE</code> module and checks the current 132 * name of object. <p> 133 * 134 * Has <b>OK</b> status if the name of the object is equals to 135 * 'lower_to_upper(generic)' 136 */ 137 @Test _loadModuleNew()138 public void _loadModuleNew() { 139 boolean result = true ; 140 141 oObj.loadModuleNew( 142 new TransliterationModulesNew[] 143 {TransliterationModulesNew.LOWERCASE_UPPERCASE}, loc); 144 145 String name = oObj.getName(); 146 result = name.equals("lower_to_upper(generic)"); 147 System.out.println("getName return: " + name); 148 149 Assert.assertTrue("loadModuleNew()", result); 150 } 151 152 /** 153 * Calls the method for load LOWERCASE_UPPERCASE module and 154 * checks the name returned by the method <code>getName</code>. <p> 155 * Has <b>OK</b> status if the method <code>getName</code> returns the 156 * string "lower_to_upper(generic)". 157 */ 158 @Test _loadModuleByImplName()159 public void _loadModuleByImplName() { 160 System.out.println("Load module LOWERCASE_UPPERCASE"); 161 oObj.loadModuleByImplName("LOWERCASE_UPPERCASE", loc); 162 163 String name = oObj.getName(); 164 boolean res = name.equals("lower_to_upper(generic)"); 165 System.out.println("getName return: " + name); 166 167 Assert.assertTrue("loadModuleByImplName()", res); 168 } 169 170 /** 171 * Calls the method for load UPPERCASE_LOWERCASE module and 172 * checks the name returned by the method <code>getName</code>. <p> 173 * Has <b>OK</b> status if the method <code>getName</code> returns the 174 * string "upper_to_lower(generic)". 175 */ 176 @Test _loadModulesByImplNames()177 public void _loadModulesByImplNames() { 178 System.out.println("Load module UPPERCASE_LOWERCASE"); 179 oObj.loadModulesByImplNames(new String[]{"UPPERCASE_LOWERCASE"}, loc); 180 181 String name = oObj.getName(); 182 boolean res = name.equals("upper_to_lower(generic)"); 183 System.out.println("getName return: " + name); 184 185 Assert.assertTrue("loadModulesByImplNames()", res); 186 } 187 188 /** 189 * Loads <code>LOWERCASE_UPPERCASE</code> module and checks current type. 190 * <p>Has <b>OK</b> status if the type is <code>ONE_TO_ONE</code> 191 */ 192 @Test _getType()193 public void _getType() { 194 oObj.loadModule(TransliterationModules.LOWERCASE_UPPERCASE, loc); 195 boolean result = oObj.getType() == TransliterationType.ONE_TO_ONE; 196 Assert.assertTrue("getType()", result); 197 } 198 199 /** 200 * Loads UPPERCASE_LOWERCASE module and 201 * checks the name returned by the method <code>getName</code>. <p> 202 * 203 * Has <b>OK</b> status if the method <code>getName</code> returns the 204 * string "upper_to_lower(generic)". 205 */ 206 @Test _getName()207 public void _getName() { 208 oObj.loadModule(TransliterationModules.LOWERCASE_UPPERCASE, loc); 209 210 String name = oObj.getName(); 211 boolean res = name.equals("lower_to_upper(generic)"); 212 System.out.println("getName return: " + name); 213 214 Assert.assertTrue("getName()", res); 215 } 216 217 /** 218 * First loads <code>LOWERCASE_UPPERCASE</code> module. 219 * Then tries to transliterate (make uppercase) a substring. <p> 220 * Has <b>OK</b> status if all chars were made uppercase, 221 * and array returned has size as substring length, and its 222 * elements are positions of substring characters in the source 223 * string. 224 */ 225 @Test _transliterate()226 public void _transliterate() { 227 oObj.loadModule(TransliterationModules.LOWERCASE_UPPERCASE, loc); 228 229 int[][] offs = new int[1][] ; 230 231 String out = oObj.transliterate("AaBbCc", 1, 4, offs) ; 232 233 boolean result = "ABBC".equals(out) && offs[0].length == 4 && 234 offs[0][0] == 1 && 235 offs[0][1] == 2 && 236 offs[0][2] == 3 && 237 offs[0][3] == 4 ; 238 239 Assert.assertTrue("transliterate()", result) ; 240 } 241 242 243 /** 244 * First loads <code>LOWERCASE_UPPERCASE</code> module. 245 * Tries to transliterate a range of two characters. <p> 246 * Has <b>OK</b> status if the appropriate String array 247 * returned (not null, length = 4, with two ranges 248 * (a, i), (A, I) in any order). 249 */ 250 @Test _transliterateRange()251 public void _transliterateRange() { 252 oObj.loadModule(TransliterationModules.IGNORE_CASE, loc); 253 254 String[] out = oObj.transliterateRange("a", "i") ; 255 256 System.out.println("transliterateRange return:"); 257 for(int i = 0; i < out.length; i++) { 258 System.out.println(out[i]); 259 } 260 261 boolean bOK = out != null && 262 out.length == 4 && 263 ("A".equals(out[0]) && "I".equals(out[1]) && 264 "a".equals(out[2]) && "i".equals(out[3])) || 265 ("a".equals(out[0]) && "i".equals(out[1]) && 266 "A".equals(out[2]) && "I".equals(out[3])) ; 267 268 if (!bOK) { 269 System.out.println("Unexpected range returned :"); 270 for (int i = 0; i < out.length; i++) { 271 System.out.print("'" + out[i] +"', "); 272 } 273 System.out.println(); 274 } 275 276 Assert.assertTrue("transliterateRange()", bOK); 277 } 278 279 /** 280 * This method is used internally by <code>equals</code> 281 * method so it indirectly tested in this method. <p> 282 * Always has <b>OK</b> status. 283 */ 284 @Test _folding()285 public void _folding() { 286 oObj.loadModule(TransliterationModules.LOWERCASE_UPPERCASE, loc); 287 288 int[][] offs = new int[1][] ; 289 290 String out = oObj.folding("AaBbCc", 1, 4, offs) ; 291 292 boolean result = "ABBC".equals(out) && offs[0].length == 4 && 293 offs[0][0] == 1 && 294 offs[0][1] == 2 && 295 offs[0][2] == 3 && 296 offs[0][3] == 4 ; 297 298 299 Assert.assertTrue("folding()", result) ; 300 } 301 302 303 /** 304 * First loads <code>LOWERCASE_UPPERCASE</code> module. 305 * Tries to compare two equal substrings. <p> 306 * Has <b>OK</b> status if the method returned <code>true</code>. 307 */ 308 @Test _equals()309 public void _equals() { 310 oObj.loadModule(TransliterationModules.LOWERCASE_UPPERCASE, loc); 311 312 int[] match1 = new int[1], 313 match2 = new int[1] ; 314 315 boolean res = oObj.equals("aAbBcC", 1, 3, match1, "aAbBcC", 1, 316 3, match2) ; 317 318 System.out.println("Returned : " + res + " Match1 = " + match1[0] + 319 " Match2 = " + match2[0]) ; 320 321 Assert.assertTrue("equals()", res) ; 322 } 323 324 /** 325 * Test performed for sets of equal substrings, not equal 326 * substrings, and with out of bounds offset and length 327 * parameters.<p> 328 * 329 * Has <b>OK</b> status if comparings of equal substrings 330 * always return 0, if comparisons of none equal returns 331 * proper value according to lexicographical order and if 332 * comparisons with invalid parameters return none 0 value. 333 */ 334 @Test _compareSubstring()335 public void _compareSubstring() { 336 oObj.loadModule(TransliterationModules.LOWERCASE_UPPERCASE, loc); 337 boolean result = true ; 338 339 // substrings below must be equal 340 result &= testSubstring("", 0, 0, "", 0, 0, 0) ; 341 result &= testSubstring("aa", 1, 0, "", 0, 0, 0) ; 342 result &= testSubstring("aa", 1, 0, "aa", 2, 0, 0) ; 343 result &= testSubstring("a", 0, 1, "a", 0, 1, 0) ; 344 result &= testSubstring("ab", 0, 2, "ab", 0, 2, 0) ; 345 result &= testSubstring("abc", 1, 2, "abc", 1, 2, 0) ; 346 result &= testSubstring("abcdef", 0, 3, "123abc", 3, 3, 0) ; 347 result &= testSubstring("abcdef", 1, 1, "123abc", 4, 1, 0) ; 348 349 // substrings below must NOT be equal 350 result &= testSubstring("a", 0, 1, "a", 0, 0, 1) ; 351 result &= testSubstring("aaa", 1, 1, "", 0, 0, 1) ; 352 result &= testSubstring("bbb", 2, 1, "aaa", 2, 1, 1) ; 353 result &= testSubstring("abc", 0, 3, "abc", 0, 2, 1) ; 354 result &= testSubstring("bbc", 1, 2, "bbc", 0, 2, 1) ; 355 356 // testing with wrong offsets and lengths 357 358 Assert.assertTrue("compareSubstring()", result) ; 359 } 360 361 /** 362 * Performs tesing of two substrings. Also testing of opposite 363 * substrings order performed. 364 * @return <code>true</code> if substrings are equal and returned 365 * value is 0 for both orders, 366 * if substrings are different and expected value 367 * returned for direct order and opposite value returned for 368 * opposite order. 369 */ testSubstring(String str1, int p1, int len1, String str2, int p2, int len2, int expRes)370 private boolean testSubstring(String str1, int p1, int len1, 371 String str2, int p2, int len2, int expRes) { 372 373 boolean ret = true ; 374 375 int res = -666 ; 376 try { 377 res = oObj.compareSubstring(str1, p1, len1, str2, p2, len2); 378 } catch (java.lang.NullPointerException e) { 379 System.out.println("Exception while method calling occurs :" + e); 380 } 381 382 if (res != expRes) { 383 System.out.print("Comparing FAILED; return: " + res + ", expected: " + 384 expRes + " "); 385 ret = false ; 386 } else { 387 System.out.print("Comparing OK : "); 388 } 389 System.out.println("('" + str1 + "', " + p1 + ", " + len1 + ", '" + 390 str2 + "', " + p2 + ", " + len2 + ")"); 391 392 res = -666 ; 393 try { 394 res = oObj.compareSubstring(str2, p2, len2, str1, p1, len1); 395 } catch (java.lang.NullPointerException e) { 396 System.out.println("Exception while method calling occurs :" + e); 397 } 398 399 if (res != -expRes) { 400 System.out.print("Comparing FAILED; return: " + res + ", expected: " + 401 -expRes + " "); 402 ret = false ; 403 } else { 404 System.out.print("Comparing OK :"); 405 } 406 System.out.println("('" + str2 + "', " + p2 + ", " + len2 + ", '" + 407 str1 + "', " + p1 + ", " + len1 + ")"); 408 409 return ret ; 410 } 411 412 /** 413 * Test performed for sets of equal strings and not equal 414 * strings.<p> 415 * 416 * Has <b>OK</b> status if comparings of equal strings 417 * always return 0 and if comparisons of none equal returns 418 * proper value according to lexicographical order . 419 */ 420 @Test _compareString()421 public void _compareString() { 422 oObj.loadModule(TransliterationModules.LOWERCASE_UPPERCASE, loc); 423 boolean result = true ; 424 425 result &= testString("", "", 0) ; 426 result &= testString("a", "", 1) ; 427 result &= testString("a", "a", 0) ; 428 result &= testString("A", "a", 0) ; 429 result &= testString("b", "a", 1) ; 430 result &= testString("\n", "\n", 0) ; 431 result &= testString("\n", "\t", 1) ; 432 result &= testString("aaa", "aaa", 0) ; 433 result &= testString("aaA", "aaa", 0) ; 434 result &= testString("aaa", "aa", 1) ; 435 result &= testString("ab", "aaa", 1) ; 436 result &= testString("aba", "aa", 1) ; 437 result &= testString("aaa\t\na", "aaa\t\na", 0) ; 438 result &= testString("aaa\t\nb", "aaa\t\na", 1) ; 439 440 Assert.assertTrue("compareString()", result) ; 441 } 442 443 /** 444 * Performs tesing of two strings. If the expected value is not 0 445 * (i.e. strings are not equal), then also testing of opposite 446 * strings order performed. 447 * @return <code>true</code> if strings are equal and returned 448 * value is 0, if strings are different and expected value 449 * returned for direct order and opposite value returned for 450 * opposite order. 451 */ testString(String str1, String str2, int expRes)452 protected boolean testString(String str1, String str2, int expRes) { 453 if (expRes == 0) return testString(str1, str2, expRes, false) ; 454 return testString(str1, str2, expRes, true) ; 455 } 456 testString(String str1, String str2, int expRes, boolean testReverse)457 private boolean testString(String str1, String str2, int expRes, 458 boolean testReverse) { 459 460 boolean ret = true ; 461 462 int res = -666 ; 463 try { 464 res = oObj.compareString(str1, str2); 465 } catch (java.lang.NullPointerException e) { 466 System.out.println("Exception while method calling occurs :" + e); 467 } 468 469 if (res == expRes) { 470 System.out.println("Comparing of '" + str1 + "' and '" + str2 + "' OK" ); 471 } else { 472 System.out.println("Comparing of '" + str1 + "' and '" + str2 + 473 "' FAILED; return: " + res + ", expected: " + expRes); 474 ret = false ; 475 } 476 477 if (!testReverse) return ret ; 478 479 res = -666 ; 480 try { 481 res = oObj.compareString(str2, str1); 482 } catch (java.lang.NullPointerException e) { 483 System.out.println("Exception while method calling occurs :" + e); 484 } 485 486 if (res == -expRes) { 487 System.out.println("Comparing of '" + str2 + "' and '" + str1 + "' OK" ); 488 } else { 489 System.out.println("Comparing of '" + str2 + "' and '" + str1 + 490 "' FAILED; return: " + res + ", expected: " + -expRes); 491 ret = false ; 492 } 493 494 return ret ; 495 } 496 } 497