Update ooo320-m1
[ooovba.git] / qadevOOo / tests / java / ifc / i18n / _XCollator.java
blob81a5c5ff4f4ebece402ab67c2b8a3712a103c475
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: _XCollator.java,v $
10 * $Revision: 1.4 $
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 java.text.Collator;
35 import lib.MultiMethodTest;
37 import com.sun.star.i18n.CollatorOptions;
38 import com.sun.star.i18n.XCollator;
39 import com.sun.star.lang.Locale;
41 /**
42 * Testing <code>com.sun.star.i18n.XCollator</code>
43 * interface methods :
44 * <ul>
45 * <li><code> compareSubstring()</code></li>
46 * <li><code> compareString()</code></li>
47 * <li><code> loadDefaultCollator()</code></li>
48 * <li><code> loadCollatorAlgorithm()</code></li>
49 * <li><code> listCollatorAlgorithms()</code></li>
50 * <li><code> loadCollatorAlgorithmWithEndUserOption()</code></li>
51 * <li><code> listCollatorOptions()</code></li>
52 * </ul> <p>
53 * Test is <b> NOT </b> multithread compilant. <p>
54 * @see com.sun.star.i18n.XCollator
56 public class _XCollator extends MultiMethodTest {
57 public XCollator oObj = null;
58 private String[] alg = null ;
59 private int[] opt = null ;
60 Locale loc = new Locale("en", "EN", "");
62 /**
63 * Just retrieves a list of algorithms. <p>
64 * Has <b>OK</b> status if non-zero length array returned.
66 public void _listCollatorAlgorithms() {
67 alg = oObj.listCollatorAlgorithms(loc) ;
68 log.println("Collator algorithms :");
69 if (alg != null) {
70 for (int i = 0; i < alg.length; i++) {
71 log.println(" '" + alg[i] + "'") ;
73 tRes.tested("listCollatorAlgorithms()", alg.length > 0) ;
74 } else {
75 tRes.tested("listCollatorAlgorithms()", false) ;
79 /**
80 * Just gets a list of options for some collator. <p>
81 * Has <b>OK</b> status if not null value returned.<p>
82 * The following method tests are to be completed successfully before :
83 * <ul>
84 * <li> <code> listCollatorAlgorithms </code> : to have some
85 * algorithm name. </li>
86 * </ul>
88 public void _listCollatorOptions() {
89 requiredMethod("listCollatorAlgorithms()") ;
90 opt = oObj.listCollatorOptions(alg[0]) ;
91 log.println("Collator '" + alg[0] + "' options :");
92 if (opt != null) {
93 for (int i = 0; i < opt.length; i++) {
94 log.println(" " + opt[i]) ;
96 tRes.tested("listCollatorOptions()", true) ;
97 } else {
98 tRes.tested("listCollatorOptions()", false) ;
103 * Calls the method with no options and with options(IGNORE_CASE),
104 * compares strings.<p>
105 * Has <b>OK</b> status if compareString() returned correct values.
107 public void _loadDefaultCollator() {
108 oObj.loadDefaultCollator(loc, 0);
109 boolean res = oObj.compareString("A", "a") != 0;
110 oObj.loadDefaultCollator(loc,
111 CollatorOptions.CollatorOptions_IGNORE_CASE);
112 res &= oObj.compareString("a", "A") == 0;
113 tRes.tested("loadDefaultCollator()", res) ;
117 * Calls the method with no options and with options(IGNORE_CASE),
118 * compares strings.<p>
119 * Has <b>OK</b> status if compareString() returned correct values.
120 * The following method tests are to be completed successfully before :
121 * <ul>
122 * <li> <code> listCollatorAlgorithms </code> : to have some
123 * algorithm name. </li>
124 * </ul>
126 public void _loadCollatorAlgorithm() {
127 requiredMethod("listCollatorAlgorithms()");
128 oObj.loadCollatorAlgorithm(alg[0], loc,
129 CollatorOptions.CollatorOptions_IGNORE_CASE);
130 boolean res = oObj.compareString("A", "a") == 0;
131 oObj.loadCollatorAlgorithm(alg[0], loc, 0);
132 res &= oObj.compareString("a", "A") != 0;
133 tRes.tested("loadCollatorAlgorithm()", res);
137 * Calls the method with no options and with options(IGNORE_CASE),
138 * compares strings.<p>
139 * Has <b>OK</b> status if compareString() returned correct values.
140 * The following method tests are to be completed successfully before :
141 * <ul>
142 * <li> <code> listCollatorAlgorithms </code> : to have some
143 * algorithm name. </li>
144 * </ul>
146 public void _loadCollatorAlgorithmWithEndUserOption() {
147 requiredMethod("listCollatorAlgorithms()");
148 oObj.loadCollatorAlgorithmWithEndUserOption(alg[0], loc,
149 new int[] {0});
150 boolean res = oObj.compareString("A", "a") != 0;
151 oObj.loadCollatorAlgorithmWithEndUserOption(alg[0], loc,
152 new int[] {CollatorOptions.CollatorOptions_IGNORE_CASE});
153 res = oObj.compareString("A", "a") == 0;
154 tRes.tested("loadCollatorAlgorithmWithEndUserOption()", res);
158 * Test is performed for locales : en, ru, ja, zh, ko.
159 * Default collator is loaded for each locale. Then collation
160 * is performed for different combination of symbols from range of
161 * this locale.<p>
162 * Has <b>OK</b> status if comparing of different strings
163 * returns not 0 value, then comparing in the opposite
164 * order returns value with opposite sign, and comparing
165 * of two equal strings returns 0. The such comparing is performed
166 * for one character strings.
168 public void _compareSubstring() {
169 boolean result = true ;
170 char[] chars = new char[2] ;
171 Collator col = null ;
173 log.println(" #### Testing English locale ####") ;
174 oObj.loadDefaultCollator(loc, 0) ;
175 col = Collator.getInstance(new java.util.Locale("en", "EN")) ;
176 for (char ch = 0x0020; ch < 0x007F; ch ++) {
177 chars[0] = ch ; chars[1] = (char) (ch + 1) ;
178 result &= testCompareSubstring(chars, col) ;
181 log.println(" #### Testing Russian locale ####") ;
182 oObj.loadDefaultCollator(
183 new com.sun.star.lang.Locale("ru", "RU", ""), 0) ;
184 col = Collator.getInstance(new java.util.Locale("ru", "RU")) ;
185 for (char ch = 0x0410; ch < 0x0450; ch ++) {
186 chars[0] = ch ; chars[1] = (char) (ch + 1) ;
187 result &= testCompareSubstring(chars, col) ;
190 log.println(" #### Testing Japan locale ####") ;
191 oObj.loadDefaultCollator(
192 new com.sun.star.lang.Locale("ja", "JP", ""), 0) ;
193 col = Collator.getInstance(new java.util.Locale("ja", "JP")) ;
194 for (char ch = 0x4E00; ch < 0x4EFD; ch ++) {
195 chars[0] = ch ; chars[1] = (char) (ch + 1) ;
196 result &= testCompareSubstring(chars, col) ;
199 log.println(" #### Testing China locale ####") ;
200 oObj.loadDefaultCollator(new Locale("zh", "CN", ""), 0) ;
201 col = Collator.getInstance(new java.util.Locale("zh", "CN")) ;
202 for (char ch = 0x4E00; ch < 0x4EFD; ch ++) {
203 chars[0] = ch ; chars[1] = (char) (ch + 1) ;
204 result &= testCompareSubstring(chars, col) ;
207 log.println(" #### Testing Korean locale ####") ;
208 oObj.loadDefaultCollator(new Locale("ko", "KR", ""), 0) ;
209 col = Collator.getInstance(new java.util.Locale("ko", "KR")) ;
210 for (char ch = 0x4E00; ch < 0x4EFD; ch ++) {
211 chars[0] = ch ; chars[1] = (char) (ch + 1) ;
212 result &= testCompareSubstring(chars, col) ;
215 tRes.tested("compareSubstring()", result) ;
219 * Test is performed for locales : en, ru, ja, zh, ko.
220 * Default collator is loaded for each locale. Then collation
221 * is performed for different combination of symbols from range of
222 * this locale.<p>
223 * Has <b>OK</b> status if comparing of different strings
224 * returns not 0 value, then comparing in the opposite
225 * order returns value with opposite sign, and comparing
226 * of two equal strings returns 0. The such comparing is performed
227 * for one character strings.
229 public void _compareString() {
230 boolean result = true ;
231 char[] chars = new char[2] ;
232 Collator col = null ;
233 log.println(" #### Testing English locale ####") ;
234 oObj.loadDefaultCollator(
235 new com.sun.star.lang.Locale("en", "EN", ""), 0) ;
236 col = Collator.getInstance(new java.util.Locale("en", "EN")) ;
237 for (char ch = 0x0020; ch < 0x007F; ch ++) {
238 chars[0] = ch ; chars[1] = (char) (ch + 1) ;
239 result &= testCompareString(chars, col) ;
242 log.println(" #### Testing Russian locale ####") ;
243 oObj.loadDefaultCollator(
244 new com.sun.star.lang.Locale("ru", "RU", ""), 0) ;
245 col = Collator.getInstance(new java.util.Locale("ru", "RU")) ;
246 for (char ch = 0x0410; ch < 0x0450; ch ++) {
247 chars[0] = ch ; chars[1] = (char) (ch + 1) ;
248 result &= testCompareString(chars, col) ;
251 log.println(" #### Testing Japan locale ####") ;
252 oObj.loadDefaultCollator(
253 new com.sun.star.lang.Locale("ja", "JP", ""), 0) ;
254 col = Collator.getInstance(new java.util.Locale("ja", "JP")) ;
255 for (char ch = 0x4E00; ch < 0x4EFD; ch ++) {
256 chars[0] = ch ; chars[1] = (char) (ch + 1) ;
257 result &= testCompareString(chars, col) ;
260 log.println(" #### Testing China locale ####") ;
261 oObj.loadDefaultCollator(new Locale("zh", "CN", ""), 0) ;
262 col = Collator.getInstance(new java.util.Locale("zh", "CN")) ;
263 for (char ch = 0x4E00; ch < 0x4EFD; ch ++) {
264 chars[0] = ch ; chars[1] = (char) (ch + 1) ;
265 result &= testCompareString(chars, col) ;
268 log.println(" #### Testing Korean locale ####") ;
269 oObj.loadDefaultCollator(new Locale("ko", "KR", ""), 0) ;
270 col = Collator.getInstance(new java.util.Locale("ko", "KR")) ;
271 for (char ch = 0x4E00; ch < 0x4EFD; ch ++) {
272 chars[0] = ch ; chars[1] = (char) (ch + 1) ;
273 result &= testCompareString(chars, col) ;
276 tRes.tested("compareString()", result) ;
281 * Testing compareString() method. At first method is testing single chars
282 * comparing, then strings comparing.
283 * @param locChar sequence of at list two characters of a given locale
284 * to be used in comparing.
285 * @param col Collator for a given locale
286 * @return true if:
287 * <ol>
288 * <li> if comparing of two identical characters returns zero</li>
289 * <li> if comparing of two different characters returns non zero</li>
290 * <li> if comparing of two identical strings, composed of given chars
291 * returns zero</li>
292 * <li> if comparing of two different strings, composed of given chars
293 * returns non zero</li>
294 * </ol>
296 public boolean testCompareString(char[] locChar, Collator col) {
297 boolean result = true;
298 int res;
299 String msg = "";
301 String char0 = "_"+new String(new char[] {locChar[0]});
302 String char1 = "_"+new String(new char[] {locChar[1]});
303 res = oObj.compareString(char0 , char0) ;
304 if (res != 0) {
305 msg += " Testing collation of single equal characters ("
306 + toUnicode(char0) + ") ... FAILED\n" ;
308 result &= res == 0 ;
309 res = oObj.compareString(char0, char1) ;
310 if (res == 0) {
311 msg += " Testing collation of single different" +
312 " characters (" + toUnicode(char0+char1) +
313 ") ... FAILED (0 returned)\n" ;
314 msg += " Java collator returned " +
315 col.compare(char0, char1) + "\n" ;
316 result = false ;
317 } else { // opposite order - sum of results must be 0
318 res += oObj.compareString(char1, char0) ;
319 if (res != 0) {
320 msg += " Testing collation of single different" +
321 " characters (" + toUnicode(char0+char1) +
322 ") ... FAILED\n" ;
324 result &= res == 0 ;
327 String str1 = new String(new char[] {locChar[0], locChar[0],
328 locChar[1], locChar[1], locChar[1]}) ;
329 String str2 = new String(new char[] {locChar[0], locChar[0],
330 locChar[0], locChar[1], locChar[1]}) ;
332 res = oObj.compareString(str1 , str1) ;
333 if (res != 0) {
334 msg += " Testing collation of equal strings (" +
335 toUnicode(str1) + ") ... FAILED\n" ;
337 result &= res == 0 ;
338 res = oObj.compareString(str1, str2) ;
339 if (res == 0) {
340 msg += " Testing collation of different strings ((" +
341 toUnicode(str1) + "),(" + toUnicode(str2) +
342 ")) ... FAILED (0 returned)\n" ;
343 msg += " Java collator returned " +
344 col.compare(str1, str2) + "\n" ;
345 result = false ;
346 } else { // opposite order - sum of results must be
347 res += oObj.compareString(str2, str1) ;
348 if (res != 0) {
349 msg += " Testing collation of different strings ((" +
350 toUnicode(str1) + "),(" + toUnicode(str2) +
351 ")) ... FAILED\n" ;
353 result &= res == 0 ;
356 if (!result) {
357 log.println(msg) ;
359 return result ;
364 * Testing compareSubstring() method. Method is testing substrings comparing.
365 * @param locChar sequence of at list two characters of a given locale
366 * to be used in comparing.
367 * @param col Collator for a given locale
368 * @return true if:
369 * <ol>
370 * <li> if comparing of two identical substrings of strings, composed
371 * of given chars returns zero</li>
372 * <li> if comparing of two different substrings of strings, composed
373 * of given chars returns non zero</li>
374 * </ol>
376 public boolean testCompareSubstring(char[] locChar, Collator col) {
377 boolean result = true ;
378 int res ;
379 String msg = "" ;
381 String str1 = new String(new char[] {locChar[0], locChar[0],
382 locChar[1], locChar[1], locChar[1]}) ;
383 String str2 = new String(new char[] {locChar[0], locChar[0],
384 locChar[0], locChar[1], locChar[1]}) ;
386 res = oObj.compareSubstring(str1, 1, 2 , str2, 2, 2) ;
387 if (res != 0) {
388 msg += " Testing collation of equal substrings (" +
389 toUnicode(str1) + ") ... FAILED\n" ;
391 result &= res == 0 ;
392 res = oObj.compareSubstring(str1, 1, 2, str2, 1, 2) ;
393 if (res == 0) {
394 msg += " Testing collation of different strings ((" +
395 toUnicode(str1.substring(1, 3)) + "),(" +
396 toUnicode(str2.substring(1, 3))
397 + ")) ... FAILED (0 returned)\n" ;
398 msg += " Java collator returned " + col.compare
399 (str1.substring(1, 3), str2.substring(1, 3)) + "\n" ;
400 result = false ;
401 } else { // opposite order - sum of results must be
402 res += oObj.compareSubstring(str2, 1, 2, str1, 1, 2) ;
403 if (res != 0) {
404 msg += " Testing collation of different strings ((" +
405 toUnicode(str1) + "),(" + toUnicode(str2) +
406 ")) ... FAILED\n" ;
408 result &= res == 0 ;
411 if (!result) {
412 log.println(msg) ;
414 return result ;
419 * Transforms string to unicode hex codes.
420 * @param str String to be transformed
422 public String toUnicode(String str) {
423 char[] chars = str.toCharArray() ;
424 String res = "" ;
425 for (int i = 0; i < chars.length; i++) {
426 if (i != 0) res += "," ;
427 res += Integer.toHexString(chars[i]) ;
429 return res ;