1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 package ifc.i18n;
25 
26 import java.text.Collator;
27 
28 import lib.MultiMethodTest;
29 
30 import com.sun.star.i18n.CollatorOptions;
31 import com.sun.star.i18n.XCollator;
32 import com.sun.star.lang.Locale;
33 
34 /**
35 * Testing <code>com.sun.star.i18n.XCollator</code>
36 * interface methods :
37 * <ul>
38 *  <li><code> compareSubstring()</code></li>
39 *  <li><code> compareString()</code></li>
40 *  <li><code> loadDefaultCollator()</code></li>
41 *  <li><code> loadCollatorAlgorithm()</code></li>
42 *  <li><code> listCollatorAlgorithms()</code></li>
43 *  <li><code> loadCollatorAlgorithmWithEndUserOption()</code></li>
44 *  <li><code> listCollatorOptions()</code></li>
45 * </ul> <p>
46 * Test is <b> NOT </b> multithread compilant. <p>
47 * @see com.sun.star.i18n.XCollator
48 */
49 public class _XCollator extends MultiMethodTest {
50     public XCollator oObj = null;
51     private String[] alg = null ;
52     private int[] opt = null ;
53     Locale loc = new Locale("en", "EN", "");
54 
55     /**
56     * Just retrieves a list of algorithms. <p>
57     * Has <b>OK</b> status if non-zero length array returned.
58     */
_listCollatorAlgorithms()59     public void _listCollatorAlgorithms() {
60         alg = oObj.listCollatorAlgorithms(loc) ;
61         log.println("Collator algorithms :");
62         if (alg != null) {
63             for (int i = 0; i < alg.length; i++) {
64                 log.println("  '" + alg[i] + "'") ;
65             }
66             tRes.tested("listCollatorAlgorithms()", alg.length > 0) ;
67         } else {
68             tRes.tested("listCollatorAlgorithms()", false) ;
69         }
70     }
71 
72     /**
73     * Just gets a list of options for some collator. <p>
74     * Has <b>OK</b> status if not null value returned.<p>
75     * The following method tests are to be completed successfully before :
76     * <ul>
77     *  <li> <code> listCollatorAlgorithms </code> : to have some
78     *    algorithm name. </li>
79     * </ul>
80     */
_listCollatorOptions()81     public void _listCollatorOptions() {
82         requiredMethod("listCollatorAlgorithms()") ;
83         opt = oObj.listCollatorOptions(alg[0]) ;
84         log.println("Collator '" + alg[0] + "' options :");
85         if (opt != null) {
86             for (int i = 0; i < opt.length; i++) {
87                 log.println("  " + opt[i]) ;
88             }
89             tRes.tested("listCollatorOptions()", true) ;
90         } else {
91             tRes.tested("listCollatorOptions()", false) ;
92         }
93     }
94 
95     /**
96     * Calls the method with no options and with options(IGNORE_CASE),
97     * compares strings.<p>
98     * Has <b>OK</b> status if compareString() returned correct values.
99     */
_loadDefaultCollator()100     public void _loadDefaultCollator() {
101         oObj.loadDefaultCollator(loc, 0);
102         boolean res = oObj.compareString("A", "a") != 0;
103         oObj.loadDefaultCollator(loc,
104             CollatorOptions.CollatorOptions_IGNORE_CASE);
105         res &= oObj.compareString("a", "A") == 0;
106         tRes.tested("loadDefaultCollator()", res) ;
107     }
108 
109     /**
110     * Calls the method with no options and with options(IGNORE_CASE),
111     * compares strings.<p>
112     * Has <b>OK</b> status if compareString() returned correct values.
113     * The following method tests are to be completed successfully before :
114     * <ul>
115     *  <li> <code> listCollatorAlgorithms </code> : to have some
116     *    algorithm name. </li>
117     * </ul>
118     */
_loadCollatorAlgorithm()119     public void _loadCollatorAlgorithm() {
120         requiredMethod("listCollatorAlgorithms()");
121         oObj.loadCollatorAlgorithm(alg[0], loc,
122             CollatorOptions.CollatorOptions_IGNORE_CASE);
123         boolean res = oObj.compareString("A", "a") == 0;
124         oObj.loadCollatorAlgorithm(alg[0], loc, 0);
125         res &= oObj.compareString("a", "A") != 0;
126         tRes.tested("loadCollatorAlgorithm()", res);
127     }
128 
129     /**
130     * Calls the method with no options and with options(IGNORE_CASE),
131     * compares strings.<p>
132     * Has <b>OK</b> status if compareString() returned correct values.
133     * The following method tests are to be completed successfully before :
134     * <ul>
135     *  <li> <code> listCollatorAlgorithms </code> : to have some
136     *    algorithm name. </li>
137     * </ul>
138     */
_loadCollatorAlgorithmWithEndUserOption()139     public void _loadCollatorAlgorithmWithEndUserOption() {
140         requiredMethod("listCollatorAlgorithms()");
141         oObj.loadCollatorAlgorithmWithEndUserOption(alg[0], loc,
142             new int[] {0});
143         boolean res = oObj.compareString("A", "a") != 0;
144         oObj.loadCollatorAlgorithmWithEndUserOption(alg[0], loc,
145             new int[] {CollatorOptions.CollatorOptions_IGNORE_CASE});
146         res = oObj.compareString("A", "a") == 0;
147         tRes.tested("loadCollatorAlgorithmWithEndUserOption()", res);
148     }
149 
150     /**
151     * Test is performed for locales : en, ru, ja, zh, ko.
152     * Default collator is loaded for each locale. Then collation
153     * is performed for different combination of symbols from range of
154     * this locale.<p>
155     * Has <b>OK</b> status if comparing of different strings
156     * returns not 0 value, then comparing in the opposite
157     * order returns value with opposite sign, and comparing
158     * of two equal strings returns 0. The such comparing is performed
159     * for one character strings.
160     */
_compareSubstring()161     public void _compareSubstring() {
162         boolean result = true ;
163         char[] chars = new char[2] ;
164         Collator col = null ;
165 
166         log.println(" #### Testing English locale ####") ;
167         oObj.loadDefaultCollator(loc, 0) ;
168         col = Collator.getInstance(new java.util.Locale("en", "EN")) ;
169         for (char ch = 0x0020; ch < 0x007F; ch ++) {
170             chars[0] = ch ; chars[1] = (char) (ch + 1) ;
171             result &= testCompareSubstring(chars, col) ;
172         }
173 
174         log.println(" #### Testing Russian locale ####") ;
175         oObj.loadDefaultCollator(
176             new com.sun.star.lang.Locale("ru", "RU", ""), 0) ;
177         col = Collator.getInstance(new java.util.Locale("ru", "RU")) ;
178         for (char ch = 0x0410; ch < 0x0450; ch ++) {
179             chars[0] = ch ; chars[1] = (char) (ch + 1) ;
180             result &= testCompareSubstring(chars, col) ;
181         }
182 
183         log.println(" #### Testing Japan locale ####") ;
184         oObj.loadDefaultCollator(
185             new com.sun.star.lang.Locale("ja", "JP", ""), 0) ;
186         col = Collator.getInstance(new java.util.Locale("ja", "JP")) ;
187         for (char ch = 0x4E00; ch < 0x4EFD; ch ++) {
188             chars[0] = ch ; chars[1] = (char) (ch + 1) ;
189             result &= testCompareSubstring(chars, col) ;
190         }
191 
192         log.println(" #### Testing China locale ####") ;
193         oObj.loadDefaultCollator(new Locale("zh", "CN", ""), 0) ;
194         col = Collator.getInstance(new java.util.Locale("zh", "CN")) ;
195         for (char ch = 0x4E00; ch < 0x4EFD; ch ++) {
196             chars[0] = ch ; chars[1] = (char) (ch + 1) ;
197             result &= testCompareSubstring(chars, col) ;
198         }
199 
200         log.println(" #### Testing Korean locale ####") ;
201         oObj.loadDefaultCollator(new Locale("ko", "KR", ""), 0) ;
202         col = Collator.getInstance(new java.util.Locale("ko", "KR")) ;
203         for (char ch = 0x4E00; ch < 0x4EFD; ch ++) {
204             chars[0] = ch ; chars[1] = (char) (ch + 1) ;
205             result &= testCompareSubstring(chars, col) ;
206         }
207 
208         tRes.tested("compareSubstring()", result) ;
209     }
210 
211     /**
212     * Test is performed for locales : en, ru, ja, zh, ko.
213     * Default collator is loaded for each locale. Then collation
214     * is performed for different combination of symbols from range of
215     * this locale.<p>
216     * Has <b>OK</b> status if comparing of different strings
217     * returns not 0 value, then comparing in the opposite
218     * order returns value with opposite sign, and comparing
219     * of two equal strings returns 0. The such comparing is performed
220     * for one character strings.
221     */
_compareString()222     public void _compareString() {
223         boolean result = true ;
224         char[] chars = new char[2] ;
225         Collator col = null ;
226         log.println(" #### Testing English locale ####") ;
227         oObj.loadDefaultCollator(
228             new com.sun.star.lang.Locale("en", "EN", ""), 0) ;
229         col = Collator.getInstance(new java.util.Locale("en", "EN")) ;
230         for (char ch = 0x0020; ch < 0x007F; ch ++) {
231             chars[0] = ch ; chars[1] = (char) (ch + 1) ;
232             result &= testCompareString(chars, col) ;
233         }
234 
235         log.println(" #### Testing Russian locale ####") ;
236         oObj.loadDefaultCollator(
237             new com.sun.star.lang.Locale("ru", "RU", ""), 0) ;
238         col = Collator.getInstance(new java.util.Locale("ru", "RU")) ;
239         for (char ch = 0x0410; ch < 0x0450; ch ++) {
240             chars[0] = ch ; chars[1] = (char) (ch + 1) ;
241             result &= testCompareString(chars, col) ;
242         }
243 
244         log.println(" #### Testing Japan locale ####") ;
245         oObj.loadDefaultCollator(
246             new com.sun.star.lang.Locale("ja", "JP", ""), 0) ;
247         col = Collator.getInstance(new java.util.Locale("ja", "JP")) ;
248         for (char ch = 0x4E00; ch < 0x4EFD; ch ++) {
249             chars[0] = ch ; chars[1] = (char) (ch + 1) ;
250             result &= testCompareString(chars, col) ;
251         }
252 
253         log.println(" #### Testing China locale ####") ;
254         oObj.loadDefaultCollator(new Locale("zh", "CN", ""), 0) ;
255         col = Collator.getInstance(new java.util.Locale("zh", "CN")) ;
256         for (char ch = 0x4E00; ch < 0x4EFD; ch ++) {
257             chars[0] = ch ; chars[1] = (char) (ch + 1) ;
258             result &= testCompareString(chars, col) ;
259         }
260 
261         log.println(" #### Testing Korean locale ####") ;
262         oObj.loadDefaultCollator(new Locale("ko", "KR", ""), 0) ;
263         col = Collator.getInstance(new java.util.Locale("ko", "KR")) ;
264         for (char ch = 0x4E00; ch < 0x4EFD; ch ++) {
265             chars[0] = ch ; chars[1] = (char) (ch + 1) ;
266             result &= testCompareString(chars, col) ;
267         }
268 
269         tRes.tested("compareString()", result) ;
270     }
271 
272 
273     /**
274     * Testing compareString() method. At first method is testing single chars
275     * comparing, then strings comparing.
276     * @param locChar sequence of at list two characters of a given locale
277     * to be used in comparing.
278     * @param col Collator for a given locale
279     * @return true if:
280     * <ol>
281     *  <li> if comparing of two identical characters returns zero</li>
282     *  <li> if comparing of two different characters returns non zero</li>
283     *  <li> if comparing of two identical strings, composed of given chars
284     *  returns zero</li>
285     *  <li> if comparing of two different strings, composed of given chars
286     *  returns non zero</li>
287     * </ol>
288     */
testCompareString(char[] locChar, Collator col)289     public boolean testCompareString(char[] locChar, Collator col) {
290         boolean result = true;
291         int res;
292         String msg = "";
293 
294         String char0 = "_"+new String(new char[] {locChar[0]});
295         String char1 = "_"+new String(new char[] {locChar[1]});
296         res = oObj.compareString(char0 , char0) ;
297         if (res != 0) {
298             msg += "  Testing collation of single equal characters ("
299                 + toUnicode(char0) + ") ... FAILED\n" ;
300         }
301         result &= res == 0 ;
302         res = oObj.compareString(char0, char1) ;
303         if (res == 0) {
304             msg += "  Testing collation of single different" +
305                 " characters (" + toUnicode(char0+char1) +
306                 ") ... FAILED (0 returned)\n" ;
307             msg += "  Java collator returned " +
308                 col.compare(char0, char1) + "\n" ;
309             result = false ;
310         } else { // opposite order - sum of results must be 0
311             res += oObj.compareString(char1, char0) ;
312             if (res != 0) {
313                 msg += "  Testing collation of single different" +
314                     " characters (" + toUnicode(char0+char1) +
315                     ") ... FAILED\n" ;
316             }
317             result &= res == 0 ;
318         }
319 
320         String str1 = new String(new char[] {locChar[0], locChar[0],
321             locChar[1], locChar[1], locChar[1]}) ;
322         String str2 = new String(new char[] {locChar[0], locChar[0],
323             locChar[0], locChar[1], locChar[1]}) ;
324 
325         res = oObj.compareString(str1 , str1) ;
326         if (res != 0) {
327             msg += "  Testing collation of equal strings (" +
328                 toUnicode(str1) + ") ... FAILED\n" ;
329         }
330         result &= res == 0 ;
331         res = oObj.compareString(str1, str2) ;
332         if (res == 0) {
333             msg += "  Testing collation of different strings ((" +
334                 toUnicode(str1) + "),(" + toUnicode(str2) +
335                 ")) ... FAILED (0 returned)\n" ;
336             msg += "  Java collator returned " +
337                 col.compare(str1, str2) + "\n" ;
338             result = false ;
339         } else { // opposite order - sum of results must be
340             res += oObj.compareString(str2, str1) ;
341             if (res != 0) {
342                 msg += "  Testing collation of different strings ((" +
343                     toUnicode(str1) + "),(" + toUnicode(str2) +
344                     ")) ... FAILED\n" ;
345             }
346             result &= res == 0 ;
347         }
348 
349         if (!result) {
350             log.println(msg) ;
351         }
352         return result ;
353     }
354 
355 
356     /**
357     * Testing compareSubstring() method. Method is testing substrings comparing.
358     * @param locChar sequence of at list two characters of a given locale
359     * to be used in comparing.
360     * @param col Collator for a given locale
361     * @return true if:
362     * <ol>
363     *  <li> if comparing of two identical substrings of strings, composed
364     *  of given chars returns zero</li>
365     *  <li> if comparing of two different substrings of strings, composed
366     *  of given chars returns non zero</li>
367     * </ol>
368     */
testCompareSubstring(char[] locChar, Collator col)369     public boolean testCompareSubstring(char[] locChar, Collator col) {
370         boolean result = true ;
371         int res ;
372         String msg = "" ;
373 
374         String str1 = new String(new char[] {locChar[0], locChar[0],
375             locChar[1], locChar[1], locChar[1]}) ;
376         String str2 = new String(new char[] {locChar[0], locChar[0],
377             locChar[0], locChar[1], locChar[1]}) ;
378 
379         res = oObj.compareSubstring(str1, 1, 2 , str2, 2, 2) ;
380         if (res != 0) {
381             msg += "  Testing collation of equal substrings (" +
382                 toUnicode(str1) + ") ... FAILED\n" ;
383         }
384         result &= res == 0 ;
385         res = oObj.compareSubstring(str1, 1, 2, str2, 1, 2) ;
386         if (res == 0) {
387             msg += "  Testing collation of different strings ((" +
388                 toUnicode(str1.substring(1, 3)) + "),(" +
389                 toUnicode(str2.substring(1, 3))
390                 + ")) ... FAILED (0 returned)\n" ;
391             msg += "  Java collator returned " + col.compare
392                 (str1.substring(1, 3), str2.substring(1, 3)) + "\n" ;
393             result = false ;
394         } else { // opposite order - sum of results must be
395             res += oObj.compareSubstring(str2, 1, 2, str1, 1, 2) ;
396             if (res != 0) {
397                 msg += "  Testing collation of different strings ((" +
398                     toUnicode(str1) + "),(" + toUnicode(str2) +
399                     ")) ... FAILED\n" ;
400             }
401             result &= res == 0 ;
402         }
403 
404         if (!result) {
405             log.println(msg) ;
406         }
407         return result ;
408     }
409 
410 
411     /**
412     * Transforms string to unicode hex codes.
413     * @param str String to be transformed
414     */
toUnicode(String str)415     public String toUnicode(String str) {
416         char[] chars = str.toCharArray() ;
417         String res = "" ;
418         for (int i = 0; i < chars.length; i++) {
419             if (i != 0) res += "," ;
420             res += Integer.toHexString(chars[i]) ;
421         }
422         return res ;
423     }
424 
425 }
426 
427