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 org.apache.harmony.text.tests.java.text; 19 20import tests.support.Support_Locale; 21import dalvik.annotation.AndroidOnly; 22import dalvik.annotation.KnownFailure; 23import dalvik.annotation.TestLevel; 24import dalvik.annotation.TestTargetClass; 25import dalvik.annotation.TestTargetNew; 26 27import junit.framework.TestCase; 28 29import java.io.ByteArrayInputStream; 30import java.io.ByteArrayOutputStream; 31import java.io.ObjectInputStream; 32import java.io.ObjectOutputStream; 33import java.text.DecimalFormat; 34import java.text.DecimalFormatSymbols; 35import java.text.NumberFormat; 36import java.util.Currency; 37import java.util.Locale; 38 39@TestTargetClass(DecimalFormatSymbols.class) 40public class DecimalFormatSymbolsTest extends TestCase { 41 42 DecimalFormatSymbols dfs; 43 44 DecimalFormatSymbols dfsUS; 45 46 /** 47 * @tests java.text.DecimalFormatSymbols#DecimalFormatSymbols() Test of 48 * method java.text.DecimalFormatSymbols#DecimalFormatSymbols(). 49 */ 50 @TestTargetNew( 51 level = TestLevel.COMPLETE, 52 notes = "", 53 method = "DecimalFormatSymbols", 54 args = {} 55 ) 56 public void test_Constructor() { 57 // Test for method java.text.DecimalFormatSymbols() 58 try { 59 new DecimalFormatSymbols(); 60 } catch (Exception e) { 61 fail("Unexpected exception " + e.toString()); 62 } 63 } 64 65 /** 66 * @tests java.text.DecimalFormatSymbols#DecimalFormatSymbols(java.util.Locale) 67 */ 68 @TestTargetNew( 69 level = TestLevel.COMPLETE, 70 notes = "", 71 method = "DecimalFormatSymbols", 72 args = {java.util.Locale.class} 73 ) 74 public void test_ConstructorLjava_util_Locale() { 75 DecimalFormatSymbols dfs = new DecimalFormatSymbols(new Locale("en", 76 "us")); 77 assertEquals("Returned incorrect symbols", '%', dfs.getPercent()); 78 79 try { 80 new DecimalFormatSymbols(null); 81 fail("NullPointerException was not thrown."); 82 } catch(NullPointerException npe) { 83 //expected 84 } 85 } 86 87 /** 88 * @tests java.text.DecimalFormatSymbols#clone() Test of method 89 * java.text.DecimalFormatSymbols#clone(). Case 1: Compare of 90 * internal variables of cloned objects. Case 2: Compare of clones. 91 * Case 3: Change the content of the clone. 92 */ 93 @TestTargetNew( 94 level = TestLevel.COMPLETE, 95 notes = "", 96 method = "clone", 97 args = {} 98 ) 99 public void test_clone() { 100 try { 101 // case 1: Compare of internal variables of cloned objects 102 DecimalFormatSymbols fs = new DecimalFormatSymbols(Locale.US); 103 DecimalFormatSymbols fsc = (DecimalFormatSymbols) fs.clone(); 104 assertEquals(fs.getCurrency(), fsc.getCurrency()); 105 106 // case 2: Compare of clones 107 fs = new DecimalFormatSymbols(); 108 DecimalFormatSymbols fsc2 = (DecimalFormatSymbols) (fs.clone()); 109 // make sure the objects are equal 110 assertTrue("Object's clone isn't equal!", fs.equals(fsc2)); 111 112 // case 3: 113 // change the content of the clone and make sure it's not equal 114 // anymore 115 // verifies that it's data is now distinct from the original 116 fs.setNaN("not-a-number"); 117 assertTrue("Object's changed clone should not be equal!", !fs 118 .equals(fsc2)); 119 } catch (Exception e) { 120 fail("Unexpected exception " + e.toString()); 121 } 122 } 123 124 /** 125 * @tests java.text.DecimalFormatSymbols#equals(java.lang.Object) 126 */ 127 @TestTargetNew( 128 level = TestLevel.COMPLETE, 129 notes = "", 130 method = "equals", 131 args = {java.lang.Object.class} 132 ) 133 public void test_equalsLjava_lang_Object() { 134 assertTrue("Equal objects returned false", dfs.equals(dfs.clone())); 135 dfs.setDigit('B'); 136 assertTrue("Un-Equal objects returned true", !dfs 137 .equals(new DecimalFormatSymbols())); 138 139 } 140 141 /** 142 * @tests java.text.DecimalFormatSymbols#getCurrency() 143 */ 144 @TestTargetNew( 145 level = TestLevel.COMPLETE, 146 notes = "", 147 method = "getCurrency", 148 args = {} 149 ) 150 public void test_getCurrency() { 151 Locale csCzLocale = new Locale("cs", "CZ"); 152 Locale czLocale = new Locale("", "CZ"); 153 Locale csLocale = new Locale("cs", ""); 154 Locale deLocale = new Locale("de", "AT"); 155 Locale[] requiredLocales = {Locale.US, csCzLocale, czLocale, csLocale, deLocale}; 156 if (!Support_Locale.areLocalesAvailable(requiredLocales)) { 157 // locale dependent test, bug 1943269 158 return; 159 } 160 Currency currency = Currency.getInstance("USD"); 161 assertEquals("Returned incorrect currency", 162 dfsUS.getCurrency(), currency); 163 164 // use cs_CZ instead 165 //Currency currK = Currency.getInstance("KRW"); 166 Currency currC = Currency.getInstance("CZK"); 167 Currency currX = Currency.getInstance("XXX"); 168 Currency currE = Currency.getInstance("EUR"); 169 // Currency currF = Currency.getInstance("FRF"); 170 171 172 DecimalFormatSymbols dfs1 = new DecimalFormatSymbols(csCzLocale); 173 assertTrue("Test1: Returned incorrect currency", 174 dfs1.getCurrency() == currC); 175 assertEquals("Test1: Returned incorrect currencySymbol", "K\u010d", dfs1 176 .getCurrencySymbol()); 177 assertEquals("Test1: Returned incorrect intlCurrencySymbol", "CZK", 178 dfs1.getInternationalCurrencySymbol()); 179 180 181 dfs1 = new DecimalFormatSymbols(czLocale); 182 assertTrue("Test2: Returned incorrect currency", 183 dfs1.getCurrency() == currC); 184 // BEGIN android-changed 185 // ICU4.2.1, the currency symbol for CZK is changed from CZK to K\u010d in en.txt. 186 assertEquals("Test2: Returned incorrect currencySymbol", "K\u010d", dfs1 187 .getCurrencySymbol()); 188 // END android-changed 189 assertEquals("Test2: Returned incorrect intlCurrencySymbol", "CZK", 190 dfs1.getInternationalCurrencySymbol()); 191 192 dfs1 = new DecimalFormatSymbols(csLocale); 193 assertEquals("Test3: Returned incorrect currency", 194 currX, dfs1.getCurrency()); 195 assertEquals("Test3: Returned incorrect currencySymbol", "\u00a4", dfs1 196 .getCurrencySymbol()); 197 assertEquals("Test3: Returned incorrect intlCurrencySymbol", "XXX", 198 dfs1.getInternationalCurrencySymbol()); 199 200 dfs1 = new DecimalFormatSymbols(deLocale); 201 assertTrue("Test4: Returned incorrect currency", 202 dfs1.getCurrency() == currE); 203 assertEquals("Test4: Returned incorrect currencySymbol", "\u20ac", dfs1 204 .getCurrencySymbol()); 205 assertEquals("Test4: Returned incorrect intlCurrencySymbol", "EUR", 206 dfs1.getInternationalCurrencySymbol()); 207 208 // RI fails these tests since it doesn't have the PREEURO variant 209 // dfs1 = new DecimalFormatSymbols(new Locale("fr", "FR","PREEURO")); 210 // assertTrue("Test5: Returned incorrect currency", dfs1.getCurrency() 211 // == currF); 212 // assertTrue("Test5: Returned incorrect currencySymbol", 213 // dfs1.getCurrencySymbol().equals("F")); 214 // assertTrue("Test5: Returned incorrect intlCurrencySymbol", 215 // dfs1.getInternationalCurrencySymbol().equals("FRF")); 216 } 217 218 /** 219 * @tests java.text.DecimalFormatSymbols#getCurrencySymbol() 220 */ 221 @TestTargetNew( 222 level = TestLevel.COMPLETE, 223 notes = "", 224 method = "getCurrencySymbol", 225 args = {} 226 ) 227 public void test_getCurrencySymbol() { 228 assertEquals("Returned incorrect currencySymbol", "$", dfsUS 229 .getCurrencySymbol()); 230 } 231 232 /** 233 * @tests java.text.DecimalFormatSymbols#getDecimalSeparator() 234 */ 235 @TestTargetNew( 236 level = TestLevel.COMPLETE, 237 notes = "", 238 method = "getDecimalSeparator", 239 args = {} 240 ) 241 public void test_getDecimalSeparator() { 242 dfs.setDecimalSeparator('*'); 243 assertEquals("Returned incorrect DecimalSeparator symbol", '*', dfs 244 .getDecimalSeparator()); 245 } 246 247 /** 248 * @tests java.text.DecimalFormatSymbols#getDigit() 249 */ 250 @TestTargetNew( 251 level = TestLevel.COMPLETE, 252 notes = "", 253 method = "getDigit", 254 args = {} 255 ) 256 public void test_getDigit() { 257 dfs.setDigit('*'); 258 assertEquals("Returned incorrect Digit symbol", '*', dfs.getDigit()); 259 } 260 261 /** 262 * @tests java.text.DecimalFormatSymbols#getGroupingSeparator() 263 */ 264 @TestTargetNew( 265 level = TestLevel.COMPLETE, 266 notes = "", 267 method = "getGroupingSeparator", 268 args = {} 269 ) 270 public void test_getGroupingSeparator() { 271 dfs.setGroupingSeparator('*'); 272 assertEquals("Returned incorrect GroupingSeparator symbol", '*', dfs 273 .getGroupingSeparator()); 274 } 275 276 /** 277 * @tests java.text.DecimalFormatSymbols#getInfinity() 278 */ 279 @TestTargetNew( 280 level = TestLevel.COMPLETE, 281 notes = "", 282 method = "getInfinity", 283 args = {} 284 ) 285 public void test_getInfinity() { 286 dfs.setInfinity("&"); 287 assertTrue("Returned incorrect Infinity symbol", 288 dfs.getInfinity() == "&"); 289 } 290 291 /** 292 * @tests java.text.DecimalFormatSymbols#getInternationalCurrencySymbol() 293 */ 294 @TestTargetNew( 295 level = TestLevel.COMPLETE, 296 notes = "", 297 method = "getInternationalCurrencySymbol", 298 args = {} 299 ) 300 public void test_getInternationalCurrencySymbol() { 301 assertEquals("Returned incorrect InternationalCurrencySymbol", "USD", 302 dfsUS.getInternationalCurrencySymbol()); 303 } 304 305 /** 306 * @tests java.text.DecimalFormatSymbols#getMinusSign() 307 */ 308 @TestTargetNew( 309 level = TestLevel.COMPLETE, 310 notes = "", 311 method = "getMinusSign", 312 args = {} 313 ) 314 public void test_getMinusSign() { 315 dfs.setMinusSign('&'); 316 assertEquals("Returned incorrect MinusSign symbol", '&', dfs 317 .getMinusSign()); 318 } 319 320 /** 321 * @tests java.text.DecimalFormatSymbols#getMonetaryDecimalSeparator() Test 322 * of method 323 * java.text.DecimalFormatSymbols#getMonetaryDecimalSeparator(). 324 */ 325 @TestTargetNew( 326 level = TestLevel.COMPLETE, 327 notes = "", 328 method = "getMonetaryDecimalSeparator", 329 args = {} 330 ) 331 public void test_getMonetaryDecimalSeparator() { 332 try { 333 dfs.setMonetaryDecimalSeparator(','); 334 assertEquals("Returned incorrect MonetaryDecimalSeparator symbol", 335 ',', dfs.getMonetaryDecimalSeparator()); 336 } catch (Exception e) { 337 fail("Unexpected exception " + e.toString()); 338 } 339 } 340 341 /** 342 * @tests java.text.DecimalFormatSymbols#getNaN() 343 */ 344 @TestTargetNew( 345 level = TestLevel.COMPLETE, 346 notes = "", 347 method = "getNaN", 348 args = {} 349 ) 350 public void test_getNaN() { 351 dfs.setNaN("NAN!!"); 352 assertEquals("Returned incorrect nan symbol", "NAN!!", dfs.getNaN()); 353 } 354 355 /** 356 * @tests java.text.DecimalFormatSymbols#getPatternSeparator() 357 */ 358 @TestTargetNew( 359 level = TestLevel.COMPLETE, 360 notes = "", 361 method = "getPatternSeparator", 362 args = {} 363 ) 364 public void test_getPatternSeparator() { 365 dfs.setPatternSeparator('X'); 366 assertEquals("Returned incorrect PatternSeparator symbol", 'X', dfs 367 .getPatternSeparator()); 368 } 369 370 /** 371 * @tests java.text.DecimalFormatSymbols#getPercent() 372 */ 373 @TestTargetNew( 374 level = TestLevel.COMPLETE, 375 notes = "", 376 method = "getPercent", 377 args = {} 378 ) 379 public void test_getPercent() { 380 dfs.setPercent('*'); 381 assertEquals("Returned incorrect Percent symbol", '*', dfs.getPercent()); 382 } 383 384 /** 385 * @tests java.text.DecimalFormatSymbols#getPerMill() 386 */ 387 @TestTargetNew( 388 level = TestLevel.COMPLETE, 389 notes = "", 390 method = "getPerMill", 391 args = {} 392 ) 393 public void test_getPerMill() { 394 dfs.setPerMill('#'); 395 assertEquals("Returned incorrect PerMill symbol", '#', dfs.getPerMill()); 396 } 397 398 /** 399 * @tests java.text.DecimalFormatSymbols#getZeroDigit() 400 */ 401 @TestTargetNew( 402 level = TestLevel.COMPLETE, 403 notes = "", 404 method = "getZeroDigit", 405 args = {} 406 ) 407 public void test_getZeroDigit() { 408 dfs.setZeroDigit('*'); 409 assertEquals("Returned incorrect ZeroDigit symbol", '*', dfs 410 .getZeroDigit()); 411 } 412 413 /** 414 * @tests java.text.DecimalFormatSymbols#hashCode() Test of method 415 * java.text.DecimalFormatSymbols#hashCode(). 416 */ 417 @TestTargetNew( 418 level = TestLevel.COMPLETE, 419 notes = "", 420 method = "hashCode", 421 args = {} 422 ) 423 @AndroidOnly("Succeeds against Android.") 424 public void test_hashCode() { 425 try { 426 DecimalFormatSymbols dfs1 = new DecimalFormatSymbols(); 427 DecimalFormatSymbols dfs2 = (DecimalFormatSymbols) dfs1.clone(); 428 assertTrue("Hash codes of equal object are equal", dfs2 429 .hashCode() == dfs1.hashCode()); 430 dfs1.setInfinity("infinity_infinity"); 431 assertTrue("Hash codes of non-equal objects are equal", dfs2 432 .hashCode() != dfs1.hashCode()); 433 } catch (Exception e) { 434 fail("Unexpected exception " + e.toString()); 435 } 436 } 437 438 /** 439 * @tests java.text.DecimalFormatSymbols#setCurrency(java.util.Currency) 440 */ 441 @TestTargetNew( 442 level = TestLevel.COMPLETE, 443 notes = "", 444 method = "setCurrency", 445 args = {java.util.Currency.class} 446 ) 447 public void test_setCurrencyLjava_util_Currency() { 448 Locale locale = Locale.CANADA; 449 DecimalFormatSymbols dfs = ((DecimalFormat) NumberFormat 450 .getCurrencyInstance(locale)).getDecimalFormatSymbols(); 451 452 try { 453 dfs.setCurrency(null); 454 fail("Expected NullPointerException"); 455 } catch (NullPointerException e) { 456 } 457 458 Currency currency = Currency.getInstance("JPY"); 459 dfs.setCurrency(currency); 460 461 assertTrue("Returned incorrect currency", currency == dfs.getCurrency()); 462 assertEquals("Returned incorrect currency symbol", currency.getSymbol( 463 locale), dfs.getCurrencySymbol()); 464 assertTrue("Returned incorrect international currency symbol", currency 465 .getCurrencyCode().equals(dfs.getInternationalCurrencySymbol())); 466 } 467 468 /** 469 * @tests java.text.DecimalFormatSymbols#setCurrencySymbol(java.lang.String) 470 * Test of method 471 * java.text.DecimalFormatSymbols#setCurrencySymbol(java.lang.String). 472 */ 473 @TestTargetNew( 474 level = TestLevel.COMPLETE, 475 notes = "", 476 method = "setCurrencySymbol", 477 args = {java.lang.String.class} 478 ) 479 public void test_setCurrencySymbolLjava_lang_String() { 480 try { 481 dfs.setCurrencySymbol("$"); 482 assertEquals("Returned incorrect CurrencySymbol symbol", "$", dfs 483 .getCurrencySymbol()); 484 } catch (Exception e) { 485 fail("Unexpected exception " + e.toString()); 486 } 487 } 488 489 /** 490 * @tests java.text.DecimalFormatSymbols#setDecimalSeparator(char) 491 */ 492 @TestTargetNew( 493 level = TestLevel.COMPLETE, 494 notes = "", 495 method = "setDecimalSeparator", 496 args = {char.class} 497 ) 498 public void test_setDecimalSeparatorC() { 499 dfs.setDecimalSeparator('*'); 500 assertEquals("Returned incorrect DecimalSeparator symbol", '*', dfs 501 .getDecimalSeparator()); 502 } 503 504 /** 505 * @tests java.text.DecimalFormatSymbols#setDigit(char) 506 */ 507 @TestTargetNew( 508 level = TestLevel.COMPLETE, 509 notes = "", 510 method = "setDigit", 511 args = {char.class} 512 ) 513 public void test_setDigitC() { 514 dfs.setDigit('*'); 515 assertEquals("Returned incorrect Digit symbol", '*', dfs.getDigit()); 516 } 517 518 /** 519 * @tests java.text.DecimalFormatSymbols#setGroupingSeparator(char) 520 */ 521 @TestTargetNew( 522 level = TestLevel.COMPLETE, 523 notes = "", 524 method = "setGroupingSeparator", 525 args = {char.class} 526 ) 527 public void test_setGroupingSeparatorC() { 528 dfs.setGroupingSeparator('*'); 529 assertEquals("Returned incorrect GroupingSeparator symbol", '*', dfs 530 .getGroupingSeparator()); 531 } 532 533 /** 534 * @tests java.text.DecimalFormatSymbols#setInfinity(java.lang.String) 535 */ 536 @TestTargetNew( 537 level = TestLevel.COMPLETE, 538 notes = "", 539 method = "setInfinity", 540 args = {java.lang.String.class} 541 ) 542 public void test_setInfinityLjava_lang_String() { 543 dfs.setInfinity("&"); 544 assertTrue("Returned incorrect Infinity symbol", 545 dfs.getInfinity() == "&"); 546 } 547 548 /** 549 * @tests java.text.DecimalFormatSymbols#setInternationalCurrencySymbol(java.lang.String) 550 */ 551 @TestTargetNew( 552 level = TestLevel.COMPLETE, 553 notes = "", 554 method = "setInternationalCurrencySymbol", 555 args = {java.lang.String.class} 556 ) 557 @KnownFailure("getCurrency() doesn't return null for bogus currency code.") 558 public void test_setInternationalCurrencySymbolLjava_lang_String() { 559 Locale locale = Locale.CANADA; 560 DecimalFormatSymbols dfs = ((DecimalFormat) NumberFormat 561 .getCurrencyInstance(locale)).getDecimalFormatSymbols(); 562 Currency currency = Currency.getInstance("JPY"); 563 dfs.setInternationalCurrencySymbol(currency.getCurrencyCode()); 564 565 assertTrue("Test1: Returned incorrect currency", currency == dfs 566 .getCurrency()); 567 assertEquals("Test1: Returned incorrect currency symbol", currency 568 .getSymbol(locale), dfs.getCurrencySymbol()); 569 assertTrue("Test1: Returned incorrect international currency symbol", 570 currency.getCurrencyCode().equals( 571 dfs.getInternationalCurrencySymbol())); 572 573 String symbol = dfs.getCurrencySymbol(); 574 dfs.setInternationalCurrencySymbol("bogus"); 575 assertNull("Test2: Returned incorrect currency", dfs.getCurrency()); 576 assertTrue("Test2: Returned incorrect currency symbol", dfs 577 .getCurrencySymbol().equals(symbol)); 578 assertEquals("Test2: Returned incorrect international currency symbol", 579 "bogus", dfs.getInternationalCurrencySymbol()); 580 } 581 582 /** 583 * @tests java.text.DecimalFormatSymbols#setMinusSign(char) 584 */ 585 @TestTargetNew( 586 level = TestLevel.COMPLETE, 587 notes = "", 588 method = "setMinusSign", 589 args = {char.class} 590 ) 591 public void test_setMinusSignC() { 592 dfs.setMinusSign('&'); 593 assertEquals("Returned incorrect MinusSign symbol", '&', dfs 594 .getMinusSign()); 595 } 596 597 /** 598 * @tests java.text.DecimalFormatSymbols#setMonetaryDecimalSeparator(char) 599 * Test of method 600 * java.text.DecimalFormatSymbols#setMonetaryDecimalSeparator(char). 601 */ 602 @TestTargetNew( 603 level = TestLevel.COMPLETE, 604 notes = "", 605 method = "setMonetaryDecimalSeparator", 606 args = {char.class} 607 ) 608 public void test_setMonetaryDecimalSeparatorC() { 609 try { 610 dfs.setMonetaryDecimalSeparator('#'); 611 assertEquals("Returned incorrect MonetaryDecimalSeparator symbol", 612 '#', dfs.getMonetaryDecimalSeparator()); 613 } catch (Exception e) { 614 fail("Unexpected exception " + e.toString()); 615 } 616 } 617 618 /** 619 * @tests java.text.DecimalFormatSymbols#setNaN(java.lang.String) 620 */ 621 @TestTargetNew( 622 level = TestLevel.COMPLETE, 623 notes = "", 624 method = "setNaN", 625 args = {java.lang.String.class} 626 ) 627 public void test_setNaNLjava_lang_String() { 628 dfs.setNaN("NAN!!"); 629 assertEquals("Returned incorrect nan symbol", "NAN!!", dfs.getNaN()); 630 } 631 632 /** 633 * @tests java.text.DecimalFormatSymbols#setPatternSeparator(char) 634 */ 635 @TestTargetNew( 636 level = TestLevel.COMPLETE, 637 notes = "", 638 method = "setPatternSeparator", 639 args = {char.class} 640 ) 641 public void test_setPatternSeparatorC() { 642 dfs.setPatternSeparator('X'); 643 assertEquals("Returned incorrect PatternSeparator symbol", 'X', dfs 644 .getPatternSeparator()); 645 } 646 647 /** 648 * @tests java.text.DecimalFormatSymbols#setPercent(char) 649 */ 650 @TestTargetNew( 651 level = TestLevel.COMPLETE, 652 notes = "", 653 method = "setPercent", 654 args = {char.class} 655 ) 656 public void test_setPercentC() { 657 dfs.setPercent('*'); 658 assertEquals("Returned incorrect Percent symbol", '*', dfs.getPercent()); 659 } 660 661 /** 662 * @tests java.text.DecimalFormatSymbols#setPerMill(char) 663 */ 664 @TestTargetNew( 665 level = TestLevel.COMPLETE, 666 notes = "", 667 method = "setPerMill", 668 args = {char.class} 669 ) 670 public void test_setPerMillC() { 671 dfs.setPerMill('#'); 672 assertEquals("Returned incorrect PerMill symbol", '#', dfs.getPerMill()); 673 } 674 675 /** 676 * @tests java.text.DecimalFormatSymbols#setZeroDigit(char) 677 */ 678 @TestTargetNew( 679 level = TestLevel.COMPLETE, 680 notes = "", 681 method = "setZeroDigit", 682 args = {char.class} 683 ) 684 public void test_setZeroDigitC() { 685 dfs.setZeroDigit('*'); 686 assertEquals("Set incorrect ZeroDigit symbol", '*', dfs.getZeroDigit()); 687 } 688 689 /** 690 * Sets up the fixture, for example, open a network connection. This method 691 * is called before a test is executed. 692 */ 693 protected void setUp() { 694 dfs = new DecimalFormatSymbols(); 695 dfsUS = new DecimalFormatSymbols(new Locale("en", "us")); 696 } 697 698 /** 699 * Tears down the fixture, for example, close a network connection. This 700 * method is called after a test is executed. 701 */ 702 protected void tearDown() { 703 } 704 705 // Test serialization mechanism of DecimalFormatSymbols 706 @TestTargetNew( 707 level = TestLevel.COMPLETE, 708 notes = "Checks serialization mechanism.", 709 method = "!SerializationSelf", 710 args = {} 711 ) 712 public void test_serialization() throws Exception { 713 DecimalFormatSymbols symbols = new DecimalFormatSymbols(Locale.FRANCE); 714 Currency currency = symbols.getCurrency(); 715 assertNotNull(currency); 716 717 // serialize 718 ByteArrayOutputStream byteOStream = new ByteArrayOutputStream(); 719 ObjectOutputStream objectOStream = new ObjectOutputStream(byteOStream); 720 objectOStream.writeObject(symbols); 721 722 // and deserialize 723 ObjectInputStream objectIStream = new ObjectInputStream( 724 new ByteArrayInputStream(byteOStream.toByteArray())); 725 DecimalFormatSymbols symbolsD = (DecimalFormatSymbols) objectIStream 726 .readObject(); 727 728 // The associated currency will not persist 729 currency = symbolsD.getCurrency(); 730 assertNotNull(currency); 731 } 732 733 // Use RI to write DecimalFormatSymbols out, use Harmony to read 734 // DecimalFormatSymbols in. The read symbol will be equal with those 735 // instantiated inside Harmony. 736 737 // This assertion will not come into existence the other way around. This is 738 // probably caused by different serialization mechanism used by RI and 739 // Harmony. 740 @TestTargetNew( 741 level = TestLevel.PARTIAL, 742 notes = "Make sure all fields have non default values.", 743 method = "!SerializationGolden", 744 args = {} 745 ) 746 @KnownFailure("Deserialized object is not equal to the original object." + 747 "Test passes on RI.") 748 public void test_RIHarmony_compatible() throws Exception { 749 ObjectInputStream i = null; 750 try { 751 DecimalFormatSymbols symbols = new DecimalFormatSymbols( 752 Locale.FRANCE); 753 i = new ObjectInputStream( 754 getClass() 755 .getClassLoader() 756 .getResourceAsStream( 757 "serialization/java/text/DecimalFormatSymbols.ser")); 758 DecimalFormatSymbols symbolsD = (DecimalFormatSymbols) i 759 .readObject(); 760 assertEquals(symbols, symbolsD); 761 } catch(NullPointerException e) { 762 assertNotNull("Failed to load /serialization/java/text/" + 763 "DecimalFormatSymbols.ser", i); 764 } finally { 765 try { 766 if (i != null) { 767 i.close(); 768 } 769 } catch (Exception e) { 770 } 771 } 772 assertDecimalFormatSymbolsRIFrance(dfs); 773 } 774 775 static void assertDecimalFormatSymbolsRIFrance(DecimalFormatSymbols dfs) { 776 // Values based on Java 1.5 RI DecimalFormatSymbols for Locale.FRANCE 777 /* 778 * currency = [EUR] 779 * currencySymbol = [U+20ac] // EURO SIGN 780 * decimalSeparator = [,][U+002c] 781 * digit = [#][U+0023] 782 * groupingSeparator = [U+00a0] // NON-BREAKING SPACE 783 * infinity = [U+221e] // INFINITY 784 * internationalCurrencySymbol = [EUR] 785 * minusSign = [-][U+002d] 786 * monetaryDecimalSeparator = [,][U+002c] 787 * naN = [U+fffd] // REPLACEMENT CHARACTER 788 * patternSeparator = [;][U+003b] 789 * perMill = [U+2030] // PER MILLE 790 * percent = [%][U+0025] 791 * zeroDigit = [0][U+0030] 792 */ 793 assertEquals("EUR", dfs.getCurrency().getCurrencyCode()); 794 assertEquals("\u20AC", dfs.getCurrencySymbol()); 795 assertEquals(',', dfs.getDecimalSeparator()); 796 assertEquals('#', dfs.getDigit()); 797 assertEquals('\u00a0', dfs.getGroupingSeparator()); 798 assertEquals("\u221e", dfs.getInfinity()); 799 assertEquals("EUR", dfs.getInternationalCurrencySymbol()); 800 assertEquals('-', dfs.getMinusSign()); 801 assertEquals(',', dfs.getMonetaryDecimalSeparator()); 802 // RI's default NaN is U+FFFD, Harmony's is based on ICU 803 // This suggests an RI bug, assuming that non-UTF8 bytes are UTF8 and 804 // getting a conversion failure. 805 assertEquals("\uFFFD", dfs.getNaN()); 806 assertEquals('\u003b', dfs.getPatternSeparator()); 807 assertEquals('\u2030', dfs.getPerMill()); 808 assertEquals('%', dfs.getPercent()); 809 assertEquals('0', dfs.getZeroDigit()); 810 } 811} 812