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 compilant. <p>
46 * @see com.sun.star.i18n.XTransliteration
48 public class _XTransliteration
extends MultiMethodTest
{
50 public XTransliteration oObj
= null;
51 private String
[] mod
= null ;
52 private Locale loc
= new Locale("en", "EN", "") ;
55 * Gets all available transliteration modules. <p>
56 * Has <b>OK</b> status if array returned has at least
59 public void _getAvailableModules() {
60 mod
= oObj
.getAvailableModules(loc
, TransliterationType
.ONE_TO_ONE
);
63 log
.println("Available modules :") ;
64 for (int i
= 0; i
< mod
.length
; i
++) {
65 log
.println(" '" + mod
[i
] + "'") ;
68 log
.println("!!! NULL returned !!!") ;
71 tRes
.tested("getAvailableModules()", mod
!= null && mod
.length
> 0) ;
75 * Calls the method for load IGNORE_CASE module and checks the name returned
76 * by the method <code>getName</code>. <p>
77 * Has <b>OK</b> status if the method <code>getName</code> returns the
78 * string "case ignore (generic)".
80 public void _loadModule() {
81 log
.println("Load module IGNORE_CASE");
82 oObj
.loadModule(TransliterationModules
.IGNORE_CASE
, loc
);
84 String name
= oObj
.getName();
85 boolean res
= name
.equals("case ignore (generic)");
86 log
.println("getName return: " + name
);
88 tRes
.tested("loadModule()", res
);
92 * Loads <code>LOWERCASE_UPPERCASE</code> module and checks the current
95 * Has <b>OK</b> status if the name of the object is equals to
96 * 'lower_to_upper(generic)'
98 public void _loadModuleNew() {
99 boolean result
= true ;
102 new TransliterationModulesNew
[]
103 {TransliterationModulesNew
.LOWERCASE_UPPERCASE
}, loc
);
105 String name
= oObj
.getName();
106 result
= name
.equals("lower_to_upper(generic)");
107 log
.println("getName return: " + name
);
109 tRes
.tested("loadModuleNew()", result
);
113 * Calls the method for load LOWERCASE_UPPERCASE module and
114 * checks the name returned by the method <code>getName</code>. <p>
115 * Has <b>OK</b> status if the method <code>getName</code> returns the
116 * string "lower_to_upper(generic)".
118 public void _loadModuleByImplName() {
119 log
.println("Load module LOWERCASE_UPPERCASE");
120 oObj
.loadModuleByImplName("LOWERCASE_UPPERCASE", loc
);
122 String name
= oObj
.getName();
123 boolean res
= name
.equals("lower_to_upper(generic)");
124 log
.println("getName return: " + name
);
126 tRes
.tested("loadModuleByImplName()", res
);
130 * Calls the method for load UPPERCASE_LOWERCASE module and
131 * checks the name returned by the method <code>getName</code>. <p>
132 * Has <b>OK</b> status if the method <code>getName</code> returns the
133 * string "upper_to_lower(generic)".
135 public void _loadModulesByImplNames() {
136 log
.println("Load module UPPERCASE_LOWERCASE");
137 oObj
.loadModulesByImplNames(new String
[]{"UPPERCASE_LOWERCASE"}, loc
);
139 String name
= oObj
.getName();
140 boolean res
= name
.equals("upper_to_lower(generic)");
141 log
.println("getName return: " + name
);
143 tRes
.tested("loadModulesByImplNames()", res
);
147 * Loads <code>LOWERCASE_UPPERCASE</code> module and checks current type.
148 * <p>Has <b>OK</b> status if the type is <code>ONE_TO_ONE</code>
150 public void _getType() {
151 oObj
.loadModule(TransliterationModules
.LOWERCASE_UPPERCASE
, loc
);
152 boolean result
= oObj
.getType() == TransliterationType
.ONE_TO_ONE
;
153 tRes
.tested("getType()", result
);
157 * Loads UPPERCASE_LOWERCASE module and
158 * checks the name returned by the method <code>getName</code>. <p>
160 * Has <b>OK</b> status if the method <code>getName</code> returns the
161 * string "upper_to_lower(generic)".
163 public void _getName() {
164 oObj
.loadModule(TransliterationModules
.LOWERCASE_UPPERCASE
, loc
);
166 String name
= oObj
.getName();
167 boolean res
= name
.equals("lower_to_upper(generic)");
168 log
.println("getName return: " + name
);
170 tRes
.tested("getName()", res
);
174 * First loads <code>LOWERCASE_UPPERCASE</code> module.
175 * Then tries to transliterate (make uppercase) a substring. <p>
176 * Has <b>OK</b> status if all chars were made uppercase,
177 * and array returned has size as substring length, and its
178 * elements are positions of substring characters in the source
181 public void _transliterate() {
182 oObj
.loadModule(TransliterationModules
.LOWERCASE_UPPERCASE
, loc
);
184 int[][] offs
= new int[1][] ;
186 String out
= oObj
.transliterate("AaBbCc", 1, 4, offs
) ;
188 boolean result
= "ABBC".equals(out
) && offs
[0].length
== 4 &&
194 tRes
.tested("transliterate()", result
) ;
199 * First loads <code>LOWERCASE_UPPERCASE</code> module.
200 * Tries to transliterate a range of two characters. <p>
201 * Has <b>OK</b> status if the appropriate String array
202 * returned (not null, length = 4, with two ranges
203 * (a, i), (A, I) in any order).
205 public void _transliterateRange() {
206 oObj
.loadModule(TransliterationModules
.IGNORE_CASE
, loc
);
208 String
[] out
= oObj
.transliterateRange("a", "i") ;
210 log
.println("transliterateRange return:");
211 for(int i
= 0; i
< out
.length
; i
++) {
215 boolean bOK
= out
!= null &&
217 ("A".equals(out
[0]) && "I".equals(out
[1]) &&
218 "a".equals(out
[2]) && "i".equals(out
[3])) ||
219 ("a".equals(out
[0]) && "i".equals(out
[1]) &&
220 "A".equals(out
[2]) && "I".equals(out
[3])) ;
223 log
.println("Unexpected range returned :");
224 for (int i
= 0; i
< out
.length
; i
++) {
225 log
.print("'" + out
[i
] +"', ");
230 tRes
.tested("transliterateRange()", bOK
);
234 * This method is used internally by <code>equals</code>
235 * method so it indirectly tested in this method. <p>
236 * Always has <b>OK</b> status.
238 public void _folding() {
239 oObj
.loadModule(TransliterationModules
.LOWERCASE_UPPERCASE
, loc
);
241 int[][] offs
= new int[1][] ;
243 String out
= oObj
.folding("AaBbCc", 1, 4, offs
) ;
245 boolean result
= "ABBC".equals(out
) && offs
[0].length
== 4 &&
252 tRes
.tested("folding()", result
) ;
257 * First loads <code>LOWERCASE_UPPERCASE</code> module.
258 * Tries to compare two equal substrings. <p>
259 * Has <b>OK</b> status if the method returned <code>true</code>.
261 public void _equals() {
262 oObj
.loadModule(TransliterationModules
.LOWERCASE_UPPERCASE
, loc
);
264 int[] match1
= new int[1],
265 match2
= new int[1] ;
267 boolean res
= oObj
.equals("aAbBcC", 1, 3, match1
, "aAbBcC", 1,
270 log
.println("Returned : " + res
+ " Match1 = " + match1
[0] +
271 " Match2 = " + match2
[0]) ;
273 tRes
.tested("equals()", res
) ;
277 * Test performed for sets of equal substrings, not equal
278 * substrings, and with out of bounds offset and length
281 * Has <b>OK</b> status if comparings of equal substrings
282 * always return 0, if comparisons of none equal returns
283 * proper value according to lexicographical order and if
284 * comparisons with invalid parameters return none 0 value.
286 public void _compareSubstring() {
287 oObj
.loadModule(TransliterationModules
.LOWERCASE_UPPERCASE
, loc
);
288 boolean result
= true ;
290 // substrings below must be equal
291 result
&= testSubstring("", 0, 0, "", 0, 0, 0) ;
292 result
&= testSubstring("aa", 1, 0, "", 0, 0, 0) ;
293 result
&= testSubstring("aa", 1, 0, "aa", 2, 0, 0) ;
294 result
&= testSubstring("a", 0, 1, "a", 0, 1, 0) ;
295 result
&= testSubstring("ab", 0, 2, "ab", 0, 2, 0) ;
296 result
&= testSubstring("abc", 1, 2, "abc", 1, 2, 0) ;
297 result
&= testSubstring("abcdef", 0, 3, "123abc", 3, 3, 0) ;
298 result
&= testSubstring("abcdef", 1, 1, "123abc", 4, 1, 0) ;
300 // substrings below must NOT be equal
301 result
&= testSubstring("a", 0, 1, "a", 0, 0, 1) ;
302 result
&= testSubstring("aaa", 1, 1, "", 0, 0, 1) ;
303 result
&= testSubstring("bbb", 2, 1, "aaa", 2, 1, 1) ;
304 result
&= testSubstring("abc", 0, 3, "abc", 0, 2, 1) ;
305 result
&= testSubstring("bbc", 1, 2, "bbc", 0, 2, 1) ;
307 // testing with wrong offsets and lengths
309 tRes
.tested("compareSubstring()", result
) ;
313 * Performs tesing of two substrings. Also testing of opposite
314 * substrings order performed.
315 * @return <code>true</code> if substrings are equal and retruned
316 * value is 0 for both orders,
317 * if substrings are different and expected value
318 * returned for direct order and opposite value returned for
321 private boolean testSubstring(String str1
, int p1
, int len1
,
322 String str2
, int p2
, int len2
, int expRes
) {
328 res
= oObj
.compareSubstring(str1
, p1
, len1
, str2
, p2
, len2
);
329 } catch (java
.lang
.NullPointerException e
) {
330 log
.println("Exception while method calling occurs :" + e
);
334 log
.print("Comparing FAILED; return: " + res
+ ", expected: " +
338 log
.print("Comparing OK : ");
340 log
.println("('" + str1
+ "', " + p1
+ ", " + len1
+ ", '" +
341 str2
+ "', " + p2
+ ", " + len2
+ ")");
345 res
= oObj
.compareSubstring(str2
, p2
, len2
, str1
, p1
, len1
);
346 } catch (java
.lang
.NullPointerException e
) {
347 log
.println("Exception while method calling occurs :" + e
);
350 if (res
!= -expRes
) {
351 log
.print("Comparing FAILED; return: " + res
+ ", expected: " +
355 log
.print("Comparing OK :");
357 log
.println("('" + str2
+ "', " + p2
+ ", " + len2
+ ", '" +
358 str1
+ "', " + p1
+ ", " + len1
+ ")");
364 * Test performed for sets of equal strings and not equal
367 * Has <b>OK</b> status if comparings of equal strings
368 * always return 0 and if comparisons of none equal returns
369 * proper value according to lexicographical order .
371 public void _compareString() {
372 oObj
.loadModule(TransliterationModules
.LOWERCASE_UPPERCASE
, loc
);
373 boolean result
= true ;
375 result
&= testString("", "", 0) ;
376 result
&= testString("a", "", 1) ;
377 result
&= testString("a", "a", 0) ;
378 result
&= testString("A", "a", 0) ;
379 result
&= testString("b", "a", 1) ;
380 result
&= testString("\n", "\n", 0) ;
381 result
&= testString("\n", "\t", 1) ;
382 result
&= testString("aaa", "aaa", 0) ;
383 result
&= testString("aaA", "aaa", 0) ;
384 result
&= testString("aaa", "aa", 1) ;
385 result
&= testString("ab", "aaa", 1) ;
386 result
&= testString("aba", "aa", 1) ;
387 result
&= testString("aaa\t\na", "aaa\t\na", 0) ;
388 result
&= testString("aaa\t\nb", "aaa\t\na", 1) ;
390 tRes
.tested("compareString()", result
) ;
394 * Performs tesing of two strings. If the expected value is not 0
395 * (i.e. strings are not equal), then also testing of opposite
396 * strings order performed.
397 * @return <code>true</code> if strings are equal and retruned
398 * value is 0, if strings are different and expected value
399 * returned for direct order and opposite value returned for
402 protected boolean testString(String str1
, String str2
, int expRes
) {
403 if (expRes
== 0) return testString(str1
, str2
, expRes
, false) ;
404 return testString(str1
, str2
, expRes
, true) ;
407 private boolean testString(String str1
, String str2
, int expRes
,
408 boolean testReverse
) {
414 res
= oObj
.compareString(str1
, str2
);
415 } catch (java
.lang
.NullPointerException e
) {
416 log
.println("Exception while method calling occurs :" + e
);
420 log
.println("Comparing of '" + str1
+ "' and '" + str2
+ "' OK" );
422 log
.println("Comparing of '" + str1
+ "' and '" + str2
+
423 "' FAILED; return: " + res
+ ", expected: " + expRes
);
427 if (!testReverse
) return ret
;
431 res
= oObj
.compareString(str2
, str1
);
432 } catch (java
.lang
.NullPointerException e
) {
433 log
.println("Exception while method calling occurs :" + e
);
436 if (res
== -expRes
) {
437 log
.println("Comparing of '" + str2
+ "' and '" + str1
+ "' OK" );
439 log
.println("Comparing of '" + str2
+ "' and '" + str1
+
440 "' FAILED; return: " + res
+ ", expected: " + -expRes
);