xref: /trunk/test/testuno/source/api/i18n/XCharacterClassificationTest.java (revision 07b0567639065325a4ab02b0e8addd3ac9d3dae1)
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 com.sun.star.i18n.KParseTokens;
27 import com.sun.star.i18n.KParseType;
28 import com.sun.star.i18n.ParseResult;
29 import com.sun.star.i18n.XCharacterClassification;
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.After;
34 import org.junit.AfterClass;
35 import org.junit.Before;
36 import org.junit.BeforeClass;
37 import org.junit.Assert;
38 import org.junit.Test;
39 import org.openoffice.test.uno.UnoApp;
40 
41 /**
42  * Testing <code>com.sun.star.i18n.XCharacterClassification</code>
43  * interface methods:
44  * <ul>
45  *  <li><code> toUpper() </code></li>
46  *  <li><code> toLower() </code></li>
47  *  <li><code> toTitle() </code></li>
48  *  <li><code> getType() </code></li>
49  *  <li><code> getCharacterType() </code></li>
50  *  <li><code> getStringType() </code></li>
51  *  <li><code> getCharacterDirection() </code></li>
52  *  <li><code> getScript() </code></li>
53  *  <li><code> parseAnyToken() </code></li>
54  *  <li><code> parsePredefinedToken() </code></li>
55  * </ul><p>
56  * Test is <b> NOT </b> multithread compliant. <p>
57  * @see com.sun.star.i18n.XCharacterClassification
58  */
59 public class XCharacterClassificationTest {
60     private static final UnoApp app = new UnoApp();
61 
62     private XComponentContext xContext = null;
63     public XCharacterClassification oObj = null;
64     public String[] languages = new String[]{"de","en","es","fr","ja","ko","zh"};
65     public String[] countries = new String[]{"DE","US","ES","FR","JP","KR","CN"};
66 
67     public String[] charstyles_java = new String[] {"UNASSIGNED","UPPERCASE_LETTER",
68         "LOWERCASE_LETTER","TITLECASE_LETTER","MODIFIER_LETTER","OTHER_LETTER",
69         "NON_SPACING_MARK","ENCLOSING_MARK","COMBINING_SPACING_MARK",
70         "DECIMAL_DIGIT_NUMBER","LETTER_NUMBER","OTHER_NUMBER","SPACE_SEPARATOR",
71         "LINE_SEPARATOR","PARAGRAPH_SEPARATOR","CONTROL","FORMAT","none17",
72         "PRIVATE_USE","none19","DASH_PUNCTUATION","START_PUNCTUATION","END_PUNCTUATION",
73         "CONNECTOR_PUNCTUATION","OTHER_PUNCTUATION","MATH_SYMBOL","CURRENCY_SYMBOL",
74         "MODIFIER_SYMBOL","OTHER_SYMBOL"};
75 
76     public String[] charstyles_office = new String[] {"UNASSIGNED","UPPERCASE_LETTER",
77         "LOWERCASE_LETTER","TITLECASE_LETTER","MODIFIER_LETTER","OTHER_LETTER",
78         "NON_SPACING_MARK","ENCLOSING_MARK","COMBINING_SPACING_MARK",
79         "DECIMAL_DIGIT_NUMBER","LETTER_NUMBER","OTHER_NUMBER","SPACE_SEPARATOR",
80         "LINE_SEPARATOR","PARAGRAPH_SEPARATOR","CONTROL","FORMAT","PRIVATE_USE",
81         "OTHER_PUNCTUATION","DASH_PUNCTUATION","START_PUNCTUATION","END_PUNCTUATION",
82         "CONNECTOR_PUNCTUATION",
83         "OTHER_PUNCTUATION","MATH_SYMBOL","CURRENCY_SYMBOL","MODIFIER_SYMBOL",
84         "OTHER_SYMBOL","INITIAL_PUNCTUATION","FINAL_PUNCTUATION","GENERAL_TYPES_COUNT"};
85 
86     public String[] unicode_script = new String[] {"U_BASIC_LATIN","U_LATIN_1_SUPPLEMENT",
87         "U_LATIN_EXTENDED_A","U_LATIN_EXTENDED_B","U_IPA_EXTENSIONS","U_SPACING_MODIFIER_LETTERS",
88         "U_COMBINING_DIACRITICAL_MARKS","U_GREEK","U_CYRILLIC","U_ARMENIAN","U_HEBREW",
89         "U_ARABIC","U_SYRIAC","U_THAANA","U_DEVANAGARI","U_BENGALI","U_GURMUKHI",
90         "U_GUJARATI","U_ORIYA","U_TAMIL","U_TELUGU","U_KANNADA","U_MALAYALAM",
91         "U_SINHALA","U_THAI","U_LAO","U_TIBETAN","U_MYANMAR","U_GEORGIAN",
92         "U_HANGUL_JAMO","U_ETHIOPIC","U_CHEROKEE","U_UNIFIED_CANADIAN_ABORIGINAL_SYLLABICS",
93         "U_OGHAM","U_RUNIC","U_KHMER","U_MONGOLIAN","U_LATIN_EXTENDED_ADDITIONAL",
94         "U_GREEK_EXTENDED","U_GENERAL_PUNCTUATION","U_SUPERSCRIPTS_AND_SUBSCRIPTS",
95         "U_CURRENCY_SYMBOLS","U_COMBINING_MARKS_FOR_SYMBOLS","U_LETTERLIKE_SYMBOLS",
96         "U_NUMBER_FORMS","U_ARROWS","U_MATHEMATICAL_OPERATORS","U_MISCELLANEOUS_TECHNICAL",
97         "U_CONTROL_PICTURES","U_OPTICAL_CHARACTER_RECOGNITION","U_ENCLOSED_ALPHANUMERICS",
98         "U_BOX_DRAWING","U_BLOCK_ELEMENTS","U_GEOMETRIC_SHAPES","U_MISCELLANEOUS_SYMBOLS",
99         "U_DINGBATS","U_BRAILLE_PATTERNS","U_CJK_RADICALS_SUPPLEMENT","U_KANGXI_RADICALS",
100         "U_IDEOGRAPHIC_DESCRIPTION_CHARACTERS","U_CJK_SYMBOLS_AND_PUNCTUATION",
101         "U_HIRAGANA","U_KATAKANA","U_BOPOMOFO","U_HANGUL_COMPATIBILITY_JAMO","U_KANBUN",
102         "U_BOPOMOFO_EXTENDED","U_ENCLOSED_CJK_LETTERS_AND_MONTHS","U_CJK_COMPATIBILITY",
103         "U_CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A","U_CJK_UNIFIED_IDEOGRAPHS","U_YI_SYLLABLES",
104         "U_YI_RADICALS","U_HANGUL_SYLLABLES","U_HIGH_SURROGATES","U_HIGH_PRIVATE_USE_SURROGATES",
105         "U_LOW_SURROGATES","U_PRIVATE_USE_AREA","U_CJK_COMPATIBILITY_IDEOGRAPHS",
106         "U_ALPHABETIC_PRESENTATION_FORMS","U_ARABIC_PRESENTATION_FORMS_A","U_COMBINING_HALF_MARKS",
107         "U_CJK_COMPATIBILITY_FORMS","U_SMALL_FORM_VARIANTS","U_ARABIC_PRESENTATION_FORMS_B",
108         "U_SPECIALS","U_HALFWIDTH_AND_FULLWIDTH_FORMS","U_CHAR_SCRIPT_COUNT","U_NO_SCRIPT"};
109 
110     // setup and close connections
111     @BeforeClass
112     public static void setUpConnection() throws Exception
113     {
114         app.start();
115     }
116 
117     @AfterClass
118     public static void tearDownConnection() throws InterruptedException, com.sun.star.uno.Exception
119     {
120         app.close();
121     }
122 
123     @Before
124     public void before() throws Exception {
125         xContext = app.getComponentContext();
126         oObj = UnoRuntime.queryInterface(
127             XCharacterClassification.class,
128             xContext.getServiceManager().createInstanceWithContext("com.sun.star.i18n.CharacterClassification", xContext)
129         );
130     }
131 
132     /**
133     * Test calls the method for different locales. Then each result is compared
134     * with a string, converted to a upper case using
135     * <code>java.lang.String</code> method <code>toUpperCase()</code>.<p>
136     * Has <b> OK </b> status if string, returned by the method is equal to
137     * a string that is returned by String.toUpperCase() for all locales.
138     */
139     @Test
140     public void _toUpper() {
141         boolean res = true;
142         char[] characters = new char[]{586,65,97,498,721,4588,772,8413,3404};
143         String toCheck = new String(characters);
144         String get = "";
145         String exp = "";
146 
147         for (int i=0;i<7;i++) {
148             get = oObj.toUpper(toCheck, 0, toCheck.length(), getLocale(i));
149             exp = toCheck.toUpperCase(
150                 new java.util.Locale(languages[i], countries[i]));
151             res &= get.equals(exp);
152             if (!res) {
153                 System.out.println("FAILED for: language=" + languages[i] +
154                     " ; country=" + countries[i]);
155                 System.out.println("Expected: " + exp);
156                 System.out.println("Gained : " + get);
157             }
158         }
159         Assert.assertTrue("toUpper()", res);
160     }
161 
162     /**
163     * Test calls the method for different locales. Then each result is compared
164     * with a string, converted to a lower case using
165     * <code>java.lang.String</code> method <code>toLowerCase()</code>.<p>
166     * Has <b> OK </b> status if string, returned by the method is equal to
167     * a string that is returned by String.toLowerCase() for all locales.
168     */
169     @Test
170     public void _toLower() {
171         boolean res = true;
172         char[] characters = new char[]{586,65,97,498,721,4588,772,8413,3404};
173         String toCheck = new String(characters);
174         String get = "";
175         String exp = "";
176 
177         for (int i=0;i<7;i++) {
178             get = oObj.toLower(toCheck,0,toCheck.length(),getLocale(i));
179             exp = toCheck.toLowerCase(
180                 new java.util.Locale(languages[i],countries[i]));
181             res &= get.equals(exp);
182             if (!res) {
183                 System.out.println("FAILED for: language=" + languages[i]
184                     + " ; country=" + countries[i]);
185                 System.out.println("Expected: " + exp);
186                 System.out.println("Gained : " + get);
187             }
188         }
189         Assert.assertTrue("toLower()", res);
190     }
191 
192     /**
193     * Test calls the method for different locales. Then each result is compared
194     * with a string, converted to a title case using
195     * <code>java.lang.Character</code> method <code>toTitleCase()</code>.<p>
196     * Has <b> OK </b> status if string, returned by the method is equal to
197     * a string that was converted using Character.toTitleCase() for all locales.
198     */
199     @Test
200     public void _toTitle() {
201         boolean res = true;
202         String toCheck = new String(new char[]{8112});
203         String get = "";
204         String exp = "";
205 
206         for (int i=0;i<7;i++) {
207             get = oObj.toTitle(toCheck, 0, 1, getLocale(i));
208             exp = new String(
209                 new char[]{Character.toTitleCase(toCheck.toCharArray()[0])});
210             res &= get.equals(exp);
211             if (!res) {
212                 System.out.println("FAILED for: language=" + languages[i]
213                     + " ; country=" + countries[i]);
214                 System.out.println("Expected: " + exp);
215                 System.out.println("Gained : " + get);
216             }
217         }
218         Assert.assertTrue("toTitle()", res);
219     }
220 
221     /**
222     * At first we define <code>int[]</code> and <code>char[]</code> arrays of
223     * unicode symbol numbers, arranged as sequences, where symbols are sorted
224     * by type, so the character of <code>i<sup><small>th</small></sup></code>
225     * type is located on <code>i<sup><small>th</small></sup></code> position.<p>
226     * Has <b> OK </b> status if for all 30 types the method returns value, that
227     * is equal to an element number.<p>
228     * @see com.sun.star.i18n.CharType
229     */
230     @Test
231     public void _getType() {
232         boolean res = true;
233         // If or when this list gets out of date again,
234         // find new characters of the required type with:
235         // for (int i = 30; i <= 0xffff; i++) {
236         //     String s = new String("" + (char)i);
237         //     int type = oObj.getType(s, 0);
238         //     System.out.println("character " + i + " has type " + charstyles_office[type]);
239         // }
240         char[] characters = new char[]{888,65,97,498,721,4588,772,8413,3404,
241             48,8544,179,32,8232,8233,144,8204,57344,56320,45,40,41,95,3852,247,
242             3647,901,3896,171,187};
243         String toCheck = new String(characters);
244 
245         for (int i=0;i<characters.length;i++) {
246             int get = oObj.getType(toCheck, i);
247             res &= (charstyles_office[get] == charstyles_office[i]);
248             if (!res) {
249                 System.out.println("Code: " + Integer.toHexString((int)characters[i]));
250                 System.out.println("Gained: " + charstyles_office[get]);
251                 System.out.println("Expected: " + charstyles_office[i]);
252             }
253         }
254         Assert.assertTrue("getType()", res);
255     }
256 
257     /**
258     * After defining string to be checked and array of expected types, test
259     * calls the method for each character of a string and for all locales.<p>
260     * Has <b> OK </b> status if the method returns type, expected for a given
261     * character and locale.
262     */
263     @Test
264     public void _getCharacterType() {
265         boolean res = true;
266         // If or when this list gets out of date again,
267         // find new characters of the required type with:
268         //for (int i = 32; i <= 0xffff; i++) {
269         //    int charType = oObj.getCharacterType("" + (char)i, 0, getLocale(1));
270         //    if (charType == 32) {
271         //        System.out.println("character " + i + " has type 32");
272         //    }
273         //}
274         String toCheck = "Ab0*";
275         int[] expected = new int[]{226,228,97,32};
276 
277         for (int i=0;i<toCheck.length();i++) {
278             for (int j=1;j<7;j++) {
279                 int get = oObj.getCharacterType(toCheck, i, getLocale(j));
280                 res &= (get == expected[i]);
281                 if (!res) {
282                     System.out.println("FAILED for: language=" + languages[j] +
283                         " ; country=" + countries[j]);
284                     System.out.println("Symbol: " + toCheck.toCharArray()[i]);
285                     System.out.println("Gained: " + get);
286                     System.out.println("Expected: " + expected[i]);
287                 }
288             }
289         }
290         Assert.assertTrue("getCharacterType()", res);
291     }
292 
293     /**
294     * After defining array of strings to be checked and array of expected types,
295     * test calls the method for each string of an array and for all locales.<p>
296     * Has <b> OK </b> status if the method returns type, expected for a given
297     * string and locale.
298     */
299     @Test
300     public void _getStringType() {
301         boolean res = true;
302         String[] toCheck = new String[]{"01234","AAAAA","bbbbb","AA()bb"};
303         int[] exp = new int[]{97,226,228,230};
304 
305         for (int j=0;j<toCheck.length;j++) {
306             for (int i=0;i<7;i++) {
307                 int get = oObj.getStringType(toCheck[j], 0,
308                     toCheck[j].length(), getLocale(i));
309                 res &= (get == exp[j]);
310                 if (!res) {
311                     System.out.println("FAILED for: language=" + languages[i] +
312                         " ; country=" + countries[i]);
313                     System.out.println("Expected: " + exp[j]);
314                     System.out.println("Gained : " + get);
315                 }
316             }
317         }
318         Assert.assertTrue("getStringType()", res);
319     }
320 
321     /**
322     * After string to be checked is initialized (all symbols are sorted
323     * by direction, so the character of <code>i<sup><small>th</small></sup></code>
324     * direction is located on <code>i<sup><small>th</small></sup></code>
325     * position), test calls the method for every character of that string. <p>
326     * Has <b> OK </b> status if the method returns direction, that's equal to
327     * a symbol position in the string.
328     */
329     @Test
330     public void _getCharacterDirection() {
331         boolean res = true;
332         String toCheck = new String(new char[]{65,1470,48,43,35,1632,44,10,
333                                 9,12,33,8234,8237,1563,8235,8238,8236,768,1});
334         for (short i=0;i<19;i++) {
335             short get = oObj.getCharacterDirection(toCheck, i);
336             res &= (get == i);
337             if (!res) {
338                 System.out.println("Code :" + toCheck.toCharArray()[i]);
339                 System.out.println("Gained: " + get);
340                 System.out.println("Expected: " + i);
341             }
342         }
343         Assert.assertTrue("getCharacterDirection()", res);
344     }
345 
346     /**
347     * At first we define <code>int[]</code> and <code>char[]</code> arrays of
348     * unicode symbol numbers, arranged as sequences, where symbols are sorted
349     * by type, so the character of <code>i<sup><small>th</small></sup></code>
350     * type is located on <code>i<sup><small>th</small></sup></code> position.<p>
351     * Has <b> OK </b> status if for each character method returns value, that
352     * is equal to a number where element is located in array. Also method has
353     * <b> OK </b> status for symbol with code 55296, because it doesn't work
354     * since it hasn't the right neighborhood.<p>
355     * @see "http://ppewww.ph.gla.ac.uk/~flavell/unicode/unidata.html"
356     */
357     @Test
358     public void _getScript() {
359         boolean res = true;
360         char[] characters = new char[]{65,128,256,384,592,750,773,924,1030,1331,1448,
361             1569,1792,1936,2313,2465,2570,2707,2822,2972,3079,3240,3337,3464,3590,
362             3745,3906,4097,4274,4357,4621,5040,5200,5776,5806,6030,6155,7683,7943,
363             8202,8319,8352,8413,8452,8545,8616,8715,8965,9217,9281,9336,9474,9608,9719,
364             9734,9999,10247,11911,12034,12274,12294,12358,12456,12552,12605,12688,12727,
365             12806,13065,13312,19968,40964,42152,44032,55296,56192,56320,57344,63744,
366             64257,64370,65056,65073,65131,65146,65532,65288};
367         String toCheck = new String(characters);
368 
369         for (int i=0;i<characters.length;i++) {
370             int get = oObj.getScript(toCheck, i);
371             //The HIGH_SURROGATE 55296 doesn't work since it hasn't the right
372             //neighborhood. Neither does 56192 - it combines with 55296 into another code point.
373             if (toCheck.charAt(i) != 55296 && toCheck.charAt(i) != 56192) {
374                 res &= (get == i);
375             }
376             if (!res) {
377                 System.out.println("-- " + toCheck.substring(i, i + 1).hashCode());
378                 System.out.println("Code: " + Integer.toHexString((int)characters[i]));
379                 System.out.println("Gained: " + get + " (" + (0 <= get && get < unicode_script.length ? unicode_script[get] : "out of range") + ")");
380                 System.out.println("Expected: " + unicode_script[i]);
381             }
382         }
383         Assert.assertTrue("getScript()", res);
384     }
385 
386     /**
387     * After defining a string to be parsed and parse conditions (flags), test
388     * calls the method for different locales three times with different parameters,
389     * checking result after every call.  <p>
390     * Has <b> OK </b> status if the method returns right results all three
391     * times.
392     */
393     @Test
394     public void _parseAnyToken() {
395         int nStartFlags = KParseTokens.ANY_ALPHA | KParseTokens.ASC_UNDERSCORE;
396         int nContFlags = KParseTokens.ANY_ALNUM | KParseTokens.ASC_UNDERSCORE
397                         | KParseTokens.ASC_DOT;
398         String toCheck = " 18 i18n ^";
399         ParseResult pRes = null;
400         boolean res = true;
401 
402         for (int i=0;i<7;i++) {
403             pRes = oObj.parseAnyToken(toCheck, 1, getLocale(i),
404                 nStartFlags, "", nContFlags, "");
405             res = ( (pRes.CharLen==2)
406                  && (pRes.TokenType==32)
407                  && (pRes.Value==18.0) );
408             pRes = oObj.parseAnyToken(toCheck, 4, getLocale(i),
409                 nStartFlags, "", nContFlags, "");
410             res &= ( (pRes.CharLen==4)
411                   && (pRes.TokenType==4)
412                   && (pRes.Value==0.0) );
413             pRes = oObj.parseAnyToken(toCheck, 9, getLocale(i),
414                 nStartFlags, "", nContFlags, "");
415             res &= ( (pRes.CharLen==1)
416                   && (pRes.TokenType==1)
417                   && (pRes.Value==0.0) );
418         }
419         Assert.assertTrue("parseAnyToken()", res);
420     }
421 
422     /**
423     * After defining a string to be parsed and parse conditions (flags), test
424     * calls the method for different locales two times with different parameters,
425     * checking result after every call. <p>
426     * Has <b> OK </b> status if the method returns right results.
427     */
428     @Test
429     public void _parsePredefinedToken() {
430         int nStartFlags = KParseTokens.ANY_ALPHA | KParseTokens.ASC_UNDERSCORE;
431         int nContFlags = nStartFlags;
432         String toCheck = " 18 int";
433         ParseResult pRes = null;
434         boolean res = true;
435 
436         for (int i=0;i<7;i++) {
437             pRes = oObj.parsePredefinedToken(KParseType.IDENTNAME, toCheck,
438                 1, getLocale(i), nStartFlags, "", nContFlags, "");
439             res = (pRes.CharLen==0);
440             pRes = oObj.parsePredefinedToken(KParseType.IDENTNAME, toCheck,
441                 4, getLocale(i), nStartFlags, "", nContFlags, "");
442             res &= ( (pRes.CharLen==3)
443                   && (pRes.TokenType==4)
444                   && (pRes.Value==0.0) );
445         }
446         Assert.assertTrue("parsePredefinedToken()", res);
447     }
448 
449 
450     /**
451     * Method returns locale for a given language and country.
452     * @param k index of needed locale.
453     */
454     private Locale getLocale(int k) {
455         return new Locale(languages[k],countries[k],"");
456     }
457 
458 
459 } // end XCharacterClassification
460