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