1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
30 import java
.text
.Collator
;
32 import lib
.MultiMethodTest
;
34 import com
.sun
.star
.i18n
.CollatorOptions
;
35 import com
.sun
.star
.i18n
.XCollator
;
36 import com
.sun
.star
.lang
.Locale
;
39 * Testing <code>com.sun.star.i18n.XCollator</code>
42 * <li><code> compareSubstring()</code></li>
43 * <li><code> compareString()</code></li>
44 * <li><code> loadDefaultCollator()</code></li>
45 * <li><code> loadCollatorAlgorithm()</code></li>
46 * <li><code> listCollatorAlgorithms()</code></li>
47 * <li><code> loadCollatorAlgorithmWithEndUserOption()</code></li>
48 * <li><code> listCollatorOptions()</code></li>
50 * Test is <b> NOT </b> multithread compilant. <p>
51 * @see com.sun.star.i18n.XCollator
53 public class _XCollator
extends MultiMethodTest
{
54 public XCollator oObj
= null;
55 private String
[] alg
= null ;
56 private int[] opt
= null ;
57 Locale loc
= new Locale("en", "EN", "");
60 * Just retrieves a list of algorithms. <p>
61 * Has <b>OK</b> status if non-zero length array returned.
63 public void _listCollatorAlgorithms() {
64 alg
= oObj
.listCollatorAlgorithms(loc
) ;
65 log
.println("Collator algorithms :");
67 for (int i
= 0; i
< alg
.length
; i
++) {
68 log
.println(" '" + alg
[i
] + "'") ;
70 tRes
.tested("listCollatorAlgorithms()", alg
.length
> 0) ;
72 tRes
.tested("listCollatorAlgorithms()", false) ;
77 * Just gets a list of options for some collator. <p>
78 * Has <b>OK</b> status if not null value returned.<p>
79 * The following method tests are to be completed successfully before :
81 * <li> <code> listCollatorAlgorithms </code> : to have some
82 * algorithm name. </li>
85 public void _listCollatorOptions() {
86 requiredMethod("listCollatorAlgorithms()") ;
87 opt
= oObj
.listCollatorOptions(alg
[0]) ;
88 log
.println("Collator '" + alg
[0] + "' options :");
90 for (int i
= 0; i
< opt
.length
; i
++) {
91 log
.println(" " + opt
[i
]) ;
93 tRes
.tested("listCollatorOptions()", true) ;
95 tRes
.tested("listCollatorOptions()", false) ;
100 * Calls the method with no options and with options(IGNORE_CASE),
101 * compares strings.<p>
102 * Has <b>OK</b> status if compareString() returned correct values.
104 public void _loadDefaultCollator() {
105 oObj
.loadDefaultCollator(loc
, 0);
106 boolean res
= oObj
.compareString("A", "a") != 0;
107 oObj
.loadDefaultCollator(loc
,
108 CollatorOptions
.CollatorOptions_IGNORE_CASE
);
109 res
&= oObj
.compareString("a", "A") == 0;
110 tRes
.tested("loadDefaultCollator()", res
) ;
114 * Calls the method with no options and with options(IGNORE_CASE),
115 * compares strings.<p>
116 * Has <b>OK</b> status if compareString() returned correct values.
117 * The following method tests are to be completed successfully before :
119 * <li> <code> listCollatorAlgorithms </code> : to have some
120 * algorithm name. </li>
123 public void _loadCollatorAlgorithm() {
124 requiredMethod("listCollatorAlgorithms()");
125 oObj
.loadCollatorAlgorithm(alg
[0], loc
,
126 CollatorOptions
.CollatorOptions_IGNORE_CASE
);
127 boolean res
= oObj
.compareString("A", "a") == 0;
128 oObj
.loadCollatorAlgorithm(alg
[0], loc
, 0);
129 res
&= oObj
.compareString("a", "A") != 0;
130 tRes
.tested("loadCollatorAlgorithm()", res
);
134 * Calls the method with no options and with options(IGNORE_CASE),
135 * compares strings.<p>
136 * Has <b>OK</b> status if compareString() returned correct values.
137 * The following method tests are to be completed successfully before :
139 * <li> <code> listCollatorAlgorithms </code> : to have some
140 * algorithm name. </li>
143 public void _loadCollatorAlgorithmWithEndUserOption() {
144 requiredMethod("listCollatorAlgorithms()");
145 oObj
.loadCollatorAlgorithmWithEndUserOption(alg
[0], loc
,
147 boolean res
= oObj
.compareString("A", "a") != 0;
148 oObj
.loadCollatorAlgorithmWithEndUserOption(alg
[0], loc
,
149 new int[] {CollatorOptions
.CollatorOptions_IGNORE_CASE
});
150 res
= oObj
.compareString("A", "a") == 0;
151 tRes
.tested("loadCollatorAlgorithmWithEndUserOption()", res
);
155 * Test is performed for locales : en, ru, ja, zh, ko.
156 * Default collator is loaded for each locale. Then collation
157 * is performed for different combination of symbols from range of
159 * Has <b>OK</b> status if comparing of different strings
160 * returns not 0 value, then comparing in the opposite
161 * order returns value with opposite sign, and comparing
162 * of two equal strings returns 0. The such comparing is performed
163 * for one character strings.
165 public void _compareSubstring() {
166 boolean result
= true ;
167 char[] chars
= new char[2] ;
168 Collator col
= null ;
170 log
.println(" #### Testing English locale ####") ;
171 oObj
.loadDefaultCollator(loc
, 0) ;
172 col
= Collator
.getInstance(new java
.util
.Locale("en", "EN")) ;
173 for (char ch
= 0x0020; ch
< 0x007F; ch
++) {
174 chars
[0] = ch
; chars
[1] = (char) (ch
+ 1) ;
175 result
&= testCompareSubstring(chars
, col
) ;
178 log
.println(" #### Testing Russian locale ####") ;
179 oObj
.loadDefaultCollator(
180 new com
.sun
.star
.lang
.Locale("ru", "RU", ""), 0) ;
181 col
= Collator
.getInstance(new java
.util
.Locale("ru", "RU")) ;
182 for (char ch
= 0x0410; ch
< 0x0450; ch
++) {
183 chars
[0] = ch
; chars
[1] = (char) (ch
+ 1) ;
184 result
&= testCompareSubstring(chars
, col
) ;
187 log
.println(" #### Testing Japan locale ####") ;
188 oObj
.loadDefaultCollator(
189 new com
.sun
.star
.lang
.Locale("ja", "JP", ""), 0) ;
190 col
= Collator
.getInstance(new java
.util
.Locale("ja", "JP")) ;
191 for (char ch
= 0x4E00; ch
< 0x4EFD; ch
++) {
192 chars
[0] = ch
; chars
[1] = (char) (ch
+ 1) ;
193 result
&= testCompareSubstring(chars
, col
) ;
196 log
.println(" #### Testing China locale ####") ;
197 oObj
.loadDefaultCollator(new Locale("zh", "CN", ""), 0) ;
198 col
= Collator
.getInstance(new java
.util
.Locale("zh", "CN")) ;
199 for (char ch
= 0x4E00; ch
< 0x4EFD; ch
++) {
200 chars
[0] = ch
; chars
[1] = (char) (ch
+ 1) ;
201 result
&= testCompareSubstring(chars
, col
) ;
204 log
.println(" #### Testing Korean locale ####") ;
205 oObj
.loadDefaultCollator(new Locale("ko", "KR", ""), 0) ;
206 col
= Collator
.getInstance(new java
.util
.Locale("ko", "KR")) ;
207 for (char ch
= 0x4E00; ch
< 0x4EFD; ch
++) {
208 chars
[0] = ch
; chars
[1] = (char) (ch
+ 1) ;
209 result
&= testCompareSubstring(chars
, col
) ;
212 tRes
.tested("compareSubstring()", result
) ;
216 * Test is performed for locales : en, ru, ja, zh, ko.
217 * Default collator is loaded for each locale. Then collation
218 * is performed for different combination of symbols from range of
220 * Has <b>OK</b> status if comparing of different strings
221 * returns not 0 value, then comparing in the opposite
222 * order returns value with opposite sign, and comparing
223 * of two equal strings returns 0. The such comparing is performed
224 * for one character strings.
226 public void _compareString() {
227 boolean result
= true ;
228 char[] chars
= new char[2] ;
229 Collator col
= null ;
230 log
.println(" #### Testing English locale ####") ;
231 oObj
.loadDefaultCollator(
232 new com
.sun
.star
.lang
.Locale("en", "EN", ""), 0) ;
233 col
= Collator
.getInstance(new java
.util
.Locale("en", "EN")) ;
234 for (char ch
= 0x0020; ch
< 0x007F; ch
++) {
235 chars
[0] = ch
; chars
[1] = (char) (ch
+ 1) ;
236 result
&= testCompareString(chars
, col
) ;
239 log
.println(" #### Testing Russian locale ####") ;
240 oObj
.loadDefaultCollator(
241 new com
.sun
.star
.lang
.Locale("ru", "RU", ""), 0) ;
242 col
= Collator
.getInstance(new java
.util
.Locale("ru", "RU")) ;
243 for (char ch
= 0x0410; ch
< 0x0450; ch
++) {
244 chars
[0] = ch
; chars
[1] = (char) (ch
+ 1) ;
245 result
&= testCompareString(chars
, col
) ;
248 log
.println(" #### Testing Japan locale ####") ;
249 oObj
.loadDefaultCollator(
250 new com
.sun
.star
.lang
.Locale("ja", "JP", ""), 0) ;
251 col
= Collator
.getInstance(new java
.util
.Locale("ja", "JP")) ;
252 for (char ch
= 0x4E00; ch
< 0x4EFD; ch
++) {
253 chars
[0] = ch
; chars
[1] = (char) (ch
+ 1) ;
254 result
&= testCompareString(chars
, col
) ;
257 log
.println(" #### Testing China locale ####") ;
258 oObj
.loadDefaultCollator(new Locale("zh", "CN", ""), 0) ;
259 col
= Collator
.getInstance(new java
.util
.Locale("zh", "CN")) ;
260 for (char ch
= 0x4E00; ch
< 0x4EFD; ch
++) {
261 chars
[0] = ch
; chars
[1] = (char) (ch
+ 1) ;
262 result
&= testCompareString(chars
, col
) ;
265 log
.println(" #### Testing Korean locale ####") ;
266 oObj
.loadDefaultCollator(new Locale("ko", "KR", ""), 0) ;
267 col
= Collator
.getInstance(new java
.util
.Locale("ko", "KR")) ;
268 for (char ch
= 0x4E00; ch
< 0x4EFD; ch
++) {
269 chars
[0] = ch
; chars
[1] = (char) (ch
+ 1) ;
270 result
&= testCompareString(chars
, col
) ;
273 tRes
.tested("compareString()", result
) ;
278 * Testing compareString() method. At first method is testing single chars
279 * comparing, then strings comparing.
280 * @param locChar sequence of at list two characters of a given locale
281 * to be used in comparing.
282 * @param col Collator for a given locale
285 * <li> if comparing of two identical characters returns zero</li>
286 * <li> if comparing of two different characters returns non zero</li>
287 * <li> if comparing of two identical strings, composed of given chars
289 * <li> if comparing of two different strings, composed of given chars
290 * returns non zero</li>
293 public boolean testCompareString(char[] locChar
, Collator col
) {
294 boolean result
= true;
298 String char0
= "_"+new String(new char[] {locChar
[0]});
299 String char1
= "_"+new String(new char[] {locChar
[1]});
300 res
= oObj
.compareString(char0
, char0
) ;
302 msg
+= " Testing collation of single equal characters ("
303 + toUnicode(char0
) + ") ... FAILED\n" ;
306 res
= oObj
.compareString(char0
, char1
) ;
308 msg
+= " Testing collation of single different" +
309 " characters (" + toUnicode(char0
+char1
) +
310 ") ... FAILED (0 returned)\n" ;
311 msg
+= " Java collator returned " +
312 col
.compare(char0
, char1
) + "\n" ;
314 } else { // opposite order - sum of results must be 0
315 res
+= oObj
.compareString(char1
, char0
) ;
317 msg
+= " Testing collation of single different" +
318 " characters (" + toUnicode(char0
+char1
) +
324 String str1
= new String(new char[] {locChar
[0], locChar
[0],
325 locChar
[1], locChar
[1], locChar
[1]}) ;
326 String str2
= new String(new char[] {locChar
[0], locChar
[0],
327 locChar
[0], locChar
[1], locChar
[1]}) ;
329 res
= oObj
.compareString(str1
, str1
) ;
331 msg
+= " Testing collation of equal strings (" +
332 toUnicode(str1
) + ") ... FAILED\n" ;
335 res
= oObj
.compareString(str1
, str2
) ;
337 msg
+= " Testing collation of different strings ((" +
338 toUnicode(str1
) + "),(" + toUnicode(str2
) +
339 ")) ... FAILED (0 returned)\n" ;
340 msg
+= " Java collator returned " +
341 col
.compare(str1
, str2
) + "\n" ;
343 } else { // opposite order - sum of results must be
344 res
+= oObj
.compareString(str2
, str1
) ;
346 msg
+= " Testing collation of different strings ((" +
347 toUnicode(str1
) + "),(" + toUnicode(str2
) +
361 * Testing compareSubstring() method. Method is testing substrings comparing.
362 * @param locChar sequence of at list two characters of a given locale
363 * to be used in comparing.
364 * @param col Collator for a given locale
367 * <li> if comparing of two identical substrings of strings, composed
368 * of given chars returns zero</li>
369 * <li> if comparing of two different substrings of strings, composed
370 * of given chars returns non zero</li>
373 public boolean testCompareSubstring(char[] locChar
, Collator col
) {
374 boolean result
= true ;
378 String str1
= new String(new char[] {locChar
[0], locChar
[0],
379 locChar
[1], locChar
[1], locChar
[1]}) ;
380 String str2
= new String(new char[] {locChar
[0], locChar
[0],
381 locChar
[0], locChar
[1], locChar
[1]}) ;
383 res
= oObj
.compareSubstring(str1
, 1, 2 , str2
, 2, 2) ;
385 msg
+= " Testing collation of equal substrings (" +
386 toUnicode(str1
) + ") ... FAILED\n" ;
389 res
= oObj
.compareSubstring(str1
, 1, 2, str2
, 1, 2) ;
391 msg
+= " Testing collation of different strings ((" +
392 toUnicode(str1
.substring(1, 3)) + "),(" +
393 toUnicode(str2
.substring(1, 3))
394 + ")) ... FAILED (0 returned)\n" ;
395 msg
+= " Java collator returned " + col
.compare
396 (str1
.substring(1, 3), str2
.substring(1, 3)) + "\n" ;
398 } else { // opposite order - sum of results must be
399 res
+= oObj
.compareSubstring(str2
, 1, 2, str1
, 1, 2) ;
401 msg
+= " Testing collation of different strings ((" +
402 toUnicode(str1
) + "),(" + toUnicode(str2
) +
416 * Transforms string to unicode hex codes.
417 * @param str String to be transformed
419 public String
toUnicode(String str
) {
420 char[] chars
= str
.toCharArray() ;
422 for (int i
= 0; i
< chars
.length
; i
++) {
423 if (i
!= 0) res
+= "," ;
424 res
+= Integer
.toHexString(chars
[i
]) ;