2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 import lib
.MultiMethodTest
;
23 import com
.sun
.star
.i18n
.TransliterationModules
;
24 import com
.sun
.star
.i18n
.TransliterationModulesNew
;
25 import com
.sun
.star
.i18n
.TransliterationType
;
26 import com
.sun
.star
.i18n
.XTransliteration
;
27 import com
.sun
.star
.lang
.Locale
;
30 * Testing <code>com.sun.star.i18n.XTransliteration</code>
33 * <li><code> getName()</code></li>
34 * <li><code> getType()</code></li>
35 * <li><code> loadModule()</code></li>
36 * <li><code> loadModuleNew()</code></li>
37 * <li><code> loadModuleByImplName()</code></li>
38 * <li><code> loadModulesByImplNames()</code></li>
39 * <li><code> getAvailableModules()</code></li>
40 * <li><code> transliterate()</code></li>
41 * <li><code> folding()</code></li>
42 * <li><code> equals()</code></li>
43 * <li><code> transliterateRange()</code></li>
45 * Test is <b> NOT </b> multithread compliant. <p>
46 * @see com.sun.star.i18n.XTransliteration
48 public class _XTransliteration
extends MultiMethodTest
{
50 public XTransliteration oObj
= null;
51 private final Locale loc
= new Locale("en", "EN", "") ;
54 * Gets all available transliteration modules. <p>
55 * Has <b>OK</b> status if array returned has at least
58 public void _getAvailableModules() {
59 String
[] mod
= oObj
.getAvailableModules(loc
, TransliterationType
.ONE_TO_ONE
);
62 log
.println("Available modules :") ;
63 for (int i
= 0; i
< mod
.length
; i
++) {
64 log
.println(" '" + mod
[i
] + "'") ;
67 log
.println("!!! NULL returned !!!") ;
70 tRes
.tested("getAvailableModules()", mod
!= null && mod
.length
> 0) ;
74 * Calls the method for load IGNORE_CASE module and checks the name returned
75 * by the method <code>getName</code>. <p>
76 * Has <b>OK</b> status if the method <code>getName</code> returns the
77 * string "case ignore (generic)".
79 public void _loadModule() {
80 log
.println("Load module IGNORE_CASE");
81 oObj
.loadModule(TransliterationModules
.IGNORE_CASE
, loc
);
83 String name
= oObj
.getName();
84 boolean res
= name
.equals("case ignore (generic)");
85 log
.println("getName return: " + name
);
87 tRes
.tested("loadModule()", res
);
91 * Loads <code>LOWERCASE_UPPERCASE</code> module and checks the current
94 * Has <b>OK</b> status if the name of the object is equals to
95 * 'lower_to_upper(generic)'
97 public void _loadModuleNew() {
98 boolean result
= true ;
101 new TransliterationModulesNew
[]
102 {TransliterationModulesNew
.LOWERCASE_UPPERCASE
}, loc
);
104 String name
= oObj
.getName();
105 result
= name
.equals("lower_to_upper(generic)");
106 log
.println("getName return: " + name
);
108 tRes
.tested("loadModuleNew()", result
);
112 * Calls the method for load LOWERCASE_UPPERCASE module and
113 * checks the name returned by the method <code>getName</code>. <p>
114 * Has <b>OK</b> status if the method <code>getName</code> returns the
115 * string "lower_to_upper(generic)".
117 public void _loadModuleByImplName() {
118 log
.println("Load module LOWERCASE_UPPERCASE");
119 oObj
.loadModuleByImplName("LOWERCASE_UPPERCASE", loc
);
121 String name
= oObj
.getName();
122 boolean res
= name
.equals("lower_to_upper(generic)");
123 log
.println("getName return: " + name
);
125 tRes
.tested("loadModuleByImplName()", res
);
129 * Calls the method for load UPPERCASE_LOWERCASE module and
130 * checks the name returned by the method <code>getName</code>. <p>
131 * Has <b>OK</b> status if the method <code>getName</code> returns the
132 * string "upper_to_lower(generic)".
134 public void _loadModulesByImplNames() {
135 log
.println("Load module UPPERCASE_LOWERCASE");
136 oObj
.loadModulesByImplNames(new String
[]{"UPPERCASE_LOWERCASE"}, loc
);
138 String name
= oObj
.getName();
139 boolean res
= name
.equals("upper_to_lower(generic)");
140 log
.println("getName return: " + name
);
142 tRes
.tested("loadModulesByImplNames()", res
);
146 * Loads <code>LOWERCASE_UPPERCASE</code> module and checks current type.
147 * <p>Has <b>OK</b> status if the type is <code>ONE_TO_ONE</code>
149 public void _getType() {
150 oObj
.loadModule(TransliterationModules
.LOWERCASE_UPPERCASE
, loc
);
151 boolean result
= oObj
.getType() == TransliterationType
.ONE_TO_ONE
;
152 tRes
.tested("getType()", result
);
156 * Loads UPPERCASE_LOWERCASE module and
157 * checks the name returned by the method <code>getName</code>. <p>
159 * Has <b>OK</b> status if the method <code>getName</code> returns the
160 * string "upper_to_lower(generic)".
162 public void _getName() {
163 oObj
.loadModule(TransliterationModules
.LOWERCASE_UPPERCASE
, loc
);
165 String name
= oObj
.getName();
166 boolean res
= name
.equals("lower_to_upper(generic)");
167 log
.println("getName return: " + name
);
169 tRes
.tested("getName()", res
);
173 * First loads <code>LOWERCASE_UPPERCASE</code> module.
174 * Then tries to transliterate (make uppercase) a substring. <p>
175 * Has <b>OK</b> status if all chars were made uppercase,
176 * and array returned has size as substring length, and its
177 * elements are positions of substring characters in the source
180 public void _transliterate() {
181 oObj
.loadModule(TransliterationModules
.LOWERCASE_UPPERCASE
, loc
);
183 int[][] offs
= new int[1][] ;
185 String out
= oObj
.transliterate("AaBbCc", 1, 4, offs
) ;
187 boolean result
= "ABBC".equals(out
) && offs
[0].length
== 4 &&
193 tRes
.tested("transliterate()", result
) ;
198 * First loads <code>LOWERCASE_UPPERCASE</code> module.
199 * Tries to transliterate a range of two characters. <p>
200 * Has <b>OK</b> status if the appropriate String array
201 * returned (not null, length = 4, with two ranges
202 * (a, i), (A, I) in any order).
204 public void _transliterateRange() {
205 oObj
.loadModule(TransliterationModules
.IGNORE_CASE
, loc
);
207 String
[] out
= oObj
.transliterateRange("a", "i") ;
209 log
.println("transliterateRange return:");
210 for(int i
= 0; i
< out
.length
; i
++) {
214 boolean bOK
= out
.length
== 4 &&
215 ("A".equals(out
[0]) && "I".equals(out
[1]) &&
216 "a".equals(out
[2]) && "i".equals(out
[3])) ||
217 ("a".equals(out
[0]) && "i".equals(out
[1]) &&
218 "A".equals(out
[2]) && "I".equals(out
[3])) ;
221 log
.println("Unexpected range returned :");
222 for (int i
= 0; i
< out
.length
; i
++) {
223 log
.print("'" + out
[i
] +"', ");
228 tRes
.tested("transliterateRange()", bOK
);
232 * This method is used internally by <code>equals</code>
233 * method so it indirectly tested in this method. <p>
234 * Always has <b>OK</b> status.
236 public void _folding() {
237 oObj
.loadModule(TransliterationModules
.LOWERCASE_UPPERCASE
, loc
);
239 int[][] offs
= new int[1][] ;
241 String out
= oObj
.folding("AaBbCc", 1, 4, offs
) ;
243 boolean result
= "ABBC".equals(out
) && offs
[0].length
== 4 &&
250 tRes
.tested("folding()", result
) ;
255 * First loads <code>LOWERCASE_UPPERCASE</code> module.
256 * Tries to compare two equal substrings. <p>
257 * Has <b>OK</b> status if the method returned <code>true</code>.
259 public void _equals() {
260 oObj
.loadModule(TransliterationModules
.LOWERCASE_UPPERCASE
, loc
);
262 int[] match1
= new int[1],
263 match2
= new int[1] ;
265 boolean res
= oObj
.equals("aAbBcC", 1, 3, match1
, "aAbBcC", 1,
268 log
.println("Returned : " + res
+ " Match1 = " + match1
[0] +
269 " Match2 = " + match2
[0]) ;
271 tRes
.tested("equals()", res
) ;
275 * Test performed for sets of equal substrings, not equal
276 * substrings, and with out of bounds offset and length
279 * Has <b>OK</b> status if comparings of equal substrings
280 * always return 0, if comparisons of none equal returns
281 * proper value according to lexicographical order and if
282 * comparisons with invalid parameters return none 0 value.
284 public void _compareSubstring() {
285 oObj
.loadModule(TransliterationModules
.LOWERCASE_UPPERCASE
, loc
);
286 boolean result
= true ;
288 // substrings below must be equal
289 result
&= testSubstring("", 0, 0, "", 0, 0, 0) ;
290 result
&= testSubstring("aa", 1, 0, "", 0, 0, 0) ;
291 result
&= testSubstring("aa", 1, 0, "aa", 2, 0, 0) ;
292 result
&= testSubstring("a", 0, 1, "a", 0, 1, 0) ;
293 result
&= testSubstring("ab", 0, 2, "ab", 0, 2, 0) ;
294 result
&= testSubstring("abc", 1, 2, "abc", 1, 2, 0) ;
295 result
&= testSubstring("abcdef", 0, 3, "123abc", 3, 3, 0) ;
296 result
&= testSubstring("abcdef", 1, 1, "123abc", 4, 1, 0) ;
298 // substrings below must NOT be equal
299 result
&= testSubstring("a", 0, 1, "a", 0, 0, 1) ;
300 result
&= testSubstring("aaa", 1, 1, "", 0, 0, 1) ;
301 result
&= testSubstring("bbb", 2, 1, "aaa", 2, 1, 1) ;
302 result
&= testSubstring("abc", 0, 3, "abc", 0, 2, 1) ;
303 result
&= testSubstring("bbc", 1, 2, "bbc", 0, 2, 1) ;
305 // testing with wrong offsets and lengths
307 tRes
.tested("compareSubstring()", result
) ;
311 * Performs tesing of two substrings. Also testing of opposite
312 * substrings order performed.
313 * @return <code>true</code> if substrings are equal and retruned
314 * value is 0 for both orders,
315 * if substrings are different and expected value
316 * returned for direct order and opposite value returned for
319 private boolean testSubstring(String str1
, int p1
, int len1
,
320 String str2
, int p2
, int len2
, int expRes
) {
326 res
= oObj
.compareSubstring(str1
, p1
, len1
, str2
, p2
, len2
);
327 } catch (NullPointerException e
) {
328 log
.println("Exception while method calling occurs :" + e
);
332 log
.print("Comparing FAILED; return: " + res
+ ", expected: " +
336 log
.print("Comparing OK : ");
338 log
.println("('" + str1
+ "', " + p1
+ ", " + len1
+ ", '" +
339 str2
+ "', " + p2
+ ", " + len2
+ ")");
343 res
= oObj
.compareSubstring(str2
, p2
, len2
, str1
, p1
, len1
);
344 } catch (NullPointerException e
) {
345 log
.println("Exception while method calling occurs :" + e
);
348 if (res
!= -expRes
) {
349 log
.print("Comparing FAILED; return: " + res
+ ", expected: " +
353 log
.print("Comparing OK :");
355 log
.println("('" + str2
+ "', " + p2
+ ", " + len2
+ ", '" +
356 str1
+ "', " + p1
+ ", " + len1
+ ")");
362 * Test performed for sets of equal strings and not equal
365 * Has <b>OK</b> status if comparings of equal strings
366 * always return 0 and if comparisons of none equal returns
367 * proper value according to lexicographical order .
369 public void _compareString() {
370 oObj
.loadModule(TransliterationModules
.LOWERCASE_UPPERCASE
, loc
);
371 boolean result
= true ;
373 result
&= testString("", "", 0) ;
374 result
&= testString("a", "", 1) ;
375 result
&= testString("a", "a", 0) ;
376 result
&= testString("A", "a", 0) ;
377 result
&= testString("b", "a", 1) ;
378 result
&= testString("\n", "\n", 0) ;
379 result
&= testString("\n", "\t", 1) ;
380 result
&= testString("aaa", "aaa", 0) ;
381 result
&= testString("aaA", "aaa", 0) ;
382 result
&= testString("aaa", "aa", 1) ;
383 result
&= testString("ab", "aaa", 1) ;
384 result
&= testString("aba", "aa", 1) ;
385 result
&= testString("aaa\t\na", "aaa\t\na", 0) ;
386 result
&= testString("aaa\t\nb", "aaa\t\na", 1) ;
388 tRes
.tested("compareString()", result
) ;
392 * Performs tesing of two strings. If the expected value is not 0
393 * (i.e. strings are not equal), then also testing of opposite
394 * strings order performed.
395 * @return <code>true</code> if strings are equal and retruned
396 * value is 0, if strings are different and expected value
397 * returned for direct order and opposite value returned for
400 protected boolean testString(String str1
, String str2
, int expRes
) {
401 if (expRes
== 0) return testString(str1
, str2
, expRes
, false) ;
402 return testString(str1
, str2
, expRes
, true) ;
405 private boolean testString(String str1
, String str2
, int expRes
,
406 boolean testReverse
) {
412 res
= oObj
.compareString(str1
, str2
);
413 } catch (NullPointerException e
) {
414 log
.println("Exception while method calling occurs :" + e
);
418 log
.println("Comparing of '" + str1
+ "' and '" + str2
+ "' OK" );
420 log
.println("Comparing of '" + str1
+ "' and '" + str2
+
421 "' FAILED; return: " + res
+ ", expected: " + expRes
);
425 if (!testReverse
) return ret
;
429 res
= oObj
.compareString(str2
, str1
);
430 } catch (NullPointerException e
) {
431 log
.println("Exception while method calling occurs :" + e
);
434 if (res
== -expRes
) {
435 log
.println("Comparing of '" + str2
+ "' and '" + str1
+ "' OK" );
437 log
.println("Comparing of '" + str2
+ "' and '" + str1
+
438 "' FAILED; return: " + res
+ ", expected: " + -expRes
);