xref: /trunk/test/testuno/source/api/i18n/XLocaleDataTest.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 
24 package api.i18n;
25 
26 import com.sun.star.i18n.Calendar;
27 import com.sun.star.i18n.Currency;
28 import com.sun.star.i18n.ForbiddenCharacters;
29 import com.sun.star.i18n.FormatElement;
30 import com.sun.star.i18n.Implementation;
31 import com.sun.star.i18n.LanguageCountryInfo;
32 import com.sun.star.i18n.LocaleDataItem;
33 import com.sun.star.i18n.XLocaleData;
34 import com.sun.star.lang.Locale;
35 import com.sun.star.uno.UnoRuntime;
36 import com.sun.star.uno.XComponentContext;
37 import org.junit.AfterClass;
38 import org.junit.Before;
39 import org.junit.BeforeClass;
40 import org.junit.Assert;
41 import org.junit.Test;
42 import org.openoffice.test.uno.UnoApp;
43 
44 /**
45 * Testing <code>com.sun.star.i18n.XLocaleData</code>
46 * interface methods:
47 * <ul>
48 *  <li><code> getLanguageCountryInfo() </code></li>
49 *  <li><code> getLocaleItem() </code></li>
50 *  <li><code> getAllCalendars() </code></li>
51 *  <li><code> getAllCurrencies() </code></li>
52 *  <li><code> getAllFormats() </code></li>
53 *  <li><code> getCollatorImplementations() </code></li>
54 *  <li><code> getSearchOptions() </code></li>
55 *  <li><code> getCollationOptions() </code></li>
56 *  <li><code> getTransliterations() </code></li>
57 *  <li><code> getForbiddenCharacters() </code></li>
58 *  <li><code> getReservedWord() </code></li>
59 *  <li><code> getAllInstalledLocaleNames() </code></li>
60 * </ul><p>
61 * Test is <b> NOT </b> multithread compliant. <p>
62 * @see com.sun.star.i18n.XLocaleData
63 */
64 public class XLocaleDataTest {
65     private static final UnoApp app = new UnoApp();
66 
67     private XComponentContext xContext = null;
68     public XLocaleData oObj = null;
69     public String[] languages = new String[]{"de","en","es","fr","ja","ko","ko"};
70     public String[] countries = new String[]{"DE","US","ES","FR","JP","KR","KR"};
71 
72     // setup and close connections
73     @BeforeClass
setUpConnection()74     public static void setUpConnection() throws Exception
75     {
76         app.start();
77     }
78 
79     @AfterClass
tearDownConnection()80     public static void tearDownConnection() throws InterruptedException, com.sun.star.uno.Exception
81     {
82         app.close();
83     }
84 
85     @Before
before()86     public void before() throws Exception, java.lang.Exception
87     {
88         xContext = app.getComponentContext();
89         final Object object = xContext.getServiceManager().createInstanceWithContext("com.sun.star.i18n.LocaleData", xContext);
90         oObj = UnoRuntime.queryInterface(XLocaleData.class, object);
91     }
92 
93     /**
94     * Test calls the method, then result is checked. <p>
95     * Has <b> OK </b> status if structure, returned by the method includes
96     * correct values of fields 'Language' and 'Country' for all given locales.
97     */
98     @Test
_getLanguageCountryInfo()99     public void _getLanguageCountryInfo() {
100         boolean res = true;
101         LanguageCountryInfo lci = null;
102 
103         for (int i=0;i<7;i++) {
104             lci = oObj.getLanguageCountryInfo(getLocale(i));
105             /* For debug purposes
106             System.out.println("Using: language="+languages[i]+" ; country="+countries[i]);
107             System.out.println("Getting: ");
108             System.out.println("\t Language="+lci.Language);
109             System.out.println("\t LanguageDefaultName="+lci.LanguageDefaultName);
110             System.out.println("\t Country="+lci.Country);
111             System.out.println("\t CountryDefaultName="+lci.CountryDefaultName);
112             boolean lang = ( lci.Language.equals(languages[i]) );
113             if (!lang) System.out.println("getting false for language: "+lci.LanguageDefaultName);
114             lang = ( lci.Country.equals(countries[i]) );
115             if (!lang) System.out.println("getting false for country: "+lci.CountryDefaultName);
116             */
117             res &= ( ( lci.Language.equals(languages[i]) ) && ( lci.Country.equals(countries[i]) ) );
118         }
119         Assert.assertTrue("getLanguageCountryInfo()",res);
120     }
121 
122     /**
123     * Test calls the method, then result is checked. <p>
124     * Has <b> OK </b> status if structure, returned by the method consists of
125     * non empty strings for all given locales.
126     */
127     @Test
_getLocaleItem()128     public void _getLocaleItem() {
129         boolean res = true;
130         LocaleDataItem ldi = null;
131 
132         for (int i=0;i<7;i++) {
133             ldi = oObj.getLocaleItem(getLocale(i));
134 
135             boolean locRes = true ;
136 
137             locRes &= (! ldi.dateSeparator.equals(""));
138             locRes &= (! ldi.decimalSeparator.equals(""));
139             locRes &= (! ldi.doubleQuotationEnd.equals(""));
140             locRes &= (! ldi.doubleQuotationStart.equals(""));
141             locRes &= (! ldi.listSeparator.equals(""));
142             locRes &= (! ldi.LongDateDayOfWeekSeparator.equals(""));
143             locRes &= (! ldi.LongDateDaySeparator.equals(""));
144             locRes &= (! ldi.LongDateMonthSeparator.equals(""));
145             locRes &= (! ldi.LongDateYearSeparator.equals(""));
146             locRes &= (! ldi.measurementSystem.equals(""));
147             locRes &= (! ldi.quotationEnd.equals(""));
148             locRes &= (! ldi.quotationStart.equals(""));
149             locRes &= (! ldi.thousandSeparator.equals(""));
150             locRes &= (! ldi.time100SecSeparator.equals(""));
151             locRes &= (! ldi.timeAM.equals(""));
152             locRes &= (! ldi.timePM.equals(""));
153             locRes &= (! ldi.timeSeparator.equals(""));
154             locRes &= (! ldi.unoID.equals(""));
155 
156             if (!locRes) {
157                 /* for debugging puposes
158                 System.out.println("FAILED for: language="+languages[i]+" ; country="+countries[i]);
159                 System.out.println("Getting: ");
160                 System.out.println("\t DateSeparator="+ldi.dateSeparator);
161                 System.out.println("\t decimalSeparator="+ldi.decimalSeparator);
162                 System.out.println("\t doubleQuotationEnd="+ldi.doubleQuotationEnd);
163                 System.out.println("\t doubleQuotationStart="+ldi.doubleQuotationStart);
164                 System.out.println("\t listSeparator="+ldi.listSeparator);
165                 System.out.println("\t LongDateDayOfWeekSeparator="+ldi.LongDateDayOfWeekSeparator+"end");
166                 System.out.println("\t LongDateDaySeparator="+ldi.LongDateDaySeparator+"end");
167                 System.out.println("\t LongDateMonthSeparator="+ldi.LongDateMonthSeparator+"end");
168                 System.out.println("\t LongDateYearSeparator="+ldi.LongDateYearSeparator+"end");
169                 System.out.println("\t measurementSystem="+ldi.measurementSystem);
170                 System.out.println("\t quotationEnd="+ldi.quotationEnd);
171                 System.out.println("\t quotationStart="+ldi.quotationStart);
172                 System.out.println("\t thousandSeparator="+ldi.thousandSeparator);
173                 System.out.println("\t time100SecSeparator="+ldi.time100SecSeparator);
174                 System.out.println("\t timeAM="+ldi.timeAM);
175                 System.out.println("\t timePM="+ldi.timePM);
176                 System.out.println("\t timeSeparator="+ldi.timeSeparator);
177                 System.out.println("\t unoID="+ldi.unoID);
178                  */
179             }
180         }
181         Assert.assertTrue("getLocaleItem()",res);
182     }
183 
184     /**
185     * Test calls the method for several locales; result is checked
186     * after each call. <p>
187     * Has <b> OK </b> status if all elements of the returned sequence are
188     * correct for all given locales. (boolean method goodCalendar() with a
189     * calendar as an argument returns true)
190     */
191     @Test
_getAllCalendars()192     public void _getAllCalendars() {
193         boolean res = true;
194         boolean printit = false;
195         Calendar[] calendar = new Calendar[1];
196 
197         for (int i=0;i<7;i++) {
198             calendar = oObj.getAllCalendars(getLocale(i));
199             for (int j=0;j<calendar.length;j++) {
200                 if (! goodCalendar(calendar[j]) ) {
201                     printit = true;
202                 }
203                 res &= goodCalendar(calendar[j]);
204             }
205             if (printit) System.out.println("FAILED for: language="+languages[i]+" ; country="+countries[i]);
206             printit = false;
207         }
208         Assert.assertTrue("getAllCalendars()", res);
209     }
210 
211     /**
212     * Test calls the method for several locales; result is checked
213     * after each call. <p>
214     * Has <b> OK </b> status if all elements of the returned sequence are
215     * correct for all given locales. (boolean method goodCurrency() with a
216     * currency as an argument returns true)
217     */
218     @Test
_getAllCurrencies()219     public void _getAllCurrencies() {
220         boolean res = true;
221         boolean printit = false;
222         Currency[] currency = new Currency[1];
223 
224         for (int i=0;i<7;i++) {
225             currency = oObj.getAllCurrencies(getLocale(i));
226             for (int j=0;j<currency.length;j++) {
227                 if (! goodCurrency(currency[j]) ) {
228                     printit=true;
229                 }
230                 res &= goodCurrency(currency[j]);
231             }
232             if (printit) System.out.println("FAILED for: language="+languages[i]+" ; country="+countries[i]);
233             printit =false;
234         }
235         Assert.assertTrue("getAllCurrencies()",res);
236     }
237 
238     /**
239     * Test calls the method for several locales; result is checked
240     * after each call. <p>
241     * Has <b> OK </b> status if all elements of the returned sequence are
242     * correct for all given locales. (boolean method goodFormat() with a
243     * format as an argument returns true)
244     */
245     @Test
_getAllFormats()246     public void _getAllFormats() {
247         boolean res = true;
248         boolean printit = false;
249         FormatElement[] format = new FormatElement[1];
250 
251         for (int i=0;i<7;i++) {
252             format = oObj.getAllFormats(getLocale(i));
253             for (int j=0;j<format.length;j++) {
254                 if (! goodFormat(format[j]) ) {
255                     printit = true;
256                 }
257                 res &= goodFormat(format[j]);
258             }
259             if (printit) System.out.println("FAILED for: language="+languages[i]+" ; country="+countries[i]);
260             printit =false;
261         }
262         Assert.assertTrue("getAllFormats()",res);
263     }
264 
265     /**
266     * Test calls the method for several locales; result is checked
267     * after each call. <p>
268     * Has <b> OK </b> status if all structs, returned by the method have non
269     * empty field 'UnoID' for all given locales.
270     */
271     @Test
_getCollatorImplementations()272     public void _getCollatorImplementations() {
273         boolean res = true;
274         boolean printit = false;
275         Implementation[] impl = new Implementation[1];
276 
277         for (int i=0;i<7;i++) {
278             impl = oObj.getCollatorImplementations(getLocale(i));
279             for (int j=0;j<impl.length;j++) {
280                 if ((impl[j].unoID.equals(""))) {
281                     printit = true;
282                 }
283                 res &= (!impl[j].unoID.equals(""));
284             }
285             if (printit) System.out.println("FAILED for: language=" + languages[i]
286                 + " ; country=" + countries[i]);
287             printit = false;
288         }
289         Assert.assertTrue("getCollatorImplementations()", res);
290     }
291 
292     /**
293     * Test calls the method for several locales; result is checked
294     * after each call. <p>
295     * Has <b> OK </b> status if all strings, returned by the method are not
296     * empty for all given locales.
297     */
298     @Test
_getSearchOptions()299     public void _getSearchOptions() {
300         boolean res = true;
301         boolean printit = false;
302         String[] str = new String[1];
303 
304         for (int i=0;i<7;i++) {
305             str = oObj.getSearchOptions(getLocale(i));
306             for (int j=0;j<str.length;j++) {
307                 if ((str[j].equals(""))) {
308                     printit = true;
309                 }
310                 res &= (!str.equals(""));
311             }
312             if (printit) System.out.println("FAILED for: language=" + languages[i]
313                 + " ; country=" + countries[i]);
314             printit = false;
315         }
316         Assert.assertTrue("getSearchOptions()",res);
317     }
318 
319     /**
320     * Test calls the method for several locales; result is checked
321     * after each call. <p>
322     * Has <b> OK </b> status if all strings, returned by the method are not
323     * empty for all given locales.
324     */
325     @Test
_getCollationOptions()326     public void _getCollationOptions() {
327         boolean res = true;
328         boolean printit = false;
329         String[] str = new String[1];
330 
331         for (int i=0;i<7;i++) {
332             str = oObj.getCollationOptions(getLocale(i));
333             for (int j=0;j<str.length;j++) {
334                 if ((str[j].equals(""))) {
335                     printit = true;
336                 }
337                 res &= (!str.equals(""));
338             }
339             if (printit) System.out.println("FAILED for: language=" + languages[i]
340                 + " ; country=" + countries[i]);
341             printit = false;
342         }
343         Assert.assertTrue("getCollationOptions()", res);
344     }
345 
346     /**
347     * Test calls the method for several locales; result is checked
348     * after each call. <p>
349     * Has <b> OK </b> status if all strings, returned by the method are not
350     * empty for all given locales.
351     */
352     @Test
_getTransliterations()353     public void _getTransliterations() {
354         boolean res = true;
355         boolean printit = false;
356         String[] str = new String[1];
357 
358         for (int i=0;i<7;i++) {
359             str = oObj.getTransliterations(getLocale(i));
360             for (int j=0;j<str.length;j++) {
361                 if ((str[j].equals(""))) {
362                     printit = true;
363                 }
364                 res &= (!str.equals(""));
365             }
366             if (printit) System.out.println("FAILED for: language=" + languages[i]
367                 + " ; country=" + countries[i]);
368             printit = false;
369         }
370         Assert.assertTrue("getTransliterations()", res);
371     }
372 
373     /**
374     * Test calls the method for several locales; result is checked
375     * after each call. <p>
376     * Has <b> OK </b> status if the method returns structure with non-empty
377     * fields for all given locales.
378     */
379     @Test
_getForbiddenCharacters()380     public void _getForbiddenCharacters() {
381         boolean res = true;
382         ForbiddenCharacters fc = null;
383 
384         //the forbidden characters are only available for the asian languages
385         for (int i=4;i<7;i++) {
386             fc = oObj.getForbiddenCharacters(getLocale(i));
387             res &= !( fc.beginLine.equals("") || fc.endLine.equals("") );
388             if ( !res ) {
389                 System.out.println("FAILED for: language="+languages[i]+" ; country="+countries[i]);
390             }
391         }
392         Assert.assertTrue("getForbiddenCharacters()", res);
393     }
394 
395 
396     /**
397     * Test calls the method for several locales; result is checked
398     * after each call. <p>
399     * Has <b> OK </b> status if all strings, returned by the method are not
400     * empty for all given locales.
401     */
402     @Test
_getReservedWord()403     public void _getReservedWord() {
404         boolean res = true;
405         boolean printit = false;
406         String[] str = new String[1];
407 
408         for (int i=0;i<7;i++) {
409             str = oObj.getReservedWord(getLocale(i));
410             for (int j=0;j<str.length;j++) {
411                 if ((str[j].equals(""))) {
412                     printit = true;
413                 }
414                 res &= (!str.equals(""));
415             }
416             if (printit) System.out.println("FAILED for: language=" + languages[i]
417                 + " ; country=" + countries[i]);
418             printit = false;
419         }
420         Assert.assertTrue("getReservedWord()", res);
421     }
422 
423 
424     /**
425     * Test calls the method. Then result is checked for all given locales.<p>
426     * Has <b> OK </b> status if locale sequence, returned by the method contains
427     * given locales.
428     */
429     @Test
_getAllInstalledLocaleNames()430     public void _getAllInstalledLocaleNames() {
431         boolean res = true;
432         Locale[] locs = oObj.getAllInstalledLocaleNames();
433 
434         //check if the languages used here are part of this array
435         for (int i=0;i<7;i++) {
436             res &= contains(locs, getLocale(i));
437         }
438         Assert.assertTrue("getAllInstalledLocaleNames()",res);
439     }
440 
441 
442     /**
443     * Method returns locale for a given language and country.
444     * @param k index of needed locale.
445     * @return Locale by the index from arrays defined above
446     */
getLocale(int k)447     public Locale getLocale(int k) {
448         return new Locale(languages[k],countries[k],"");
449     }
450 
451     /**
452     * Method checks given calendar for non empty fields.
453     * @param calendar Calendar to be checked
454     */
goodCalendar(Calendar calendar)455     public boolean goodCalendar(Calendar calendar) {
456         boolean good = true;
457         for (int i=0;i<calendar.Days.length;i++) {
458             //System.out.println("Day "+i+"(AbbrevName): "+calendar.Days[i].AbbrevName);
459             good &= (! calendar.Days[i].AbbrevName.equals("") );
460             //System.out.println("Day "+i+"(FullName): "+calendar.Days[i].FullName);
461             good &= (! calendar.Days[i].FullName.equals("") );
462             //System.out.println("Day "+i+"(ID): "+calendar.Days[i].ID);
463             good &= (! calendar.Days[i].ID.equals("") );
464         }
465         for (int i=0;i<calendar.Months.length;i++) {
466             //System.out.println("Day "+i+"(AbbrevName): "+calendar.Months[i].AbbrevName);
467             good &= (! calendar.Months[i].AbbrevName.equals("") );
468             //System.out.println("Day "+i+"(FullName): "+calendar.Months[i].FullName);
469             good &= (! calendar.Months[i].FullName.equals("") );
470             //System.out.println("Day "+i+"(ID): "+calendar.Months[i].ID);
471             good &= (! calendar.Months[i].ID.equals("") );
472         }
473         for (int i=0;i<calendar.Eras.length;i++) {
474             //System.out.println("Era "+i+"(AbbrevName): "+calendar.Eras[i].AbbrevName);
475             good &= (! calendar.Eras[i].AbbrevName.equals("") );
476             //System.out.println("Era "+i+"(FullName): "+calendar.Eras[i].FullName);
477             good &= (! calendar.Eras[i].FullName.equals("") );
478             //System.out.println("Era "+i+"(ID): "+calendar.Eras[i].ID);
479             good &= (! calendar.Eras[i].ID.equals("") );
480         }
481         //System.out.println("Start of Week: "+calendar.StartOfWeek);
482         good &= (! calendar.StartOfWeek.equals("") );
483         //System.out.println("MinimumNumberOfDaysForFirstWeek: "+calendar.MinimumNumberOfDaysForFirstWeek);
484         //System.out.println("Default: "+calendar.Default);
485         //System.out.println("Name: "+calendar.Name);
486         good &= (! calendar.Name.equals("") );
487         return good;
488     }
489 
490     /**
491     * Method checks given currency for non empty fields.
492     * @param currency Currency to be checked
493     */
goodCurrency(Currency currency)494     public boolean goodCurrency(Currency currency) {
495         boolean good = true;
496         good &= (!currency.BankSymbol.equals(""));
497         good &= (!currency.Name.equals(""));
498         good &= (!currency.Symbol.equals(""));
499         return good;
500     }
501 
502     /**
503     * Method checks given format for non empty fields.
504     * @param format Format to be checked
505     */
goodFormat(FormatElement format)506     public boolean goodFormat(FormatElement format) {
507         boolean good = true;
508         good &= (!format.formatCode.equals(""));
509         good &= (!format.formatKey.equals(""));
510         good &= (!format.formatType.equals(""));
511         good &= (!format.formatUsage.equals(""));
512         return good;
513     }
514 
515     /**
516     * Method checks that locale sequence contains given locale.
517     * @param locs Locale sequence
518     * @param oneLoc given locale
519     */
contains(Locale[] locs, Locale oneLoc)520     public boolean contains(Locale[] locs, Locale oneLoc) {
521         boolean cont = false;
522         for (int j=0;j<locs.length;j++) {
523             if ( (locs[j].Country.equals(oneLoc.Country)) &&
524                 (locs[j].Language.equals(oneLoc.Language)) ) {
525                     cont=true;
526                     break;
527             }
528         }
529         if (!cont) System.out.println("Not contained: " + oneLoc.Language);
530         return cont;
531     }
532 
533 } // end XLocaleData
534