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