xref: /trunk/test/testuno/source/api/i18n/XCollatorTest.java (revision 2f709283d1bd576d3b419fe5eab3c9c4e094bc79)
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 api.i18n;
25 
26 import java.text.Collator;
27 
28 import com.sun.star.i18n.CollatorOptions;
29 import com.sun.star.i18n.XCollator;
30 import com.sun.star.lang.Locale;
31 import com.sun.star.uno.UnoRuntime;
32 import com.sun.star.uno.XComponentContext;
33 import org.junit.AfterClass;
34 import org.junit.Before;
35 import org.junit.BeforeClass;
36 import org.junit.Assert;
37 import org.junit.Test;
38 import org.openoffice.test.uno.UnoApp;
39 
40 /**
41 * Testing <code>com.sun.star.i18n.XCollator</code>
42 * interface methods :
43 * <ul>
44 *  <li><code> compareSubstring()</code></li>
45 *  <li><code> compareString()</code></li>
46 *  <li><code> loadDefaultCollator()</code></li>
47 *  <li><code> loadCollatorAlgorithm()</code></li>
48 *  <li><code> listCollatorAlgorithms()</code></li>
49 *  <li><code> loadCollatorAlgorithmWithEndUserOption()</code></li>
50 *  <li><code> listCollatorOptions()</code></li>
51 * </ul> <p>
52 * Test is <b> NOT </b> multithread compliant. <p>
53 * @see com.sun.star.i18n.XCollator
54 */
55 public class XCollatorTest {
56     private static final UnoApp app = new UnoApp();
57 
58     private XComponentContext xContext = null;
59     private XCollator oObj = null;
60     private String[] alg = null ;
61     private int[] opt = null ;
62     Locale loc = new Locale("en", "EN", "");
63 
64     // setup and close connections
65     @BeforeClass
66     public static void setUpConnection() throws Exception
67     {
68         app.start();
69     }
70 
71     @AfterClass
72     public static void tearDownConnection() throws InterruptedException, com.sun.star.uno.Exception
73     {
74         app.close();
75     }
76 
77     @Before
78     public void before() throws Exception, java.lang.Exception
79     {
80         xContext = app.getComponentContext();
81         final Object object = xContext.getServiceManager().createInstanceWithContext("com.sun.star.i18n.Collator", xContext);
82         oObj = UnoRuntime.queryInterface(XCollator.class, object);
83         alg = oObj.listCollatorAlgorithms(loc);
84     }
85 
86     /**
87     * Just retrieves a list of algorithms. <p>
88     * Has <b>OK</b> status if non-zero length array returned.
89     */
90     @Test
91     public void _listCollatorAlgorithms() {
92         String[] algorithms = oObj.listCollatorAlgorithms(loc) ;
93         System.out.println("Collator algorithms :");
94         if (algorithms != null) {
95             for (int i = 0; i < alg.length; i++) {
96                 System.out.println("  '" + algorithms[i] + "'") ;
97             }
98             Assert.assertTrue("listCollatorAlgorithms()", algorithms.length > 0) ;
99         } else {
100             Assert.fail("listCollatorAlgorithms()");
101         }
102     }
103 
104     /**
105     * Just gets a list of options for some collator. <p>
106     * Has <b>OK</b> status if not null value returned.<p>
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>
112     */
113     @Test
114     public void _listCollatorOptions() {
115         opt = oObj.listCollatorOptions(alg[0]) ;
116         System.out.println("Collator '" + alg[0] + "' options :");
117         if (opt != null) {
118             for (int i = 0; i < opt.length; i++) {
119                 System.out.println("  " + opt[i]) ;
120             }
121             Assert.assertTrue("listCollatorOptions()", true) ;
122         } else {
123             Assert.fail("listCollatorOptions()") ;
124         }
125     }
126 
127     /**
128     * Calls the method with no options and with options(IGNORE_CASE),
129     * compares strings.<p>
130     * Has <b>OK</b> status if compareString() returned correct values.
131     */
132     @Test
133     public void _loadDefaultCollator() {
134         oObj.loadDefaultCollator(loc, 0);
135         boolean res = oObj.compareString("A", "a") != 0;
136         oObj.loadDefaultCollator(loc,
137             CollatorOptions.CollatorOptions_IGNORE_CASE);
138         res &= oObj.compareString("a", "A") == 0;
139         Assert.assertTrue("loadDefaultCollator()", res) ;
140     }
141 
142     /**
143     * Calls the method with no options and with options(IGNORE_CASE),
144     * compares strings.<p>
145     * Has <b>OK</b> status if compareString() returned correct values.
146     * The following method tests are to be completed successfully before :
147     * <ul>
148     *  <li> <code> listCollatorAlgorithms </code> : to have some
149     *    algorithm name. </li>
150     * </ul>
151     */
152     @Test
153     public void _loadCollatorAlgorithm() {
154         oObj.loadCollatorAlgorithm(alg[0], loc,
155             CollatorOptions.CollatorOptions_IGNORE_CASE);
156         boolean res = oObj.compareString("A", "a") == 0;
157         oObj.loadCollatorAlgorithm(alg[0], loc, 0);
158         res &= oObj.compareString("a", "A") != 0;
159         Assert.assertTrue("loadCollatorAlgorithm()", res);
160     }
161 
162     /**
163     * Calls the method with no options and with options(IGNORE_CASE),
164     * compares strings.<p>
165     * Has <b>OK</b> status if compareString() returned correct values.
166     * The following method tests are to be completed successfully before :
167     * <ul>
168     *  <li> <code> listCollatorAlgorithms </code> : to have some
169     *    algorithm name. </li>
170     * </ul>
171     */
172     @Test
173     public void _loadCollatorAlgorithmWithEndUserOption() {
174         oObj.loadCollatorAlgorithmWithEndUserOption(alg[0], loc,
175             new int[] {0});
176         boolean res = oObj.compareString("A", "a") != 0;
177         oObj.loadCollatorAlgorithmWithEndUserOption(alg[0], loc,
178             new int[] {CollatorOptions.CollatorOptions_IGNORE_CASE});
179         res = oObj.compareString("A", "a") == 0;
180         Assert.assertTrue("loadCollatorAlgorithmWithEndUserOption()", res);
181     }
182 
183     /**
184     * Test is performed for locales : en, ru, ja, zh, ko.
185     * Default collator is loaded for each locale. Then collation
186     * is performed for different combination of symbols from range of
187     * this locale.<p>
188     * Has <b>OK</b> status if comparing of different strings
189     * returns not 0 value, then comparing in the opposite
190     * order returns value with opposite sign, and comparing
191     * of two equal strings returns 0. The such comparing is performed
192     * for one character strings.
193     */
194     @Test
195     public void _compareSubstring() {
196         boolean result = true ;
197         char[] chars = new char[2] ;
198         Collator col = null ;
199 
200         System.out.println(" #### Testing English locale ####") ;
201         oObj.loadDefaultCollator(loc, 0) ;
202         col = Collator.getInstance(new java.util.Locale("en", "EN")) ;
203         for (char ch = 0x0020; ch < 0x007F; ch ++) {
204             chars[0] = ch ; chars[1] = (char) (ch + 1) ;
205             result &= testCompareSubstring(chars, col) ;
206         }
207 
208         System.out.println(" #### Testing Russian locale ####") ;
209         oObj.loadDefaultCollator(
210             new com.sun.star.lang.Locale("ru", "RU", ""), 0) ;
211         col = Collator.getInstance(new java.util.Locale("ru", "RU")) ;
212         for (char ch = 0x0410; ch < 0x0450; ch ++) {
213             chars[0] = ch ; chars[1] = (char) (ch + 1) ;
214             result &= testCompareSubstring(chars, col) ;
215         }
216 
217         System.out.println(" #### Testing Japan locale ####") ;
218         oObj.loadDefaultCollator(
219             new com.sun.star.lang.Locale("ja", "JP", ""), 0) ;
220         col = Collator.getInstance(new java.util.Locale("ja", "JP")) ;
221         for (char ch = 0x4E00; ch < 0x4EFD; ch ++) {
222             chars[0] = ch ; chars[1] = (char) (ch + 1) ;
223             result &= testCompareSubstring(chars, col) ;
224         }
225 
226         System.out.println(" #### Testing China locale ####") ;
227         oObj.loadDefaultCollator(new Locale("zh", "CN", ""), 0) ;
228         col = Collator.getInstance(new java.util.Locale("zh", "CN")) ;
229         for (char ch = 0x4E00; ch < 0x4EFD; ch ++) {
230             chars[0] = ch ; chars[1] = (char) (ch + 1) ;
231             result &= testCompareSubstring(chars, col) ;
232         }
233 
234         System.out.println(" #### Testing Korean locale ####") ;
235         oObj.loadDefaultCollator(new Locale("ko", "KR", ""), 0) ;
236         col = Collator.getInstance(new java.util.Locale("ko", "KR")) ;
237         for (char ch = 0x4E00; ch < 0x4EFD; ch ++) {
238             chars[0] = ch ; chars[1] = (char) (ch + 1) ;
239             result &= testCompareSubstring(chars, col) ;
240         }
241 
242         Assert.assertTrue("compareSubstring()", result) ;
243     }
244 
245     /**
246     * Test is performed for locales : en, ru, ja, zh, ko.
247     * Default collator is loaded for each locale. Then collation
248     * is performed for different combination of symbols from range of
249     * this locale.<p>
250     * Has <b>OK</b> status if comparing of different strings
251     * returns not 0 value, then comparing in the opposite
252     * order returns value with opposite sign, and comparing
253     * of two equal strings returns 0. The such comparing is performed
254     * for one character strings.
255     */
256     @Test
257     public void _compareString() {
258         boolean result = true ;
259         char[] chars = new char[2] ;
260         Collator col = null ;
261         System.out.println(" #### Testing English locale ####") ;
262         oObj.loadDefaultCollator(
263             new com.sun.star.lang.Locale("en", "EN", ""), 0) ;
264         col = Collator.getInstance(new java.util.Locale("en", "EN")) ;
265         for (char ch = 0x0020; ch < 0x007F; ch ++) {
266             chars[0] = ch ; chars[1] = (char) (ch + 1) ;
267             result &= testCompareString(chars, col) ;
268         }
269 
270         System.out.println(" #### Testing Russian locale ####") ;
271         oObj.loadDefaultCollator(
272             new com.sun.star.lang.Locale("ru", "RU", ""), 0) ;
273         col = Collator.getInstance(new java.util.Locale("ru", "RU")) ;
274         for (char ch = 0x0410; ch < 0x0450; ch ++) {
275             chars[0] = ch ; chars[1] = (char) (ch + 1) ;
276             result &= testCompareString(chars, col) ;
277         }
278 
279         System.out.println(" #### Testing Japan locale ####") ;
280         oObj.loadDefaultCollator(
281             new com.sun.star.lang.Locale("ja", "JP", ""), 0) ;
282         col = Collator.getInstance(new java.util.Locale("ja", "JP")) ;
283         for (char ch = 0x4E00; ch < 0x4EFD; ch ++) {
284             chars[0] = ch ; chars[1] = (char) (ch + 1) ;
285             result &= testCompareString(chars, col) ;
286         }
287 
288         System.out.println(" #### Testing China locale ####") ;
289         oObj.loadDefaultCollator(new Locale("zh", "CN", ""), 0) ;
290         col = Collator.getInstance(new java.util.Locale("zh", "CN")) ;
291         for (char ch = 0x4E00; ch < 0x4EFD; ch ++) {
292             chars[0] = ch ; chars[1] = (char) (ch + 1) ;
293             result &= testCompareString(chars, col) ;
294         }
295 
296         System.out.println(" #### Testing Korean locale ####") ;
297         oObj.loadDefaultCollator(new Locale("ko", "KR", ""), 0) ;
298         col = Collator.getInstance(new java.util.Locale("ko", "KR")) ;
299         for (char ch = 0x4E00; ch < 0x4EFD; ch ++) {
300             chars[0] = ch ; chars[1] = (char) (ch + 1) ;
301             result &= testCompareString(chars, col) ;
302         }
303 
304         Assert.assertTrue("compareString()", result) ;
305     }
306 
307     /**
308     * Testing compareString() method. At first method is testing single chars
309     * comparing, then strings comparing.
310     * @param locChar sequence of at list two characters of a given locale
311     * to be used in comparing.
312     * @param col Collator for a given locale
313     * @return true if:
314     * <ol>
315     *  <li> if comparing of two identical characters returns zero</li>
316     *  <li> if comparing of two different characters returns non zero</li>
317     *  <li> if comparing of two identical strings, composed of given chars
318     *  returns zero</li>
319     *  <li> if comparing of two different strings, composed of given chars
320     *  returns non zero</li>
321     * </ol>
322     */
323     public boolean testCompareString(char[] locChar, Collator col) {
324         boolean result = true;
325         int res;
326         String msg = "";
327 
328         String char0 = "_"+new String(new char[] {locChar[0]});
329         String char1 = "_"+new String(new char[] {locChar[1]});
330         res = oObj.compareString(char0 , char0) ;
331         if (res != 0) {
332             msg += "  Testing collation of single equal characters ("
333                 + toUnicode(char0) + ") ... FAILED\n" ;
334         }
335         result &= res == 0 ;
336         res = oObj.compareString(char0, char1) ;
337         if (res == 0) {
338             msg += "  Testing collation of single different" +
339                 " characters (" + toUnicode(char0+char1) +
340                 ") ... FAILED (0 returned)\n" ;
341             msg += "  Java collator returned " +
342                 col.compare(char0, char1) + "\n" ;
343             result = false ;
344         } else { // opposite order - sum of results must be 0
345             res += oObj.compareString(char1, char0) ;
346             if (res != 0) {
347                 msg += "  Testing collation of single different" +
348                     " characters (" + toUnicode(char0+char1) +
349                     ") ... FAILED\n" ;
350             }
351             result &= res == 0 ;
352         }
353 
354         String str1 = new String(new char[] {locChar[0], locChar[0],
355             locChar[1], locChar[1], locChar[1]}) ;
356         String str2 = new String(new char[] {locChar[0], locChar[0],
357             locChar[0], locChar[1], locChar[1]}) ;
358 
359         res = oObj.compareString(str1 , str1) ;
360         if (res != 0) {
361             msg += "  Testing collation of equal strings (" +
362                 toUnicode(str1) + ") ... FAILED\n" ;
363         }
364         result &= res == 0 ;
365         res = oObj.compareString(str1, str2) ;
366         if (res == 0) {
367             msg += "  Testing collation of different strings ((" +
368                 toUnicode(str1) + "),(" + toUnicode(str2) +
369                 ")) ... FAILED (0 returned)\n" ;
370             msg += "  Java collator returned " +
371                 col.compare(str1, str2) + "\n" ;
372             result = false ;
373         } else { // opposite order - sum of results must be
374             res += oObj.compareString(str2, str1) ;
375             if (res != 0) {
376                 msg += "  Testing collation of different strings ((" +
377                     toUnicode(str1) + "),(" + toUnicode(str2) +
378                     ")) ... FAILED\n" ;
379             }
380             result &= res == 0 ;
381         }
382 
383         if (!result) {
384             System.out.println(msg) ;
385         }
386         return result ;
387     }
388 
389     /**
390     * Testing compareSubstring() method. Method is testing substrings comparing.
391     * @param locChar sequence of at list two characters of a given locale
392     * to be used in comparing.
393     * @param col Collator for a given locale
394     * @return true if:
395     * <ol>
396     *  <li> if comparing of two identical substrings of strings, composed
397     *  of given chars returns zero</li>
398     *  <li> if comparing of two different substrings of strings, composed
399     *  of given chars returns non zero</li>
400     * </ol>
401     */
402     public boolean testCompareSubstring(char[] locChar, Collator col) {
403         boolean result = true ;
404         int res ;
405         String msg = "" ;
406 
407         String str1 = new String(new char[] {locChar[0], locChar[0],
408             locChar[1], locChar[1], locChar[1]}) ;
409         String str2 = new String(new char[] {locChar[0], locChar[0],
410             locChar[0], locChar[1], locChar[1]}) ;
411 
412         res = oObj.compareSubstring(str1, 1, 2 , str2, 2, 2) ;
413         if (res != 0) {
414             msg += "  Testing collation of equal substrings (" +
415                 toUnicode(str1) + ") ... FAILED\n" ;
416         }
417         result &= res == 0 ;
418         res = oObj.compareSubstring(str1, 1, 2, str2, 1, 2) ;
419         if (res == 0) {
420             msg += "  Testing collation of different strings ((" +
421                 toUnicode(str1.substring(1, 3)) + "),(" +
422                 toUnicode(str2.substring(1, 3))
423                 + ")) ... FAILED (0 returned)\n" ;
424             msg += "  Java collator returned " + col.compare
425                 (str1.substring(1, 3), str2.substring(1, 3)) + "\n" ;
426             result = false ;
427         } else { // opposite order - sum of results must be
428             res += oObj.compareSubstring(str2, 1, 2, str1, 1, 2) ;
429             if (res != 0) {
430                 msg += "  Testing collation of different strings ((" +
431                     toUnicode(str1) + "),(" + toUnicode(str2) +
432                     ")) ... FAILED\n" ;
433             }
434             result &= res == 0 ;
435         }
436 
437         if (!result) {
438             System.out.println(msg) ;
439         }
440         return result ;
441     }
442 
443     /**
444     * Transforms string to unicode hex codes.
445     * @param str String to be transformed
446     */
447     public String toUnicode(String str) {
448         char[] chars = str.toCharArray() ;
449         String res = "" ;
450         for (int i = 0; i < chars.length; i++) {
451             if (i != 0) res += "," ;
452             res += Integer.toHexString(chars[i]) ;
453         }
454         return res ;
455     }
456 
457 }
458