merge the formfield patch from ooo-build
[ooovba.git] / qadevOOo / tests / java / ifc / i18n / _XTransliteration.java
blob201294fd1524fe47a29df4985aa449782afd8148
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: _XTransliteration.java,v $
10 * $Revision: 1.5 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 package ifc.i18n;
33 import lib.MultiMethodTest;
35 import com.sun.star.i18n.TransliterationModules;
36 import com.sun.star.i18n.TransliterationModulesNew;
37 import com.sun.star.i18n.TransliterationType;
38 import com.sun.star.i18n.XTransliteration;
39 import com.sun.star.lang.Locale;
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 compilant. <p>
58 * @see com.sun.star.i18n.XTransliteration
60 public class _XTransliteration extends MultiMethodTest {
62 public XTransliteration oObj = null;
63 private String[] mod = null ;
64 private Locale loc = new Locale("en", "EN", "") ;
66 /**
67 * Gets all available transliteration modules. <p>
68 * Has <b>OK</b> status if array returned has at least
69 * one module name.
71 public void _getAvailableModules() {
72 mod = oObj.getAvailableModules(loc, TransliterationType.ONE_TO_ONE);
74 if (mod != null) {
75 log.println("Available modules :") ;
76 for (int i = 0; i < mod.length; i++) {
77 log.println(" '" + mod[i] + "'") ;
79 } else {
80 log.println("!!! NULL returned !!!") ;
83 tRes.tested("getAvailableModules()", mod != null && mod.length > 0) ;
86 /**
87 * Calls the method for load IGNORE_CASE module and checks the name returned
88 * by the method <code>getName</code>. <p>
89 * Has <b>OK</b> status if the method <code>getName</code> returns the
90 * string "case ignore (generic)".
92 public void _loadModule() {
93 log.println("Load module IGNORE_CASE");
94 oObj.loadModule(TransliterationModules.IGNORE_CASE, loc);
96 String name = oObj.getName();
97 boolean res = name.equals("case ignore (generic)");
98 log.println("getName return: " + name);
100 tRes.tested("loadModule()", res );
104 * Loads <code>LOWERCASE_UPPERCASE</code> module and checks the current
105 * name of object. <p>
107 * Has <b>OK</b> status if the name of the object is equals to
108 * 'lower_to_upper(generic)'
110 public void _loadModuleNew() {
111 boolean result = true ;
113 oObj.loadModuleNew(
114 new TransliterationModulesNew[]
115 {TransliterationModulesNew.LOWERCASE_UPPERCASE}, loc);
117 String name = oObj.getName();
118 result = name.equals("lower_to_upper(generic)");
119 log.println("getName return: " + name);
121 tRes.tested("loadModuleNew()", result);
125 * Calls the method for load LOWERCASE_UPPERCASE module and
126 * checks the name returned by the method <code>getName</code>. <p>
127 * Has <b>OK</b> status if the method <code>getName</code> returns the
128 * string "lower_to_upper(generic)".
130 public void _loadModuleByImplName() {
131 log.println("Load module LOWERCASE_UPPERCASE");
132 oObj.loadModuleByImplName("LOWERCASE_UPPERCASE", loc);
134 String name = oObj.getName();
135 boolean res = name.equals("lower_to_upper(generic)");
136 log.println("getName return: " + name);
138 tRes.tested("loadModuleByImplName()", res);
142 * Calls the method for load UPPERCASE_LOWERCASE module and
143 * checks the name returned by the method <code>getName</code>. <p>
144 * Has <b>OK</b> status if the method <code>getName</code> returns the
145 * string "upper_to_lower(generic)".
147 public void _loadModulesByImplNames() {
148 log.println("Load module UPPERCASE_LOWERCASE");
149 oObj.loadModulesByImplNames(new String[]{"UPPERCASE_LOWERCASE"}, loc);
151 String name = oObj.getName();
152 boolean res = name.equals("upper_to_lower(generic)");
153 log.println("getName return: " + name);
155 tRes.tested("loadModulesByImplNames()", res);
159 * Loads <code>LOWERCASE_UPPERCASE</code> module and checks current type.
160 * <p>Has <b>OK</b> status if the type is <code>ONE_TO_ONE</code>
162 public void _getType() {
163 oObj.loadModule(TransliterationModules.LOWERCASE_UPPERCASE, loc);
164 boolean result = oObj.getType() == TransliterationType.ONE_TO_ONE;
165 tRes.tested("getType()", result);
169 * Loads UPPERCASE_LOWERCASE module and
170 * checks the name returned by the method <code>getName</code>. <p>
172 * Has <b>OK</b> status if the method <code>getName</code> returns the
173 * string "upper_to_lower(generic)".
175 public void _getName() {
176 oObj.loadModule(TransliterationModules.LOWERCASE_UPPERCASE, loc);
178 String name = oObj.getName();
179 boolean res = name.equals("lower_to_upper(generic)");
180 log.println("getName return: " + name);
182 tRes.tested("getName()", res);
186 * First loads <code>LOWERCASE_UPPERCASE</code> module.
187 * Then tries to transliterate (make uppercase) a substring. <p>
188 * Has <b>OK</b> status if all chars were made uppercase,
189 * and array returned has size as substring length, and its
190 * elements are positions of substring characters in the source
191 * string.
193 public void _transliterate() {
194 oObj.loadModule(TransliterationModules.LOWERCASE_UPPERCASE, loc);
196 int[][] offs = new int[1][] ;
198 String out = oObj.transliterate("AaBbCc", 1, 4, offs) ;
200 boolean result = "ABBC".equals(out) && offs[0].length == 4 &&
201 offs[0][0] == 1 &&
202 offs[0][1] == 2 &&
203 offs[0][2] == 3 &&
204 offs[0][3] == 4 ;
206 tRes.tested("transliterate()", result) ;
211 * First loads <code>LOWERCASE_UPPERCASE</code> module.
212 * Tries to transliterate a range of two characters. <p>
213 * Has <b>OK</b> status if the appropriate String array
214 * returned (not null, length = 4, with two ranges
215 * (a, i), (A, I) in any order).
217 public void _transliterateRange() {
218 oObj.loadModule(TransliterationModules.IGNORE_CASE, loc);
220 String[] out = oObj.transliterateRange("a", "i") ;
222 log.println("transliterateRange return:");
223 for(int i = 0; i < out.length; i++) {
224 log.println(out[i]);
227 boolean bOK = out != null &&
228 out.length == 4 &&
229 ("A".equals(out[0]) && "I".equals(out[1]) &&
230 "a".equals(out[2]) && "i".equals(out[3])) ||
231 ("a".equals(out[0]) && "i".equals(out[1]) &&
232 "A".equals(out[2]) && "I".equals(out[3])) ;
234 if (!bOK) {
235 log.println("Unexpected range returned :");
236 for (int i = 0; i < out.length; i++) {
237 log.print("'" + out[i] +"', ");
239 log.println();
242 tRes.tested("transliterateRange()", bOK);
246 * This method is used internally by <code>equals</code>
247 * method so it indirectly tested in this method. <p>
248 * Always has <b>OK</b> status.
250 public void _folding() {
251 oObj.loadModule(TransliterationModules.LOWERCASE_UPPERCASE, loc);
253 int[][] offs = new int[1][] ;
255 String out = oObj.folding("AaBbCc", 1, 4, offs) ;
257 boolean result = "ABBC".equals(out) && offs[0].length == 4 &&
258 offs[0][0] == 1 &&
259 offs[0][1] == 2 &&
260 offs[0][2] == 3 &&
261 offs[0][3] == 4 ;
264 tRes.tested("folding()", result) ;
269 * First loads <code>LOWERCASE_UPPERCASE</code> module.
270 * Tries to compare two equal substrings. <p>
271 * Has <b>OK</b> status if the method returned <code>true</code>.
273 public void _equals() {
274 oObj.loadModule(TransliterationModules.LOWERCASE_UPPERCASE, loc);
276 int[] match1 = new int[1],
277 match2 = new int[1] ;
279 boolean res = oObj.equals("aAbBcC", 1, 3, match1, "aAbBcC", 1,
280 3, match2) ;
282 log.println("Returned : " + res + " Match1 = " + match1[0] +
283 " Match2 = " + match2[0]) ;
285 tRes.tested("equals()", res) ;
289 * Test performed for sets of equal substrings, not equal
290 * substrings, and with out of bounds offset and length
291 * parameters.<p>
293 * Has <b>OK</b> status if comparings of equal substrings
294 * always return 0, if comparisons of none equal returns
295 * proper value according to lexicographical order and if
296 * comparisons with invalid parameters return none 0 value.
298 public void _compareSubstring() {
299 oObj.loadModule(TransliterationModules.LOWERCASE_UPPERCASE, loc);
300 boolean result = true ;
302 // substrings below must be equal
303 result &= testSubstring("", 0, 0, "", 0, 0, 0) ;
304 result &= testSubstring("aa", 1, 0, "", 0, 0, 0) ;
305 result &= testSubstring("aa", 1, 0, "aa", 2, 0, 0) ;
306 result &= testSubstring("a", 0, 1, "a", 0, 1, 0) ;
307 result &= testSubstring("ab", 0, 2, "ab", 0, 2, 0) ;
308 result &= testSubstring("abc", 1, 2, "abc", 1, 2, 0) ;
309 result &= testSubstring("abcdef", 0, 3, "123abc", 3, 3, 0) ;
310 result &= testSubstring("abcdef", 1, 1, "123abc", 4, 1, 0) ;
312 // substrings below must NOT be equal
313 result &= testSubstring("a", 0, 1, "a", 0, 0, 1) ;
314 result &= testSubstring("aaa", 1, 1, "", 0, 0, 1) ;
315 result &= testSubstring("bbb", 2, 1, "aaa", 2, 1, 1) ;
316 result &= testSubstring("abc", 0, 3, "abc", 0, 2, 1) ;
317 result &= testSubstring("bbc", 1, 2, "bbc", 0, 2, 1) ;
319 // testing with wrong offsets and lengths
321 tRes.tested("compareSubstring()", result) ;
325 * Performs tesing of two substrings. Also testing of opposite
326 * substrings order performed.
327 * @return <code>true</code> if substrings are equal and retruned
328 * value is 0 for both orders,
329 * if substrings are different and expected value
330 * returned for direct order and opposite value returned for
331 * opposite order.
333 private boolean testSubstring(String str1, int p1, int len1,
334 String str2, int p2, int len2, int expRes) {
336 boolean ret = true ;
338 int res = -666 ;
339 try {
340 res = oObj.compareSubstring(str1, p1, len1, str2, p2, len2);
341 } catch (java.lang.NullPointerException e) {
342 log.println("Exception while method calling occurs :" + e);
345 if (res != expRes) {
346 log.print("Comparing FAILED; return: " + res + ", expected: " +
347 expRes + " ");
348 ret = false ;
349 } else {
350 log.print("Comparing OK : ");
352 log.println("('" + str1 + "', " + p1 + ", " + len1 + ", '" +
353 str2 + "', " + p2 + ", " + len2 + ")");
355 res = -666 ;
356 try {
357 res = oObj.compareSubstring(str2, p2, len2, str1, p1, len1);
358 } catch (java.lang.NullPointerException e) {
359 log.println("Exception while method calling occurs :" + e);
362 if (res != -expRes) {
363 log.print("Comparing FAILED; return: " + res + ", expected: " +
364 -expRes + " ");
365 ret = false ;
366 } else {
367 log.print("Comparing OK :");
369 log.println("('" + str2 + "', " + p2 + ", " + len2 + ", '" +
370 str1 + "', " + p1 + ", " + len1 + ")");
372 return ret ;
376 * Test performed for sets of equal strings and not equal
377 * strings.<p>
379 * Has <b>OK</b> status if comparings of equal strings
380 * always return 0 and if comparisons of none equal returns
381 * proper value according to lexicographical order .
383 public void _compareString() {
384 oObj.loadModule(TransliterationModules.LOWERCASE_UPPERCASE, loc);
385 boolean result = true ;
387 result &= testString("", "", 0) ;
388 result &= testString("a", "", 1) ;
389 result &= testString("a", "a", 0) ;
390 result &= testString("A", "a", 0) ;
391 result &= testString("b", "a", 1) ;
392 result &= testString("\n", "\n", 0) ;
393 result &= testString("\n", "\t", 1) ;
394 result &= testString("aaa", "aaa", 0) ;
395 result &= testString("aaA", "aaa", 0) ;
396 result &= testString("aaa", "aa", 1) ;
397 result &= testString("ab", "aaa", 1) ;
398 result &= testString("aba", "aa", 1) ;
399 result &= testString("aaa\t\na", "aaa\t\na", 0) ;
400 result &= testString("aaa\t\nb", "aaa\t\na", 1) ;
402 tRes.tested("compareString()", result) ;
406 * Performs tesing of two strings. If the expected value is not 0
407 * (i.e. strings are not equal), then also testing of opposite
408 * strings order performed.
409 * @return <code>true</code> if strings are equal and retruned
410 * value is 0, if strings are different and expected value
411 * returned for direct order and opposite value returned for
412 * opposite order.
414 protected boolean testString(String str1, String str2, int expRes) {
415 if (expRes == 0) return testString(str1, str2, expRes, false) ;
416 return testString(str1, str2, expRes, true) ;
419 private boolean testString(String str1, String str2, int expRes,
420 boolean testReverse) {
422 boolean ret = true ;
424 int res = -666 ;
425 try {
426 res = oObj.compareString(str1, str2);
427 } catch (java.lang.NullPointerException e) {
428 log.println("Exception while method calling occurs :" + e);
431 if (res == expRes) {
432 log.println("Comparing of '" + str1 + "' and '" + str2 + "' OK" );
433 } else {
434 log.println("Comparing of '" + str1 + "' and '" + str2 +
435 "' FAILED; return: " + res + ", expected: " + expRes);
436 ret = false ;
439 if (!testReverse) return ret ;
441 res = -666 ;
442 try {
443 res = oObj.compareString(str2, str1);
444 } catch (java.lang.NullPointerException e) {
445 log.println("Exception while method calling occurs :" + e);
448 if (res == -expRes) {
449 log.println("Comparing of '" + str2 + "' and '" + str1 + "' OK" );
450 } else {
451 log.println("Comparing of '" + str2 + "' and '" + str1 +
452 "' FAILED; return: " + res + ", expected: " + -expRes);
453 ret = false ;
456 return ret ;