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 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
;
30 * Testing <code>com.sun.star.i18n.XCollator</code>
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>
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", "");
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 :");
58 for (int i
= 0; i
< alg
.length
; i
++) {
59 log
.println(" '" + alg
[i
] + "'") ;
61 tRes
.tested("listCollatorAlgorithms()", alg
.length
> 0) ;
63 tRes
.tested("listCollatorAlgorithms()", false) ;
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 :
72 * <li> <code> listCollatorAlgorithms </code> : to have some
73 * algorithm name. </li>
76 public void _listCollatorOptions() {
77 requiredMethod("listCollatorAlgorithms()") ;
78 opt
= oObj
.listCollatorOptions(alg
[0]) ;
79 log
.println("Collator '" + alg
[0] + "' options :");
81 for (int i
= 0; i
< opt
.length
; i
++) {
82 log
.println(" " + opt
[i
]) ;
84 tRes
.tested("listCollatorOptions()", true) ;
86 tRes
.tested("listCollatorOptions()", false) ;
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 :
110 * <li> <code> listCollatorAlgorithms </code> : to have some
111 * algorithm name. </li>
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 :
130 * <li> <code> listCollatorAlgorithms </code> : to have some
131 * algorithm name. </li>
134 public void _loadCollatorAlgorithmWithEndUserOption() {
135 requiredMethod("listCollatorAlgorithms()");
136 oObj
.loadCollatorAlgorithmWithEndUserOption(alg
[0], loc
,
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
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
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
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
280 * <li> if comparing of two different strings, composed of given chars
281 * returns non zero</li>
284 public boolean testCompareString(char[] locChar
, Collator col
) {
285 boolean result
= true;
289 String char0
= "_"+new String(new char[] {locChar
[0]});
290 String char1
= "_"+new String(new char[] {locChar
[1]});
291 res
= oObj
.compareString(char0
, char0
) ;
293 msg
+= " Testing collation of single equal characters ("
294 + toUnicode(char0
) + ") ... FAILED\n" ;
297 res
= oObj
.compareString(char0
, char1
) ;
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" ;
305 } else { // opposite order - sum of results must be 0
306 res
+= oObj
.compareString(char1
, char0
) ;
308 msg
+= " Testing collation of single different" +
309 " characters (" + toUnicode(char0
+char1
) +
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
) ;
322 msg
+= " Testing collation of equal strings (" +
323 toUnicode(str1
) + ") ... FAILED\n" ;
326 res
= oObj
.compareString(str1
, str2
) ;
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" ;
334 } else { // opposite order - sum of results must be
335 res
+= oObj
.compareString(str2
, str1
) ;
337 msg
+= " Testing collation of different strings ((" +
338 toUnicode(str1
) + "),(" + toUnicode(str2
) +
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
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>
364 public boolean testCompareSubstring(char[] locChar
, Collator col
) {
365 boolean result
= true ;
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) ;
376 msg
+= " Testing collation of equal substrings (" +
377 toUnicode(str1
) + ") ... FAILED\n" ;
380 res
= oObj
.compareSubstring(str1
, 1, 2, str2
, 1, 2) ;
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" ;
389 } else { // opposite order - sum of results must be
390 res
+= oObj
.compareSubstring(str2
, 1, 2, str1
, 1, 2) ;
392 msg
+= " Testing collation of different strings ((" +
393 toUnicode(str1
) + "),(" + toUnicode(str2
) +
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() ;
413 for (int i
= 0; i
< chars
.length
; i
++) {
414 if (i
!= 0) res
+= "," ;
415 res
+= Integer
.toHexString(chars
[i
]) ;