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