1 /* 2 ******************************************************************************* 3 * Copyright (C) 2004-2014, International Business Machines Corporation and * 4 * others. All Rights Reserved. * 5 ******************************************************************************* 6 */ 7 8 package com.ibm.icu.dev.test.format; 9 10 import java.util.ArrayList; 11 import java.util.List; 12 import java.util.MissingResourceException; 13 import java.util.ResourceBundle; 14 15 import com.ibm.icu.dev.test.TestFmwk; 16 import com.ibm.icu.text.BreakIterator; 17 import com.ibm.icu.text.Collator; 18 import com.ibm.icu.text.DateFormat; 19 import com.ibm.icu.text.NumberFormat; 20 import com.ibm.icu.text.SimpleDateFormat; 21 import com.ibm.icu.util.BuddhistCalendar; 22 import com.ibm.icu.util.Calendar; 23 import com.ibm.icu.util.Currency; 24 import com.ibm.icu.util.GlobalizationPreferences; 25 import com.ibm.icu.util.GregorianCalendar; 26 import com.ibm.icu.util.IslamicCalendar; 27 import com.ibm.icu.util.JapaneseCalendar; 28 import com.ibm.icu.util.TimeZone; 29 import com.ibm.icu.util.ULocale; 30 31 32 33 public class GlobalizationPreferencesTest extends TestFmwk { 34 main(String[] args)35 public static void main(String[] args) throws Exception { 36 new GlobalizationPreferencesTest().run(args); 37 } 38 TestDefault()39 public void TestDefault() { 40 GlobalizationPreferences gp = new GlobalizationPreferences(); 41 ULocale defLocale = new ULocale("en_US"); 42 ULocale defFallbackLocale = new ULocale("en"); 43 44 if (!defLocale.equals(ULocale.getDefault())) { 45 // Locale.US is always used as the default locale in the test environment 46 // If not, some test cases will fail... 47 errln("FAIL: The default locale of the test environment must be en_US"); 48 } 49 50 logln("Default locale: " + defLocale.toString()); 51 52 // First locale is en_US 53 ULocale gpLocale0 = gp.getLocale(0); 54 logln("Primary locale: " + gpLocale0.toString()); 55 if (!gpLocale0.equals(defLocale)) { 56 errln("FAIL: The primary locale is not en_US"); 57 } 58 59 // Second locale is en 60 ULocale gpLocale1 = gp.getLocale(1); 61 logln("Secondary locale: " + gpLocale1.toString()); 62 if (!gpLocale1.equals(defFallbackLocale)) { 63 errln("FAIL: The secondary locale is not en"); 64 } 65 66 // Third locale is null 67 ULocale gpLocale2 = gp.getLocale(2); 68 if (gpLocale2 != null) { 69 errln("FAIL: Number of locales must be 2"); 70 } 71 72 // Calendar locale 73 Calendar cal = gp.getCalendar(); 74 ULocale calLocale = cal.getLocale(ULocale.VALID_LOCALE); 75 logln("Calendar locale: " + calLocale.toString()); 76 if (!calLocale.equals(defLocale)) { 77 errln("FAIL: The calendar locale must match with the default JVM locale"); 78 } 79 80 // Collator locale 81 Collator coll = gp.getCollator(); 82 ULocale collLocale = coll.getLocale(ULocale.VALID_LOCALE); 83 logln("Collator locale: " + collLocale.toString()); 84 if (!collLocale.equals(defLocale)) { 85 errln("FAIL: The collator locale must match with the default JVM locale"); 86 } 87 88 // BreakIterator locale 89 BreakIterator brk = gp.getBreakIterator(GlobalizationPreferences.BI_CHARACTER); 90 ULocale brkLocale = brk.getLocale(ULocale.VALID_LOCALE); 91 logln("BreakIterator locale: " + brkLocale.toString()); 92 if (!brkLocale.equals(defLocale)) { 93 errln("FAIL: The break iterator locale must match with the default JVM locale"); 94 } 95 96 /* Skip - Bug#5209 97 // DateFormat locale 98 DateFormat df = gp.getDateFormat(GlobalizationPreferences.DF_FULL, GlobalizationPreferences.DF_NONE); 99 ULocale dfLocale = df.getLocale(ULocale.VALID_LOCALE); 100 logln("DateFormat locale: " + dfLocale.toString()); 101 if (!dfLocale.equals(defLocale)) { 102 errln("FAIL: The date format locale must match with the default JVM locale"); 103 } 104 */ 105 106 // NumberFormat locale 107 NumberFormat nf = gp.getNumberFormat(GlobalizationPreferences.NF_NUMBER); 108 ULocale nfLocale = nf.getLocale(ULocale.VALID_LOCALE); 109 logln("NumberFormat locale: " + nfLocale.toString()); 110 if (!nfLocale.equals(defLocale)) { 111 errln("FAIL: The number format locale must match with the default JVM locale"); 112 } 113 } 114 TestFreezable()115 public void TestFreezable() { 116 logln("Create a new GlobalizationPreference object"); 117 GlobalizationPreferences gp = new GlobalizationPreferences(); 118 if (gp.isFrozen()) { 119 errln("FAIL: This object is not yet frozen"); 120 } 121 122 logln("Call reset()"); 123 boolean bSet = true; 124 try { 125 gp.reset(); 126 } catch (UnsupportedOperationException uoe) { 127 bSet = false; 128 } 129 if (!bSet) { 130 errln("FAIL: reset() must not throw an exception before frozen"); 131 } 132 133 // Freeze the object 134 logln("Freeze the object"); 135 gp.freeze(); 136 if (!gp.isFrozen()) { 137 errln("FAIL: This object is already fronzen"); 138 } 139 140 // reset() 141 logln("Call reset() after frozen"); 142 bSet = true; 143 try { 144 gp.reset(); 145 } catch (UnsupportedOperationException uoe) { 146 bSet = false; 147 } 148 if (bSet) { 149 errln("FAIL: reset() must be blocked after frozen"); 150 } 151 152 // setLocales(ULocale[]) 153 logln("Call setLocales(ULocale[]) after frozen"); 154 bSet = true; 155 try { 156 gp.setLocales(new ULocale[] {new ULocale("fr_FR")}); 157 } catch (UnsupportedOperationException uoe) { 158 bSet = false; 159 } 160 if (bSet) { 161 errln("FAIL: setLocales(ULocale[]) must be blocked after frozen"); 162 } 163 164 // setLocales(ULocale[]) 165 logln("Call setLocales(List) after frozen"); 166 bSet = true; 167 ArrayList list = new ArrayList(1); 168 list.add(new ULocale("fr_FR")); 169 try { 170 gp.setLocales(list); 171 } catch (UnsupportedOperationException uoe) { 172 bSet = false; 173 } 174 if (bSet) { 175 errln("FAIL: setLocales(List) must be blocked after frozen"); 176 } 177 178 // setLocales(String) 179 logln("Call setLocales(String) after frozen"); 180 bSet = true; 181 try { 182 gp.setLocales("pt-BR,es;q=0.7"); 183 } catch (UnsupportedOperationException uoe) { 184 bSet = false; 185 } 186 if (bSet) { 187 errln("FAIL: setLocales(String) must be blocked after frozen"); 188 } 189 190 // setLocale(ULocale) 191 logln("Call setLocale(ULocale) after frozen"); 192 bSet = true; 193 try { 194 gp.setLocale(new ULocale("fi_FI")); 195 } catch (UnsupportedOperationException uoe) { 196 bSet = false; 197 } 198 if (bSet) { 199 errln("FAIL: setLocale(ULocale) must be blocked after frozen"); 200 } 201 202 // setTerritory(String) 203 logln("Call setTerritory(String) after frozen"); 204 bSet = true; 205 try { 206 gp.setTerritory("AU"); 207 } catch (UnsupportedOperationException uoe) { 208 bSet = false; 209 } 210 if (bSet) { 211 errln("FAIL: setTerritory(String) must be blocked after frozen"); 212 } 213 214 // Modifiable clone 215 logln("Create a modifiable clone"); 216 GlobalizationPreferences gp1 = (GlobalizationPreferences)gp.cloneAsThawed(); 217 218 if (gp1.isFrozen()) { 219 errln("FAIL: The object returned by cloneAsThawed() must not be frozen yet"); 220 } 221 222 // setLocale(ULocale) 223 logln("Call setLocale(ULocale) of the modifiable clone"); 224 bSet = true; 225 try { 226 gp1.setLocale(new ULocale("fr_FR")); 227 } catch (UnsupportedOperationException uoe) { 228 bSet = false; 229 } 230 if (!bSet) { 231 errln("FAIL: setLocales(ULocale) must not throw an exception before frozen"); 232 } 233 } 234 235 static String[][] INPUT_LOCALEIDS = { 236 {"en_US"}, 237 {"fr_CA", "fr"}, 238 {"fr", "fr_CA"}, 239 {"es", "fr", "en_US"}, 240 {"zh_CN", "zh_Hans", "zh_Hans_CN"}, 241 {"en_US_123"}, 242 {"es_US", "es"}, 243 {"de_DE", "es", "fr_FR"}, 244 }; 245 246 static String[] ACCEPT_LANGUAGES = { 247 "en-US", 248 "fr-CA,fr;q=0.5", 249 "fr_CA;q=0.5,fr", 250 "es,fr;q=0.76,en_US;q=0.75", 251 "zh-CN,zh-Hans;q=0.5,zh-Hans-CN;q=0.1", 252 "en-US-123", 253 " es\t; q =0.5 \t, es-US ;q =1", 254 "fr-FR; q=0.5, de-DE, es", 255 }; 256 257 static String[][] RESULTS_LOCALEIDS = { 258 {"en_US", "en"}, 259 {"fr_CA", "fr"}, 260 {"fr_CA", "fr"}, 261 {"es", "fr", "en_US", "en"}, 262 {"zh_Hans_CN", "zh_CN", "zh_Hans", "zh"}, 263 {"en_US_123", "en_US", "en"}, 264 {"es_US", "es"}, 265 {"de_DE", "de", "es", "fr_FR", "fr"}, 266 }; 267 TestSetLocales()268 public void TestSetLocales() { 269 GlobalizationPreferences gp = new GlobalizationPreferences(); 270 271 // setLocales(List) 272 for (int i = 0; i < INPUT_LOCALEIDS.length; i++) { 273 String[] localeStrings = INPUT_LOCALEIDS[i]; 274 ArrayList locales = new ArrayList(); 275 StringBuffer sb = new StringBuffer(); 276 for (int j = 0; j < localeStrings.length; j++) { 277 locales.add(new ULocale(localeStrings[j])); 278 if (j != 0) { 279 sb.append(", "); 280 } 281 sb.append(localeStrings[j]); 282 } 283 logln("Input locales: " + sb.toString()); 284 285 gp.reset(); 286 gp.setLocales(locales); 287 288 List resultLocales = gp.getLocales(); 289 if (resultLocales.size() != RESULTS_LOCALEIDS[i].length) { 290 errln("FAIL: Number of locales mismatch - GP:" + resultLocales.size() 291 + " Expected:" + RESULTS_LOCALEIDS[i].length); 292 } else { 293 294 for (int j = 0; j < RESULTS_LOCALEIDS[i].length; j++) { 295 ULocale loc = gp.getLocale(j); 296 logln("Locale[" + j + "]: " + loc.toString()); 297 if (!gp.getLocale(j).toString().equals(RESULTS_LOCALEIDS[i][j])) { 298 errln("FAIL: Locale index(" + j + ") does not match - GP:" + loc.toString() 299 + " Expected:" + RESULTS_LOCALEIDS[i][j]); 300 } 301 } 302 } 303 } 304 305 // setLocales(ULocale[]) 306 for (int i = 0; i < INPUT_LOCALEIDS.length; i++) { 307 String[] localeStrings = INPUT_LOCALEIDS[i]; 308 ULocale[] localeArray = new ULocale[INPUT_LOCALEIDS[i].length]; 309 StringBuffer sb = new StringBuffer(); 310 for (int j = 0; j < localeStrings.length; j++) { 311 localeArray[j] = new ULocale(localeStrings[j]); 312 if (j != 0) { 313 sb.append(", "); 314 } 315 sb.append(localeStrings[j]); 316 } 317 logln("Input locales: " + sb.toString()); 318 319 gp.reset(); 320 gp.setLocales(localeArray); 321 322 List resultLocales = gp.getLocales(); 323 if (resultLocales.size() != RESULTS_LOCALEIDS[i].length) { 324 errln("FAIL: Number of locales mismatch - GP:" + resultLocales.size() 325 + " Expected:" + RESULTS_LOCALEIDS[i].length); 326 } else { 327 328 for (int j = 0; j < RESULTS_LOCALEIDS[i].length; j++) { 329 ULocale loc = gp.getLocale(j); 330 logln("Locale[" + j + "]: " + loc.toString()); 331 if (!gp.getLocale(j).toString().equals(RESULTS_LOCALEIDS[i][j])) { 332 errln("FAIL: Locale index(" + j + ") does not match - GP:" + loc.toString() 333 + " Expected:" + RESULTS_LOCALEIDS[i][j]); 334 } 335 } 336 } 337 } 338 339 // setLocales(String) 340 for (int i = 0; i < ACCEPT_LANGUAGES.length; i++) { 341 String acceptLanguage = ACCEPT_LANGUAGES[i]; 342 logln("Accept language: " + acceptLanguage); 343 344 gp.reset(); 345 gp.setLocales(acceptLanguage); 346 347 List resultLocales = gp.getLocales(); 348 if (resultLocales.size() != RESULTS_LOCALEIDS[i].length) { 349 errln("FAIL: Number of locales mismatch - GP:" + resultLocales.size() 350 + " Expected:" + RESULTS_LOCALEIDS[i].length); 351 } else { 352 353 for (int j = 0; j < RESULTS_LOCALEIDS[i].length; j++) { 354 ULocale loc = gp.getLocale(j); 355 logln("Locale[" + j + "]: " + loc.toString()); 356 if (!gp.getLocale(j).toString().equals(RESULTS_LOCALEIDS[i][j])) { 357 errln("FAIL: Locale index(" + j + ") does not match - GP:" + loc.toString() 358 + " Expected:" + RESULTS_LOCALEIDS[i][j]); 359 } 360 } 361 } 362 } 363 364 365 // accept-language without q-value 366 logln("Set accept-language - de,de-AT"); 367 gp.setLocales("de,de-AT"); 368 if (!gp.getLocale(0).toString().equals("de_AT")) { 369 errln("FAIL: getLocale(0) returns " + gp.getLocale(0).toString() + " Expected: de_AT"); 370 } 371 372 // Invalid accept-language 373 logln("Set locale - ko_KR"); 374 gp.setLocale(new ULocale("ko_KR")); 375 boolean bException = false; 376 try { 377 logln("Set invlaid accept-language - ko=100"); 378 gp.setLocales("ko=100"); 379 } catch (IllegalArgumentException iae) { 380 logln("IllegalArgumentException was thrown"); 381 bException = true; 382 } 383 if (!bException) { 384 errln("FAIL: IllegalArgumentException was not thrown for illegal accept-language - ko=100"); 385 } 386 if (!gp.getLocale(0).toString().equals("ko_KR")) { 387 errln("FAIL: Previous valid locale list had gone"); 388 } 389 } 390 TestResourceBundle()391 public void TestResourceBundle() { 392 String baseName = "com.ibm.icu.dev.data.resources.TestDataElements"; 393 ResourceBundle rb; 394 395 logln("Get a resource bundle " + baseName + 396 " using GlobalizationPreferences initialized by locales - en_GB, en_US"); 397 GlobalizationPreferences gp = new GlobalizationPreferences(); 398 ULocale[] locales = new ULocale[2]; 399 locales[0] = new ULocale("en_GB"); 400 locales[1] = new ULocale("en_US"); 401 gp.setLocales(locales); 402 403 try { 404 rb = gp.getResourceBundle(baseName); 405 String str = rb.getString("from_en_US"); 406 if (!str.equals("This data comes from en_US")) { 407 errln("FAIL: from_en_US is not from en_US bundle"); 408 } 409 } catch (MissingResourceException mre) { 410 errln("FAIL: Missing resouces"); 411 } 412 413 gp.reset(); 414 415 logln("Get a resource bundle " + baseName + 416 " using GlobalizationPreferences initialized by locales - ja, en_US_California"); 417 418 locales = new ULocale[2]; 419 locales[0] = new ULocale("ja"); 420 locales[1] = new ULocale("en_US_California"); 421 gp.setLocales(locales); 422 423 try { 424 rb = gp.getResourceBundle(baseName, Thread.currentThread().getContextClassLoader()); 425 String str = rb.getString("from_en_US"); 426 if (!str.equals("This data comes from en_US")) { 427 errln("FAIL: from_en_US is not from en_US bundle"); 428 } 429 } catch (MissingResourceException mre) { 430 errln("FAIL: Missing resouces"); 431 } 432 433 logln("Get a resource bundle which does not exist"); 434 boolean bException = false; 435 try { 436 rb = gp.getResourceBundle("foo.bar.XXX"); 437 } catch (MissingResourceException mre) { 438 logln("Missing resource exception for getting resource bundle - foo.bar.XXX"); 439 bException = true; 440 } 441 if (!bException) { 442 errln("FAIL: MissingResourceException must be thrown for RB - foo.bar.XXX"); 443 } 444 } 445 TestTerritory()446 public void TestTerritory() { 447 GlobalizationPreferences gp = new GlobalizationPreferences(); 448 449 // Territory for unsupported language locale 450 logln("Set locale - ang"); 451 gp.setLocale(new ULocale("ang")); 452 String territory = gp.getTerritory(); 453 if (!territory.equals("US")) { 454 errln("FAIL: Territory is " + territory + " - Expected: US"); 455 } 456 457 // Territory for language only locale "fr" 458 logln("Set locale - fr"); 459 gp.setLocale(new ULocale("fr")); 460 territory = gp.getTerritory(); 461 if (!territory.equals("FR")) { 462 errln("FAIL: Territory is " + territory + " - Expected: FR"); 463 } 464 465 466 // Set explicity territory 467 logln("Set explicit territory - CA"); 468 gp.setTerritory("CA"); 469 territory = gp.getTerritory(); 470 if (!territory.equals("CA")) { 471 errln("FAIL: Territory is " + territory + " - Expected: CA"); 472 } 473 474 // Freeze 475 logln("Freeze this object"); 476 gp.freeze(); 477 478 boolean bFrozen = false; 479 try { 480 gp.setTerritory("FR"); 481 } catch (UnsupportedOperationException uoe) { 482 logln("setTerritory is blocked"); 483 bFrozen = true; 484 } 485 if (!bFrozen) { 486 errln("FAIL: setTerritory must be blocked after frozen"); 487 } 488 territory = gp.getTerritory(); 489 if (!territory.equals("CA")) { 490 errln("FAIL: Territory is not CA"); 491 } 492 493 // Safe clone 494 GlobalizationPreferences gp1 = (GlobalizationPreferences)gp.cloneAsThawed(); 495 territory = gp1.getTerritory(); 496 if (!territory.equals("CA")) { 497 errln("FAIL: Territory is " + territory + " - Expected: CA"); 498 } 499 500 gp1.reset(); 501 ULocale[] locales = new ULocale[2]; 502 locales[0] = new ULocale("ja"); 503 locales[1] = new ULocale("zh_Hant_TW"); 504 505 logln("Set locales - ja, zh_Hant_TW"); 506 gp1.setLocales(locales); 507 508 territory = gp1.getTerritory(); 509 if (!territory.equals("TW")) { 510 errln("FAIL: Territory is " + territory + " - Expected: TW"); 511 } 512 } 513 TestCurrency()514 public void TestCurrency() { 515 GlobalizationPreferences gp = new GlobalizationPreferences(); 516 517 // Set language only locale - ja 518 logln("Set locale - ja"); 519 gp.setLocale(new ULocale("ja")); 520 Currency cur = gp.getCurrency(); 521 String code = cur.getCurrencyCode(); 522 if (!code.equals("JPY")) { 523 errln("FAIL: Currency is " + code + " - Expected: JPY"); 524 } 525 526 gp.reset(); 527 // Set locales with territory 528 logln("Set locale - ja_US"); 529 gp.setLocale(new ULocale("ja_US")); 530 cur = gp.getCurrency(); 531 code = cur.getCurrencyCode(); 532 if (!code.equals("USD")) { 533 errln("FAIL: Currency is " + code + " - Expected: USD"); 534 } 535 536 // Set locales with territory in the second locale 537 logln("Set locales - it, en_US"); 538 ULocale[] locales = new ULocale[2]; 539 locales[0] = new ULocale("it"); 540 locales[1] = new ULocale("en_US"); 541 gp.setLocales(locales); 542 cur = gp.getCurrency(); 543 code = cur.getCurrencyCode(); 544 if (!code.equals("USD")) { 545 errln("FAIL: Currency is " + code + " - Expected: USD"); 546 } 547 548 // Set explicit territory 549 logln("Set territory - DE"); 550 gp.setTerritory("DE"); 551 cur = gp.getCurrency(); 552 code = cur.getCurrencyCode(); 553 if (!code.equals("EUR")) { 554 errln("FAIL: Currency is " + code + " - Expected: EUR"); 555 } 556 557 // Set explicit currency 558 Currency ecur = Currency.getInstance("BRL"); 559 gp.setCurrency(ecur); 560 logln("Set explicit currency - BRL"); 561 cur = gp.getCurrency(); 562 code = cur.getCurrencyCode(); 563 if (!code.equals("BRL")) { 564 errln("FAIL: Currency is " + code + " - Expected: BRL"); 565 } 566 567 // Set explicit territory again 568 logln("Set territory - JP"); 569 cur = gp.getCurrency(); 570 code = cur.getCurrencyCode(); 571 if (!code.equals("BRL")) { 572 errln("FAIL: Currency is " + code + " - Expected: BRL"); 573 } 574 575 // Freeze 576 logln("Freeze this object"); 577 Currency ecur2 = Currency.getInstance("CHF"); 578 boolean bFrozen = false; 579 gp.freeze(); 580 try { 581 gp.setCurrency(ecur2); 582 } catch (UnsupportedOperationException uoe) { 583 logln("setCurrency is blocked"); 584 bFrozen = true; 585 } 586 if (!bFrozen) { 587 errln("FAIL: setCurrency must be blocked"); 588 } 589 590 // Safe clone 591 logln("cloneAsThawed"); 592 GlobalizationPreferences gp1 = (GlobalizationPreferences)gp.cloneAsThawed(); 593 cur = gp.getCurrency(); 594 code = cur.getCurrencyCode(); 595 if (!code.equals("BRL")) { 596 errln("FAIL: Currency is " + code + " - Expected: BRL"); 597 } 598 599 // Set ecplicit currency 600 gp1.setCurrency(ecur2); 601 cur = gp1.getCurrency(); 602 code = cur.getCurrencyCode(); 603 if (!code.equals("CHF")) { 604 errln("FAIL: Currency is " + code + " - Expected: CHF"); 605 } 606 } 607 TestCalendar()608 public void TestCalendar() { 609 GlobalizationPreferences gp = new GlobalizationPreferences(); 610 611 // Set locale - pt_BR 612 logln("Set locale - pt"); 613 gp.setLocale(new ULocale("pt")); 614 Calendar cal = gp.getCalendar(); 615 String calType = cal.getType(); 616 if (!calType.equals("gregorian")) { 617 errln("FAIL: Calendar type is " + calType + " Expected: gregorian"); 618 } 619 620 // Set a list of locales 621 logln("Set locales - en, en_JP, en_GB"); 622 ULocale[] locales = new ULocale[3]; 623 locales[0] = new ULocale("en"); 624 locales[1] = new ULocale("en_JP"); 625 locales[2] = new ULocale("en_GB"); 626 gp.setLocales(locales); 627 628 cal = gp.getCalendar(); 629 ULocale calLocale = cal.getLocale(ULocale.VALID_LOCALE); 630 if (!calLocale.equals(locales[2])) { 631 errln("FAIL: Calendar locale is " + calLocale.toString() + " - Expected: en_GB"); 632 } 633 634 // Set ecplicit calendar 635 logln("Set Japanese calendar to this object"); 636 JapaneseCalendar jcal = new JapaneseCalendar(); 637 gp.setCalendar(jcal); 638 cal = gp.getCalendar(); 639 calType = cal.getType(); 640 if (!calType.equals("japanese")) { 641 errln("FAIL: Calendar type is " + calType + " Expected: japanese"); 642 } 643 644 jcal.setFirstDayOfWeek(3); 645 if (cal.getFirstDayOfWeek() == jcal.getFirstDayOfWeek()) { 646 errln("FAIL: Calendar returned by getCalendar must be a safe copy"); 647 } 648 cal.setFirstDayOfWeek(3); 649 Calendar cal1 = gp.getCalendar(); 650 if (cal1.getFirstDayOfWeek() == cal.getFirstDayOfWeek()) { 651 errln("FAIL: Calendar returned by getCalendar must be a safe copy"); 652 } 653 654 // Freeze 655 logln("Freeze this object"); 656 IslamicCalendar ical = new IslamicCalendar(); 657 boolean bFrozen = false; 658 gp.freeze(); 659 try { 660 gp.setCalendar(ical); 661 } catch (UnsupportedOperationException uoe) { 662 logln("setCalendar is blocked"); 663 bFrozen = true; 664 } 665 if (!bFrozen) { 666 errln("FAIL: setCalendar must be blocked"); 667 } 668 669 // Safe clone 670 logln("cloneAsThawed"); 671 GlobalizationPreferences gp1 = (GlobalizationPreferences)gp.cloneAsThawed(); 672 cal = gp.getCalendar(); 673 calType = cal.getType(); 674 if (!calType.equals("japanese")) { 675 errln("FAIL: Calendar type afte clone is " + calType + " Expected: japanese"); 676 } 677 678 logln("Set islamic calendar"); 679 gp1.setCalendar(ical); 680 cal = gp1.getCalendar(); 681 calType = cal.getType(); 682 if (!calType.equals("islamic-civil")) { // default constructed IslamicCalendar is islamic-civil 683 errln("FAIL: Calendar type afte clone is " + calType + " Expected: islamic-civil"); 684 } 685 } 686 TestTimeZone()687 public void TestTimeZone() { 688 GlobalizationPreferences gp = new GlobalizationPreferences(); 689 690 // Set locale - zh_CN 691 logln("Set locale - zh_CN"); 692 gp.setLocale(new ULocale("zh_CN")); 693 TimeZone tz = gp.getTimeZone(); 694 String tzid = tz.getID(); 695 if (!tzid.equals("Asia/Shanghai")) { 696 errln("FAIL: Time zone ID is " + tzid + " Expected: Asia/Shanghai"); 697 } 698 699 // Set locale - en 700 logln("Set locale - en"); 701 gp.setLocale(new ULocale("en")); 702 tz = gp.getTimeZone(); 703 tzid = tz.getID(); 704 if (!tzid.equals("America/New_York")) { 705 errln("FAIL: Time zone ID is " + tzid + " Expected: America/New_York"); 706 } 707 708 // Set territory - GB 709 logln("Set territory - GB"); 710 gp.setTerritory("GB"); 711 tz = gp.getTimeZone(); 712 tzid = tz.getID(); 713 if (!tzid.equals("Europe/London")) { 714 errln("FAIL: Time zone ID is " + tzid + " Expected: Europe/London"); 715 } 716 717 // Check if getTimeZone returns a safe clone 718 tz.setID("Bad_ID"); 719 tz = gp.getTimeZone(); 720 tzid = tz.getID(); 721 if (!tzid.equals("Europe/London")) { 722 errln("FAIL: Time zone ID is " + tzid + " Expected: Europe/London"); 723 } 724 725 // Set explicit time zone 726 TimeZone jst = TimeZone.getTimeZone("Asia/Tokyo"); 727 String customJstId = "Japan_Standard_Time"; 728 jst.setID(customJstId); 729 gp.setTimeZone(jst); 730 tz = gp.getTimeZone(); 731 tzid = tz.getID(); 732 if (!tzid.equals(customJstId)) { 733 errln("FAIL: Time zone ID is " + tzid + " Expected: " + customJstId); 734 } 735 736 // Freeze 737 logln("Freeze this object"); 738 TimeZone cst = TimeZone.getTimeZone("Europe/Paris"); 739 boolean bFrozen = false; 740 gp.freeze(); 741 try { 742 gp.setTimeZone(cst); 743 } catch (UnsupportedOperationException uoe) { 744 logln("setTimeZone is blocked"); 745 bFrozen = true; 746 } 747 if (!bFrozen) { 748 errln("FAIL: setTimeZone must be blocked"); 749 } 750 751 // Modifiable clone 752 logln("cloneAsThawed"); 753 GlobalizationPreferences gp1 = (GlobalizationPreferences)gp.cloneAsThawed(); 754 tz = gp1.getTimeZone(); 755 tzid = tz.getID(); 756 if (!tzid.equals(customJstId)) { 757 errln("FAIL: Time zone ID is " + tzid + " Expected: " + customJstId); 758 } 759 760 // Set explicit time zone 761 gp1.setTimeZone(cst); 762 tz = gp1.getTimeZone(); 763 tzid = tz.getID(); 764 if (!tzid.equals(cst.getID())) { 765 errln("FAIL: Time zone ID is " + tzid + " Expected: " + cst.getID()); 766 } 767 } 768 TestCollator()769 public void TestCollator() { 770 GlobalizationPreferences gp = new GlobalizationPreferences(); 771 772 // Set locale - tr 773 logln("Set locale - tr"); 774 gp.setLocale(new ULocale("tr")); 775 Collator coll = gp.getCollator(); 776 String locStr = coll.getLocale(ULocale.VALID_LOCALE).toString(); 777 if (!locStr.equals("tr")) { 778 errln("FAIL: Collator locale is " + locStr + " Expected: tr"); 779 } 780 781 // Unsupported collator locale - zun 782 logln("Set locale - zun"); 783 gp.setLocale(new ULocale("zun")); 784 coll = gp.getCollator(); 785 locStr = coll.getLocale(ULocale.VALID_LOCALE).toString(); 786 if (!locStr.equals("")) { 787 errln("FAIL: Collator locale is \"" + locStr + "\" Expected: \"\"(empty)"); 788 } 789 790 // Set locales - en_JP, fr, en_US, fr_FR 791 logln("Set locale - en_JP, fr, en_US, fr_FR"); 792 ULocale[] locales = new ULocale[4]; 793 locales[0] = new ULocale("en_JP"); 794 locales[1] = new ULocale("fr"); 795 locales[2] = new ULocale("en_US"); 796 locales[3] = new ULocale("fr_FR"); 797 gp.setLocales(locales); 798 coll = gp.getCollator(); 799 locStr = coll.getLocale(ULocale.VALID_LOCALE).toString(); 800 if (!locStr.equals("fr")) { 801 errln("FAIL: Collator locale is " + locStr + " Expected: fr"); 802 } 803 804 // Set explicit Collator 805 Collator coll1 = Collator.getInstance(new ULocale("it")); 806 coll1.setDecomposition(Collator.CANONICAL_DECOMPOSITION); 807 logln("Set collator for it in canonical deconposition mode"); 808 gp.setCollator(coll1); 809 coll1.setStrength(Collator.IDENTICAL); 810 coll = gp.getCollator(); 811 locStr = coll.getLocale(ULocale.VALID_LOCALE).toString(); 812 if (!locStr.equals("it")) { 813 errln("FAIL: Collator locale is " + locStr + " Expected: it"); 814 } 815 if (coll1.equals(coll)) { 816 errln("FAIL: setCollator must use a safe copy of a Collator"); 817 } 818 819 // Freeze 820 logln("Freeze this object"); 821 boolean isFrozen = false; 822 gp.freeze(); 823 try { 824 gp.setCollator(coll1); 825 } catch (UnsupportedOperationException uoe) { 826 logln("setCollator is blocked"); 827 isFrozen = true; 828 } 829 if (!isFrozen) { 830 errln("FAIL: setCollator must be blocked after freeze"); 831 } 832 833 // Modifiable clone 834 logln("cloneAsThawed"); 835 GlobalizationPreferences gp1 = (GlobalizationPreferences)gp.cloneAsThawed(); 836 coll = gp1.getCollator(); 837 locStr = coll.getLocale(ULocale.VALID_LOCALE).toString(); 838 if (!locStr.equals("it")) { 839 errln("FAIL: Collator locale is " + locStr + " Expected: it"); 840 } 841 if (coll.getDecomposition() != Collator.CANONICAL_DECOMPOSITION) { 842 errln("FAIL: Decomposition mode is not CANONICAL_DECOMPOSITION"); 843 } 844 845 // Set custom collator again 846 gp1.setCollator(coll1); 847 coll = gp1.getCollator(); 848 if (coll.getStrength() != Collator.IDENTICAL) { 849 errln("FAIL: Strength is not IDENTICAL"); 850 } 851 } 852 TestBreakIterator()853 public void TestBreakIterator() { 854 GlobalizationPreferences gp = new GlobalizationPreferences(); 855 856 // Unsupported break iterator locale - aar 857 logln("Set locale - aar"); 858 gp.setLocale(new ULocale("aar")); 859 BreakIterator brk = gp.getBreakIterator(GlobalizationPreferences.BI_LINE); 860 String locStr = brk.getLocale(ULocale.VALID_LOCALE).toString(); 861 if (!locStr.equals("root")) { 862 errln("FAIL: Line break iterator locale is " + locStr + " Expected: root"); 863 } 864 865 // Set locale - es 866 logln("Set locale - es"); 867 gp.setLocale(new ULocale("es")); 868 brk = gp.getBreakIterator(GlobalizationPreferences.BI_CHARACTER); 869 /* TODO: JB#5383 870 locStr = brk.getLocale(ULocale.VALID_LOCALE).toString(); 871 if (!locStr.equals("es")) { 872 errln("FAIL: Character break iterator locale is " + locStr + " Expected: es"); 873 } 874 */ 875 876 // Set explicit break sentence iterator 877 logln("Set break iterator for sentence using locale hu_HU"); 878 BreakIterator brk1 = BreakIterator.getSentenceInstance(new ULocale("hu_HU")); 879 gp.setBreakIterator(GlobalizationPreferences.BI_SENTENCE, brk1); 880 881 brk = gp.getBreakIterator(GlobalizationPreferences.BI_SENTENCE); 882 /* TODO: JB#5210 883 locStr = brk.getLocale(ULocale.VALID_LOCALE).toString(); 884 if (!locStr.equals("hu_HU")) { 885 errln("FAIL: Sentence break locale is " + locStr + " Expected: hu_HU"); 886 } 887 */ 888 brk.setText("This is a test case. Is this a new instance?"); 889 brk.next(); 890 if (brk1.current() == brk.current()) { 891 errln("FAIL: getBreakIterator must return a new instance"); 892 } 893 894 // Illegal argument 895 logln("Get break iterator type 100"); 896 boolean illegalArg = false; 897 try { 898 brk = gp.getBreakIterator(100); 899 } catch (IllegalArgumentException iae) { 900 logln("Break iterator type 100 is illegal"); 901 illegalArg = true; 902 } 903 if (!illegalArg) { 904 errln("FAIL: getBreakIterator must throw IllegalArgumentException for type 100"); 905 } 906 logln("Set break iterator type -1"); 907 illegalArg = false; 908 try { 909 gp.setBreakIterator(-1, brk1); 910 } catch (IllegalArgumentException iae) { 911 logln("Break iterator type -1 is illegal"); 912 illegalArg = true; 913 } 914 if (!illegalArg) { 915 errln("FAIL: getBreakIterator must throw IllegalArgumentException for type -1"); 916 } 917 918 // Freeze 919 logln("Freeze this object"); 920 BreakIterator brk2 = BreakIterator.getTitleInstance(new ULocale("es_MX")); 921 boolean isFrozen = false; 922 gp.freeze(); 923 try { 924 gp.setBreakIterator(GlobalizationPreferences.BI_TITLE, brk2); 925 } catch (UnsupportedOperationException uoe) { 926 logln("setBreakIterator is blocked"); 927 isFrozen = true; 928 } 929 if (!isFrozen) { 930 errln("FAIL: setBreakIterator must be blocked after frozen"); 931 } 932 933 // Modifiable clone 934 logln("cloneAsThawed"); 935 GlobalizationPreferences gp1 = (GlobalizationPreferences)gp.cloneAsThawed(); 936 brk = gp1.getBreakIterator(GlobalizationPreferences.BI_WORD); 937 /* TODO: JB#5383 938 locStr = brk.getLocale(ULocale.VALID_LOCALE).toString(); 939 if (!locStr.equals("es")) { 940 errln("FAIL: Word break iterator locale is " + locStr + " Expected: es"); 941 } 942 */ 943 944 ULocale frFR = new ULocale("fr_FR"); 945 BreakIterator brkC = BreakIterator.getCharacterInstance(frFR); 946 BreakIterator brkW = BreakIterator.getWordInstance(frFR); 947 BreakIterator brkL = BreakIterator.getLineInstance(frFR); 948 BreakIterator brkS = BreakIterator.getSentenceInstance(frFR); 949 BreakIterator brkT = BreakIterator.getTitleInstance(frFR); 950 951 gp1.setBreakIterator(GlobalizationPreferences.BI_CHARACTER, brkC); 952 gp1.setBreakIterator(GlobalizationPreferences.BI_WORD, brkW); 953 gp1.setBreakIterator(GlobalizationPreferences.BI_LINE, brkL); 954 gp1.setBreakIterator(GlobalizationPreferences.BI_SENTENCE, brkS); 955 gp1.setBreakIterator(GlobalizationPreferences.BI_TITLE, brkT); 956 957 /* TODO: JB#5210 958 locStr = brkC.getLocale(ULocale.VALID_LOCALE).toString(); 959 if (!locStr.equals("ja_JP")) { 960 errln("FAIL: Character break iterator locale is " + locStr + " Expected: fr_FR"); 961 } 962 locStr = brkW.getLocale(ULocale.VALID_LOCALE).toString(); 963 if (!locStr.equals("ja_JP")) { 964 errln("FAIL: Word break iterator locale is " + locStr + " Expected: fr_FR"); 965 } 966 locStr = brkL.getLocale(ULocale.VALID_LOCALE).toString(); 967 if (!locStr.equals("ja_JP")) { 968 errln("FAIL: Line break iterator locale is " + locStr + " Expected: fr_FR"); 969 } 970 locStr = brkS.getLocale(ULocale.VALID_LOCALE).toString(); 971 if (!locStr.equals("ja_JP")) { 972 errln("FAIL: Sentence break iterator locale is " + locStr + " Expected: fr_FR"); 973 } 974 locStr = brkT.getLocale(ULocale.VALID_LOCALE).toString(); 975 if (!locStr.equals("ja_JP")) { 976 errln("FAIL: Title break iterator locale is " + locStr + " Expected: fr_FR"); 977 } 978 */ 979 } 980 TestDisplayName()981 public void TestDisplayName() { 982 GlobalizationPreferences gp = new GlobalizationPreferences(); 983 984 ULocale loc_fr_FR_Paris = new ULocale("fr_FR_Paris"); 985 ULocale loc_peo = new ULocale("peo"); 986 987 // Locale list - fr_FR_Paris 988 ArrayList locales1 = new ArrayList(1); 989 locales1.add(loc_fr_FR_Paris); 990 991 // Locale list - ain, fr_FR_Paris 992 ArrayList locales2 = new ArrayList(2); 993 locales2.add(loc_peo); 994 locales2.add(loc_fr_FR_Paris); 995 996 logln("Locales: <default> | <fr_FR_Paris> | <ain, fr_FR_Paris>"); 997 998 // ID_LOCALE 999 String id = "zh_Hant_HK"; 1000 String name1 = gp.getDisplayName(id, GlobalizationPreferences.ID_LOCALE); 1001 gp.setLocales(locales1); 1002 String name2 = gp.getDisplayName(id, GlobalizationPreferences.ID_LOCALE); 1003 gp.setLocales(locales2); 1004 String name3 = gp.getDisplayName(id, GlobalizationPreferences.ID_LOCALE); 1005 1006 logln("Locale[zh_Hant_HK]: " + name1 + " | " + name2 + " | " + name3); 1007 if (name1.equals(name2) || !name2.equals(name3)) { 1008 errln("FAIL: Locale ID"); 1009 } 1010 1011 // ID_LANGUAGE 1012 gp.reset(); 1013 id = "fr"; 1014 name1 = gp.getDisplayName(id, GlobalizationPreferences.ID_LANGUAGE); 1015 gp.setLocales(locales1); 1016 name2 = gp.getDisplayName(id, GlobalizationPreferences.ID_LANGUAGE); 1017 gp.setLocales(locales2); 1018 name3 = gp.getDisplayName(id, GlobalizationPreferences.ID_LANGUAGE); 1019 1020 logln("Language[fr]: " + name1 + " | " + name2 + " | " + name3); 1021 if (name1.equals(name2) || !name2.equals(name3)) { 1022 errln("FAIL: Language ID"); 1023 } 1024 1025 // ID_SCRIPT 1026 gp.reset(); 1027 id = "cyrl"; 1028 name1 = gp.getDisplayName(id, GlobalizationPreferences.ID_SCRIPT); 1029 gp.setLocales(locales1); 1030 name2 = gp.getDisplayName(id, GlobalizationPreferences.ID_SCRIPT); 1031 gp.setLocales(locales2); 1032 name3 = gp.getDisplayName(id, GlobalizationPreferences.ID_SCRIPT); 1033 1034 logln("Script[cyrl]: " + name1 + " | " + name2 + " | " + name3); 1035 if (name1.equals(name2) || !name2.equals(name3)) { 1036 errln("FAIL: Script ID"); 1037 } 1038 1039 // ID_TERRITORY 1040 gp.reset(); 1041 id = "JP"; 1042 name1 = gp.getDisplayName(id, GlobalizationPreferences.ID_TERRITORY); 1043 gp.setLocales(locales1); 1044 name2 = gp.getDisplayName(id, GlobalizationPreferences.ID_TERRITORY); 1045 gp.setLocales(locales2); 1046 name3 = gp.getDisplayName(id, GlobalizationPreferences.ID_TERRITORY); 1047 1048 logln("Territory[JP]: " + name1 + " | " + name2 + " | " + name3); 1049 if (name1.equals(name2) || !name2.equals(name3)) { 1050 errln("FAIL: Territory ID"); 1051 } 1052 1053 // ID_VARIANT 1054 gp.reset(); 1055 id = "NEDIS"; 1056 name1 = gp.getDisplayName(id, GlobalizationPreferences.ID_VARIANT); 1057 gp.setLocales(locales1); 1058 name2 = gp.getDisplayName(id, GlobalizationPreferences.ID_VARIANT); 1059 gp.setLocales(locales2); 1060 name3 = gp.getDisplayName(id, GlobalizationPreferences.ID_VARIANT); 1061 1062 logln("Variant[NEDIS]: " + name1 + " | " + name2 + " | " + name3); 1063 if (name1.equals(name2) || !name2.equals(name3)) { 1064 errln("FAIL: Variant ID"); 1065 } 1066 1067 // ID_KEYWORD 1068 gp.reset(); 1069 id = "collation"; 1070 name1 = gp.getDisplayName(id, GlobalizationPreferences.ID_KEYWORD); 1071 gp.setLocales(locales1); 1072 name2 = gp.getDisplayName(id, GlobalizationPreferences.ID_KEYWORD); 1073 gp.setLocales(locales2); 1074 name3 = gp.getDisplayName(id, GlobalizationPreferences.ID_KEYWORD); 1075 1076 logln("Keyword[collation]: " + name1 + " | " + name2 + " | " + name3); 1077 if (name1.equals(name2) || !name2.equals(name3)) { 1078 errln("FAIL: Keyword ID"); 1079 } 1080 1081 // ID_KEYWORD_VALUE 1082 gp.reset(); 1083 id = "collation=traditional"; 1084 name1 = gp.getDisplayName(id, GlobalizationPreferences.ID_KEYWORD_VALUE); 1085 gp.setLocales(locales1); 1086 name2 = gp.getDisplayName(id, GlobalizationPreferences.ID_KEYWORD_VALUE); 1087 gp.setLocales(locales2); 1088 name3 = gp.getDisplayName(id, GlobalizationPreferences.ID_KEYWORD_VALUE); 1089 1090 logln("Keyword value[traditional]: " + name1 + " | " + name2 + " | " + name3); 1091 if (name1.equals(name2) || !name2.equals(name3)) { 1092 errln("FAIL: Keyword value ID"); 1093 } 1094 1095 // ID_CURRENCY_SYMBOL 1096 gp.reset(); 1097 id = "USD"; 1098 name1 = gp.getDisplayName(id, GlobalizationPreferences.ID_CURRENCY_SYMBOL); 1099 gp.setLocales(locales1); 1100 name2 = gp.getDisplayName(id, GlobalizationPreferences.ID_CURRENCY_SYMBOL); 1101 gp.setLocales(locales2); 1102 name3 = gp.getDisplayName(id, GlobalizationPreferences.ID_CURRENCY_SYMBOL); 1103 1104 logln("Currency symbol[USD]: " + name1 + " | " + name2 + " | " + name3); 1105 String dollar = "$"; 1106 String us_dollar = "$US"; 1107 if (!name1.equals(dollar) || !name2.equals(us_dollar) || !name3.equals(us_dollar)) { 1108 errln("FAIL: Currency symbol ID"); 1109 } 1110 1111 // ID_CURRENCY 1112 gp.reset(); 1113 id = "USD"; 1114 name1 = gp.getDisplayName(id, GlobalizationPreferences.ID_CURRENCY); 1115 gp.setLocales(locales1); 1116 name2 = gp.getDisplayName(id, GlobalizationPreferences.ID_CURRENCY); 1117 gp.setLocales(locales2); 1118 name3 = gp.getDisplayName(id, GlobalizationPreferences.ID_CURRENCY); 1119 1120 logln("Currency[USD]: " + name1 + " | " + name2 + " | " + name3); 1121 if (name1.equals(name2) || !name2.equals(name3)) { 1122 errln("FAIL: Currency ID"); 1123 } 1124 1125 // ID_TIMEZONE 1126 gp.reset(); 1127 id = "Europe/Paris"; 1128 name1 = gp.getDisplayName(id, GlobalizationPreferences.ID_TIMEZONE); 1129 gp.setLocales(locales1); 1130 name2 = gp.getDisplayName(id, GlobalizationPreferences.ID_TIMEZONE); 1131 gp.setLocales(locales2); 1132 name3 = gp.getDisplayName(id, GlobalizationPreferences.ID_TIMEZONE); 1133 1134 logln("Timezone[Europe/Paris]: " + name1 + " | " + name2 + " | " + name3); 1135 if (name1.equals(name2) || !name2.equals(name3)) { 1136 errln("FAIL: Timezone ID"); 1137 } 1138 1139 // Illegal ID 1140 gp.reset(); 1141 boolean illegalArg = false; 1142 try { 1143 name1 = gp.getDisplayName(id, -1); 1144 } catch (IllegalArgumentException iae) { 1145 logln("Illegal type -1"); 1146 illegalArg = true; 1147 } 1148 if (!illegalArg) { 1149 errln("FAIL: getDisplayName must throw IllegalArgumentException for type -1"); 1150 } 1151 1152 illegalArg = false; 1153 try { 1154 name1 = gp.getDisplayName(id, 100); 1155 } catch (IllegalArgumentException iae) { 1156 logln("Illegal type 100"); 1157 illegalArg = true; 1158 } 1159 if (!illegalArg) { 1160 errln("FAIL: getDisplayName must throw IllegalArgumentException for type 100"); 1161 } 1162 } 1163 TestDateFormat()1164 public void TestDateFormat() { 1165 GlobalizationPreferences gp = new GlobalizationPreferences(); 1166 1167 String pattern; 1168 DateFormat df; 1169 1170 // Set unsupported locale - ach 1171 logln("Set locale - ach"); 1172 gp.setLocale(new ULocale("ach")); 1173 1174 // Date - short 1175 df = gp.getDateFormat(GlobalizationPreferences.DF_SHORT, GlobalizationPreferences.DF_NONE); 1176 pattern = ((SimpleDateFormat)df).toPattern(); 1177 // root pattern must be used 1178 if (!pattern.equals("y-MM-dd")) { 1179 errln("FAIL: SHORT date pattern is " + pattern + " Expected: y-MM-dd"); 1180 } 1181 1182 // Set locale - fr, fr_CA, fr_FR 1183 ArrayList lcls = new ArrayList(3); 1184 lcls.add(new ULocale("fr")); 1185 lcls.add(new ULocale("fr_CA")); 1186 lcls.add(new ULocale("fr_FR")); 1187 logln("Set locales - fr, fr_CA, fr_FR"); 1188 gp.setLocales(lcls); 1189 // Date - short 1190 df = gp.getDateFormat(GlobalizationPreferences.DF_SHORT, GlobalizationPreferences.DF_NONE); 1191 pattern = ((SimpleDateFormat)df).toPattern(); 1192 // fr_CA pattern must be used 1193 if (!pattern.equals("yy-MM-dd")) { 1194 errln("FAIL: SHORT date pattern is " + pattern + " Expected: yy-MM-dd"); 1195 } 1196 1197 1198 // Set locale - en_GB 1199 logln("Set locale - en_GB"); 1200 gp.setLocale(new ULocale("en_GB")); 1201 1202 // Date - full 1203 df = gp.getDateFormat(GlobalizationPreferences.DF_FULL, GlobalizationPreferences.DF_NONE); 1204 pattern = ((SimpleDateFormat)df).toPattern(); 1205 if (!pattern.equals("EEEE, d MMMM y")) { 1206 errln("FAIL: FULL date pattern is " + pattern + " Expected: EEEE, d MMMM y"); 1207 } 1208 1209 // Date - long 1210 df = gp.getDateFormat(GlobalizationPreferences.DF_LONG, GlobalizationPreferences.DF_NONE); 1211 pattern = ((SimpleDateFormat)df).toPattern(); 1212 if (!pattern.equals("d MMMM y")) { 1213 errln("FAIL: LONG date pattern is " + pattern + " Expected: d MMMM y"); 1214 } 1215 1216 // Date - medium 1217 df = gp.getDateFormat(GlobalizationPreferences.DF_MEDIUM, GlobalizationPreferences.DF_NONE); 1218 pattern = ((SimpleDateFormat)df).toPattern(); 1219 if (!pattern.equals("d MMM y")) { 1220 errln("FAIL: MEDIUM date pattern is " + pattern + " Expected: d MMM y"); 1221 } 1222 1223 // Date - short 1224 df = gp.getDateFormat(GlobalizationPreferences.DF_SHORT, GlobalizationPreferences.DF_NONE); 1225 pattern = ((SimpleDateFormat)df).toPattern(); 1226 if (!pattern.equals("dd/MM/y")) { 1227 errln("FAIL: SHORT date pattern is " + pattern + " Expected: dd/MM/y"); 1228 } 1229 1230 // Time - full 1231 df = gp.getDateFormat(GlobalizationPreferences.DF_NONE, GlobalizationPreferences.DF_FULL); 1232 pattern = ((SimpleDateFormat)df).toPattern(); 1233 if (!pattern.equals("HH:mm:ss zzzz")) { 1234 errln("FAIL: FULL time pattern is " + pattern + " Expected: HH:mm:ss zzzz"); 1235 } 1236 1237 // Time - long 1238 df = gp.getDateFormat(GlobalizationPreferences.DF_NONE, GlobalizationPreferences.DF_LONG); 1239 pattern = ((SimpleDateFormat)df).toPattern(); 1240 if (!pattern.equals("HH:mm:ss z")) { 1241 errln("FAIL: LONG time pattern is " + pattern + " Expected: HH:mm:ss z"); 1242 } 1243 1244 // Time - medium 1245 df = gp.getDateFormat(GlobalizationPreferences.DF_NONE, GlobalizationPreferences.DF_MEDIUM); 1246 pattern = ((SimpleDateFormat)df).toPattern(); 1247 if (!pattern.equals("HH:mm:ss")) { 1248 errln("FAIL: MEDIUM time pattern is " + pattern + " Expected: HH:mm:ss"); 1249 } 1250 1251 // Time - short 1252 df = gp.getDateFormat(GlobalizationPreferences.DF_NONE, GlobalizationPreferences.DF_SHORT); 1253 pattern = ((SimpleDateFormat)df).toPattern(); 1254 if (!pattern.equals("HH:mm")) { 1255 errln("FAIL: SHORT time pattern is " + pattern + " Expected: HH:mm"); 1256 } 1257 1258 // Date/Time - full 1259 df = gp.getDateFormat(GlobalizationPreferences.DF_FULL, GlobalizationPreferences.DF_FULL); 1260 pattern = ((SimpleDateFormat)df).toPattern(); 1261 if (!pattern.equals("EEEE, d MMMM y 'at' HH:mm:ss zzzz")) { 1262 errln("FAIL: FULL date/time pattern is " + pattern + " Expected: EEEE, d MMMM y 'at' HH:mm:ss zzzz"); 1263 } 1264 1265 // Invalid style 1266 boolean illegalArg = false; 1267 try { 1268 df = gp.getDateFormat(-1, GlobalizationPreferences.DF_NONE); 1269 } catch (IllegalArgumentException iae) { 1270 logln("Illegal date style -1"); 1271 illegalArg = true; 1272 } 1273 if (!illegalArg) { 1274 errln("FAIL: getDateFormat() must throw IllegalArgumentException for dateStyle -1"); 1275 } 1276 1277 illegalArg = false; 1278 try { 1279 df = gp.getDateFormat(GlobalizationPreferences.DF_NONE, GlobalizationPreferences.DF_NONE); 1280 } catch (IllegalArgumentException iae) { 1281 logln("Illegal style - dateStyle:DF_NONE / timeStyle:DF_NONE"); 1282 illegalArg = true; 1283 } 1284 if (!illegalArg) { 1285 errln("FAIL: getDateFormat() must throw IllegalArgumentException for dateStyle:DF_NONE/timeStyle:DF_NONE"); 1286 } 1287 1288 // Set explicit time zone 1289 logln("Set timezone - America/Sao_Paulo"); 1290 TimeZone tz = TimeZone.getTimeZone("America/Sao_Paulo"); 1291 gp.setTimeZone(tz); 1292 df = gp.getDateFormat(GlobalizationPreferences.DF_LONG, GlobalizationPreferences.DF_MEDIUM); 1293 String tzid = df.getTimeZone().getID(); 1294 if (!tzid.equals("America/Sao_Paulo")) { 1295 errln("FAIL: The DateFormat instance must use timezone America/Sao_Paulo"); 1296 } 1297 1298 // Set explicit calendar 1299 logln("Set calendar - japanese"); 1300 Calendar jcal = new JapaneseCalendar(); 1301 jcal.setTimeZone(TimeZone.getTimeZone("Asia/Tokyo")); 1302 gp.setCalendar(jcal); 1303 df = gp.getDateFormat(GlobalizationPreferences.DF_SHORT, GlobalizationPreferences.DF_SHORT); 1304 Calendar dfCal = df.getCalendar(); 1305 if (!(dfCal instanceof JapaneseCalendar)) { 1306 errln("FAIL: The DateFormat instance must use Japanese calendar"); 1307 } 1308 // TimeZone must be still America/Sao_Paulo 1309 tzid = df.getTimeZone().getID(); 1310 if (!tzid.equals("America/Sao_Paulo")) { 1311 errln("FAIL: The DateFormat instance must use timezone America/Sao_Paulo"); 1312 } 1313 1314 // Set explicit DateFormat 1315 logln("Set explicit date format - full date"); 1316 DateFormat customFD = DateFormat.getDateInstance(new IslamicCalendar(), DateFormat.FULL, new ULocale("ar_SA")); 1317 customFD.setTimeZone(TimeZone.getTimeZone("Asia/Riyadh")); 1318 gp.setDateFormat(GlobalizationPreferences.DF_FULL, GlobalizationPreferences.DF_NONE, customFD); 1319 df = gp.getDateFormat(GlobalizationPreferences.DF_FULL, GlobalizationPreferences.DF_NONE); 1320 dfCal = df.getCalendar(); 1321 if (!(dfCal instanceof IslamicCalendar)) { 1322 errln("FAIL: The DateFormat instance must use Islamic calendar"); 1323 } 1324 // TimeZone in the custom DateFormat is overridden by GP's timezone setting 1325 tzid = df.getTimeZone().getID(); 1326 if (!tzid.equals("America/Sao_Paulo")) { 1327 errln("FAIL: The DateFormat instance must use timezone America/Sao_Paulo"); 1328 } 1329 1330 // Freeze 1331 logln("Freeze this object"); 1332 gp.freeze(); 1333 DateFormat customLD = DateFormat.getDateInstance(new BuddhistCalendar(), DateFormat.LONG, new ULocale("th")); 1334 customLD.setTimeZone(TimeZone.getTimeZone("Asia/Bangkok")); 1335 boolean isFrozen = false; 1336 try { 1337 gp.setDateFormat(GlobalizationPreferences.DF_LONG, GlobalizationPreferences.DF_NONE, customLD); 1338 } catch (UnsupportedOperationException uoe) { 1339 logln("setDateFormat is blocked"); 1340 isFrozen = true; 1341 } 1342 if (!isFrozen) { 1343 errln("FAIL: setDateFormat must be blocked after frozen"); 1344 } 1345 1346 // Modifiable clone 1347 logln("cloneAsThawed"); 1348 GlobalizationPreferences gp1 = (GlobalizationPreferences)gp.cloneAsThawed(); 1349 gp1.setDateFormat(GlobalizationPreferences.DF_LONG, GlobalizationPreferences.DF_NONE, customLD); 1350 1351 df = gp1.getDateFormat(GlobalizationPreferences.DF_SHORT, GlobalizationPreferences.DF_SHORT); 1352 dfCal = df.getCalendar(); 1353 if (!(dfCal instanceof JapaneseCalendar)) { 1354 errln("FAIL: The DateFormat instance must use Japanese calendar"); 1355 } 1356 // TimeZone must be still America/Sao_Paulo 1357 tzid = df.getTimeZone().getID(); 1358 if (!tzid.equals("America/Sao_Paulo")) { 1359 errln("FAIL: The DateFormat instance must use timezone America/Sao_Paulo"); 1360 } 1361 1362 df = gp1.getDateFormat(GlobalizationPreferences.DF_LONG, GlobalizationPreferences.DF_NONE); 1363 dfCal = df.getCalendar(); 1364 if (!(dfCal instanceof BuddhistCalendar)) { 1365 errln("FAIL: The DateFormat instance must use Buddhist calendar"); 1366 } 1367 // TimeZone must be still America/Sao_Paulo 1368 tzid = df.getTimeZone().getID(); 1369 if (!tzid.equals("America/Sao_Paulo")) { 1370 errln("FAIL: The DateFormat instance must use timezone America/Sao_Paulo"); 1371 } 1372 1373 } 1374 TestNumberFormat()1375 public void TestNumberFormat() { 1376 GlobalizationPreferences gp = new GlobalizationPreferences(); 1377 1378 NumberFormat nf; 1379 String numStr; 1380 double num = 123456.789; 1381 1382 // Set unsupported locale with supported territory ang_KR 1383 logln("Set locale - ang_KR"); 1384 gp.setLocale(new ULocale("ang_KR")); 1385 nf = gp.getNumberFormat(GlobalizationPreferences.NF_CURRENCY); 1386 numStr = nf.format(num); 1387 if (!numStr.equals("\u20a9\u00a0123,457")) { 1388 errln("FAIL: Number string is " + numStr + " Expected: \u20a9\u00a0123,457"); 1389 } 1390 1391 // Set locale - de_DE 1392 logln("Set locale - de_DE"); 1393 gp.setLocale(new ULocale("de_DE")); 1394 1395 // NF_NUMBER 1396 logln("NUMBER type"); 1397 nf = gp.getNumberFormat(GlobalizationPreferences.NF_NUMBER); 1398 numStr = nf.format(num); 1399 if (!numStr.equals("123.456,789")) { 1400 errln("FAIL: Number string is " + numStr + " Expected: 123.456,789"); 1401 } 1402 1403 // NF_CURRENCY 1404 logln("CURRENCY type"); 1405 nf = gp.getNumberFormat(GlobalizationPreferences.NF_CURRENCY); 1406 numStr = nf.format(num); 1407 if (!numStr.equals("123.456,79\u00a0\u20AC")) { 1408 errln("FAIL: Number string is " + numStr + " Expected: 123.456,79\u00a0\u20AC"); 1409 } 1410 1411 // NF_PERCENT 1412 logln("PERCENT type"); 1413 nf = gp.getNumberFormat(GlobalizationPreferences.NF_PERCENT); 1414 numStr = nf.format(num); 1415 if (!numStr.equals("12.345.679\u00a0%")) { 1416 errln("FAIL: Number string is " + numStr + " Expected: 12.345.679\u00a0%"); 1417 } 1418 1419 // NF_SCIENTIFIC 1420 logln("SCIENTIFIC type"); 1421 nf = gp.getNumberFormat(GlobalizationPreferences.NF_SCIENTIFIC); 1422 numStr = nf.format(num); 1423 if (!numStr.equals("1,23456789E5")) { 1424 errln("FAIL: Number string is " + numStr + " Expected: 1,23456789E5"); 1425 } 1426 1427 // NF_INTEGER 1428 logln("INTEGER type"); 1429 nf = gp.getNumberFormat(GlobalizationPreferences.NF_INTEGER); 1430 numStr = nf.format(num); 1431 if (!numStr.equals("123.457")) { 1432 errln("FAIL: Number string is " + numStr + " Expected: 123.457"); 1433 } 1434 1435 // Invalid number type 1436 logln("INVALID type"); 1437 boolean illegalArg = false; 1438 try { 1439 nf = gp.getNumberFormat(100); 1440 } catch (IllegalArgumentException iae) { 1441 logln("Illegal number format type 100"); 1442 illegalArg = true; 1443 } 1444 if (!illegalArg) { 1445 errln("FAIL: getNumberFormat must throw IllegalArgumentException for type 100"); 1446 } 1447 illegalArg = false; 1448 try { 1449 nf = gp.getNumberFormat(-1); 1450 } catch (IllegalArgumentException iae) { 1451 logln("Illegal number format type -1"); 1452 illegalArg = true; 1453 } 1454 if (!illegalArg) { 1455 errln("FAIL: getNumberFormat must throw IllegalArgumentException for type -1"); 1456 } 1457 1458 // Set explicit territory 1459 logln("Set territory - US"); 1460 gp.setTerritory("US"); 1461 nf = gp.getNumberFormat(GlobalizationPreferences.NF_CURRENCY); 1462 numStr = nf.format(num); 1463 if (!numStr.equals("123.456,79\u00a0$")) { 1464 errln("FAIL: Number string is " + numStr + " Expected: 123.456,79\u00a0$"); 1465 } 1466 1467 // Set explicit currency 1468 logln("Set currency - GBP"); 1469 gp.setCurrency(Currency.getInstance("GBP")); 1470 nf = gp.getNumberFormat(GlobalizationPreferences.NF_CURRENCY); 1471 numStr = nf.format(num); 1472 if (!numStr.equals("123.456,79\u00a0\u00A3")) { 1473 errln("FAIL: Number string is " + numStr + " Expected: 123.456,79\u00a0\u00A3"); 1474 } 1475 1476 // Set exliplicit NumberFormat 1477 logln("Set explicit NumberFormat objects"); 1478 NumberFormat customNum = NumberFormat.getNumberInstance(new ULocale("he_IL")); 1479 gp.setNumberFormat(GlobalizationPreferences.NF_NUMBER, customNum); 1480 NumberFormat customCur = NumberFormat.getCurrencyInstance(new ULocale("zh_CN")); 1481 gp.setNumberFormat(GlobalizationPreferences.NF_CURRENCY, customCur); 1482 NumberFormat customPct = NumberFormat.getPercentInstance(new ULocale("el_GR")); 1483 gp.setNumberFormat(GlobalizationPreferences.NF_PERCENT, customPct); 1484 NumberFormat customSci = NumberFormat.getScientificInstance(new ULocale("ru_RU")); 1485 gp.setNumberFormat(GlobalizationPreferences.NF_SCIENTIFIC, customSci); 1486 NumberFormat customInt = NumberFormat.getIntegerInstance(new ULocale("pt_PT")); 1487 gp.setNumberFormat(GlobalizationPreferences.NF_INTEGER, customInt); 1488 1489 1490 nf = gp.getNumberFormat(GlobalizationPreferences.NF_NUMBER); 1491 if (!nf.getLocale(ULocale.VALID_LOCALE).toString().equals("he_IL")) { 1492 errln("FAIL: The NumberFormat instance must use locale he_IL"); 1493 } 1494 nf = gp.getNumberFormat(GlobalizationPreferences.NF_CURRENCY); 1495 if (!nf.getLocale(ULocale.VALID_LOCALE).toString().equals("zh_CN")) { 1496 errln("FAIL: The NumberFormat instance must use locale zh_CN"); 1497 } 1498 nf = gp.getNumberFormat(GlobalizationPreferences.NF_PERCENT); 1499 if (!nf.getLocale(ULocale.VALID_LOCALE).toString().equals("el_GR")) { 1500 errln("FAIL: The NumberFormat instance must use locale el_GR"); 1501 } 1502 nf = gp.getNumberFormat(GlobalizationPreferences.NF_SCIENTIFIC); 1503 if (!nf.getLocale(ULocale.VALID_LOCALE).toString().equals("ru_RU")) { 1504 errln("FAIL: The NumberFormat instance must use locale ru_RU"); 1505 } 1506 nf = gp.getNumberFormat(GlobalizationPreferences.NF_INTEGER); 1507 if (!nf.getLocale(ULocale.VALID_LOCALE).toString().equals("pt_PT")) { 1508 errln("FAIL: The NumberFormat instance must use locale pt_PT"); 1509 } 1510 1511 NumberFormat customNum1 = NumberFormat.getNumberInstance(new ULocale("hi_IN")); 1512 1513 // Freeze 1514 logln("Freeze this object"); 1515 boolean isFrozen = false; 1516 gp.freeze(); 1517 try { 1518 gp.setNumberFormat(GlobalizationPreferences.NF_NUMBER, customNum1); 1519 } catch (UnsupportedOperationException uoe) { 1520 logln("setNumberFormat is blocked"); 1521 isFrozen = true; 1522 } 1523 if (!isFrozen) { 1524 errln("FAIL: setNumberFormat must be blocked after frozen"); 1525 } 1526 1527 // Create a modifiable clone 1528 GlobalizationPreferences gp1 = (GlobalizationPreferences)gp.cloneAsThawed(); 1529 1530 // Number type format's locale is still he_IL 1531 nf = gp1.getNumberFormat(GlobalizationPreferences.NF_NUMBER); 1532 if (!nf.getLocale(ULocale.VALID_LOCALE).toString().equals("he_IL")) { 1533 errln("FAIL: The NumberFormat instance must use locale he_IL"); 1534 } 1535 1536 logln("Set custom number format using locale hi_IN"); 1537 gp1.setNumberFormat(GlobalizationPreferences.NF_NUMBER, customNum1); 1538 nf = gp1.getNumberFormat(GlobalizationPreferences.NF_NUMBER); 1539 if (!nf.getLocale(ULocale.VALID_LOCALE).toString().equals("hi_IN")) { 1540 errln("FAIL: The NumberFormat instance must use locale hi_IN"); 1541 } 1542 } 1543 1544 /* 1545 * JB#5380 GlobalizationPreferences#getCalendar() should return a Calendar object 1546 * initialized with the current time 1547 */ TestJB5380()1548 public void TestJB5380() { 1549 GlobalizationPreferences gp = new GlobalizationPreferences(); 1550 GregorianCalendar gcal = new GregorianCalendar(); 1551 1552 // set way old date 1553 gcal.set(Calendar.YEAR, 1950); 1554 1555 // set calendar to GP 1556 gp.setCalendar(gcal); 1557 1558 Calendar cal = gp.getCalendar(); 1559 // Calendar instance returned from GP should be initialized 1560 // by the current time 1561 long timeDiff = System.currentTimeMillis() - cal.getTimeInMillis(); 1562 if (Math.abs(timeDiff) > 1000) { 1563 // if difference is more than 1 second.. 1564 errln("FAIL: The Calendar was not initialized by current time - difference:" + timeDiff); 1565 } 1566 } 1567 } 1568