xref: /AOO42X/main/qadevOOo/tests/java/ifc/i18n/_XCalendar.java (revision b0efeae40e43e6d4ccd561d22ec612d42773857b)
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.CalendarDisplayIndex;
29 import com.sun.star.i18n.CalendarFieldIndex;
30 import com.sun.star.i18n.CalendarItem;
31 import com.sun.star.i18n.XCalendar;
32 import com.sun.star.i18n.XLocaleData;
33 import com.sun.star.lang.Locale;
34 import com.sun.star.lang.XMultiServiceFactory;
35 import com.sun.star.uno.UnoRuntime;
36 
37 /**
38 * Testing <code>com.sun.star.i18n.XCalendar</code>
39 * interface methods :
40 * <ul>
41 *  <li><code> loadDefaultCalendar()</code></li>
42 *  <li><code> loadCalendar()</code></li>
43 *  <li><code> getLoadedCalendar()</code></li>
44 *  <li><code> getAllCalendars()</code></li>
45 *  <li><code> getUniqueID()</code></li>
46 *  <li><code> setDateTime()</code></li>
47 *  <li><code> getDateTime()</code></li>
48 *  <li><code> setValue()</code></li>
49 *  <li><code> getValue()</code></li>
50 *  <li><code> isValid()</code></li>
51 *  <li><code> addValue()</code></li>
52 *  <li><code> getFirstDayOfWeek()</code></li>
53 *  <li><code> setFirstDayOfWeek()</code></li>
54 *  <li><code> setMinimumNumberOfDaysForFirstWeek()</code></li>
55 *  <li><code> getMinimumNumberOfDaysForFirstWeek()</code></li>
56 *  <li><code> getNumberOfMonthsInYear()</code></li>
57 *  <li><code> getNumberOfDaysInWeek()</code></li>
58 *  <li><code> getMonths()</code></li>
59 *  <li><code> getDays()</code></li>
60 *  <li><code> getDisplayName()</code></li>
61 * </ul> <p>
62 * Test is <b> NOT </b> multithread compliant. <p>
63 * @see com.sun.star.i18n.XCalendar
64 */
65 public class _XCalendar extends MultiMethodTest {
66     private boolean debug = false;
67     public XCalendar oObj = null;
68     public String[][] calendars;
69     public int[] count;
70     public double newDTime = 1000.75;
71     public short newValue = 2;
72     public short firstDay = 2;
73     public short mdfw = 3;
74     double aOriginalDTime = 0;
75     Locale[] installed_locales;
76 
77     public void before() {
78         XLocaleData locData = null;
79         try {
80             locData = (XLocaleData) UnoRuntime.queryInterface(
81                 XLocaleData.class,
82                     ((XMultiServiceFactory)tParam.getMSF()).createInstance(
83                     "com.sun.star.i18n.LocaleData"));
84         } catch (com.sun.star.uno.Exception e) {
85 
86         }
87         installed_locales = locData.getAllInstalledLocaleNames();
88         calendars = new String[installed_locales.length][];
89         count = new int[installed_locales.length];
90         oObj.loadDefaultCalendar(installed_locales[0]);
91         aOriginalDTime = oObj.getDateTime();
92 <<<<<<< HEAD:main/qadevOOo/tests/java/ifc/i18n/_XCalendar.java
93 
94         debug = tParam.getBool("DebugIsActive");
95 =======
96 
97         debug = false;
98 >>>>>>> 3309286857 (pre-commit auto remove trailing whitespace from java files (#382)):test/testuno/source/api/i18n/XCalendarTest.java
99     }
100 
101     /**
102      * Restore the changed time during the test to the original value of the
103      * machine: has to be correct for the following interface tests.
104      */
105     public void after() {
106         oObj.loadDefaultCalendar(installed_locales[0]);
107         oObj.setDateTime(aOriginalDTime);
108     }
109 
110     /**
111     * Loads default calendar for different locales. <p>
112     * Has <b> OK </b> status if method loads calendar, that is
113     * default for a given locale.
114     */
115     public void _loadDefaultCalendar() {
116         boolean res = true;
117 
118         for (int i=0; i<installed_locales.length; i++) {
119             String lang = "Language: "+installed_locales[i].Language +
120                           ", Country: "+ installed_locales[i].Country +
121                           ", Variant: "+ installed_locales[i].Country;
122             oObj.loadDefaultCalendar(installed_locales[i]);
123             if (oObj.getLoadedCalendar().Default) {
124                 //log.println(lang + " ... OK");
125             } else {
126                 log.println(lang + " ... FAILED");
127             }
128             res &= oObj.getLoadedCalendar().Default;
129         }
130 
131         tRes.tested("loadDefaultCalendar()", res);
132     }
133 
134     /**
135     * Tries to obtain calendars for a number of locales. <p>
136     * Has <b> OK </b> status if the method returns more than zero calendars for
137     * every locale.
138     */
139     public void _getAllCalendars() {
140         boolean res = true;
141 
142         for (int i=0; i<installed_locales.length; i++) {
143             String lang = "Language: "+installed_locales[i].Language +
144                           ", Country: "+ installed_locales[i].Country +
145                           ", Variant: "+ installed_locales[i].Country;
146             calendars[i] = oObj.getAllCalendars(installed_locales[i]);
147             count[i] = calendars[i].length-1;
148             if (calendars[i].length > 0) {
149                 //log.println(lang + " ... OK");
150             } else {
151                 log.println(lang + " ... FAILED");
152             }
153             res &= (calendars[i].length > 0);
154         }
155         tRes.tested("getAllCalendars()", res);
156     }
157 
158     /**
159     * Loads calendars for a number of locales. <p>
160     * Has <b> OK </b> status if loaded calendar names are equal to gotten
161     * calendar names after loading.<p>
162     * The following method tests are to be completed successfully before :
163     * <ul>
164     *  <li> <code> getAllCalendars() </code> : gets all calendars for a given
165     *  locale </li>
166     * </ul>
167     */
168     public void _loadCalendar() {
169         boolean res = true;
170         requiredMethod("getAllCalendars()");
171 
172         for (int i=0; i<installed_locales.length; i++) {
173             String lang = "Language: "+installed_locales[i].Language +
174                           ", Country: "+ installed_locales[i].Country +
175                           ", Variant: "+ installed_locales[i].Country;
176             oObj.loadCalendar(calendars[i][0], installed_locales[i]);
177             if (calendars[i][0].equals(oObj.getLoadedCalendar().Name)) {
178                 //log.println(lang + " ... OK");
179             } else {
180                 log.println(lang + " ... FAILED");
181             }
182             res &= calendars[i][0].equals(oObj.getLoadedCalendar().Name);
183         }
184 
185         tRes.tested("loadCalendar()", res);
186     }
187 
188     /**
189     * Test calls the method, then result is checked. <p>
190     * Has <b> OK </b> status if loaded calendar names are equal to gotten
191     * calendar names after loading.<p>
192     * The following method tests are to be completed successfully before :
193     * <ul>
194     *  <li> <code> loadCalendar() </code> : loads calendar using a given name
195     *  and locale </li>
196     * </ul>
197     */
198     public void _getLoadedCalendar() {
199         boolean res = true;
200 
201         requiredMethod("loadCalendar()");
202         for (int i=0; i<installed_locales.length; i++) {
203             String lang = "Language: "+installed_locales[i].Language +
204                           ", Country: "+ installed_locales[i].Country +
205                           ", Variant: "+ installed_locales[i].Country;
206             oObj.loadCalendar(calendars[i][0], installed_locales[i]);
207             if (calendars[i][0].equals(oObj.getLoadedCalendar().Name)) {
208                 //log.println(lang + " ... OK");
209             } else {
210                 log.println(lang + " ... FAILED");
211             }
212             res &= calendars[i][0].equals(oObj.getLoadedCalendar().Name);
213         }
214         tRes.tested("getLoadedCalendar()", res);
215     }
216 
217     /**
218     * Test calls the method, then result is checked. <p>
219     * Has <b> OK </b> status if the method returns value that's equal to a
220     * calendar name. <p>
221     * The following method tests are to be completed successfully before :
222     * <ul>
223     *  <li> <code> loadCalendar() </code> :  loads calendar using a given name
224     *  and locale </li>
225     * </ul>
226     */
227     public void _getUniqueID() {
228         boolean res = true;
229         for (int i=0; i<installed_locales.length; i++) {
230             String lang = "Language: "+installed_locales[i].Language +
231                           ", Country: "+ installed_locales[i].Country +
232                           ", Variant: "+ installed_locales[i].Country;
233             oObj.loadCalendar(calendars[i][0], installed_locales[i]);
234             String uID = oObj.getUniqueID();
235             if (uID.equals(calendars[i][0])) {
236                 //log.println(lang + " ... OK");
237             } else {
238                 log.println(lang + " ... FAILED");
239             }
240             res &= uID.equals(calendars[i][0]);
241         }
242 
243         tRes.tested("getUniqueID()",res);
244     }
245 
246     /**
247     * Test calls the method, then result is checked. <p>
248     * Has <b> OK </b> status if the method returns value, that's equal to
249     * value set before. <p>
250     */
251 
252     public void _setDateTime() {
253         boolean res = true;
254 
255         for (int i=0; i<installed_locales.length; i++) {
256             String lang = "Language: "+installed_locales[i].Language +
257                           ", Country: "+ installed_locales[i].Country +
258                           ", Variant: "+ installed_locales[i].Country;
259             oObj.setDateTime(newDTime);
260             double aDTime = oObj.getDateTime();
261             if (aDTime == newDTime) {
262                 //log.println(lang + " ... OK");
263             } else {
264                 log.println(lang + " ... FAILED");
265             }
266             res &= (aDTime == newDTime);
267         }
268 
269         tRes.tested("setDateTime()", res);
270     }
271 
272     /**
273     * Test calls the method, then result is checked. <p>
274     * Has <b> OK </b> status if the method returns value, that's equal to
275     * value set before. <p>
276     */
277 
278     public void _getDateTime() {
279         boolean res = true;
280 
281         for (int i=0; i<installed_locales.length; i++) {
282             String lang = "Language: "+installed_locales[i].Language +
283                           ", Country: "+ installed_locales[i].Country +
284                           ", Variant: "+ installed_locales[i].Country;
285             oObj.setDateTime(newDTime);
286             double aDTime = oObj.getDateTime();
287             if (aDTime == newDTime) {
288                 //log.println(lang + " ... OK");
289             } else {
290                 log.println(lang + " ... FAILED");
291             }
292             res &= (aDTime == newDTime);
293         }
294         tRes.tested("getDateTime()", res);
295     }
296 
297     /**
298     * Test calls the method, then result is checked. <p>
299     * Has <b> OK </b> status if the method returns value, that's equal to
300     * value set before. <p>
301     */
302 
303     public void _setValue() {
304         boolean res = true;
305         for (int i=0; i<installed_locales.length; i++) {
306             String error = "";
307             String lang = "Language: "+installed_locales[i].Language +
308                           ", Country: "+ installed_locales[i].Country +
309                           ", Variant: "+ installed_locales[i].Variant +
310                           ", Name: "+calendars[i][count[i]];
311             String[] names = new String[]{"DAY_OF_MONTH",
312                 "HOUR","MINUTE","SECOND","MILLISECOND",
313                 "YEAR","MONTH"};
314             oObj.loadCalendar(calendars[i][count[i]],installed_locales[i]);
315             short[] fields = new short[]{CalendarFieldIndex.DAY_OF_MONTH,
316                                          CalendarFieldIndex.HOUR,
317                                          CalendarFieldIndex.MINUTE,
318                                          CalendarFieldIndex.SECOND,
319                                          CalendarFieldIndex.MILLISECOND,
320                                          CalendarFieldIndex.YEAR,
321                                          CalendarFieldIndex.MONTH
322             };
323             for (int k=0; k<fields.length;k++) {
324 
325                 oObj.setDateTime(0.0);
326 
327                 // save the current values for debug purposes
328                 short[] oldValues = new short[fields.length];
329                 for (int n=0; n < oldValues.length; n++){
330                     oldValues[n] = oObj.getValue(fields[n]);
331                 }
332 
333                 short set = oObj.getValue(fields[k]);
334                 if (fields[k] == CalendarFieldIndex.MONTH) set = newValue;
335                 oObj.setValue(fields[k],set);
336                 short get = oObj.getValue(fields[k]);
337                 if (get != set) {
338                     if (debug)
339                         log.println("ERROR occur: tried to set " + names[k] + " to value " + set);
340                         log.println("list of values BEFORE set " + names[k] + " to value " + set + ":");
341                         for (int n=0; n < oldValues.length; n++){
342                             log.println(names[n] + ":" + oldValues[n]);
343                         }
344                         log.println("list of values AFTER set " + names[k] + " to value " + set + ":");
345                         for (int n=0; n < fields.length;n++){
346                             log.println(names[n] + ":" + oObj.getValue(fields[n]));
347                         }
348 
349                     error += "failed for "+names[k]+" expected "+
350                                 set+" gained "+get+" ; \n";
351                 }
352             }
353             if (error.equals("")) {
354                 log.println(lang + " ... OK");
355             } else {
356                 log.println("*** "+lang + " ... FAILED ***");
357                 log.println(error);
358             }
359             res &= (error.equals(""));
360         }
361 
362         tRes.tested("setValue()", res);
363     }
364 
365     /**
366     * Test calls the method, then result is checked. <p>
367     * Has <b> OK </b> status if the method returns value, that's equal to
368     * value set before. <p>
369     */
370 
371     public void _getValue() {
372         boolean res = true;
373 
374         requiredMethod("setValue()");
375         short aValue = oObj.getValue(CalendarFieldIndex.MONTH);
376         res &= (aValue == newValue);
377         if (!res){
378             log.println("the returned value is not the expected value:");
379             log.println("expexted: " + newValue + "  returned value: " + aValue);
380         }
381         tRes.tested("getValue()", res);
382     }
383 
384     /**
385     * Test calls the method, then result is checked. <p>
386     * Has <b> OK </b> status if value, added by the method is greater than
387     * previously defined "newValue".
388     * <p>
389     * The following method tests are to be completed successfully before :
390     * <ul>
391     *  <li> <code> getValue() </code> : gets the value of a field </li>
392     * </ul>
393     */
394     public void _addValue() {
395         boolean res = true;
396 
397         requiredMethod("getValue()");
398         oObj.addValue(CalendarFieldIndex.MONTH, 1);
399         short aValue = oObj.getValue(CalendarFieldIndex.MONTH);
400         res &= (aValue > newValue);
401         if (!res){
402             log.println("the returned value is not the expected value:");
403             log.println("expexted: " + newValue + "  returned value: " + aValue);
404         }
405         tRes.tested("addValue()", res);
406     }
407 
408     /**
409     * Test calls the method. <p>
410     * Has <b> OK </b> status if the method successfully returns
411     * and no exceptions were thrown.
412     */
413     public void _setFirstDayOfWeek() {
414         boolean res = true;
415 
416         oObj.setFirstDayOfWeek(firstDay);
417         res &= true;
418         tRes.tested("setFirstDayOfWeek()", res);
419     }
420 
421     /**
422     * Test calls the method, then result is checked. <p>
423     * Has <b> OK </b> status if the method returns value that is equal to
424     * value set before. <p>
425     * The following method tests are to be completed successfully before :
426     * <ul>
427     *  <li> <code> setFirstDayOfWeek() </code> : set the first day of a
428     *  week</li>
429     * </ul>
430     */
431     public void _getFirstDayOfWeek() {
432         boolean res = true;
433 
434         requiredMethod("setFirstDayOfWeek()");
435         short aFirstDayOfWeek = oObj.getFirstDayOfWeek();
436         res &= (aFirstDayOfWeek == firstDay);
437         tRes.tested("getFirstDayOfWeek()", res);
438     }
439 
440     /**
441     * Test calls the method. <p>
442     * Has <b> OK </b> status if the method successfully returns
443     * and no exceptions were thrown.
444     */
445     public void _setMinimumNumberOfDaysForFirstWeek() {
446         boolean res = true;
447 
448         oObj.setMinimumNumberOfDaysForFirstWeek(mdfw);
449         res &= true;
450         tRes.tested("setMinimumNumberOfDaysForFirstWeek()", res);
451     }
452 
453     /**
454     * Test calls the method, then result is checked. <p>
455     * Has <b> OK </b> status if the method returns value that is equal to
456     * value set before. <p>
457     * The following method tests are to be completed successfully before :
458     * <ul>
459     *  <li> <code> setMinimumNumberOfDaysForFirstWeek() </code> : sets how
460     *  many days of a week must reside in the first week of a year</li>
461     * </ul>
462     */
463     public void _getMinimumNumberOfDaysForFirstWeek() {
464         boolean res = true;
465 
466         requiredMethod("setMinimumNumberOfDaysForFirstWeek()");
467         short aShort = oObj.getMinimumNumberOfDaysForFirstWeek();
468         res &= (aShort == mdfw);
469         tRes.tested("getMinimumNumberOfDaysForFirstWeek()", res);
470     }
471 
472     /**
473     * Test calls the method, then result is checked. <p>
474     * Has <b> OK </b> status if the method returns 12.
475     */
476     public void _getNumberOfMonthsInYear() {
477         boolean res = true;
478         short aShort = oObj.getNumberOfMonthsInYear();
479 
480         res &= (aShort == (short) 12);
481         tRes.tested("getNumberOfMonthsInYear()", res);
482     }
483 
484     /**
485     * Test calls the method, then result is checked. <p>
486     * Has <b> OK </b> status if the method returns 7.
487     */
488     public void _getNumberOfDaysInWeek() {
489         boolean res = true;
490         short aShort = oObj.getNumberOfDaysInWeek();
491 
492         res &= (aShort == (short) 7);
493         tRes.tested("getNumberOfDaysInWeek()", res);
494     }
495 
496     /**
497     * Test calls the method, then result is checked. <p>
498     * Has <b> OK </b> status if length of array, returned by the method is 12.
499     */
500     public void _getMonths() {
501         boolean res = true;
502         CalendarItem[] months = oObj.getMonths();
503 
504         res &= (months.length == 12);
505         tRes.tested("getMonths()", res);
506     }
507 
508     /**
509     * Test calls the method, then result is checked. <p>
510     * Has <b> OK </b> status if length of array, returned by the method is 7.
511     */
512     public void _getDays() {
513         boolean res = true;
514         CalendarItem[] Days = oObj.getDays();
515 
516         res &= (Days.length == 7);
517         tRes.tested("getDays()", res);
518     }
519 
520     /**
521     * After loading calendar, test calls the method, then result is checked.<p>
522     * Has <b> OK </b> status if length of string, returned by the method is 3.
523     */
524     public void _getDisplayName() {
525         boolean res = true;
526 
527         oObj.loadCalendar(calendars[0][0],installed_locales[0]);
528         String DisplayName = oObj.getDisplayName(CalendarDisplayIndex.MONTH,
529             newValue, (short) 0);
530         res &= (DisplayName.length() == 3);
531         tRes.tested("getDisplayName()", res);
532     }
533 
534 
535     /**
536     * The test sets obviously wrong value, then calls a method. After that the
537     * test sets correct value, and again calls a method. <p>
538     * Has <b> OK </b> status if the method returns true when valid month is
539     * set, and if the method returns false when set month is not valid.
540     */
541     public void _isValid() {
542         boolean res = true;
543 
544         oObj.loadDefaultCalendar(installed_locales[0]);
545         oObj.setValue(CalendarFieldIndex.MONTH, (short) 37);
546         res &= !oObj.isValid();
547         oObj.setValue(CalendarFieldIndex.MONTH, (short) 10);
548         res &= oObj.isValid();
549 
550         tRes.tested("isValid()", res);
551     }
552 
553     /**
554     * Method returns locale for a given language and country.
555     * @param localeIndex index of needed locale.
556     */
557 /*    public Locale getLocale(int localeIndex) {
558         return new Locale(languages[localeIndex], countries[localeIndex], "");
559     }*/
560 
561 }
562