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