1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 package ifc.i18n;
29 
30 import lib.MultiMethodTest;
31 
32 import com.sun.star.i18n.TransliterationModules;
33 import com.sun.star.i18n.TransliterationModulesNew;
34 import com.sun.star.i18n.TransliterationType;
35 import com.sun.star.i18n.XTransliteration;
36 import com.sun.star.lang.Locale;
37 
38 /**
39 * Testing <code>com.sun.star.i18n.XTransliteration</code>
40 * interface methods :
41 * <ul>
42 *  <li><code> getName()</code></li>
43 *  <li><code> getType()</code></li>
44 *  <li><code> loadModule()</code></li>
45 *  <li><code> loadModuleNew()</code></li>
46 *  <li><code> loadModuleByImplName()</code></li>
47 *  <li><code> loadModulesByImplNames()</code></li>
48 *  <li><code> getAvailableModules()</code></li>
49 *  <li><code> transliterate()</code></li>
50 *  <li><code> folding()</code></li>
51 *  <li><code> equals()</code></li>
52 *  <li><code> transliterateRange()</code></li>
53 * </ul> <p>
54 * Test is <b> NOT </b> multithread compilant. <p>
55 * @see com.sun.star.i18n.XTransliteration
56 */
57 public class _XTransliteration extends MultiMethodTest {
58 
59     public XTransliteration oObj = null;
60     private String[] mod = null ;
61     private Locale loc = new Locale("en", "EN", "") ;
62 
63     /**
64     * Gets all available transliteration modules. <p>
65     * Has <b>OK</b> status if array returned has at least
66     * one module name.
67     */
68     public void _getAvailableModules() {
69         mod = oObj.getAvailableModules(loc, TransliterationType.ONE_TO_ONE);
70 
71         if (mod != null) {
72             log.println("Available modules :") ;
73             for (int i = 0; i < mod.length; i++) {
74                 log.println("  '" + mod[i] + "'") ;
75             }
76         } else {
77             log.println("!!! NULL returned !!!") ;
78         }
79 
80         tRes.tested("getAvailableModules()", mod != null && mod.length > 0) ;
81     }
82 
83     /**
84     * Calls the method for load IGNORE_CASE module and checks the name returned
85     * by the method <code>getName</code>. <p>
86     * Has <b>OK</b> status if the method <code>getName</code> returns the
87     * string "case ignore (generic)".
88     */
89     public void _loadModule() {
90         log.println("Load module IGNORE_CASE");
91         oObj.loadModule(TransliterationModules.IGNORE_CASE, loc);
92 
93         String name = oObj.getName();
94         boolean res = name.equals("case ignore (generic)");
95         log.println("getName return: " + name);
96 
97         tRes.tested("loadModule()", res );
98     }
99 
100     /**
101      * Loads <code>LOWERCASE_UPPERCASE</code> module and checks the current
102      * name of object. <p>
103      *
104      * Has <b>OK</b> status if the name of the object is equals to
105      * 'lower_to_upper(generic)'
106      */
107     public void _loadModuleNew() {
108         boolean result = true ;
109 
110         oObj.loadModuleNew(
111             new TransliterationModulesNew[]
112             {TransliterationModulesNew.LOWERCASE_UPPERCASE}, loc);
113 
114         String name = oObj.getName();
115         result = name.equals("lower_to_upper(generic)");
116         log.println("getName return: " + name);
117 
118         tRes.tested("loadModuleNew()", result);
119     }
120 
121     /**
122     * Calls the method for load LOWERCASE_UPPERCASE module and
123     * checks the name returned by the method <code>getName</code>. <p>
124     * Has <b>OK</b> status if the method <code>getName</code> returns the
125     * string "lower_to_upper(generic)".
126     */
127     public void _loadModuleByImplName() {
128         log.println("Load module LOWERCASE_UPPERCASE");
129         oObj.loadModuleByImplName("LOWERCASE_UPPERCASE", loc);
130 
131         String name = oObj.getName();
132         boolean res = name.equals("lower_to_upper(generic)");
133         log.println("getName return: " + name);
134 
135         tRes.tested("loadModuleByImplName()", res);
136     }
137 
138     /**
139     * Calls the method for load UPPERCASE_LOWERCASE module and
140     * checks the name returned by the method <code>getName</code>. <p>
141     * Has <b>OK</b> status if the method <code>getName</code> returns the
142     * string "upper_to_lower(generic)".
143     */
144     public void _loadModulesByImplNames() {
145         log.println("Load module UPPERCASE_LOWERCASE");
146         oObj.loadModulesByImplNames(new String[]{"UPPERCASE_LOWERCASE"}, loc);
147 
148         String name = oObj.getName();
149         boolean res = name.equals("upper_to_lower(generic)");
150         log.println("getName return: " + name);
151 
152         tRes.tested("loadModulesByImplNames()", res);
153     }
154 
155     /**
156      * Loads <code>LOWERCASE_UPPERCASE</code> module and checks current type.
157      * <p>Has <b>OK</b> status if the type is <code>ONE_TO_ONE</code>
158      */
159     public void _getType() {
160         oObj.loadModule(TransliterationModules.LOWERCASE_UPPERCASE, loc);
161         boolean result = oObj.getType() == TransliterationType.ONE_TO_ONE;
162         tRes.tested("getType()", result);
163     }
164 
165     /**
166     * Loads UPPERCASE_LOWERCASE module and
167     * checks the name returned by the method <code>getName</code>. <p>
168     *
169     * Has <b>OK</b> status if the method <code>getName</code> returns the
170     * string "upper_to_lower(generic)".
171     */
172     public void _getName() {
173         oObj.loadModule(TransliterationModules.LOWERCASE_UPPERCASE, loc);
174 
175         String name = oObj.getName();
176         boolean res = name.equals("lower_to_upper(generic)");
177         log.println("getName return: " + name);
178 
179         tRes.tested("getName()", res);
180     }
181 
182     /**
183     * First loads <code>LOWERCASE_UPPERCASE</code> module.
184     * Then tries to transliterate (make uppercase) a substring. <p>
185     * Has <b>OK</b> status if all chars were made uppercase,
186     * and array returned has size as substring length, and its
187     * elements are positions of substring characters in the source
188     * string.
189     */
190     public void _transliterate() {
191         oObj.loadModule(TransliterationModules.LOWERCASE_UPPERCASE, loc);
192 
193         int[][] offs = new int[1][] ;
194 
195         String out = oObj.transliterate("AaBbCc", 1, 4, offs) ;
196 
197         boolean result = "ABBC".equals(out) && offs[0].length == 4 &&
198             offs[0][0] == 1 &&
199             offs[0][1] == 2 &&
200             offs[0][2] == 3 &&
201             offs[0][3] == 4 ;
202 
203         tRes.tested("transliterate()", result) ;
204     }
205 
206 
207     /**
208     * First loads <code>LOWERCASE_UPPERCASE</code> module.
209     * Tries to transliterate a range of two characters. <p>
210     * Has <b>OK</b> status if the appropriate String array
211     * returned (not null, length = 4, with two ranges
212     * (a, i), (A, I) in any order).
213     */
214     public void _transliterateRange() {
215         oObj.loadModule(TransliterationModules.IGNORE_CASE, loc);
216 
217         String[] out = oObj.transliterateRange("a", "i") ;
218 
219         log.println("transliterateRange return:");
220         for(int i = 0; i < out.length; i++) {
221             log.println(out[i]);
222         }
223 
224         boolean bOK = out != null &&
225             out.length == 4 &&
226             ("A".equals(out[0]) && "I".equals(out[1]) &&
227             "a".equals(out[2]) && "i".equals(out[3])) ||
228             ("a".equals(out[0]) && "i".equals(out[1]) &&
229             "A".equals(out[2]) && "I".equals(out[3])) ;
230 
231         if (!bOK) {
232             log.println("Unexpected range returned :");
233             for (int i = 0; i < out.length; i++) {
234                 log.print("'" + out[i] +"', ");
235             }
236             log.println();
237         }
238 
239         tRes.tested("transliterateRange()", bOK);
240     }
241 
242     /**
243     * This method is used internally by <code>equals</code>
244     * method so it indirectly tested in this method. <p>
245     * Always has <b>OK</b> status.
246     */
247     public void _folding() {
248         oObj.loadModule(TransliterationModules.LOWERCASE_UPPERCASE, loc);
249 
250         int[][] offs = new int[1][] ;
251 
252         String out = oObj.folding("AaBbCc", 1, 4, offs) ;
253 
254         boolean result = "ABBC".equals(out) && offs[0].length == 4 &&
255             offs[0][0] == 1 &&
256             offs[0][1] == 2 &&
257             offs[0][2] == 3 &&
258             offs[0][3] == 4 ;
259 
260 
261         tRes.tested("folding()", result) ;
262     }
263 
264 
265     /**
266     * First loads <code>LOWERCASE_UPPERCASE</code> module.
267     * Tries to compare two equal substrings. <p>
268     * Has <b>OK</b> status if the method returned <code>true</code>.
269     */
270     public void _equals() {
271         oObj.loadModule(TransliterationModules.LOWERCASE_UPPERCASE, loc);
272 
273         int[] match1 = new int[1],
274               match2 = new int[1] ;
275 
276         boolean res = oObj.equals("aAbBcC", 1, 3, match1, "aAbBcC", 1,
277             3, match2) ;
278 
279         log.println("Returned : " + res + " Match1 = " + match1[0] +
280             " Match2 = " + match2[0]) ;
281 
282         tRes.tested("equals()", res) ;
283     }
284 
285     /**
286      * Test performed for sets of equal substrings, not equal
287      * substrings, and with out of bounds offset and length
288      * parameters.<p>
289      *
290      * Has <b>OK</b> status if comparings of equal substrings
291      * always return 0, if comparisons of none equal returns
292      * proper value according to lexicographical order and if
293      * comparisons with invalid parameters return none 0 value.
294      */
295     public void _compareSubstring() {
296         oObj.loadModule(TransliterationModules.LOWERCASE_UPPERCASE, loc);
297         boolean result = true ;
298 
299         // substrings below must be equal
300         result &= testSubstring("", 0, 0, "", 0, 0, 0) ;
301         result &= testSubstring("aa", 1, 0, "", 0, 0, 0) ;
302         result &= testSubstring("aa", 1, 0, "aa", 2, 0, 0) ;
303         result &= testSubstring("a", 0, 1, "a", 0, 1, 0) ;
304         result &= testSubstring("ab", 0, 2, "ab", 0, 2, 0) ;
305         result &= testSubstring("abc", 1, 2, "abc", 1, 2, 0) ;
306         result &= testSubstring("abcdef", 0, 3, "123abc", 3, 3, 0) ;
307         result &= testSubstring("abcdef", 1, 1, "123abc", 4, 1, 0) ;
308 
309         // substrings below must NOT be equal
310         result &= testSubstring("a", 0, 1, "a", 0, 0, 1) ;
311         result &= testSubstring("aaa", 1, 1, "", 0, 0, 1) ;
312         result &= testSubstring("bbb", 2, 1, "aaa", 2, 1, 1) ;
313         result &= testSubstring("abc", 0, 3, "abc", 0, 2, 1) ;
314         result &= testSubstring("bbc", 1, 2, "bbc", 0, 2, 1) ;
315 
316         // testing with wrong offsets and lengths
317 
318         tRes.tested("compareSubstring()", result) ;
319     }
320 
321     /**
322      * Performs tesing of two substrings. Also testing of opposite
323      * substrings order performed.
324      * @return <code>true</code> if substrings are equal and retruned
325      * value is 0 for both orders,
326      * if substrings are different and expected value
327      * returned for direct order and opposite value returned for
328      * opposite order.
329      */
330     private boolean testSubstring(String str1, int p1, int len1,
331         String str2, int p2, int len2, int expRes) {
332 
333         boolean ret = true ;
334 
335         int res = -666 ;
336         try {
337             res = oObj.compareSubstring(str1, p1, len1, str2, p2, len2);
338         } catch (java.lang.NullPointerException e) {
339             log.println("Exception while method calling occurs :" + e);
340         }
341 
342         if (res != expRes) {
343             log.print("Comparing FAILED; return: " + res + ", expected: " +
344                 expRes + " ");
345             ret = false ;
346         } else {
347             log.print("Comparing OK : ");
348         }
349         log.println("('" + str1 + "', " + p1 + ", " + len1 + ", '" +
350             str2 + "', " + p2 + ", " + len2 + ")");
351 
352         res = -666 ;
353         try {
354             res = oObj.compareSubstring(str2, p2, len2, str1, p1, len1);
355         } catch (java.lang.NullPointerException e) {
356             log.println("Exception while method calling occurs :" + e);
357         }
358 
359         if (res != -expRes) {
360             log.print("Comparing FAILED; return: " + res + ", expected: " +
361                 -expRes  + " ");
362             ret = false ;
363         } else {
364             log.print("Comparing OK :");
365         }
366         log.println("('" + str2 + "', " + p2 + ", " + len2 + ", '" +
367             str1 + "', " + p1 + ", " + len1 + ")");
368 
369         return ret ;
370     }
371 
372     /**
373      * Test performed for sets of equal strings and not equal
374      * strings.<p>
375      *
376      * Has <b>OK</b> status if comparings of equal strings
377      * always return 0 and if comparisons of none equal returns
378      * proper value according to lexicographical order .
379      */
380     public void _compareString() {
381         oObj.loadModule(TransliterationModules.LOWERCASE_UPPERCASE, loc);
382         boolean result = true ;
383 
384         result &= testString("", "", 0) ;
385         result &= testString("a", "", 1) ;
386         result &= testString("a", "a", 0) ;
387         result &= testString("A", "a", 0) ;
388         result &= testString("b", "a", 1) ;
389         result &= testString("\n", "\n", 0) ;
390         result &= testString("\n", "\t", 1) ;
391         result &= testString("aaa", "aaa", 0) ;
392         result &= testString("aaA", "aaa", 0) ;
393         result &= testString("aaa", "aa", 1) ;
394         result &= testString("ab", "aaa", 1) ;
395         result &= testString("aba", "aa", 1) ;
396         result &= testString("aaa\t\na", "aaa\t\na", 0) ;
397         result &= testString("aaa\t\nb", "aaa\t\na", 1) ;
398 
399         tRes.tested("compareString()", result) ;
400     }
401 
402     /**
403      * Performs tesing of two strings. If the expected value is not 0
404      * (i.e. strings are not equal), then also testing of opposite
405      * strings order performed.
406      * @return <code>true</code> if strings are equal and retruned
407      * value is 0, if strings are different and expected value
408      * returned for direct order and opposite value returned for
409      * opposite order.
410      */
411     protected boolean testString(String str1, String str2, int expRes) {
412         if (expRes == 0) return testString(str1, str2, expRes, false) ;
413         return testString(str1, str2, expRes, true) ;
414     }
415 
416     private boolean testString(String str1, String str2, int expRes,
417         boolean testReverse) {
418 
419         boolean ret = true ;
420 
421         int res = -666 ;
422         try {
423             res = oObj.compareString(str1, str2);
424         } catch (java.lang.NullPointerException e) {
425             log.println("Exception while method calling occurs :" + e);
426         }
427 
428         if (res == expRes) {
429             log.println("Comparing of '" + str1 + "' and '" + str2 + "' OK" );
430         } else {
431             log.println("Comparing of '" + str1 + "' and '" + str2 +
432                 "' FAILED; return: " + res + ", expected: " + expRes);
433             ret = false ;
434         }
435 
436         if (!testReverse) return ret ;
437 
438         res = -666 ;
439         try {
440             res = oObj.compareString(str2, str1);
441         } catch (java.lang.NullPointerException e) {
442             log.println("Exception while method calling occurs :" + e);
443         }
444 
445         if (res == -expRes) {
446             log.println("Comparing of '" + str2 + "' and '" + str1 + "' OK" );
447         } else {
448             log.println("Comparing of '" + str2 + "' and '" + str1 +
449                 "' FAILED; return: " + res + ", expected: " + -expRes);
450             ret = false ;
451         }
452 
453         return ret ;
454     }
455 }
456 
457