Update ooo320-m1
[ooovba.git] / qadevOOo / tests / java / ifc / i18n / _XCharacterClassification.java
blob93c80ac2f177236f3715220ff69d02edde5682d3
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: _XCharacterClassification.java,v $
10 * $Revision: 1.5 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 package ifc.i18n;
33 import lib.MultiMethodTest;
35 import com.sun.star.i18n.KParseTokens;
36 import com.sun.star.i18n.KParseType;
37 import com.sun.star.i18n.ParseResult;
38 import com.sun.star.i18n.XCharacterClassification;
39 import com.sun.star.lang.Locale;
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 compilant. <p>
57 * @see com.sun.star.i18n.XCharacterClassification
59 public class _XCharacterClassification extends MultiMethodTest {
60 public XCharacterClassification oObj = null;
61 public String[] languages = new String[]{"de","en","es","fr","ja","ko","zh"};
62 public String[] countries = new String[]{"DE","US","ES","FR","JP","KR","CN"};
64 public String[] charstyles_java = new String[] {"UNASSIGNED","UPPERCASE_LETTER",
65 "LOWERCASE_LETTER","TITLECASE_LETTER","MODIFIER_LETTER","OTHER_LETTER",
66 "NON_SPACING_MARK","ENCLOSING_MARK","COMBINING_SPACING_MARK",
67 "DECIMAL_DIGIT_NUMBER","LETTER_NUMBER","OTHER_NUMBER","SPACE_SEPARATOR",
68 "LINE_SEPARATOR","PARAGRAPH_SEPARATOR","CONTROL","FORMAT","none17",
69 "PRIVATE_USE","none19","DASH_PUNCTUATION","START_PUNCTUATION","END_PUNCTUATION",
70 "CONNECTOR_PUNCTUATION","OTHER_PUNCTUATION","MATH_SYMBOL","CURRENCY_SYMBOL",
71 "MODIFIER_SYMBOL","OTHER_SYMBOL"};
73 public String[] charstyles_office = new String[] {"UNASSIGNED","UPPERCASE_LETTER",
74 "LOWERCASE_LETTER","TITLECASE_LETTER","MODIFIER_LETTER","OTHER_LETTER",
75 "NON_SPACING_MARK","ENCLOSING_MARK","COMBINING_SPACING_MARK",
76 "DECIMAL_DIGIT_NUMBER","LETTER_NUMBER","OTHER_NUMBER","SPACE_SEPARATOR",
77 "LINE_SEPARATOR","PARAGRAPH_SEPARATOR","CONTROL","FORMAT","PRIVATE_USE",
78 "OTHER_PUNCTUATION","DASH_PUNCTUATION","START_PUNCTUATION","END_PUNCTUATION",
79 "CONNECTOR_PUNCTUATION",
80 "OTHER_PUNCTUATION","MATH_SYMBOL","CURRENCY_SYMBOL","MODIFIER_SYMBOL",
81 "OTHER_SYMBOL","INITIAL_PUNCTUATION","FINAL_PUNCTUATION","GENERAL_TYPES_COUNT"};
83 public String[] unicode_script = new String[] {"U_BASIC_LATIN","U_LATIN_1_SUPPLEMENT",
84 "U_LATIN_EXTENDED_A","U_LATIN_EXTENDED_B","U_IPA_EXTENSIONS","U_SPACING_MODIFIER_LETTERS",
85 "U_COMBINING_DIACRITICAL_MARKS","U_GREEK","U_CYRILLIC","U_ARMENIAN","U_HEBREW",
86 "U_ARABIC","U_SYRIAC","U_THAANA","U_DEVANAGARI","U_BENGALI","U_GURMUKHI",
87 "U_GUJARATI","U_ORIYA","U_TAMIL","U_TELUGU","U_KANNADA","U_MALAYALAM",
88 "U_SINHALA","U_THAI","U_LAO","U_TIBETAN","U_MYANMAR","U_GEORGIAN",
89 "U_HANGUL_JAMO","U_ETHIOPIC","U_CHEROKEE","U_UNIFIED_CANADIAN_ABORIGINAL_SYLLABICS",
90 "U_OGHAM","U_RUNIC","U_KHMER","U_MONGOLIAN","U_LATIN_EXTENDED_ADDITIONAL",
91 "U_GREEK_EXTENDED","U_GENERAL_PUNCTUATION","U_SUPERSCRIPTS_AND_SUBSCRIPTS",
92 "U_CURRENCY_SYMBOLS","U_COMBINING_MARKS_FOR_SYMBOLS","U_LETTERLIKE_SYMBOLS",
93 "U_NUMBER_FORMS","U_ARROWS","U_MATHEMATICAL_OPERATORS","U_MISCELLANEOUS_TECHNICAL",
94 "U_CONTROL_PICTURES","U_OPTICAL_CHARACTER_RECOGNITION","U_ENCLOSED_ALPHANUMERICS",
95 "U_BOX_DRAWING","U_BLOCK_ELEMENTS","U_GEOMETRIC_SHAPES","U_MISCELLANEOUS_SYMBOLS",
96 "U_DINGBATS","U_BRAILLE_PATTERNS","U_CJK_RADICALS_SUPPLEMENT","U_KANGXI_RADICALS",
97 "U_IDEOGRAPHIC_DESCRIPTION_CHARACTERS","U_CJK_SYMBOLS_AND_PUNCTUATION",
98 "U_HIRAGANA","U_KATAKANA","U_BOPOMOFO","U_HANGUL_COMPATIBILITY_JAMO","U_KANBUN",
99 "U_BOPOMOFO_EXTENDED","U_ENCLOSED_CJK_LETTERS_AND_MONTHS","U_CJK_COMPATIBILITY",
100 "U_CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A","U_CJK_UNIFIED_IDEOGRAPHS","U_YI_SYLLABLES",
101 "U_YI_RADICALS","U_HANGUL_SYLLABLES","U_HIGH_SURROGATES","U_HIGH_PRIVATE_USE_SURROGATES",
102 "U_LOW_SURROGATES","U_PRIVATE_USE_AREA","U_CJK_COMPATIBILITY_IDEOGRAPHS",
103 "U_ALPHABETIC_PRESENTATION_FORMS","U_ARABIC_PRESENTATION_FORMS_A","U_COMBINING_HALF_MARKS",
104 "U_CJK_COMPATIBILITY_FORMS","U_SMALL_FORM_VARIANTS","U_ARABIC_PRESENTATION_FORMS_B",
105 "U_SPECIALS","U_HALFWIDTH_AND_FULLWIDTH_FORMS","U_CHAR_SCRIPT_COUNT","U_NO_SCRIPT"};
108 * Test calls the method for different locales. Then each result is compared
109 * with a string, converted to a upper case using
110 * <code>java.lang.String</code> method <code>toUpperCase()</code>.<p>
111 * Has <b> OK </b> status if string, returned by the method is equal to
112 * a string that is returned by String.toUpperCase() for all locales.
114 public void _toUpper() {
115 boolean res = true;
116 char[] characters = new char[]{586,65,97,498,721,4588,772,8413,3404};
117 String toCheck = new String(characters);
118 String get = "";
119 String exp = "";
121 for (int i=0;i<7;i++) {
122 get = oObj.toUpper(toCheck, 0, toCheck.length(), getLocale(i));
123 exp = toCheck.toUpperCase(
124 new java.util.Locale(languages[i], countries[i]));
125 res &= get.equals(exp);
126 if (!res) {
127 log.println("FAILED for: language=" + languages[i] +
128 " ; country=" + countries[i]);
129 log.println("Expected: " + exp);
130 log.println("Gained : " + get);
133 tRes.tested("toUpper()", res);
137 * Test calls the method for different locales. Then each result is compared
138 * with a string, converted to a lower case using
139 * <code>java.lang.String</code> method <code>toLowerCase()</code>.<p>
140 * Has <b> OK </b> status if string, returned by the method is equal to
141 * a string that is returned by String.toLowerCase() for all locales.
143 public void _toLower() {
144 boolean res = true;
145 char[] characters = new char[]{586,65,97,498,721,4588,772,8413,3404};
146 String toCheck = new String(characters);
147 String get = "";
148 String exp = "";
150 for (int i=0;i<7;i++) {
151 get = oObj.toLower(toCheck,0,toCheck.length(),getLocale(i));
152 exp = toCheck.toLowerCase(
153 new java.util.Locale(languages[i],countries[i]));
154 res &= get.equals(exp);
155 if (!res) {
156 log.println("FAILED for: language=" + languages[i]
157 + " ; country=" + countries[i]);
158 log.println("Expected: " + exp);
159 log.println("Gained : " + get);
162 tRes.tested("toLower()", res);
166 * Test calls the method for different locales. Then each result is compared
167 * with a string, converted to a title case using
168 * <code>java.lang.Character</code> method <code>toTitleCase()</code>.<p>
169 * Has <b> OK </b> status if string, returned by the method is equal to
170 * a string that was converted using Character.toTitleCase() for all locales.
172 public void _toTitle() {
173 boolean res = true;
174 String toCheck = new String(new char[]{8112});
175 String get = "";
176 String exp = "";
178 for (int i=0;i<7;i++) {
179 get = oObj.toTitle(toCheck, 0, 1, getLocale(i));
180 exp = new String(
181 new char[]{Character.toTitleCase(toCheck.toCharArray()[0])});
182 res &= get.equals(exp);
183 if (!res) {
184 log.println("FAILED for: language=" + languages[i]
185 + " ; country=" + countries[i]);
186 log.println("Expected: " + exp);
187 log.println("Gained : " + get);
190 tRes.tested("toTitle()", res);
194 * At first we define <code>int[]</code> and <code>char[]</code> arrays of
195 * unicode symbol numbers, arranged as sequences, where symbols are sorted
196 * by type, so the character of <code>i<sup><small>th</small></sup></code>
197 * type is located on <code>i<sup><small>th</small></sup></code> position.<p>
198 * Has <b> OK </b> status if for all 30 types the method returns value, that
199 * is equal to an element number.<p>
200 * @see com.sun.star.i18n.CharType
202 public void _getType() {
203 boolean res = true;
204 char[] characters = new char[]{586,65,97,498,721,4588,772,8413,3404,
205 48,8544,179,32,8232,8233,144,8204,57344,56320,173,40,41,95,3852,247,
206 3647,901,3896,171,187};
207 int[] charsInt = new int[]{586,65,97,498,721,4588,772,8413,3404,48,
208 8544,179,32,8232,8233,144,8204,57344,56320,173,40,41,95,3852,247,
209 3647,901,3896,171,187};
210 String toCheck = new String(characters);
212 for (int i=0;i<characters.length;i++) {
213 int get = oObj.getType(toCheck, i);
214 res &= (charstyles_office[get] == charstyles_office[i]);
215 if (!res) {
216 log.println("Code :" + Integer.toHexString(charsInt[i]));
217 log.println("Gained: " + charstyles_office[get]);
218 log.println("Expected : " + charstyles_office[i]);
221 tRes.tested("getType()", res);
225 * After defining string to be checked and array of expected types, test
226 * calls the method for each character of a string and for all locales.<p>
227 * Has <b> OK </b> status if the method returns type, expected for a given
228 * character and locale.
230 public void _getCharacterType() {
231 boolean res = true;
232 String toCheck = "Ab0)";
233 int[] expected = new int[]{226,228,97,32};
235 for (int i=0;i<toCheck.length();i++) {
236 for (int j=1;j<7;j++) {
237 int get = oObj.getCharacterType(toCheck, i, getLocale(j));
238 res &= (get == expected[i]);
239 if (!res) {
240 log.println("FAILED for: language=" + languages[j] +
241 " ; country=" + countries[j]);
242 log.println("Sysmbol :" + toCheck.toCharArray()[i]);
243 log.println("Gained: " + get);
244 log.println("Expected : " + expected[i]);
248 tRes.tested("getCharacterType()", res);
252 * After defining array of strings to be checked and array of expected types,
253 * test calls the method for each string of an array and for all locales.<p>
254 * Has <b> OK </b> status if the method returns type, expected for a given
255 * string and locale.
257 public void _getStringType() {
258 boolean res = true;
259 String[] toCheck = new String[]{"01234","AAAAA","bbbbb","AA()bb"};
260 int[] exp = new int[]{97,226,228,230};
262 for (int j=0;j<toCheck.length;j++) {
263 for (int i=0;i<7;i++) {
264 int get = oObj.getStringType(toCheck[j], 0,
265 toCheck[j].length(), getLocale(i));
266 res &= (get == exp[j]);
267 if (!res) {
268 log.println("FAILED for: language=" + languages[i] +
269 " ; country=" + countries[i]);
270 log.println("Expected: " + exp[j]);
271 log.println("Gained : " + get);
275 tRes.tested("getStringType()", res);
279 * After string to be checked is initialized (all symbols are sorted
280 * by direction, so the character of <code>i<sup><small>th</small></sup></code>
281 * direction is located on <code>i<sup><small>th</small></sup></code>
282 * position), test calls the method for every character of that string. <p>
283 * Has <b> OK </b> status if the method returns direction, that's equal to
284 * a symbol position in the string.
286 public void _getCharacterDirection() {
287 boolean res = true;
288 String toCheck = new String(new char[]{65,1470,48,47,35,1632,44,10,
289 9,12,33,8234,8237,1563,8235,8238,8236,768,1});
290 for (short i=0;i<19;i++) {
291 short get = oObj.getCharacterDirection(toCheck, i);
292 res &= (get == i);
293 if (!res) {
294 log.println("Code :" + toCheck.toCharArray()[i]);
295 log.println("Gained: " + get);
296 log.println("Expected: " + i);
299 tRes.tested("getCharacterDirection()", res);
303 * At first we define <code>int[]</code> and <code>char[]</code> arrays of
304 * unicode symbol numbers, arranged as sequences, where symbols are sorted
305 * by type, so the character of <code>i<sup><small>th</small></sup></code>
306 * type is located on <code>i<sup><small>th</small></sup></code> position.<p>
307 * Has <b> OK </b> status if for each character method returns value, that
308 * is equal to a number where element is located in array. Also method has
309 * <b> OK </b> status for symbol with code 55296, because it doesn't work
310 * since it hasn't the right neighborhood.<p>
311 * @see http://ppewww.ph.gla.ac.uk/~flavell/unicode/unidata.html
313 public void _getScript() {
314 boolean res = true;
315 char[] characters = new char[]{65,128,256,384,592,750,773,924,1030,1331,1448,
316 1569,1792,1936,2313,2465,2570,2707,2822,2972,3079,3240,3337,3464,3590,
317 3745,3906,4097,4274,4357,4621,5040,5200,5776,5806,6030,6155,7683,7943,
318 8202,8319,8352,8413,8452,8545,8616,8715,8965,9217,9281,9336,9474,9608,9719,
319 9734,9999,10247,11911,12034,12274,12294,12358,12456,12552,12605,12688,12727,
320 12806,13065,13312,19968,40964,42152,44032,55296,56192,56320,57344,63744,
321 64257,64370,65056,65073,65131,65146,65532,65288};
322 int[] charsInt = new int[]{65,128,256,384,592,750,773,924,1030,1331,1448,
323 1569,1792,1936,2313,2465,2570,2707,2822,2972,3079,3240,3337,3464,3590,
324 3745,3906,4097,4274,4357,4621,5040,5200,5776,5806,6030,6155,7683,7943,
325 8202,8319,8352,8413,8452,8545,8616,8715,8965,9217,9281,9336,9474,9608,9719,
326 9734,9999,10247,11911,12034,12274,12294,12358,12456,12552,12605,12688,12727,
327 12806,13065,13312,19968,40964,42152,44032,55296,56192,56320,57344,63744,
328 64257,64370,65056,65073,65131,65146,65532,65288};
329 String toCheck = new String(characters);
331 for (int i=0;i<characters.length;i++) {
332 int get = oObj.getScript(toCheck, i);
333 res &= (get == i);
334 //The HIGH_SURROGATE 55296 doesn't work since it hasn't the right
335 //neighborhood
336 if (toCheck.substring(i, i + 1).hashCode() == 55296) res = true;
337 if (!res) {
338 log.println("-- " + toCheck.substring(i, i + 1).hashCode());
339 log.println("Code: " + Integer.toHexString(charsInt[i]));
340 log.println("Gained: " + unicode_script[get]);
341 log.println("Expected: " + unicode_script[i]);
344 tRes.tested("getScript()", res);
348 * After defining a string to be parsed and parse conditions (flags), test
349 * calls the method for different locales three times with different parameters,
350 * checking result after every call. <p>
351 * Has <b> OK </b> status if the method returns right results all three
352 * times.
354 public void _parseAnyToken() {
355 int nStartFlags = KParseTokens.ANY_ALPHA | KParseTokens.ASC_UNDERSCORE;
356 int nContFlags = KParseTokens.ANY_ALNUM | KParseTokens.ASC_UNDERSCORE
357 | KParseTokens.ASC_DOT;
358 String toCheck = " 18 i18n ^";
359 ParseResult pRes = null;
360 boolean res = true;
362 for (int i=0;i<7;i++) {
363 pRes = oObj.parseAnyToken(toCheck, 1, getLocale(i),
364 nStartFlags, "", nContFlags, "");
365 res = ( (pRes.CharLen==2)
366 && (pRes.TokenType==32)
367 && (pRes.Value==18.0) );
368 pRes = oObj.parseAnyToken(toCheck, 4, getLocale(i),
369 nStartFlags, "", nContFlags, "");
370 res &= ( (pRes.CharLen==4)
371 && (pRes.TokenType==4)
372 && (pRes.Value==0.0) );
373 pRes = oObj.parseAnyToken(toCheck, 9, getLocale(i),
374 nStartFlags, "", nContFlags, "");
375 res &= ( (pRes.CharLen==1)
376 && (pRes.TokenType==1)
377 && (pRes.Value==0.0) );
379 tRes.tested("parseAnyToken()", res);
383 * After defining a string to be parsed and parse conditions (flags), test
384 * calls the method for different locales two times with different parameters,
385 * checking result after every call. <p>
386 * Has <b> OK </b> status if the method returns right results.
388 public void _parsePredefinedToken() {
389 int nStartFlags = KParseTokens.ANY_ALPHA | KParseTokens.ASC_UNDERSCORE;
390 int nContFlags = nStartFlags;
391 String toCheck = " 18 int";
392 ParseResult pRes = null;
393 boolean res = true;
395 for (int i=0;i<7;i++) {
396 pRes = oObj.parsePredefinedToken(KParseType.IDENTNAME, toCheck,
397 1, getLocale(i), nStartFlags, "", nContFlags, "");
398 res = (pRes.CharLen==0);
399 pRes = oObj.parsePredefinedToken(KParseType.IDENTNAME, toCheck,
400 4, getLocale(i), nStartFlags, "", nContFlags, "");
401 res &= ( (pRes.CharLen==3)
402 && (pRes.TokenType==4)
403 && (pRes.Value==0.0) );
405 tRes.tested("parsePredefinedToken()", res);
410 * Method returns locale for a given language and country.
411 * @param localeIndex index of needed locale.
413 private Locale getLocale(int k) {
414 return new Locale(languages[k],countries[k],"");
418 } // end XCharacterClassification