Avoid potential negative array index access to cached text.
[LibreOffice.git] / qadevOOo / tests / java / ifc / i18n / _XCollator.java
bloba9eece7f7bcc4b16a0c22199df25b086bcf575aa
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 compliant. <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 Locale loc = new Locale("en", "EN", "");
49 /**
50 * Just retrieves a list of algorithms. <p>
51 * Has <b>OK</b> status if non-zero length array returned.
53 public void _listCollatorAlgorithms() {
54 alg = oObj.listCollatorAlgorithms(loc) ;
55 log.println("Collator algorithms :");
56 if (alg != null) {
57 for (int i = 0; i < alg.length; i++) {
58 log.println(" '" + alg[i] + "'") ;
60 tRes.tested("listCollatorAlgorithms()", alg.length > 0) ;
61 } else {
62 tRes.tested("listCollatorAlgorithms()", false) ;
66 /**
67 * Just gets a list of options for some collator. <p>
68 * Has <b>OK</b> status if not null value returned.<p>
69 * The following method tests are to be completed successfully before :
70 * <ul>
71 * <li> <code> listCollatorAlgorithms </code> : to have some
72 * algorithm name. </li>
73 * </ul>
75 public void _listCollatorOptions() {
76 requiredMethod("listCollatorAlgorithms()") ;
77 int[] opt = oObj.listCollatorOptions(alg[0]) ;
78 log.println("Collator '" + alg[0] + "' options :");
79 if (opt != null) {
80 for (int i = 0; i < opt.length; i++) {
81 log.println(" " + opt[i]) ;
83 tRes.tested("listCollatorOptions()", true) ;
84 } else {
85 tRes.tested("listCollatorOptions()", false) ;
89 /**
90 * Calls the method with no options and with options(IGNORE_CASE),
91 * compares strings.<p>
92 * Has <b>OK</b> status if compareString() returned correct values.
94 public void _loadDefaultCollator() {
95 oObj.loadDefaultCollator(loc, 0);
96 boolean res = oObj.compareString("A", "a") != 0;
97 oObj.loadDefaultCollator(loc,
98 CollatorOptions.CollatorOptions_IGNORE_CASE);
99 res &= oObj.compareString("a", "A") == 0;
100 tRes.tested("loadDefaultCollator()", res) ;
104 * Calls the method with no options and with options(IGNORE_CASE),
105 * compares strings.<p>
106 * Has <b>OK</b> status if compareString() returned correct values.
107 * The following method tests are to be completed successfully before :
108 * <ul>
109 * <li> <code> listCollatorAlgorithms </code> : to have some
110 * algorithm name. </li>
111 * </ul>
113 public void _loadCollatorAlgorithm() {
114 requiredMethod("listCollatorAlgorithms()");
115 oObj.loadCollatorAlgorithm(alg[0], loc,
116 CollatorOptions.CollatorOptions_IGNORE_CASE);
117 boolean res = oObj.compareString("A", "a") == 0;
118 oObj.loadCollatorAlgorithm(alg[0], loc, 0);
119 res &= oObj.compareString("a", "A") != 0;
120 tRes.tested("loadCollatorAlgorithm()", res);
124 * Calls the method with no options and with options(IGNORE_CASE),
125 * compares strings.<p>
126 * Has <b>OK</b> status if compareString() returned correct values.
127 * The following method tests are to be completed successfully before :
128 * <ul>
129 * <li> <code> listCollatorAlgorithms </code> : to have some
130 * algorithm name. </li>
131 * </ul>
133 public void _loadCollatorAlgorithmWithEndUserOption() {
134 requiredMethod("listCollatorAlgorithms()");
135 oObj.loadCollatorAlgorithmWithEndUserOption(alg[0], loc,
136 new int[] {0});
137 boolean res = oObj.compareString("A", "a") != 0;
138 oObj.loadCollatorAlgorithmWithEndUserOption(alg[0], loc,
139 new int[] {CollatorOptions.CollatorOptions_IGNORE_CASE});
140 res = oObj.compareString("A", "a") == 0;
141 tRes.tested("loadCollatorAlgorithmWithEndUserOption()", res);
145 * Test is performed for locales : en, ru, ja, zh, ko.
146 * Default collator is loaded for each locale. Then collation
147 * is performed for different combination of symbols from range of
148 * this locale.<p>
149 * Has <b>OK</b> status if comparing of different strings
150 * returns not 0 value, then comparing in the opposite
151 * order returns value with opposite sign, and comparing
152 * of two equal strings returns 0. The such comparing is performed
153 * for one character strings.
155 public void _compareSubstring() {
156 boolean result = true ;
157 char[] chars = new char[2] ;
158 Collator col = null ;
160 log.println(" #### Testing English locale ####") ;
161 oObj.loadDefaultCollator(loc, 0) ;
162 col = Collator.getInstance(new java.util.Locale("en", "EN")) ;
163 for (char ch = 0x0020; ch < 0x007F; ch ++) {
164 chars[0] = ch ; chars[1] = (char) (ch + 1) ;
165 result &= testCompareSubstring(chars, col) ;
168 log.println(" #### Testing Russian locale ####") ;
169 oObj.loadDefaultCollator(
170 new com.sun.star.lang.Locale("ru", "RU", ""), 0) ;
171 col = Collator.getInstance(new java.util.Locale("ru", "RU")) ;
172 for (char ch = 0x0410; ch < 0x0450; ch ++) {
173 chars[0] = ch ; chars[1] = (char) (ch + 1) ;
174 result &= testCompareSubstring(chars, col) ;
177 log.println(" #### Testing Japan locale ####") ;
178 oObj.loadDefaultCollator(
179 new com.sun.star.lang.Locale("ja", "JP", ""), 0) ;
180 col = Collator.getInstance(new java.util.Locale("ja", "JP")) ;
181 for (char ch = 0x4E00; ch < 0x4EFD; ch ++) {
182 chars[0] = ch ; chars[1] = (char) (ch + 1) ;
183 result &= testCompareSubstring(chars, col) ;
186 log.println(" #### Testing China locale ####") ;
187 oObj.loadDefaultCollator(new Locale("zh", "CN", ""), 0) ;
188 col = Collator.getInstance(new java.util.Locale("zh", "CN")) ;
189 for (char ch = 0x4E00; ch < 0x4EFD; ch ++) {
190 chars[0] = ch ; chars[1] = (char) (ch + 1) ;
191 result &= testCompareSubstring(chars, col) ;
194 log.println(" #### Testing Korean locale ####") ;
195 oObj.loadDefaultCollator(new Locale("ko", "KR", ""), 0) ;
196 col = Collator.getInstance(new java.util.Locale("ko", "KR")) ;
197 for (char ch = 0x4E00; ch < 0x4EFD; ch ++) {
198 chars[0] = ch ; chars[1] = (char) (ch + 1) ;
199 result &= testCompareSubstring(chars, col) ;
202 tRes.tested("compareSubstring()", result) ;
206 * Test is performed for locales : en, ru, ja, zh, ko.
207 * Default collator is loaded for each locale. Then collation
208 * is performed for different combination of symbols from range of
209 * this locale.<p>
210 * Has <b>OK</b> status if comparing of different strings
211 * returns not 0 value, then comparing in the opposite
212 * order returns value with opposite sign, and comparing
213 * of two equal strings returns 0. The such comparing is performed
214 * for one character strings.
216 public void _compareString() {
217 boolean result = true ;
218 char[] chars = new char[2] ;
219 Collator col = null ;
220 log.println(" #### Testing English locale ####") ;
221 oObj.loadDefaultCollator(
222 new com.sun.star.lang.Locale("en", "EN", ""), 0) ;
223 col = Collator.getInstance(new java.util.Locale("en", "EN")) ;
224 for (char ch = 0x0020; ch < 0x007F; ch ++) {
225 chars[0] = ch ; chars[1] = (char) (ch + 1) ;
226 result &= testCompareString(chars, col) ;
229 log.println(" #### Testing Russian locale ####") ;
230 oObj.loadDefaultCollator(
231 new com.sun.star.lang.Locale("ru", "RU", ""), 0) ;
232 col = Collator.getInstance(new java.util.Locale("ru", "RU")) ;
233 for (char ch = 0x0410; ch < 0x0450; ch ++) {
234 chars[0] = ch ; chars[1] = (char) (ch + 1) ;
235 result &= testCompareString(chars, col) ;
238 log.println(" #### Testing Japan locale ####") ;
239 oObj.loadDefaultCollator(
240 new com.sun.star.lang.Locale("ja", "JP", ""), 0) ;
241 col = Collator.getInstance(new java.util.Locale("ja", "JP")) ;
242 for (char ch = 0x4E00; ch < 0x4EFD; ch ++) {
243 chars[0] = ch ; chars[1] = (char) (ch + 1) ;
244 result &= testCompareString(chars, col) ;
247 log.println(" #### Testing China locale ####") ;
248 oObj.loadDefaultCollator(new Locale("zh", "CN", ""), 0) ;
249 col = Collator.getInstance(new java.util.Locale("zh", "CN")) ;
250 for (char ch = 0x4E00; ch < 0x4EFD; ch ++) {
251 chars[0] = ch ; chars[1] = (char) (ch + 1) ;
252 result &= testCompareString(chars, col) ;
255 log.println(" #### Testing Korean locale ####") ;
256 oObj.loadDefaultCollator(new Locale("ko", "KR", ""), 0) ;
257 col = Collator.getInstance(new java.util.Locale("ko", "KR")) ;
258 for (char ch = 0x4E00; ch < 0x4EFD; ch ++) {
259 chars[0] = ch ; chars[1] = (char) (ch + 1) ;
260 result &= testCompareString(chars, col) ;
263 tRes.tested("compareString()", result) ;
268 * Testing compareString() method. At first method is testing single chars
269 * comparing, then strings comparing.
270 * @param locChar sequence of at list two characters of a given locale
271 * to be used in comparing.
272 * @param col Collator for a given locale
273 * @return true if:
274 * <ol>
275 * <li> if comparing of two identical characters returns zero</li>
276 * <li> if comparing of two different characters returns non zero</li>
277 * <li> if comparing of two identical strings, composed of given chars
278 * returns zero</li>
279 * <li> if comparing of two different strings, composed of given chars
280 * returns non zero</li>
281 * </ol>
283 public boolean testCompareString(char[] locChar, Collator col) {
284 boolean result = true;
285 int res;
286 String msg = "";
288 String char0 = "_"+new String(new char[] {locChar[0]});
289 String char1 = "_"+new String(new char[] {locChar[1]});
290 res = oObj.compareString(char0 , char0) ;
291 if (res != 0) {
292 msg += " Testing collation of single equal characters ("
293 + toUnicode(char0) + ") ... FAILED\n" ;
295 result &= res == 0 ;
296 res = oObj.compareString(char0, char1) ;
297 if (res == 0) {
298 msg += " Testing collation of single different" +
299 " characters (" + toUnicode(char0+char1) +
300 ") ... FAILED (0 returned)\n" ;
301 msg += " Java collator returned " +
302 col.compare(char0, char1) + "\n" ;
303 result = false ;
304 } else { // opposite order - sum of results must be 0
305 res += oObj.compareString(char1, char0) ;
306 if (res != 0) {
307 msg += " Testing collation of single different" +
308 " characters (" + toUnicode(char0+char1) +
309 ") ... FAILED\n" ;
311 result &= res == 0 ;
314 String str1 = new String(new char[] {locChar[0], locChar[0],
315 locChar[1], locChar[1], locChar[1]}) ;
316 String str2 = new String(new char[] {locChar[0], locChar[0],
317 locChar[0], locChar[1], locChar[1]}) ;
319 res = oObj.compareString(str1 , str1) ;
320 if (res != 0) {
321 msg += " Testing collation of equal strings (" +
322 toUnicode(str1) + ") ... FAILED\n" ;
324 result &= res == 0 ;
325 res = oObj.compareString(str1, str2) ;
326 if (res == 0) {
327 msg += " Testing collation of different strings ((" +
328 toUnicode(str1) + "),(" + toUnicode(str2) +
329 ")) ... FAILED (0 returned)\n" ;
330 msg += " Java collator returned " +
331 col.compare(str1, str2) + "\n" ;
332 result = false ;
333 } else { // opposite order - sum of results must be
334 res += oObj.compareString(str2, str1) ;
335 if (res != 0) {
336 msg += " Testing collation of different strings ((" +
337 toUnicode(str1) + "),(" + toUnicode(str2) +
338 ")) ... FAILED\n" ;
340 result &= res == 0 ;
343 if (!result) {
344 log.println(msg) ;
346 return result ;
351 * Testing compareSubstring() method. Method is testing substrings comparing.
352 * @param locChar sequence of at list two characters of a given locale
353 * to be used in comparing.
354 * @param col Collator for a given locale
355 * @return true if:
356 * <ol>
357 * <li> if comparing of two identical substrings of strings, composed
358 * of given chars returns zero</li>
359 * <li> if comparing of two different substrings of strings, composed
360 * of given chars returns non zero</li>
361 * </ol>
363 public boolean testCompareSubstring(char[] locChar, Collator col) {
364 boolean result = true ;
365 int res ;
366 String msg = "" ;
368 String str1 = new String(new char[] {locChar[0], locChar[0],
369 locChar[1], locChar[1], locChar[1]}) ;
370 String str2 = new String(new char[] {locChar[0], locChar[0],
371 locChar[0], locChar[1], locChar[1]}) ;
373 res = oObj.compareSubstring(str1, 1, 2 , str2, 2, 2) ;
374 if (res != 0) {
375 msg += " Testing collation of equal substrings (" +
376 toUnicode(str1) + ") ... FAILED\n" ;
378 result &= res == 0 ;
379 res = oObj.compareSubstring(str1, 1, 2, str2, 1, 2) ;
380 if (res == 0) {
381 msg += " Testing collation of different strings ((" +
382 toUnicode(str1.substring(1, 3)) + "),(" +
383 toUnicode(str2.substring(1, 3))
384 + ")) ... FAILED (0 returned)\n" ;
385 msg += " Java collator returned " + col.compare
386 (str1.substring(1, 3), str2.substring(1, 3)) + "\n" ;
387 result = false ;
388 } else { // opposite order - sum of results must be
389 res += oObj.compareSubstring(str2, 1, 2, str1, 1, 2) ;
390 if (res != 0) {
391 msg += " Testing collation of different strings ((" +
392 toUnicode(str1) + "),(" + toUnicode(str2) +
393 ")) ... FAILED\n" ;
395 result &= res == 0 ;
398 if (!result) {
399 log.println(msg) ;
401 return result ;
406 * Transforms string to unicode hex codes.
407 * @param str String to be transformed
409 public String toUnicode(String str) {
410 char[] chars = str.toCharArray() ;
411 StringBuilder res = new StringBuilder();
412 for (int i = 0; i < chars.length; i++) {
413 if (i != 0) {
414 res.append(",");
416 res.append(Integer.toHexString(chars[i]));
418 return res.toString();