LocaleTest.java revision a61d31e2ee0ff49bf4d1fa7150f605bae836728b
1/* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18package tests.api.java.util; 19 20import dalvik.annotation.KnownFailure; 21import dalvik.annotation.TestTargetNew; 22import dalvik.annotation.TestTargets; 23import dalvik.annotation.TestLevel; 24import dalvik.annotation.TestTargetClass; 25import dalvik.annotation.AndroidOnly; 26 27import java.security.Permission; 28import java.util.Arrays; 29import java.util.HashSet; 30import java.util.List; 31import java.util.Locale; 32import java.util.MissingResourceException; 33import java.util.Set; 34 35@TestTargetClass(Locale.class) 36public class LocaleTest extends junit.framework.TestCase { 37 38 Locale testLocale; 39 40 Locale l; 41 42 Locale defaultLocale; 43 44 /** 45 * @tests java.util.Locale#Locale(java.lang.String, java.lang.String) 46 */ 47 @TestTargetNew( 48 level = TestLevel.COMPLETE, 49 notes = "", 50 method = "Locale", 51 args = {java.lang.String.class} 52 ) 53 public void test_ConstructorLjava_lang_String() { 54 // Test for method java.util.Locale(java.lang.String) 55 Locale x = new Locale("xx"); 56 assertTrue("Failed to create Locale", x.getVariant().equals("")); 57 58 try { 59 new Locale(null); 60 fail("NullPointerException expected"); 61 } catch (NullPointerException e) { 62 //expected 63 } 64 } 65 66 /** 67 * @tests java.util.Locale#Locale(java.lang.String, java.lang.String) 68 */ 69 @TestTargetNew( 70 level = TestLevel.COMPLETE, 71 notes = "", 72 method = "Locale", 73 args = {java.lang.String.class, java.lang.String.class} 74 ) 75 public void test_ConstructorLjava_lang_StringLjava_lang_String() { 76 // Test for method java.util.Locale(java.lang.String, java.lang.String) 77 Locale x = new Locale("xx", "CV"); 78 assertTrue("Failed to create Locale", x.getCountry().equals("CV") 79 && x.getVariant().equals("")); 80 81 try { 82 new Locale("xx", null); 83 fail("NullPointerException expected"); 84 } catch (NullPointerException e) { 85 //expected 86 } 87 88 try { 89 new Locale(null, "CV"); 90 fail("NullPointerException expected"); 91 } catch (NullPointerException e) { 92 //expected 93 } 94 } 95 96 /** 97 * @tests java.util.Locale#Locale(java.lang.String, java.lang.String, 98 * java.lang.String) 99 */ 100 @TestTargetNew( 101 level = TestLevel.COMPLETE, 102 notes = "", 103 method = "Locale", 104 args = {java.lang.String.class, java.lang.String.class, java.lang.String.class} 105 ) 106 public void test_ConstructorLjava_lang_StringLjava_lang_StringLjava_lang_String() { 107 // Test for method java.util.Locale(java.lang.String, java.lang.String, 108 // java.lang.String) 109 Locale x = new Locale("xx", "CV", "ZZ"); 110 assertTrue("Failed to create Locale", x.getLanguage().equals("xx") 111 && (x.getCountry().equals("CV") && x.getVariant().equals("ZZ"))); 112 try { 113 new Locale(null, "CV", "ZZ"); 114 fail("expected NullPointerException with 1st parameter == null"); 115 } catch(NullPointerException e) { 116 } 117 118 try { 119 new Locale("xx", null, "ZZ"); 120 fail("expected NullPointerException with 2nd parameter == null"); 121 } catch(NullPointerException e) { 122 } 123 124 try { 125 new Locale("xx", "CV", null); 126 fail("expected NullPointerException with 3rd parameter == null"); 127 } catch(NullPointerException e) { 128 } 129 } 130 131 /** 132 * @tests java.util.Locale#clone() 133 */ 134 @TestTargetNew( 135 level = TestLevel.COMPLETE, 136 notes = "", 137 method = "clone", 138 args = {} 139 ) 140 public void test_clone() { 141 // Test for method java.lang.Object java.util.Locale.clone() 142 assertTrue("Clone failed", l.clone().equals(l)); 143 } 144 145 /** 146 * @tests java.util.Locale#equals(java.lang.Object) 147 */ 148 @TestTargetNew( 149 level = TestLevel.COMPLETE, 150 notes = "", 151 method = "equals", 152 args = {java.lang.Object.class} 153 ) 154 public void test_equalsLjava_lang_Object() { 155 // Test for method boolean java.util.Locale.equals(java.lang.Object) 156 Locale l2 = new Locale("en", "CA", "WIN32"); 157 assertTrue("Same object returned false", testLocale.equals(testLocale)); 158 assertTrue("Same values returned false", testLocale.equals(l2)); 159 assertTrue("Different locales returned true", !testLocale.equals(l)); 160 161 } 162 163 /** 164 * @tests java.util.Locale#getAvailableLocales() 165 */ 166 @TestTargetNew( 167 level = TestLevel.COMPLETE, 168 notes = "", 169 method = "getAvailableLocales", 170 args = {} 171 ) 172 public void test_getAvailableLocales() { 173// BEGIN android-changed 174 // Test for method java.util.Locale [] 175 // java.util.Locale.getAvailableLocales() 176 // Assumes there will generally be about 10+ available locales... 177 // even in minimal configurations for android 178 try { 179 Locale[] locales = Locale.getAvailableLocales(); 180 assertTrue("Wrong number of locales: " + locales.length, locales.length > 10); 181 // regression test for HARMONY-1514 182 // HashSet can filter duplicate locales 183 Set<Locale> localesSet = new HashSet<Locale>(Arrays.asList(locales)); 184 assertEquals(localesSet.size(), locales.length); 185 } catch (Exception e) { 186 fail("Exception during test : " + e.getMessage()); 187 } 188// END android-changed 189 } 190 191 /** 192 * @tests java.util.Locale#getCountry() 193 */ 194 @TestTargetNew( 195 level = TestLevel.COMPLETE, 196 notes = "", 197 method = "getCountry", 198 args = {} 199 ) 200 public void test_getCountry() { 201 // Test for method java.lang.String java.util.Locale.getCountry() 202 assertTrue("Returned incorrect country: " + testLocale.getCountry(), 203 testLocale.getCountry().equals("CA")); 204 } 205 206 /** 207 * @tests java.util.Locale#getDefault() 208 */ 209 @TestTargetNew( 210 level = TestLevel.COMPLETE, 211 notes = "", 212 method = "getDefault", 213 args = {} 214 ) 215 public void test_getDefault() { 216 // Test for method java.util.Locale java.util.Locale.getDefault() 217 assertTrue("returns copy", Locale.getDefault() == Locale.getDefault()); 218 Locale org = Locale.getDefault(); 219 Locale.setDefault(l); 220 Locale x = Locale.getDefault(); 221 Locale.setDefault(org); 222 assertEquals("Failed to get locale", "fr_CA_WIN32", x.toString()); 223 } 224 225 /** 226 * @tests java.util.Locale#getDisplayCountry() 227 */ 228 @TestTargetNew( 229 level = TestLevel.COMPLETE, 230 notes = "", 231 method = "getDisplayCountry", 232 args = {} 233 ) 234 @AndroidOnly("ICU has different display name for countries") 235 public void test_getDisplayCountry() { 236 // Test for method java.lang.String java.util.Locale.getDisplayCountry() 237 assertTrue("Returned incorrect country: " 238 + testLocale.getDisplayCountry(), testLocale 239 .getDisplayCountry().equals("Canada")); 240 241 // Regression for Harmony-1146 242 Locale l_countryCD = new Locale("", "CD"); //$NON-NLS-1$ //$NON-NLS-2$ 243// BEGIN android-changed 244// ICU has different display name for countries 245// assertEquals("The Democratic Republic Of Congo", //$NON-NLS-1$ 246// l_countryCD.getDisplayCountry()); 247 assertEquals("Congo - Kinshasa", //$NON-NLS-1$ 248 l_countryCD.getDisplayCountry()); 249// END android-changed 250 } 251 252 /** 253 * @tests java.util.Locale#getDisplayCountry(java.util.Locale) 254 */ 255 @TestTargetNew( 256 level = TestLevel.COMPLETE, 257 notes = "", 258 method = "getDisplayCountry", 259 args = {java.util.Locale.class} 260 ) 261 public void test_getDisplayCountryLjava_util_Locale() { 262 // Test for method java.lang.String 263 // java.util.Locale.getDisplayCountry(java.util.Locale) 264 assertEquals("Returned incorrect country", "Italie", Locale.ITALY 265 .getDisplayCountry(l)); 266 } 267 268 /** 269 * @tests java.util.Locale#getDisplayLanguage() 270 */ 271 @TestTargetNew( 272 level = TestLevel.COMPLETE, 273 notes = "", 274 method = "getDisplayLanguage", 275 args = {} 276 ) 277 public void test_getDisplayLanguage() { 278 // Test for method java.lang.String 279 // java.util.Locale.getDisplayLanguage() 280 assertTrue("Returned incorrect language: " 281 + testLocale.getDisplayLanguage(), testLocale 282 .getDisplayLanguage().equals("English")); 283 284 // Regression for Harmony-1146 285 Locale l_languageAE = new Locale("ae", ""); //$NON-NLS-1$ //$NON-NLS-2$ 286 assertEquals("Avestan", l_languageAE.getDisplayLanguage()); //$NON-NLS-1$ 287 } 288 289 /** 290 * @tests java.util.Locale#getDisplayLanguage(java.util.Locale) 291 */ 292 @TestTargetNew( 293 level = TestLevel.COMPLETE, 294 notes = "", 295 method = "getDisplayLanguage", 296 args = {java.util.Locale.class} 297 ) 298 public void test_getDisplayLanguageLjava_util_Locale() { 299 // Test for method java.lang.String 300 // java.util.Locale.getDisplayLanguage(java.util.Locale) 301 assertTrue("Returned incorrect language: " 302 + testLocale.getDisplayLanguage(l), testLocale 303 .getDisplayLanguage(l).equals("anglais")); 304 } 305 306 /** 307 * @tests java.util.Locale#getDisplayName() 308 */ 309 @TestTargetNew( 310 level = TestLevel.COMPLETE, 311 notes = "", 312 method = "getDisplayName", 313 args = {} 314 ) 315 public void test_getDisplayName() { 316 // Test for method java.lang.String java.util.Locale.getDisplayName() 317 assertTrue("Returned incorrect name: " + testLocale.getDisplayName(), 318 testLocale.getDisplayName().equals("English (Canada,WIN32)")); 319 } 320 321 /** 322 * @tests java.util.Locale#getDisplayName(java.util.Locale) 323 */ 324 @TestTargetNew( 325 level = TestLevel.COMPLETE, 326 notes = "", 327 method = "getDisplayName", 328 args = {java.util.Locale.class} 329 ) 330 public void test_getDisplayNameLjava_util_Locale() { 331 // Test for method java.lang.String 332 // java.util.Locale.getDisplayName(java.util.Locale) 333 assertTrue("Returned incorrect name: " + testLocale.getDisplayName(l), 334 testLocale.getDisplayName(l).equals("anglais (Canada,WIN32)")); 335 } 336 337 /** 338 * @tests java.util.Locale#getDisplayVariant() 339 */ 340 @TestTargetNew( 341 level = TestLevel.COMPLETE, 342 notes = "", 343 method = "getDisplayVariant", 344 args = {} 345 ) 346 public void test_getDisplayVariant() { 347 // Test for method java.lang.String java.util.Locale.getDisplayVariant() 348 assertTrue("Returned incorrect variant: " 349 + testLocale.getDisplayVariant(), testLocale 350 .getDisplayVariant().equals("WIN32")); 351 } 352 353 /** 354 * @tests java.util.Locale#getDisplayVariant(java.util.Locale) 355 */ 356 @TestTargetNew( 357 level = TestLevel.COMPLETE, 358 notes = "", 359 method = "getDisplayVariant", 360 args = {java.util.Locale.class} 361 ) 362 public void test_getDisplayVariantLjava_util_Locale() { 363 // Test for method java.lang.String 364 // java.util.Locale.getDisplayVariant(java.util.Locale) 365 assertTrue("Returned incorrect variant: " 366 + testLocale.getDisplayVariant(l), testLocale 367 .getDisplayVariant(l).equals("WIN32")); 368 } 369 370 /** 371 * @tests java.util.Locale#getISO3Country() 372 */ 373 @TestTargetNew( 374 level = TestLevel.COMPLETE, 375 notes = "", 376 method = "getISO3Country", 377 args = {} 378 ) 379 public void test_getISO3Country() { 380 // Test for method java.lang.String java.util.Locale.getISO3Country() 381 assertTrue("Returned incorrect ISO3 country: " 382 + testLocale.getISO3Country(), testLocale.getISO3Country() 383 .equals("CAN")); 384 385 Locale l = new Locale("", "CD"); 386 assertEquals("COD", l.getISO3Country()); 387 388 Locale x = new Locale("xx", "C"); 389 try { 390 x.getISO3Country(); 391 } catch (MissingResourceException e) { 392 //expected 393 } 394 } 395 396 /** 397 * @tests java.util.Locale#getISO3Language() 398 */ 399 @TestTargetNew( 400 level = TestLevel.COMPLETE, 401 notes = "", 402 method = "getISO3Language", 403 args = {} 404 ) 405 public void test_getISO3Language() { 406 // Test for method java.lang.String java.util.Locale.getISO3Language() 407 assertTrue("Returned incorrect ISO3 language: " 408 + testLocale.getISO3Language(), testLocale.getISO3Language() 409 .equals("eng")); 410 411 Locale l = new Locale("ae"); 412 assertEquals("ave", l.getISO3Language()); 413 414 // Regression for Harmony-1146 415 Locale l_CountryCS = new Locale("", "CS"); //$NON-NLS-1$ //$NON-NLS-2$ 416 assertEquals("SCG", l_CountryCS.getISO3Country()); //$NON-NLS-1$ 417 418 // Regression for Harmony-1129 419 l = new Locale("ak", ""); //$NON-NLS-1$ //$NON-NLS-2$ 420 assertEquals("aka", l.getISO3Language()); //$NON-NLS-1$ 421 422 Locale x = new Locale("xx", "C"); 423 try { 424 x.getISO3Language(); 425 } catch (MissingResourceException e) { 426 //expected 427 } 428 } 429 430 /** 431 * @tests java.util.Locale#getISOCountries() 432 */ 433 @TestTargetNew( 434 level = TestLevel.COMPLETE, 435 notes = "", 436 method = "getISOCountries", 437 args = {} 438 ) 439 public void test_getISOCountries() { 440 // Test for method java.lang.String [] 441 // java.util.Locale.getISOCountries() 442 // Assumes all countries are 2 digits, and that there will always be 443 // 230 countries on the list... 444 String[] isoCountries = Locale.getISOCountries(); 445 int length = isoCountries.length; 446 int familiarCount = 0; 447 for (int i = 0; i < length; i++) { 448 if (isoCountries[i].length() != 2) { 449 fail("Wrong format for ISOCountries."); 450 } 451 if (isoCountries[i].equals("CA") || isoCountries[i].equals("BB") 452 || isoCountries[i].equals("US") 453 || isoCountries[i].equals("KR")) 454 familiarCount++; 455 } 456 assertTrue("ISOCountries missing.", familiarCount == 4 && length > 230); 457 } 458 459 /** 460 * @tests java.util.Locale#getISOLanguages() 461 */ 462 @TestTargetNew( 463 level = TestLevel.COMPLETE, 464 notes = "", 465 method = "getISOLanguages", 466 args = {} 467 ) 468 public void test_getISOLanguages() { 469 // Test for method java.lang.String [] 470 // java.util.Locale.getISOLanguages() 471 // Assumes always at least 131 ISOlanguages... 472 String[] isoLang = Locale.getISOLanguages(); 473 int length = isoLang.length; 474 assertTrue("Random element in wrong format.", (isoLang[length / 2] 475 .length() == 2) 476 && isoLang[length / 2].toLowerCase() 477 .equals(isoLang[length / 2])); 478 assertTrue("Wrong number of ISOLanguages.", length > 130); 479 } 480 481 /** 482 * @tests java.util.Locale#getLanguage() 483 */ 484 @TestTargetNew( 485 level = TestLevel.COMPLETE, 486 notes = "", 487 method = "getLanguage", 488 args = {} 489 ) 490 public void test_getLanguage() { 491 // Test for method java.lang.String java.util.Locale.getLanguage() 492 assertTrue("Returned incorrect language: " + testLocale.getLanguage(), 493 testLocale.getLanguage().equals("en")); 494 } 495 496 /** 497 * @tests java.util.Locale#getVariant() 498 */ 499 @TestTargetNew( 500 level = TestLevel.COMPLETE, 501 notes = "", 502 method = "getVariant", 503 args = {} 504 ) 505 public void test_getVariant() { 506 // Test for method java.lang.String java.util.Locale.getVariant() 507 assertTrue("Returned incorrect variant: " + testLocale.getVariant(), 508 testLocale.getVariant().equals("WIN32")); 509 } 510 511 SecurityManager sm = new SecurityManager() { 512 final String forbidenPermissionName = "user.language"; 513 514 public void checkPermission(Permission perm) { 515 if (perm.getName().equals(forbidenPermissionName)) { 516 throw new SecurityException(); 517 } 518 } 519 }; 520 /** 521 * @tests java.util.Locale#setDefault(java.util.Locale) 522 */ 523 @TestTargetNew( 524 level = TestLevel.COMPLETE, 525 notes = "", 526 method = "setDefault", 527 args = {java.util.Locale.class} 528 ) 529 public void test_setDefaultLjava_util_Locale() { 530 // Test for method void java.util.Locale.setDefault(java.util.Locale) 531 532 Locale org = Locale.getDefault(); 533 Locale.setDefault(l); 534 Locale x = Locale.getDefault(); 535 Locale.setDefault(org); 536 assertEquals("Failed to set locale", "fr_CA_WIN32", x.toString()); 537 538 Locale.setDefault(new Locale("tr", "")); 539 String res1 = "\u0069".toUpperCase(); 540 String res2 = "\u0049".toLowerCase(); 541 Locale.setDefault(org); 542 assertEquals("Wrong toUppercase conversion", "\u0130", res1); 543 assertEquals("Wrong toLowercase conversion", "\u0131", res2); 544 545 try { 546 Locale.setDefault(null); 547 fail("NullPointerException expected"); 548 } catch (NullPointerException e) { 549 //expected 550 } 551 552 SecurityManager oldSm = System.getSecurityManager(); 553 System.setSecurityManager(sm); 554 try { 555 Locale.setDefault(Locale.CANADA); 556 fail("Should throw SecurityException"); 557 } catch (SecurityException e) { 558 // expected 559 } finally { 560 System.setSecurityManager(oldSm); 561 } 562 } 563 564 /** 565 * @tests java.util.Locale#toString() 566 */ 567 @TestTargetNew( 568 level = TestLevel.COMPLETE, 569 notes = "", 570 method = "toString", 571 args = {} 572 ) 573 public void test_toString() { 574 // Test for method java.lang.String java.util.Locale.toString() 575 assertEquals("Returned incorrect string representation", "en_CA_WIN32", testLocale 576 .toString()); 577 578 Locale l = new Locale("en", ""); 579 assertEquals("Wrong representation 1", "en", l.toString()); 580 l = new Locale("", "CA"); 581 assertEquals("Wrong representation 2", "_CA", l.toString()); 582 l = new Locale("", "CA", "var"); 583 assertEquals("Wrong representation 2.5", "_CA_var", l.toString()); 584 l = new Locale("en", "", "WIN"); 585 assertEquals("Wrong representation 4", "en__WIN", l.toString()); 586 l = new Locale("en", "CA"); 587 assertEquals("Wrong representation 6", "en_CA", l.toString()); 588 l = new Locale("en", "CA", "VAR"); 589 assertEquals("Wrong representation 7", "en_CA_VAR", l.toString()); 590 591 l = new Locale("", "", "var"); 592 assertEquals("Wrong representation 8", "", l.toString()); 593 594 } 595 596 @TestTargetNew( 597 level = TestLevel.COMPLETE, 598 notes = "", 599 method = "hashCode", 600 args = {} 601 ) 602 public void test_hashCode() { 603 Locale l1 = new Locale("en", "US"); 604 Locale l2 = new Locale("fr", "CA"); 605 606 assertTrue(l1.hashCode() != l2.hashCode()); 607 } 608 609// BEGIN android-removed 610// These locales are not part of the android reference impl 611// // Regression Test for HARMONY-2953 612// public void test_getISO() { 613// Locale locale = new Locale("an"); 614// assertEquals("arg", locale.getISO3Language()); 615// 616// locale = new Locale("PS"); 617// assertEquals("pus", locale.getISO3Language()); 618// 619// List<String> languages = Arrays.asList(Locale.getISOLanguages()); 620// assertTrue(languages.contains("ak")); 621// 622// List<String> countries = Arrays.asList(Locale.getISOCountries()); 623// assertTrue(countries.contains("CS")); 624// } 625// END android-removed 626 627 /** 628 * Sets up the fixture, for example, open a network connection. This method 629 * is called before a test is executed. 630 */ 631 protected void setUp() { 632 defaultLocale = Locale.getDefault(); 633 Locale.setDefault(Locale.US); 634 testLocale = new Locale("en", "CA", "WIN32"); 635 l = new Locale("fr", "CA", "WIN32"); 636 } 637 638 /** 639 * Tears down the fixture, for example, close a network connection. This 640 * method is called after a test is executed. 641 */ 642 protected void tearDown() { 643 Locale.setDefault(defaultLocale); 644 } 645} 646