Avoid potential negative array index access to cached text.
[LibreOffice.git] / qadevOOo / tests / java / ifc / i18n / _XCalendar.java
blobd24f201229caae8292d63a55c939134710e78572
1 /*
2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 package ifc.i18n;
21 import lib.MultiMethodTest;
23 import com.sun.star.i18n.CalendarDisplayIndex;
24 import com.sun.star.i18n.CalendarFieldIndex;
25 import com.sun.star.i18n.CalendarItem;
26 import com.sun.star.i18n.XCalendar;
27 import com.sun.star.i18n.XLocaleData;
28 import com.sun.star.lang.Locale;
29 import com.sun.star.uno.UnoRuntime;
31 /**
32 * Testing <code>com.sun.star.i18n.XCalendar</code>
33 * interface methods :
34 * <ul>
35 * <li><code> loadDefaultCalendar()</code></li>
36 * <li><code> loadCalendar()</code></li>
37 * <li><code> getLoadedCalendar()</code></li>
38 * <li><code> getAllCalendars()</code></li>
39 * <li><code> getUniqueID()</code></li>
40 * <li><code> setDateTime()</code></li>
41 * <li><code> getDateTime()</code></li>
42 * <li><code> setValue()</code></li>
43 * <li><code> getValue()</code></li>
44 * <li><code> isValid()</code></li>
45 * <li><code> addValue()</code></li>
46 * <li><code> getFirstDayOfWeek()</code></li>
47 * <li><code> setFirstDayOfWeek()</code></li>
48 * <li><code> setMinimumNumberOfDaysForFirstWeek()</code></li>
49 * <li><code> getMinimumNumberOfDaysForFirstWeek()</code></li>
50 * <li><code> getNumberOfMonthsInYear()</code></li>
51 * <li><code> getNumberOfDaysInWeek()</code></li>
52 * <li><code> getMonths()</code></li>
53 * <li><code> getDays()</code></li>
54 * <li><code> getDisplayName()</code></li>
55 * </ul> <p>
56 * Test is <b> NOT </b> multithread compliant. <p>
57 * @see com.sun.star.i18n.XCalendar
59 public class _XCalendar extends MultiMethodTest {
60 private boolean debug = false;
61 public XCalendar oObj = null;
62 public String[][] calendars;
63 public int[] count;
64 public double newDTime = 1000.75;
65 public short newValue = 2;
66 public short firstDay = 2;
67 public short mdfw = 3;
68 double aOriginalDTime = 0;
69 Locale[] installed_locales;
71 @Override
72 public void before() {
73 XLocaleData locData = null;
74 try {
75 locData = UnoRuntime.queryInterface(
76 XLocaleData.class,
77 tParam.getMSF().createInstance(
78 "com.sun.star.i18n.LocaleData"));
80 installed_locales = locData.getAllInstalledLocaleNames();
81 calendars = new String[installed_locales.length][];
82 count = new int[installed_locales.length];
83 } catch (com.sun.star.uno.Exception e) {
87 oObj.loadDefaultCalendar(installed_locales[0]);
88 aOriginalDTime = oObj.getDateTime();
89 debug = tParam.getBool("DebugIsActive");
92 /**
93 * Restore the changed time during the test to the original value of the
94 * machine: has to be correct for the following interface tests.
96 @Override
97 public void after() {
98 oObj.loadDefaultCalendar(installed_locales[0]);
99 oObj.setDateTime(aOriginalDTime);
103 * Loads default calendar for different locales. <p>
104 * Has <b> OK </b> status if method loads calendar, that is
105 * default for a given locale.
107 public void _loadDefaultCalendar() {
108 boolean res = true;
110 for (int i=0; i<installed_locales.length; i++) {
111 String lang = "Language: "+installed_locales[i].Language +
112 ", Country: "+ installed_locales[i].Country +
113 ", Variant: "+ installed_locales[i].Country;
114 oObj.loadDefaultCalendar(installed_locales[i]);
115 if (!oObj.getLoadedCalendar().Default) {
116 log.println(lang + " ... FAILED");
118 res &= oObj.getLoadedCalendar().Default;
121 tRes.tested("loadDefaultCalendar()", res);
125 * Tries to obtain calendars for a number of locales. <p>
126 * Has <b> OK </b> status if the method returns more than zero calendars for
127 * every locale.
129 public void _getAllCalendars() {
130 boolean res = true;
132 for (int i=0; i<installed_locales.length; i++) {
133 String lang = "Language: "+installed_locales[i].Language +
134 ", Country: "+ installed_locales[i].Country +
135 ", Variant: "+ installed_locales[i].Country;
136 calendars[i] = oObj.getAllCalendars(installed_locales[i]);
137 count[i] = calendars[i].length-1;
138 if (calendars[i].length <= 0) {
139 log.println(lang + " ... FAILED");
141 res &= (calendars[i].length > 0);
143 tRes.tested("getAllCalendars()", res);
147 * Loads calendars for a number of locales. <p>
148 * Has <b> OK </b> status if loaded calendar names are equal to gotten
149 * calendar names after loading.<p>
150 * The following method tests are to be completed successfully before :
151 * <ul>
152 * <li> <code> getAllCalendars() </code> : gets all calendars for a given
153 * locale </li>
154 * </ul>
156 public void _loadCalendar() {
157 boolean res = true;
158 requiredMethod("getAllCalendars()");
160 for (int i=0; i<installed_locales.length; i++) {
161 String lang = "Language: "+installed_locales[i].Language +
162 ", Country: "+ installed_locales[i].Country +
163 ", Variant: "+ installed_locales[i].Country;
164 oObj.loadCalendar(calendars[i][0], installed_locales[i]);
165 if (!calendars[i][0].equals(oObj.getLoadedCalendar().Name)) {
166 log.println(lang + " ... FAILED");
168 res &= calendars[i][0].equals(oObj.getLoadedCalendar().Name);
171 tRes.tested("loadCalendar()", res);
175 * Test calls the method, then result is checked. <p>
176 * Has <b> OK </b> status if loaded calendar names are equal to gotten
177 * calendar names after loading.<p>
178 * The following method tests are to be completed successfully before :
179 * <ul>
180 * <li> <code> loadCalendar() </code> : loads calendar using a given name
181 * and locale </li>
182 * </ul>
184 public void _getLoadedCalendar() {
185 boolean res = true;
187 requiredMethod("loadCalendar()");
188 for (int i=0; i<installed_locales.length; i++) {
189 String lang = "Language: "+installed_locales[i].Language +
190 ", Country: "+ installed_locales[i].Country +
191 ", Variant: "+ installed_locales[i].Country;
192 oObj.loadCalendar(calendars[i][0], installed_locales[i]);
193 if (!calendars[i][0].equals(oObj.getLoadedCalendar().Name)) {
194 log.println(lang + " ... FAILED");
196 res &= calendars[i][0].equals(oObj.getLoadedCalendar().Name);
198 tRes.tested("getLoadedCalendar()", res);
202 * Test calls the method, then result is checked. <p>
203 * Has <b> OK </b> status if the method returns value that's equal to a
204 * calendar name. <p>
205 * The following method tests are to be completed successfully before :
206 * <ul>
207 * <li> <code> loadCalendar() </code> : loads calendar using a given name
208 * and locale </li>
209 * </ul>
211 public void _getUniqueID() {
212 boolean res = true;
213 for (int i=0; i<installed_locales.length; i++) {
214 String lang = "Language: "+installed_locales[i].Language +
215 ", Country: "+ installed_locales[i].Country +
216 ", Variant: "+ installed_locales[i].Country;
217 oObj.loadCalendar(calendars[i][0], installed_locales[i]);
218 String uID = oObj.getUniqueID();
219 if (!uID.equals(calendars[i][0])) {
220 log.println(lang + " ... FAILED");
222 res &= uID.equals(calendars[i][0]);
225 tRes.tested("getUniqueID()",res);
229 * Test calls the method, then result is checked. <p>
230 * Has <b> OK </b> status if the method returns value, that's equal to
231 * value set before. <p>
234 public void _setDateTime() {
235 boolean res = true;
237 for (int i=0; i<installed_locales.length; i++) {
238 String lang = "Language: "+installed_locales[i].Language +
239 ", Country: "+ installed_locales[i].Country +
240 ", Variant: "+ installed_locales[i].Country;
241 oObj.setDateTime(newDTime);
242 double aDTime = oObj.getDateTime();
243 if (aDTime != newDTime) {
244 log.println(lang + " ... FAILED");
246 res &= (aDTime == newDTime);
249 tRes.tested("setDateTime()", res);
253 * Test calls the method, then result is checked. <p>
254 * Has <b> OK </b> status if the method returns value, that's equal to
255 * value set before. <p>
258 public void _getDateTime() {
259 boolean res = true;
261 for (int i=0; i<installed_locales.length; i++) {
262 String lang = "Language: "+installed_locales[i].Language +
263 ", Country: "+ installed_locales[i].Country +
264 ", Variant: "+ installed_locales[i].Country;
265 oObj.setDateTime(newDTime);
266 double aDTime = oObj.getDateTime();
267 if (aDTime != newDTime) {
268 log.println(lang + " ... FAILED");
270 res &= (aDTime == newDTime);
272 tRes.tested("getDateTime()", res);
276 * Test calls the method, then result is checked. <p>
277 * Has <b> OK </b> status if the method returns value, that's equal to
278 * value set before. <p>
280 public void _setValue() {
281 boolean res = true;
282 for (int i = 0; i < installed_locales.length; i++) {
283 String error = "";
284 String lang = "Language: " + installed_locales[i].Language + ", Country: " + installed_locales[i].Country
285 + ", Variant: " + installed_locales[i].Variant + ", Name: " + calendars[i][count[i]];
286 String[] names = new String[] { "DAY_OF_MONTH", "HOUR", "MINUTE", "SECOND", "MILLISECOND", "YEAR",
287 "MONTH" };
288 oObj.loadCalendar(calendars[i][count[i]], installed_locales[i]);
289 short[] fields = new short[] { CalendarFieldIndex.DAY_OF_MONTH, CalendarFieldIndex.HOUR,
290 CalendarFieldIndex.MINUTE, CalendarFieldIndex.SECOND, CalendarFieldIndex.MILLISECOND,
291 CalendarFieldIndex.YEAR, CalendarFieldIndex.MONTH };
292 for (int k = 0; k < fields.length; k++) {
294 oObj.setDateTime(0.0);
296 // save the current values for debug purposes
297 short[] oldValues = new short[fields.length];
298 for (int n = 0; n < oldValues.length; n++) {
299 oldValues[n] = oObj.getValue(fields[n]);
302 short set = oObj.getValue(fields[k]);
303 if (fields[k] == CalendarFieldIndex.MONTH)
304 set = newValue;
305 oObj.setValue(fields[k], set);
306 short get = oObj.getValue(fields[k]);
307 if (get != set) {
308 if (debug)
309 log.println("ERROR occurred: tried to set " + names[k] + " to value " + set);
310 log.println("list of values BEFORE set " + names[k] + " to value " + set + ":");
311 for (int n = 0; n < oldValues.length; n++) {
312 log.println(names[n] + ":" + oldValues[n]);
314 log.println("list of values AFTER set " + names[k] + " to value " + set + ":");
315 for (int n = 0; n < fields.length; n++) {
316 log.println(names[n] + ":" + oObj.getValue(fields[n]));
319 error += "failed for " + names[k] + " expected " + set + " gained " + get + " ; \n";
322 if (error.equals("")) {
323 log.println(lang + " ... OK");
324 } else {
325 log.println("*** " + lang + " ... FAILED ***");
326 log.println(error);
328 res &= error.equals("");
331 tRes.tested("setValue()", res);
335 * Test calls the method, then result is checked. <p>
336 * Has <b> OK </b> status if the method returns value, that's equal to
337 * value set before. <p>
340 public void _getValue() {
341 boolean res = true;
343 requiredMethod("setValue()");
344 short aValue = oObj.getValue(CalendarFieldIndex.MONTH);
345 res &= (aValue == newValue);
346 if (!res){
347 log.println("the returned value is not the expected value:");
348 log.println("expected: " + newValue + " returned value: " + aValue);
350 tRes.tested("getValue()", res);
354 * Test calls the method, then result is checked. <p>
355 * Has <b> OK </b> status if value, added by the method is greater than
356 * previously defined "newValue".
357 * <p>
358 * The following method tests are to be completed successfully before :
359 * <ul>
360 * <li> <code> getValue() </code> : gets the value of a field </li>
361 * </ul>
363 public void _addValue() {
364 boolean res = true;
366 requiredMethod("getValue()");
367 oObj.addValue(CalendarFieldIndex.MONTH, 1);
368 short aValue = oObj.getValue(CalendarFieldIndex.MONTH);
369 res &= (aValue > newValue);
370 if (!res){
371 log.println("the returned value is not the expected value:");
372 log.println("expected: " + newValue + " returned value: " + aValue);
374 tRes.tested("addValue()", res);
378 * Test calls the method. <p>
379 * Has <b> OK </b> status if the method successfully returns
380 * and no exceptions were thrown.
382 public void _setFirstDayOfWeek() {
383 boolean res = true;
385 oObj.setFirstDayOfWeek(firstDay);
386 res &= true;
387 tRes.tested("setFirstDayOfWeek()", res);
391 * Test calls the method, then result is checked. <p>
392 * Has <b> OK </b> status if the method returns value that is equal to
393 * value set before. <p>
394 * The following method tests are to be completed successfully before :
395 * <ul>
396 * <li> <code> setFirstDayOfWeek() </code> : set the first day of a
397 * week</li>
398 * </ul>
400 public void _getFirstDayOfWeek() {
401 boolean res = true;
403 requiredMethod("setFirstDayOfWeek()");
404 short aFirstDayOfWeek = oObj.getFirstDayOfWeek();
405 res &= (aFirstDayOfWeek == firstDay);
406 tRes.tested("getFirstDayOfWeek()", res);
410 * Test calls the method. <p>
411 * Has <b> OK </b> status if the method successfully returns
412 * and no exceptions were thrown.
414 public void _setMinimumNumberOfDaysForFirstWeek() {
415 boolean res = true;
417 oObj.setMinimumNumberOfDaysForFirstWeek(mdfw);
418 res &= true;
419 tRes.tested("setMinimumNumberOfDaysForFirstWeek()", res);
423 * Test calls the method, then result is checked. <p>
424 * Has <b> OK </b> status if the method returns value that is equal to
425 * value set before. <p>
426 * The following method tests are to be completed successfully before :
427 * <ul>
428 * <li> <code> setMinimumNumberOfDaysForFirstWeek() </code> : sets how
429 * many days of a week must reside in the first week of a year</li>
430 * </ul>
432 public void _getMinimumNumberOfDaysForFirstWeek() {
433 boolean res = true;
435 requiredMethod("setMinimumNumberOfDaysForFirstWeek()");
436 short aShort = oObj.getMinimumNumberOfDaysForFirstWeek();
437 res &= (aShort == mdfw);
438 tRes.tested("getMinimumNumberOfDaysForFirstWeek()", res);
442 * Test calls the method, then result is checked. <p>
443 * Has <b> OK </b> status if the method returns 12.
445 public void _getNumberOfMonthsInYear() {
446 boolean res = true;
447 short aShort = oObj.getNumberOfMonthsInYear();
449 res &= (aShort == (short) 12);
450 tRes.tested("getNumberOfMonthsInYear()", res);
454 * Test calls the method, then result is checked. <p>
455 * Has <b> OK </b> status if the method returns 7.
457 public void _getNumberOfDaysInWeek() {
458 boolean res = true;
459 short aShort = oObj.getNumberOfDaysInWeek();
461 res &= (aShort == (short) 7);
462 tRes.tested("getNumberOfDaysInWeek()", res);
466 * Test calls the method, then result is checked. <p>
467 * Has <b> OK </b> status if length of array, returned by the method is 12.
469 public void _getMonths() {
470 boolean res = true;
471 CalendarItem[] months = oObj.getMonths();
473 res &= (months.length == 12);
474 tRes.tested("getMonths()", res);
478 * Test calls the method, then result is checked. <p>
479 * Has <b> OK </b> status if length of array, returned by the method is 7.
481 public void _getDays() {
482 boolean res = true;
483 CalendarItem[] Days = oObj.getDays();
485 res &= (Days.length == 7);
486 tRes.tested("getDays()", res);
490 * After loading calendar, test calls the method, then result is checked.<p>
491 * Has <b> OK </b> status if length of string, returned by the method is 3.
493 public void _getDisplayName() {
494 boolean res = true;
496 oObj.loadCalendar(calendars[0][0],installed_locales[0]);
497 String DisplayName = oObj.getDisplayName(CalendarDisplayIndex.MONTH,
498 newValue, (short) 0);
499 res &= (DisplayName.length() == 3);
500 tRes.tested("getDisplayName()", res);
505 * The test sets obviously wrong value, then calls a method. After that the
506 * test sets correct value, and again calls a method. <p>
507 * Has <b> OK </b> status if the method returns true when valid month is
508 * set, and if the method returns false when set month is not valid.
510 public void _isValid() {
511 boolean res = true;
513 oObj.loadDefaultCalendar(installed_locales[0]);
514 oObj.setValue(CalendarFieldIndex.MONTH, (short) 37);
515 res &= !oObj.isValid();
516 oObj.setValue(CalendarFieldIndex.MONTH, (short) 10);
517 res &= oObj.isValid();
519 tRes.tested("isValid()", res);