bump product version to 4.2.0.1
[LibreOffice.git] / qadevOOo / tests / java / ifc / i18n / _XCollator.java
blobb8d4d6eca7d4b3641b9523408224729672eee6d3
1 /*
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 .
19 package ifc.i18n;
21 import java.text.Collator;
23 import lib.MultiMethodTest;
25 import com.sun.star.i18n.CollatorOptions;
26 import com.sun.star.i18n.XCollator;
27 import com.sun.star.lang.Locale;
29 /**
30 * Testing <code>com.sun.star.i18n.XCollator</code>
31 * interface methods :
32 * <ul>
33 * <li><code> compareSubstring()</code></li>
34 * <li><code> compareString()</code></li>
35 * <li><code> loadDefaultCollator()</code></li>
36 * <li><code> loadCollatorAlgorithm()</code></li>
37 * <li><code> listCollatorAlgorithms()</code></li>
38 * <li><code> loadCollatorAlgorithmWithEndUserOption()</code></li>
39 * <li><code> listCollatorOptions()</code></li>
40 * </ul> <p>
41 * Test is <b> NOT </b> multithread compilant. <p>
42 * @see com.sun.star.i18n.XCollator
44 public class _XCollator extends MultiMethodTest {
45 public XCollator oObj = null;
46 private String[] alg = null ;
47 private int[] opt = null ;
48 Locale loc = new Locale("en", "EN", "");
50 /**
51 * Just retrieves a list of algorithms. <p>
52 * Has <b>OK</b> status if non-zero length array returned.
54 public void _listCollatorAlgorithms() {
55 alg = oObj.listCollatorAlgorithms(loc) ;
56 log.println("Collator algorithms :");
57 if (alg != null) {
58 for (int i = 0; i < alg.length; i++) {
59 log.println(" '" + alg[i] + "'") ;
61 tRes.tested("listCollatorAlgorithms()", alg.length > 0) ;
62 } else {
63 tRes.tested("listCollatorAlgorithms()", false) ;
67 /**
68 * Just gets a list of options for some collator. <p>
69 * Has <b>OK</b> status if not null value returned.<p>
70 * The following method tests are to be completed successfully before :
71 * <ul>
72 * <li> <code> listCollatorAlgorithms </code> : to have some
73 * algorithm name. </li>
74 * </ul>
76 public void _listCollatorOptions() {
77 requiredMethod("listCollatorAlgorithms()") ;
78 opt = oObj.listCollatorOptions(alg[0]) ;
79 log.println("Collator '" + alg[0] + "' options :");
80 if (opt != null) {
81 for (int i = 0; i < opt.length; i++) {
82 log.println(" " + opt[i]) ;
84 tRes.tested("listCollatorOptions()", true) ;
85 } else {
86 tRes.tested("listCollatorOptions()", false) ;
90 /**
91 * Calls the method with no options and with options(IGNORE_CASE),
92 * compares strings.<p>
93 * Has <b>OK</b> status if compareString() returned correct values.
95 public void _loadDefaultCollator() {
96 oObj.loadDefaultCollator(loc, 0);
97 boolean res = oObj.compareString("A", "a") != 0;
98 oObj.loadDefaultCollator(loc,
99 CollatorOptions.CollatorOptions_IGNORE_CASE);
100 res &= oObj.compareString("a", "A") == 0;
101 tRes.tested("loadDefaultCollator()", res) ;
105 * Calls the method with no options and with options(IGNORE_CASE),
106 * compares strings.<p>
107 * Has <b>OK</b> status if compareString() returned correct values.
108 * The following method tests are to be completed successfully before :
109 * <ul>
110 * <li> <code> listCollatorAlgorithms </code> : to have some
111 * algorithm name. </li>
112 * </ul>
114 public void _loadCollatorAlgorithm() {
115 requiredMethod("listCollatorAlgorithms()");
116 oObj.loadCollatorAlgorithm(alg[0], loc,
117 CollatorOptions.CollatorOptions_IGNORE_CASE);
118 boolean res = oObj.compareString("A", "a") == 0;
119 oObj.loadCollatorAlgorithm(alg[0], loc, 0);
120 res &= oObj.compareString("a", "A") != 0;
121 tRes.tested("loadCollatorAlgorithm()", res);
125 * Calls the method with no options and with options(IGNORE_CASE),
126 * compares strings.<p>
127 * Has <b>OK</b> status if compareString() returned correct values.
128 * The following method tests are to be completed successfully before :
129 * <ul>
130 * <li> <code> listCollatorAlgorithms </code> : to have some
131 * algorithm name. </li>
132 * </ul>
134 public void _loadCollatorAlgorithmWithEndUserOption() {
135 requiredMethod("listCollatorAlgorithms()");
136 oObj.loadCollatorAlgorithmWithEndUserOption(alg[0], loc,
137 new int[] {0});
138 boolean res = oObj.compareString("A", "a") != 0;
139 oObj.loadCollatorAlgorithmWithEndUserOption(alg[0], loc,
140 new int[] {CollatorOptions.CollatorOptions_IGNORE_CASE});
141 res = oObj.compareString("A", "a") == 0;
142 tRes.tested("loadCollatorAlgorithmWithEndUserOption()", res);
146 * Test is performed for locales : en, ru, ja, zh, ko.
147 * Default collator is loaded for each locale. Then collation
148 * is performed for different combination of symbols from range of
149 * this locale.<p>
150 * Has <b>OK</b> status if comparing of different strings
151 * returns not 0 value, then comparing in the opposite
152 * order returns value with opposite sign, and comparing
153 * of two equal strings returns 0. The such comparing is performed
154 * for one character strings.
156 public void _compareSubstring() {
157 boolean result = true ;
158 char[] chars = new char[2] ;
159 Collator col = null ;
161 log.println(" #### Testing English locale ####") ;
162 oObj.loadDefaultCollator(loc, 0) ;
163 col = Collator.getInstance(new java.util.Locale("en", "EN")) ;
164 for (char ch = 0x0020; ch < 0x007F; ch ++) {
165 chars[0] = ch ; chars[1] = (char) (ch + 1) ;
166 result &= testCompareSubstring(chars, col) ;
169 log.println(" #### Testing Russian locale ####") ;
170 oObj.loadDefaultCollator(
171 new com.sun.star.lang.Locale("ru", "RU", ""), 0) ;
172 col = Collator.getInstance(new java.util.Locale("ru", "RU")) ;
173 for (char ch = 0x0410; ch < 0x0450; ch ++) {
174 chars[0] = ch ; chars[1] = (char) (ch + 1) ;
175 result &= testCompareSubstring(chars, col) ;
178 log.println(" #### Testing Japan locale ####") ;
179 oObj.loadDefaultCollator(
180 new com.sun.star.lang.Locale("ja", "JP", ""), 0) ;
181 col = Collator.getInstance(new java.util.Locale("ja", "JP")) ;
182 for (char ch = 0x4E00; ch < 0x4EFD; ch ++) {
183 chars[0] = ch ; chars[1] = (char) (ch + 1) ;
184 result &= testCompareSubstring(chars, col) ;
187 log.println(" #### Testing China locale ####") ;
188 oObj.loadDefaultCollator(new Locale("zh", "CN", ""), 0) ;
189 col = Collator.getInstance(new java.util.Locale("zh", "CN")) ;
190 for (char ch = 0x4E00; ch < 0x4EFD; ch ++) {
191 chars[0] = ch ; chars[1] = (char) (ch + 1) ;
192 result &= testCompareSubstring(chars, col) ;
195 log.println(" #### Testing Korean locale ####") ;
196 oObj.loadDefaultCollator(new Locale("ko", "KR", ""), 0) ;
197 col = Collator.getInstance(new java.util.Locale("ko", "KR")) ;
198 for (char ch = 0x4E00; ch < 0x4EFD; ch ++) {
199 chars[0] = ch ; chars[1] = (char) (ch + 1) ;
200 result &= testCompareSubstring(chars, col) ;
203 tRes.tested("compareSubstring()", result) ;
207 * Test is performed for locales : en, ru, ja, zh, ko.
208 * Default collator is loaded for each locale. Then collation
209 * is performed for different combination of symbols from range of
210 * this locale.<p>
211 * Has <b>OK</b> status if comparing of different strings
212 * returns not 0 value, then comparing in the opposite
213 * order returns value with opposite sign, and comparing
214 * of two equal strings returns 0. The such comparing is performed
215 * for one character strings.
217 public void _compareString() {
218 boolean result = true ;
219 char[] chars = new char[2] ;
220 Collator col = null ;
221 log.println(" #### Testing English locale ####") ;
222 oObj.loadDefaultCollator(
223 new com.sun.star.lang.Locale("en", "EN", ""), 0) ;
224 col = Collator.getInstance(new java.util.Locale("en", "EN")) ;
225 for (char ch = 0x0020; ch < 0x007F; ch ++) {
226 chars[0] = ch ; chars[1] = (char) (ch + 1) ;
227 result &= testCompareString(chars, col) ;
230 log.println(" #### Testing Russian locale ####") ;
231 oObj.loadDefaultCollator(
232 new com.sun.star.lang.Locale("ru", "RU", ""), 0) ;
233 col = Collator.getInstance(new java.util.Locale("ru", "RU")) ;
234 for (char ch = 0x0410; ch < 0x0450; ch ++) {
235 chars[0] = ch ; chars[1] = (char) (ch + 1) ;
236 result &= testCompareString(chars, col) ;
239 log.println(" #### Testing Japan locale ####") ;
240 oObj.loadDefaultCollator(
241 new com.sun.star.lang.Locale("ja", "JP", ""), 0) ;
242 col = Collator.getInstance(new java.util.Locale("ja", "JP")) ;
243 for (char ch = 0x4E00; ch < 0x4EFD; ch ++) {
244 chars[0] = ch ; chars[1] = (char) (ch + 1) ;
245 result &= testCompareString(chars, col) ;
248 log.println(" #### Testing China locale ####") ;
249 oObj.loadDefaultCollator(new Locale("zh", "CN", ""), 0) ;
250 col = Collator.getInstance(new java.util.Locale("zh", "CN")) ;
251 for (char ch = 0x4E00; ch < 0x4EFD; ch ++) {
252 chars[0] = ch ; chars[1] = (char) (ch + 1) ;
253 result &= testCompareString(chars, col) ;
256 log.println(" #### Testing Korean locale ####") ;
257 oObj.loadDefaultCollator(new Locale("ko", "KR", ""), 0) ;
258 col = Collator.getInstance(new java.util.Locale("ko", "KR")) ;
259 for (char ch = 0x4E00; ch < 0x4EFD; ch ++) {
260 chars[0] = ch ; chars[1] = (char) (ch + 1) ;
261 result &= testCompareString(chars, col) ;
264 tRes.tested("compareString()", result) ;
269 * Testing compareString() method. At first method is testing single chars
270 * comparing, then strings comparing.
271 * @param locChar sequence of at list two characters of a given locale
272 * to be used in comparing.
273 * @param col Collator for a given locale
274 * @return true if:
275 * <ol>
276 * <li> if comparing of two identical characters returns zero</li>
277 * <li> if comparing of two different characters returns non zero</li>
278 * <li> if comparing of two identical strings, composed of given chars
279 * returns zero</li>
280 * <li> if comparing of two different strings, composed of given chars
281 * returns non zero</li>
282 * </ol>
284 public boolean testCompareString(char[] locChar, Collator col) {
285 boolean result = true;
286 int res;
287 String msg = "";
289 String char0 = "_"+new String(new char[] {locChar[0]});
290 String char1 = "_"+new String(new char[] {locChar[1]});
291 res = oObj.compareString(char0 , char0) ;
292 if (res != 0) {
293 msg += " Testing collation of single equal characters ("
294 + toUnicode(char0) + ") ... FAILED\n" ;
296 result &= res == 0 ;
297 res = oObj.compareString(char0, char1) ;
298 if (res == 0) {
299 msg += " Testing collation of single different" +
300 " characters (" + toUnicode(char0+char1) +
301 ") ... FAILED (0 returned)\n" ;
302 msg += " Java collator returned " +
303 col.compare(char0, char1) + "\n" ;
304 result = false ;
305 } else { // opposite order - sum of results must be 0
306 res += oObj.compareString(char1, char0) ;
307 if (res != 0) {
308 msg += " Testing collation of single different" +
309 " characters (" + toUnicode(char0+char1) +
310 ") ... FAILED\n" ;
312 result &= res == 0 ;
315 String str1 = new String(new char[] {locChar[0], locChar[0],
316 locChar[1], locChar[1], locChar[1]}) ;
317 String str2 = new String(new char[] {locChar[0], locChar[0],
318 locChar[0], locChar[1], locChar[1]}) ;
320 res = oObj.compareString(str1 , str1) ;
321 if (res != 0) {
322 msg += " Testing collation of equal strings (" +
323 toUnicode(str1) + ") ... FAILED\n" ;
325 result &= res == 0 ;
326 res = oObj.compareString(str1, str2) ;
327 if (res == 0) {
328 msg += " Testing collation of different strings ((" +
329 toUnicode(str1) + "),(" + toUnicode(str2) +
330 ")) ... FAILED (0 returned)\n" ;
331 msg += " Java collator returned " +
332 col.compare(str1, str2) + "\n" ;
333 result = false ;
334 } else { // opposite order - sum of results must be
335 res += oObj.compareString(str2, str1) ;
336 if (res != 0) {
337 msg += " Testing collation of different strings ((" +
338 toUnicode(str1) + "),(" + toUnicode(str2) +
339 ")) ... FAILED\n" ;
341 result &= res == 0 ;
344 if (!result) {
345 log.println(msg) ;
347 return result ;
352 * Testing compareSubstring() method. Method is testing substrings comparing.
353 * @param locChar sequence of at list two characters of a given locale
354 * to be used in comparing.
355 * @param col Collator for a given locale
356 * @return true if:
357 * <ol>
358 * <li> if comparing of two identical substrings of strings, composed
359 * of given chars returns zero</li>
360 * <li> if comparing of two different substrings of strings, composed
361 * of given chars returns non zero</li>
362 * </ol>
364 public boolean testCompareSubstring(char[] locChar, Collator col) {
365 boolean result = true ;
366 int res ;
367 String msg = "" ;
369 String str1 = new String(new char[] {locChar[0], locChar[0],
370 locChar[1], locChar[1], locChar[1]}) ;
371 String str2 = new String(new char[] {locChar[0], locChar[0],
372 locChar[0], locChar[1], locChar[1]}) ;
374 res = oObj.compareSubstring(str1, 1, 2 , str2, 2, 2) ;
375 if (res != 0) {
376 msg += " Testing collation of equal substrings (" +
377 toUnicode(str1) + ") ... FAILED\n" ;
379 result &= res == 0 ;
380 res = oObj.compareSubstring(str1, 1, 2, str2, 1, 2) ;
381 if (res == 0) {
382 msg += " Testing collation of different strings ((" +
383 toUnicode(str1.substring(1, 3)) + "),(" +
384 toUnicode(str2.substring(1, 3))
385 + ")) ... FAILED (0 returned)\n" ;
386 msg += " Java collator returned " + col.compare
387 (str1.substring(1, 3), str2.substring(1, 3)) + "\n" ;
388 result = false ;
389 } else { // opposite order - sum of results must be
390 res += oObj.compareSubstring(str2, 1, 2, str1, 1, 2) ;
391 if (res != 0) {
392 msg += " Testing collation of different strings ((" +
393 toUnicode(str1) + "),(" + toUnicode(str2) +
394 ")) ... FAILED\n" ;
396 result &= res == 0 ;
399 if (!result) {
400 log.println(msg) ;
402 return result ;
407 * Transforms string to unicode hex codes.
408 * @param str String to be transformed
410 public String toUnicode(String str) {
411 char[] chars = str.toCharArray() ;
412 String res = "" ;
413 for (int i = 0; i < chars.length; i++) {
414 if (i != 0) res += "," ;
415 res += Integer.toHexString(chars[i]) ;
417 return res ;