xref: /trunk/test/testuno/source/api/i18n/XExtendedIndexEntrySupplierTest.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 package api.i18n;
24 
25 import com.sun.star.i18n.CollatorOptions;
26 import com.sun.star.i18n.XExtendedIndexEntrySupplier;
27 import com.sun.star.lang.Locale;
28 import com.sun.star.uno.UnoRuntime;
29 import com.sun.star.uno.XComponentContext;
30 import org.junit.After;
31 import org.junit.AfterClass;
32 import org.junit.Before;
33 import org.junit.BeforeClass;
34 import org.junit.Assert;
35 import org.junit.Test;
36 import org.openoffice.test.uno.UnoApp;
37 import java.util.HashMap;
38 
39 
40 public class XExtendedIndexEntrySupplierTest {
41     private static final UnoApp app = new UnoApp();
42 
43     private XComponentContext xContext = null;
44     public XExtendedIndexEntrySupplier oObj;
45     protected Locale[] locales = null;
46     protected HashMap<Integer,String[]> algorithms = new HashMap<Integer,String[]>();
47 
48     // setup and close connections
49     @BeforeClass
setUpConnection()50     public static void setUpConnection() throws Exception
51     {
52         app.start();
53     }
54 
55     @AfterClass
tearDownConnection()56     public static void tearDownConnection() throws InterruptedException, com.sun.star.uno.Exception
57     {
58         app.close();
59     }
60 
61     @Before
before()62     public void before() throws Exception {
63         xContext = app.getComponentContext();
64         oObj = UnoRuntime.queryInterface(
65             XExtendedIndexEntrySupplier.class,
66             xContext.getServiceManager().createInstanceWithContext("com.sun.star.i18n.IndexEntrySupplier", xContext)
67         );
68         locales = oObj.getLocaleList();
69         algorithms = getAlgorithmList();
70     }
71 
72     @Test
_compareIndexEntry()73     public void _compareIndexEntry() {
74         getIndexKey();
75         Locale locale = new Locale("zh", "CN", "");
76         String val1 = new String(new char[]{UnicodeStringPair.getUnicodeValue(0), UnicodeStringPair.getUnicodeValue(1)});
77         String val2 = new String(new char[]{UnicodeStringPair.getUnicodeValue(1), UnicodeStringPair.getUnicodeValue(0)});
78         short result1 = oObj.compareIndexEntry(val1, "", locale, val1, "", locale);
79         short result2 = oObj.compareIndexEntry(val1, "", locale, val2, "", locale);
80         short result3 = oObj.compareIndexEntry(val2, "", locale, val1, "", locale);
81 
82         Assert.assertTrue("compareIndexEntry()", result1 == 0 && result2 + result3 == 0);
83     }
84 
85     /*
86      * gets the list of all algorithms for each listed language
87      * is OK if everyone of the returned lists are filled
88      */
89     @Test
_getAlgorithmList()90     public void _getAlgorithmList() {
91         HashMap<Integer, String[]> algorithms = getAlgorithmList();
92         boolean result = true;
93         boolean locResult = false;
94 
95         for (int i = 0; i < locales.length; i++) {
96             String[] algNames = algorithms.get(i);
97 
98             locResult = algNames != null && algNames.length > 0;
99             System.out.println("Locale " + i + ": " + locales[i].Country+","+locales[i].Language);
100 
101             for (int j=0; j<algNames.length; j++) {
102                 System.out.println("\tAlgorithm " + j + ": " + algNames[j]);
103             }
104 
105             if (!locResult) {
106                 System.out.println("No Algorithm found for " + locales[i].Country +
107                             "," + locales[i].Language);
108             }
109 
110             result &= locResult;
111         }
112 
113         Assert.assertTrue("getAlgorithmList()", result);
114     }
115 
getAlgorithmList()116     private HashMap<Integer, String[]> getAlgorithmList() {
117         HashMap<Integer, String[]> algorithms = new HashMap<Integer, String[]>();
118         for (int i = 0; i < locales.length; i++) {
119             String[] algNames = oObj.getAlgorithmList(locales[i]);
120             algorithms.put(i, algNames);
121         }
122         return algorithms;
123     }
124 
125     @Test
_getIndexKey()126     public void _getIndexKey() {
127         Assert.assertTrue("getIndexKey()", getIndexKey());
128     }
129 
getIndexKey()130     private boolean getIndexKey() {
131         char[] characters = new char[] { 19968 };
132         String getIndexFor = new String(characters);
133         for (int i = 0; i < locales.length; i++) {
134             System.out.println("Language: " + locales[i].Language);
135 
136             for (int j = 0; j < algorithms.size(); j++) {
137                 String[] algs = (String[])algorithms.get(new Integer(j));
138                 for (int k=0;k<algs.length;k++) {
139                 System.out.println("\t Algorithm :" +
140                             algs[k]);
141                 oObj.loadAlgorithm(locales[i], algs[k], CollatorOptions.CollatorOptions_IGNORE_CASE);
142                 System.out.println("\t\t Get: " +
143                             oObj.getIndexKey(getIndexFor, "", locales[i]));
144                 }
145             }
146         }
147         return true;
148     }
149 
150     /*
151      * gets a list of all locales, is OK if this list isn't empty
152      */
153     @Test
_getLocaleList()154     public void _getLocaleList() {
155         // they were fetched in before()
156         Assert.assertTrue("getLocaleList()", locales.length > 0);
157     }
158 
159     /*
160      * gets one phonetic candidate for the chinese local
161      * is ok if 'yi' is returned as expected.
162      */
163     @Test
_getPhoneticCandidate()164     public void _getPhoneticCandidate() {
165         boolean res = true;
166 
167         Locale loc = new Locale("zh", "CN", "");
168 
169         for (int i = 0;i<UnicodeStringPair.getValCount();i++) {
170 
171             char[] c = new char[]{UnicodeStringPair.getUnicodeValue(i)};
172 
173             String getting = oObj.getPhoneticCandidate(new String(c), loc);
174 
175             boolean locResult = getting.equals(UnicodeStringPair.getExpectedPhoneticString(i));
176 
177             if (!locResult) {
178                 System.out.println("Char: "+ c[0] + " (" + (int)c[0] + ")");
179                 System.out.println("Expected " + UnicodeStringPair.getExpectedPhoneticString(i));
180                 System.out.println("Getting " + getting);
181             }
182 
183             res &= locResult;
184         }
185         Assert.assertTrue("getPhoneticCandidate()", res);
186     }
187 
188     /*
189      * loads all algorithms available in all language.
190      * Is OK if no exception occurs and the method returns
191      * true for each valid algorithm and false otherwise
192      */
193     @Test
_loadAlgorithm()194     public void _loadAlgorithm() {
195         boolean res = true;
196 
197         for (int i = 0; i < algorithms.size(); i++) {
198             String[] names = (String[]) algorithms.get(new Integer(i));
199             System.out.println("loading algorithms for " + locales[i].Country +
200                         "," + locales[i].Language);
201 
202             for (int j = 0; j < names.length; j++) {
203                 System.out.println("\t Loading " + names[j]);
204 
205                 boolean localres = oObj.loadAlgorithm(locales[i], names[j],
206                                                       CollatorOptions.CollatorOptions_IGNORE_CASE);
207 
208                 if (!localres) {
209                     System.out.println("\t ... didn't work - FAILED");
210                 } else {
211                     System.out.println("\t ... worked - OK");
212                 }
213 
214                 res &= localres;
215             }
216 
217 /*            System.out.println("\tTrying to load 'dummy' algorithm");
218 
219             boolean localres = !oObj.loadAlgorithm(locales[i], "dummy",
220                                                    CollatorOptions.CollatorOptions_IGNORE_WIDTH);
221 
222             if (!localres) {
223                 System.out.println("\t ... didn't work as expected - FAILED");
224             } else {
225                 System.out.println("\t ... worked - OK");
226             }
227 
228             res &= localres;*/
229         }
230 
231         Assert.assertTrue("loadAlgorithm()", res);
232     }
233 
234     /*
235      * checks the method usePhoneticEntry(). Only the languages ja, ko and zh
236      * should return true. Has OK state if exactly this is the case.
237      */
238     @Test
_usePhoneticEntry()239     public void _usePhoneticEntry() {
240         boolean res = true;
241 
242         for (int i = 0; i < locales.length; i++) {
243             boolean expected = false;
244 
245             if (locales[i].Language.equals("ja") ||
246                     locales[i].Language.equals("ko") ||
247                     locales[i].Language.equals("zh")) {
248                 expected = true;
249             }
250 
251             boolean locResult = oObj.usePhoneticEntry(locales[i]) == expected;
252 
253             if (!locResult) {
254                 System.out.println("Failure for language " + locales[i].Language);
255                 System.out.println("Expected " + expected);
256                 System.out.println("Getting " + oObj.usePhoneticEntry(locales[i]));
257             }
258 
259             res &= locResult;
260         }
261 
262         Assert.assertTrue("usePhoneticEntry()", res);
263     }
264 
265     /**
266      * Helper class to handle the phonetic equivalence of unicode characters
267      * This class delivers an amount of unicode characters and the equivalent phonetics
268      * for the "getPhoneticCandidate" test. Equivalents are only usable for zh,CN locale.
269      */
270     public static class UnicodeStringPair {
271         final static int valCount = 78;
272         static String[] sStringEquivalence = null;
273         static char[] iUnicodeEquivalence = null;
274 
275         static {
276             sStringEquivalence = new String[valCount];
277             iUnicodeEquivalence = new char[valCount];
fillValues()278             fillValues();
279         }
280 
getValCount()281         public static int getValCount() {
282             return valCount;
283         }
284 
getExpectedPhoneticString(int index)285         public static String getExpectedPhoneticString(int index) {
286             if (index >= valCount) return null;
287             return sStringEquivalence[index];
288         }
289 
getUnicodeValue(int index)290         public static char getUnicodeValue(int index) {
291             if (index > valCount) return 0;
292             return iUnicodeEquivalence[index];
293         }
294 
fillValues()295         private static void fillValues() {
296             iUnicodeEquivalence[0] = 20049; sStringEquivalence[0] = "zhong";
297             iUnicodeEquivalence[1] = 19968; sStringEquivalence[1] = "yi";
298             iUnicodeEquivalence[2] = 19969; sStringEquivalence[2] = "ding";
299             iUnicodeEquivalence[3] = 19970; sStringEquivalence[3] = "kao";
300             iUnicodeEquivalence[4] = 19971; sStringEquivalence[4] = "qi";
301             iUnicodeEquivalence[5] = 19972; sStringEquivalence[5] = "shang";
302             iUnicodeEquivalence[6] = 19973; sStringEquivalence[6] = "xia";
303             iUnicodeEquivalence[7] = 19975; sStringEquivalence[7] = "wan";
304             iUnicodeEquivalence[8] = 19976; sStringEquivalence[8] = "zhang";
305             iUnicodeEquivalence[9] = 19977; sStringEquivalence[9] = "san";
306             iUnicodeEquivalence[10] = 19978; sStringEquivalence[10] = "shang";
307             iUnicodeEquivalence[11] = 19979; sStringEquivalence[11] = "xia";
308             iUnicodeEquivalence[12] = 19980; sStringEquivalence[12] = "ji";
309             iUnicodeEquivalence[13] = 19981; sStringEquivalence[13] = "bu";
310             iUnicodeEquivalence[14] = 19982; sStringEquivalence[14] = "yu";
311             iUnicodeEquivalence[15] = 19983; sStringEquivalence[15] = "mian";
312             iUnicodeEquivalence[16] = 19984; sStringEquivalence[16] = "gai";
313             iUnicodeEquivalence[17] = 19985; sStringEquivalence[17] = "chou";
314             iUnicodeEquivalence[18] = 19986; sStringEquivalence[18] = "chou";
315             iUnicodeEquivalence[19] = 19987; sStringEquivalence[19] = "zhuan";
316             iUnicodeEquivalence[20] = 19988; sStringEquivalence[20] = "qie";
317             iUnicodeEquivalence[21] = 19989; sStringEquivalence[21] = "pi";
318             iUnicodeEquivalence[22] = 19990; sStringEquivalence[22] = "shi";
319             iUnicodeEquivalence[23] = 19991; sStringEquivalence[23] = "shi";
320             iUnicodeEquivalence[24] = 19992; sStringEquivalence[24] = "qiu";
321             iUnicodeEquivalence[25] = 19993; sStringEquivalence[25] = "bing";
322             iUnicodeEquivalence[26] = 19994; sStringEquivalence[26] = "ye";
323             iUnicodeEquivalence[27] = 19995; sStringEquivalence[27] = "cong";
324             iUnicodeEquivalence[28] = 19996; sStringEquivalence[28] = "dong";
325             iUnicodeEquivalence[29] = 19997; sStringEquivalence[29] = "si";
326             iUnicodeEquivalence[30] = 19998; sStringEquivalence[30] = "cheng";
327             iUnicodeEquivalence[31] = 19999; sStringEquivalence[31] = "diu";
328             iUnicodeEquivalence[32] = 20000; sStringEquivalence[32] = "qiu";
329             iUnicodeEquivalence[33] = 20001; sStringEquivalence[33] = "liang";
330             iUnicodeEquivalence[34] = 20002; sStringEquivalence[34] = "diu";
331             iUnicodeEquivalence[35] = 20003; sStringEquivalence[35] = "you";
332             iUnicodeEquivalence[36] = 20004; sStringEquivalence[36] = "liang";
333             iUnicodeEquivalence[37] = 20005; sStringEquivalence[37] = "yan";
334             iUnicodeEquivalence[38] = 20006; sStringEquivalence[38] = "bing";
335             iUnicodeEquivalence[39] = 20007; sStringEquivalence[39] = "sang";
336             iUnicodeEquivalence[40] = 20008; sStringEquivalence[40] = "shu";
337             iUnicodeEquivalence[41] = 20009; sStringEquivalence[41] = "jiu";
338             iUnicodeEquivalence[42] = 20010; sStringEquivalence[42] = "ge";
339             iUnicodeEquivalence[43] = 20011; sStringEquivalence[43] = "ya";
340             iUnicodeEquivalence[44] = 20012; sStringEquivalence[44] = "qiang";
341             iUnicodeEquivalence[45] = 20013; sStringEquivalence[45] = "zhong";
342             iUnicodeEquivalence[46] = 20014; sStringEquivalence[46] = "ji";
343             iUnicodeEquivalence[47] = 20015; sStringEquivalence[47] = "jie";
344             iUnicodeEquivalence[48] = 20016; sStringEquivalence[48] = "feng";
345             iUnicodeEquivalence[49] = 20017; sStringEquivalence[49] = "guan";
346             iUnicodeEquivalence[50] = 20018; sStringEquivalence[50] = "chuan";
347             iUnicodeEquivalence[51] = 20019; sStringEquivalence[51] = "chan";
348             iUnicodeEquivalence[52] = 20020; sStringEquivalence[52] = "lin";
349             iUnicodeEquivalence[53] = 20021; sStringEquivalence[53] = "zhuo";
350             iUnicodeEquivalence[54] = 20022; sStringEquivalence[54] = "zhu";
351             iUnicodeEquivalence[55] = 20024; sStringEquivalence[55] = "wan";
352             iUnicodeEquivalence[56] = 20025; sStringEquivalence[56] = "dan";
353             iUnicodeEquivalence[57] = 20026; sStringEquivalence[57] = "wei";
354             iUnicodeEquivalence[58] = 20027; sStringEquivalence[58] = "zhu";
355             iUnicodeEquivalence[59] = 20028; sStringEquivalence[59] = "jing";
356             iUnicodeEquivalence[60] = 20029; sStringEquivalence[60] = "li";
357             iUnicodeEquivalence[61] = 20030; sStringEquivalence[61] = "ju";
358             iUnicodeEquivalence[62] = 20031; sStringEquivalence[62] = "pie";
359             iUnicodeEquivalence[63] = 20032; sStringEquivalence[63] = "fu";
360             iUnicodeEquivalence[64] = 20033; sStringEquivalence[64] = "yi";
361             iUnicodeEquivalence[65] = 20034; sStringEquivalence[65] = "yi";
362             iUnicodeEquivalence[66] = 20035; sStringEquivalence[66] = "nai";
363             iUnicodeEquivalence[67] = 20037; sStringEquivalence[67] = "jiu";
364             iUnicodeEquivalence[68] = 20038; sStringEquivalence[68] = "jiu";
365             iUnicodeEquivalence[69] = 20039; sStringEquivalence[69] = "tuo";
366             iUnicodeEquivalence[70] = 20040; sStringEquivalence[70] = "me";
367             iUnicodeEquivalence[71] = 20041; sStringEquivalence[71] = "yi";
368             iUnicodeEquivalence[72] = 20043; sStringEquivalence[72] = "zhi";
369             iUnicodeEquivalence[73] = 20044; sStringEquivalence[73] = "wu";
370             iUnicodeEquivalence[74] = 20045; sStringEquivalence[74] = "zha";
371             iUnicodeEquivalence[75] = 20046; sStringEquivalence[75] = "hu";
372             iUnicodeEquivalence[76] = 20047; sStringEquivalence[76] = "fa";
373             iUnicodeEquivalence[77] = 20048; sStringEquivalence[77] = "le";
374         }
375     }
376 }
377