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 package libcore.java.text; 18 19 import java.text.ChoiceFormat; 20 import java.text.DecimalFormat; 21 import java.text.FieldPosition; 22 import java.text.NumberFormat; 23 import java.text.ParseException; 24 import java.text.ParsePosition; 25 import java.util.Currency; 26 import java.util.Locale; 27 import junit.framework.TestCase; 28 29 public class OldNumberFormatTest extends TestCase { 30 31 public void test_getIntegerInstanceLjava_util_Locale() throws ParseException { 32 DecimalFormat format = (DecimalFormat) NumberFormat.getIntegerInstance(Locale.US); 33 assertEquals("#,##0", format.toPattern()); 34 assertEquals("-36", format.format(-35.76)); 35 assertEquals(new Long(-36), format.parse("-36")); 36 assertEquals(new Long(-36), format.parseObject("-36")); 37 assertEquals(0, format.getMaximumFractionDigits()); 38 assertTrue(format.isParseIntegerOnly()); 39 40 // try with a locale that has a different integer pattern 41 Locale chLocale = new Locale("de", "CH"); 42 format = (DecimalFormat) NumberFormat.getIntegerInstance(chLocale); 43 assertEquals("#,##0", format.toPattern()); 44 assertEquals("-36", format.format(-35.76)); 45 assertEquals(new Long(-36), format.parse("-36")); 46 assertEquals(new Long(-36), format.parseObject("-36")); 47 assertEquals(0, format.getMaximumFractionDigits()); 48 assertTrue(format.isParseIntegerOnly()); 49 50 Locale arLocale = new Locale("ar", "AE"); 51 format = (DecimalFormat) NumberFormat.getIntegerInstance(arLocale); 52 String variant = (format.toPattern().indexOf(';') > 0) ? "#,##0;-#,##0" : "#,##0"; 53 assertEquals(variant, format.toPattern()); 54 assertEquals("\u0666\u0667", format.format(67)); 55 56 assertEquals("-\u0666", format.format(-6)); 57 assertEquals(-36L, format.parse("-36")); 58 59 // New Arabic formats do not support '-' to right of digits. 60 assertEquals(36L, format.parseObject("36-")); 61 assertEquals(0, format.getMaximumFractionDigits()); 62 assertTrue(format.isParseIntegerOnly()); 63 } 64 65 public void test_setMaximumIntegerDigits() { 66 NumberFormat format = NumberFormat.getInstance(); 67 format.setMaximumIntegerDigits(2); 68 assertEquals("Wrong result: case 1", "23", format.format(123)); 69 70 format.setMaximumIntegerDigits(Integer.MIN_VALUE); 71 assertEquals("Wrong result: case 2", "0", format.format(123)); 72 } 73 74 public void test_setCurrencyLjava_util_Currency() { 75 // Test for method void setCurrency(java.util.Currency) 76 // a subclass that supports currency formatting 77 Currency currA = Currency.getInstance("ARS"); 78 NumberFormat format = NumberFormat.getInstance(new Locale("hu", "HU")); 79 format.setCurrency(currA); 80 assertSame("Returned incorrect currency", currA, format.getCurrency()); 81 82 // a subclass that doesn't support currency formatting 83 ChoiceFormat cformat = new ChoiceFormat( 84 "0#Less than one|1#one|1<Between one and two|2<Greater than two"); 85 try { 86 ((NumberFormat) cformat).setCurrency(currA); 87 fail("Expected UnsupportedOperationException"); 88 } catch (UnsupportedOperationException e) { 89 } 90 91 try { 92 NumberFormat.getInstance().setCurrency(null); 93 fail("NullPointerException was thrown."); 94 } catch(NullPointerException npe) { 95 //expected 96 } 97 98 try { 99 NumberFormat.getIntegerInstance().setCurrency(null); 100 fail("NullPointerException was thrown."); 101 } catch(NullPointerException npe) { 102 //expected 103 } 104 } 105 106 public void test_parseObjectLjava_lang_StringLjava_text_ParsePosition() { 107 // regression test for HARMONY-1003 108 assertNull(NumberFormat.getInstance().parseObject("0", 109 new ParsePosition(-1))); 110 111 parseObjectTest(NumberFormat.getInstance(), "123.123", 112 new ParsePosition(1), new Double(23.123), 7, true); 113 114 parseObjectTest(NumberFormat.getInstance(), "123.123abc123", 115 new ParsePosition(3), new Double(0.123), 7, true); 116 117 parseObjectTest(NumberFormat.getInstance(Locale.FRANCE), "asd123,123abc123", 118 new ParsePosition(3), new Double(123.123), 10, true); 119 120 parseObjectTest(NumberFormat.getInstance(Locale.FRANCE), "test test", 121 new ParsePosition(0), null, 0, false); 122 123 parseObjectTest(NumberFormat.getIntegerInstance(), 124 "asd123.123abc123", 125 new ParsePosition(3), new Long(123), 6, true); 126 127 parseObjectTest(NumberFormat.getNumberInstance(), 128 "$-123,123.123#", 129 new ParsePosition(1), new Double(-123123.123), 13, true); 130 parseObjectTest(NumberFormat.getNumberInstance(), 131 "$-123,123.123#", 132 new ParsePosition(0), null, 0, false); 133 parseObjectTest(NumberFormat.getNumberInstance(), 134 "$-123,123.123#", 135 new ParsePosition(13), null, 13, false); 136 parseObjectTest(NumberFormat.getPercentInstance(), 137 "%20.123#", 138 new ParsePosition(0), new Double(20.123), 0, false); 139 parseObjectTest(NumberFormat.getPercentInstance(), 140 "%-200,123.123#", 141 new ParsePosition(0), null, 0, false); 142 143 144 // Regression for HARMONY-1685 145 try { 146 NumberFormat.getInstance().parseObject("test", null); 147 fail("NullPointerException expected"); 148 } catch (NullPointerException e) { 149 // expected 150 } 151 } 152 153 void parseObjectTest(NumberFormat nf, String sourseStr, ParsePosition position, 154 Object resultObj, int outIndex, boolean isSuccess) { 155 int indexBefore = position.getIndex(); 156 Object result = nf.parseObject(sourseStr, position); 157 if(isSuccess) { 158 assertEquals(resultObj, result); 159 assertEquals(outIndex, position.getIndex()); 160 } else { 161 assertNull(result); 162 assertEquals(indexBefore, position.getIndex()); 163 assertEquals(outIndex, position.getErrorIndex()); 164 } 165 } 166 167 public void test_clone() { 168 169 int max_digits = 100; 170 NumberFormat nf1 = NumberFormat.getInstance(); 171 nf1.setMaximumIntegerDigits(max_digits); 172 173 NumberFormat nf2 = (NumberFormat) nf1.clone(); 174 NumberFormat nf3 = (NumberFormat) nf1.clone(); 175 176 assertTrue("Clonned object is not equal to object", nf2.equals(nf1)); 177 assertTrue("Two clonned objects are not equal", nf2.equals(nf3)); 178 179 assertTrue("Max digits value is incorrect for clonned object", nf2 180 .getMaximumIntegerDigits() == max_digits); 181 182 nf1.setMaximumIntegerDigits(10); 183 assertTrue( 184 "Max digits value is incorrect for clonned object after changing this value for object", 185 nf2.getMaximumIntegerDigits() == max_digits); 186 } 187 188 public void test_equals() { 189 190 NumberFormat nf1 = NumberFormat.getInstance(); 191 NumberFormat nf2 = NumberFormat.getInstance(); 192 193 assertTrue("Objects are not equal", nf1.equals(nf2)); 194 assertTrue("THe same Objects are not equal", nf1.equals(nf1)); 195 196 nf2.setMaximumIntegerDigits(100); 197 assertFalse("Different NumberFormat are equal", nf1.equals(nf2)); 198 199 nf2.setMaximumIntegerDigits(nf1.getMaximumIntegerDigits()); 200 assertTrue("THe same Objects are not equal", nf1.equals(nf2)); 201 202 nf1 = NumberFormat.getIntegerInstance(); 203 nf2 = NumberFormat.getIntegerInstance(Locale.CHINA); 204 assertFalse("Different NumberFormat are equal", nf1.equals(nf2)); 205 206 assertFalse("Object is equal null", nf1.equals(null)); 207 } 208 209 public void test_formatLdouble() { 210 NumberFormat nf1 = NumberFormat.getInstance(Locale.US); 211 212 String out = nf1.format(1234567890.0123456789); 213 assertEquals("Wrong result for double : " + out, "1,234,567,890.012", 214 out.toString()); 215 216 out = nf1.format(-1234567890.0123456789); 217 assertEquals("Wrong result for double : " + out, "-1,234,567,890.012", 218 out.toString()); 219 220 Locale deLocale = new Locale("de", "CH"); 221 NumberFormat nf2 = NumberFormat.getInstance(deLocale); 222 out = nf2.format(-1234567890.0123456789); 223 // use de_CH instead 224 // assertEquals("Wrong result for double : " + out, "1,234,567,890.012-", 225 // out.toString()); 226 assertEquals("Wrong result for double : " + out, "-1'234'567'890.012", out.toString()); 227 228 out = nf1.format(1.0001); 229 assertEquals("Wrong result for for double: " + out, "1", out.toString()); 230 231 out = nf1.format(5.0); 232 assertEquals("Wrong result for for double: " + out, "5", out.toString()); 233 // END Android-changed 234 } 235 236 public void test_formatLlong() { 237 NumberFormat nf1 = NumberFormat.getInstance(Locale.US); 238 239 String out = nf1.format(Long.MAX_VALUE); 240 assertEquals("Wrong result for double : " + out, 241 "9,223,372,036,854,775,807", out.toString()); 242 243 out = nf1.format(Long.MIN_VALUE); 244 assertEquals("Wrong result for double : " + out, 245 "-9,223,372,036,854,775,808", out.toString()); 246 247 Locale deLocale = new Locale("de", "CH"); 248 NumberFormat nf2 = NumberFormat.getInstance(deLocale); 249 out = nf2.format(-1234567890); 250 // use de_CH instead 251 // assertEquals("Wrong result for double : " + out, "-1 234 567 890", out 252 // .toString()); 253 assertEquals("Wrong result for double : " + out, "-1'234'567'890", out.toString()); 254 255 // the Locale data of icu uses \uc2a0 256 out = nf1.format(1); 257 assertEquals("Wrong result for for double: " + out, "1", out.toString()); 258 259 out = nf1.format(0); 260 assertEquals("Wrong result for for double: " + out, "0", out.toString()); 261 // END Android-changed 262 } 263 264 public void test_getAvailableLocales() { 265 266 Locale[] l = NumberFormat.getAvailableLocales(); 267 assertFalse("returned Locale array is null", l == null); 268 assertTrue("returned Locale length <= 0", l.length > 0); 269 Locale[] resl = Locale.getAvailableLocales(); 270 assertTrue("returned Locale arrays are different", 271 l.length == resl.length); 272 boolean isUS = false; 273 for (int i = 0; i < resl.length; i++) { 274 assertEquals("elements " + i + " are not equal: ", resl[i], l[i]); 275 if (l[i].equals(Locale.US)) 276 isUS = true; 277 } 278 assertTrue("there is no Locale.US", isUS); 279 } 280 281 public void test_getCurrencyInstance() { 282 283 Locale.setDefault(Locale.US); 284 NumberFormat format = NumberFormat.getCurrencyInstance(); 285 286 assertNotSame("Instance is null", null, format); 287 assertTrue("Object is not instance of NumberFormat", 288 format instanceof NumberFormat); 289 290 assertEquals( 291 "Test1: NumberFormat.getCurrencyInstance().format(35.76) returned wrong value", 292 "$35.76", format.format(35.76)); 293 assertEquals( 294 "Test2: NumberFormat.getCurrencyInstance().format(123456.789) returned wrong value", 295 "$123,456.79", format.format(123456.789)); 296 assertEquals( 297 "Test3: NumberFormat.getCurrencyInstance().format(0.1) returned wrong value", 298 "$0.10", format.format(0.1)); 299 assertEquals( 300 "Test4: NumberFormat.getCurrencyInstance().format(0.999) returned wrong value", 301 "$1.00", format.format(0.999)); 302 } 303 304 public void test_getCurrencyInstanceLjava_util_Locale() { 305 Locale usLocale = Locale.US; 306 NumberFormat format = NumberFormat.getCurrencyInstance(usLocale); 307 308 assertNotSame("Instance is null", null, format); 309 assertTrue(format instanceof NumberFormat); 310 311 assertEquals("$35.76", format.format(35.76)); 312 assertEquals("$123,456.79", format.format(123456.789)); 313 assertEquals("$0.10", format.format(0.1)); 314 assertEquals("$1.00", format.format(0.999)); 315 316 Locale atLocale = new Locale("de", "AT"); 317 format = NumberFormat.getCurrencyInstance(atLocale); 318 // BEGIN Android-changed: ICU uses non-breaking space after the euro sign; the RI uses ' '. 319 assertEquals("\u20ac\u00a035,76", format.format(35.76)); 320 assertEquals("\u20ac\u00a0123.456,79", format.format(123456.789)); 321 assertEquals("\u20ac\u00a00,10", format.format(0.1)); 322 assertEquals("\u20ac\u00a01,00", format.format(0.999)); 323 try { 324 NumberFormat.getCurrencyInstance(null); 325 fail("java.lang.NullPointerException is not thrown"); 326 } catch (java.lang.NullPointerException expected) { 327 } 328 } 329 330 public void test_getInstance() { 331 Locale.setDefault(Locale.US); 332 NumberFormat format = NumberFormat.getInstance(); 333 334 assertNotSame("Instance is null", null, format); 335 assertTrue("Object is not instance of NumberFormat", 336 format instanceof NumberFormat); 337 338 assertEquals( 339 "Test1: NumberFormat.getInstance().format(1234567890.0987654321) returned wrong value", 340 "1,234,567,890.099", format.format(1234567890.0987654321)); 341 assertEquals( 342 "Test2: ((DecimalFormat) NumberFormat.getInstance()).toPattern returned wrong value", 343 "#,##0.###", ((DecimalFormat) format).toPattern()); 344 assertEquals( 345 "Test3: NumberFormat.getInstance().format(123456789) returned wrong value", 346 "123,456,789", format.format(123456789)); 347 } 348 349 public void test_getInstanceLjava_util_Locale() { 350 Locale de_CH = new Locale("de", "CH"); 351 Locale.setDefault(Locale.US); 352 NumberFormat format = NumberFormat.getInstance(de_CH); 353 354 assertNotSame(null, format); 355 assertTrue(format instanceof NumberFormat); 356 357 assertEquals("1'234'567'890.099", format.format(1234567890.0987654321)); 358 assertEquals("#,##0.###", ((DecimalFormat) format).toPattern()); 359 assertEquals("123'456'789", format.format(123456789)); 360 361 try { 362 NumberFormat.getInstance(null); 363 fail("java.lang.NullPointerException is not thrown"); 364 } catch (java.lang.NullPointerException expected) { 365 } 366 } 367 368 public void test_getNumberInstance() { 369 Locale.setDefault(Locale.US); 370 NumberFormat format = NumberFormat.getNumberInstance(); 371 372 assertNotSame("Instance is null", null, format); 373 assertTrue("Object is not instance of NumberFormat", 374 format instanceof NumberFormat); 375 376 assertEquals( 377 "Test1: NumberFormat.getNumberInstance().format(1234567890.0987654321) returned wrong value", 378 "1,234,567,890.099", format.format(1234567890.0987654321)); 379 assertEquals( 380 "Test2: ((DecimalFormat) NumberFormat.getNumberInstance()).toPattern returned wrong value", 381 "#,##0.###", ((DecimalFormat) format).toPattern()); 382 assertEquals( 383 "Test3: NumberFormat.getNumberInstance().format(123456789) returned wrong value", 384 "123,456,789", format.format(123456789)); 385 } 386 387 public void test_getNumberInstanceLjava_util_Locale() { 388 Locale.setDefault(Locale.US); 389 Locale deLocale = new Locale("de", "CH"); 390 NumberFormat format = NumberFormat.getNumberInstance(deLocale); 391 assertNotSame("Instance is null", null, format); 392 assertTrue("Object is not instance of NumberFormat", format instanceof NumberFormat); 393 394 assertEquals("-1'234'567'890.099", format.format(-1234567890.0987654321)); 395 assertEquals("#,##0.###", ((DecimalFormat) format).toPattern()); 396 assertEquals("123'456'789", format.format(123456789)); 397 398 try { 399 NumberFormat.getInstance(null); 400 fail("java.lang.NullPointerException is not thrown"); 401 } catch (java.lang.NullPointerException expected) { 402 } 403 } 404 405 public void test_getPercentInstance() { 406 Locale.setDefault(Locale.US); 407 NumberFormat format = NumberFormat.getPercentInstance(); 408 409 assertNotSame("Instance is null", null, format); 410 assertTrue("Object is not instance of NumberFormat", 411 format instanceof NumberFormat); 412 413 assertEquals( 414 "Test1: NumberFormat.getPercentInstance().format(1234567890.0987654321) returned wrong value", 415 "123,456,789,010%", format.format(1234567890.0987654321)); 416 assertEquals( 417 "Test2: ((DecimalFormat) NumberFormat.getPercentInstance()).toPattern returned wrong value", 418 "#,##0%", ((DecimalFormat) format).toPattern()); 419 assertEquals( 420 "Test3: NumberFormat.getPercentInstance().format(123456789) returned wrong value", 421 "12,345,678,900%", format.format(123456789)); 422 } 423 424 public void test_getPercentInstanceLjava_util_Locale() { 425 Locale csLocale = new Locale("cs", "CZ"); 426 Locale.setDefault(Locale.US); 427 428 NumberFormat format = NumberFormat.getPercentInstance(csLocale); 429 assertNotSame("Instance is null", null, format); 430 assertTrue("Object is not instance of NumberFormat", format instanceof NumberFormat); 431 432 assertEquals("123\u00a0456\u00a0789\u00a0010\u00a0%", format.format(1234567890.0987654321)); 433 assertEquals("#,##0\u00a0%", ((DecimalFormat) format).toPattern()); 434 assertEquals("12\u00a0345\u00a0678\u00a0900\u00a0%", format.format(123456789)); 435 436 try { 437 NumberFormat.getInstance(null); 438 fail("java.lang.NullPointerException is not thrown"); 439 } catch (java.lang.NullPointerException expected) { 440 } 441 } 442 443 public void test_getMaximumFractionDigits() { 444 NumberFormat nf1 = NumberFormat.getInstance(); 445 446 nf1.setMaximumFractionDigits(Integer.MAX_VALUE); 447 int result = nf1.getMaximumFractionDigits(); 448 assertTrue("getMaximumFractionDigits returns " + result 449 + " instead of: " + Integer.MAX_VALUE, 450 result == Integer.MAX_VALUE); 451 452 nf1.setMaximumFractionDigits(0); 453 result = nf1.getMaximumFractionDigits(); 454 assertTrue("getMaximumFractionDigits returns " + result 455 + " instead of 0", result == 0); 456 457 nf1.setMinimumFractionDigits(Integer.MAX_VALUE); 458 result = nf1.getMaximumFractionDigits(); 459 assertTrue("getMaximumFractionDigits returns " + result 460 + " instead of Integer.MAX_VALUE", result == Integer.MAX_VALUE); 461 } 462 463 public void test_getMinimumFractionDigits() { 464 NumberFormat nf1 = NumberFormat.getInstance(); 465 nf1.setMinimumFractionDigits(Integer.MAX_VALUE); 466 int result = nf1.getMinimumFractionDigits(); 467 assertTrue("getMinimumFractionDigits returns " + result 468 + " instead of: " + Integer.MAX_VALUE, 469 result == Integer.MAX_VALUE); 470 471 nf1.setMaximumFractionDigits(0); 472 result = nf1.getMinimumFractionDigits(); 473 assertTrue("getMinimumFractionDigits returns " + result 474 + " instead of 0", result == 0); 475 476 nf1.setMinimumFractionDigits(52); 477 result = nf1.getMinimumFractionDigits(); 478 assertTrue("getMinimumFractionDigits returns " + result 479 + " instead of 52", result == 52); 480 } 481 482 public void test_getMaximumIntegerDigits() { 483 NumberFormat nf1 = NumberFormat.getInstance(); 484 nf1.setMaximumIntegerDigits(Integer.MAX_VALUE); 485 int result = nf1.getMaximumIntegerDigits(); 486 assertTrue("getMaximumIntegerDigits returns " + result 487 + " instead of: " + Integer.MAX_VALUE, 488 result == Integer.MAX_VALUE); 489 490 nf1.setMaximumIntegerDigits(0); 491 result = nf1.getMaximumIntegerDigits(); 492 assertTrue("getMaximumIntegerDigits returns " + result 493 + " instead of 0", result == 0); 494 495 nf1.setMinimumIntegerDigits(Integer.MAX_VALUE); 496 result = nf1.getMaximumIntegerDigits(); 497 assertTrue("getMaximumIntegerigits returns " + result 498 + " instead of Integer.MAX_VALUE", result == Integer.MAX_VALUE); 499 } 500 501 public void test_getMinimumIntegernDigits() { 502 NumberFormat nf1 = NumberFormat.getInstance(); 503 nf1.setMinimumIntegerDigits(Integer.MAX_VALUE); 504 int result = nf1.getMinimumIntegerDigits(); 505 assertTrue("getMinimumIntegerDigits returns " + result 506 + " instead of: " + Integer.MAX_VALUE, 507 result == Integer.MAX_VALUE); 508 509 nf1.setMaximumIntegerDigits(0); 510 result = nf1.getMinimumIntegerDigits(); 511 assertTrue("getMinimumIntegerDigits returns " + result 512 + " instead of 0", result == 0); 513 514 nf1.setMinimumIntegerDigits(0x12034); 515 result = nf1.getMinimumIntegerDigits(); 516 assertTrue("getMinimumIntegerDigits returns " + result 517 + " instead of 5148", result == 73780); 518 } 519 520 public void test_hashCode() { 521 522 NumberFormat nf1 = NumberFormat.getInstance(); 523 NumberFormat nf11 = NumberFormat.getInstance(); 524 NumberFormat nf2 = NumberFormat.getInstance(Locale.US); 525 NumberFormat nf3 = NumberFormat.getPercentInstance(); 526 NumberFormat nf4 = NumberFormat.getCurrencyInstance(); 527 NumberFormat nf5 = NumberFormat 528 .getNumberInstance(new Locale("mk", "MK")); 529 NumberFormat nf6 = NumberFormat.getInstance(Locale.US); 530 531 assertTrue("Hash codes are not equal: case 1", nf1.hashCode() == nf2 532 .hashCode()); 533 assertTrue("Hash codes are not equal: case 2", nf1.hashCode() == nf11 534 .hashCode()); 535 assertTrue("Hash codes are not equal: case 3", nf1.hashCode() == nf3 536 .hashCode()); 537 assertFalse("Hash codes are equal: case 4", nf3.hashCode() == nf4 538 .hashCode()); 539 assertFalse("Hash codes are equal: case 5", nf4.hashCode() == nf5 540 .hashCode()); 541 assertTrue("Hash codes are not equal: case 6", nf5.hashCode() == nf6 542 .hashCode()); 543 544 nf1.setMaximumFractionDigits(0); 545 assertTrue("Hash codes are not equal: case 7", nf1.hashCode() == nf11 546 .hashCode()); 547 } 548 549 public void test_isGroupingUsed() { 550 NumberFormat nf1 = NumberFormat.getInstance(); 551 assertTrue("grouping is not used for NumberFormat.getInstance", nf1 552 .isGroupingUsed()); 553 554 nf1.setGroupingUsed(false); 555 assertFalse( 556 "grouping is used for NumberFormat.getInstance after setting false", 557 nf1.isGroupingUsed()); 558 559 nf1.setGroupingUsed(true); 560 assertTrue( 561 "grouping is not used for NumberFormat.getInstance after setting true", 562 nf1.isGroupingUsed()); 563 } 564 565 public void test_setGroupingUsed() { 566 NumberFormat nf1 = NumberFormat.getInstance(Locale.US); 567 nf1.setGroupingUsed(false); 568 569 assertEquals("grouping is used for 1234567890.1", "1234567890.1", 570 nf1.format(1234567890.1)); 571 572 assertEquals("grouping is used for -1234567890.1", "-1234567890.1", 573 nf1.format(-1234567890.1)); 574 575 nf1.setGroupingUsed(false); 576 577 assertEquals("grouping is used for 1234567890.1", "1234567890.1", 578 nf1.format(1234567890.1)); 579 580 assertEquals("grouping is used for -1234567890.1", "-1234567890.1", 581 nf1.format(-1234567890.1)); 582 583 nf1.setGroupingUsed(true); 584 585 assertEquals("grouping is not used for 1234567890.1", 586 "1,234,567,890.1", nf1.format(1234567890.1)); 587 588 assertEquals("grouping is not used for -1234567890.1", 589 "-1,234,567,890.1", nf1.format(-1234567890.1)); 590 591 Locale csLocale = new Locale("cs", "CZ"); 592 NumberFormat nf2 = NumberFormat.getPercentInstance(csLocale); 593 nf2.setGroupingUsed(false); 594 595 assertEquals("123456789010\u00a0%", nf2.format(1234567890.1)); 596 597 assertEquals("-123456789010\u00a0%", nf2.format(-1234567890.1)); 598 assertEquals("1,234,567,890.1", nf1.format(1234567890.1)); 599 600 nf2.setGroupingUsed(true); 601 assertEquals("123\u00a0456\u00a0789\u00a0010\u00a0%", nf2.format(1234567890.1)); 602 603 assertEquals("-123\u00a0456\u00a0789\u00a0010\u00a0%", nf2.format(-1234567890.1)); 604 605 nf2.setGroupingUsed(true); 606 assertEquals("123\u00a0456\u00a0789\u00a0010\u00a0%", nf2.format(1234567890.1)); 607 608 assertEquals("-123\u00a0456\u00a0789\u00a0010\u00a0%", nf2.format(-1234567890.1)); 609 } 610 611 public void test_isParseIntegerOnly() { 612 NumberFormat nf1 = NumberFormat.getInstance(); 613 assertTrue("ParseIntegerOnly is not used for NumberFormat.getInstance", 614 nf1.isGroupingUsed()); 615 616 nf1.setParseIntegerOnly(false); 617 assertFalse( 618 "ParseIntegerOnly is used for NumberFormat.getInstance after setting false", 619 nf1.isParseIntegerOnly()); 620 621 nf1.setParseIntegerOnly(true); 622 assertTrue( 623 "ParseIntegerOnly is not used for NumberFormat.getInstance after setting true", 624 nf1.isParseIntegerOnly()); 625 } 626 627 public void test_setParseIntegerOnly() { 628 NumberFormat nf1 = NumberFormat.getInstance(Locale.US); 629 nf1.setParseIntegerOnly(true); 630 631 assertEquals("ParseIntegerOnly is not used for 1234567890.1", 632 "1,234,567,890.1", nf1.format(1234567890.1)); 633 assertEquals("ParseIntegerOnly is not used for -1234567890.1", 634 "-1,234,567,890.1", nf1.format(-1234567890.1)); 635 assertEquals("ParseIntegerOnly is not used for -1234567890.", 636 "-1,234,567,890", nf1.format(-1234567890.)); 637 638 nf1.setParseIntegerOnly(false); 639 640 assertEquals("ParseIntegerOnly is not used for 1234567890.1", 641 "1,234,567,890.1", nf1.format(1234567890.1)); 642 assertEquals("ParseIntegerOnly is not used for -1234567890.1", 643 "-1,234,567,890.1", nf1.format(-1234567890.1)); 644 assertEquals("ParseIntegerOnly is not used for -1234567890.", 645 "-1,234,567,890", nf1.format(-1234567890.)); 646 } 647 648 public void test_setMaximumFractionDigits() { 649 NumberFormat nf1 = NumberFormat.getInstance(Locale.US); 650 nf1.setMaximumFractionDigits(Integer.MAX_VALUE); 651 int result = nf1.getMaximumFractionDigits(); 652 assertTrue("setMaximumFractionDigits set " + result 653 + " instead of Integer.MAX_VALUE", result == Integer.MAX_VALUE); 654 nf1.setMaximumFractionDigits(0); 655 result = nf1.getMaximumFractionDigits(); 656 assertTrue("setMaximumFractionDigits set " + result + " instead of 0", 657 result == 0); 658 assertEquals("format of 1234567890.0987654321 returns incorrect value", 659 "1,234,567,890", nf1.format(1234567890.0987654321)); 660 nf1.setMaximumFractionDigits(5); 661 result = nf1.getMaximumFractionDigits(); 662 assertTrue("setMaximumFractionDigits set " + result + " instead of 5", 663 result == 5); 664 assertEquals( 665 "format of 1234567890.0987654321 returns incorrect value with MaximumFractionDigits = 5", 666 "1,234,567,890.09877", nf1.format(1234567890.0987654321)); 667 assertEquals( 668 "format of -1234567890 returns incorrect value with MaximumFractionDigits = 5", 669 "-1,234,567,890", nf1.format(-1234567890)); 670 nf1.setMaximumFractionDigits(Integer.MIN_VALUE); 671 result = nf1.getMaximumFractionDigits(); 672 assertTrue("setMaximumFractionDigits set " + result 673 + " instead of Integer.MIN_VALUE", result == 0); 674 assertEquals( 675 "format of 1234567890.0987654321 returns incorrect value with MaximumFractionDigits = 5", 676 "1,234,567,890", nf1.format(1234567890.0987654321)); 677 } 678 679 public void test_setMinimumFractionDigits() { 680 681 NumberFormat nf1 = NumberFormat.getInstance(Locale.US); 682 nf1.setMinimumFractionDigits(Integer.MAX_VALUE); 683 int result = nf1.getMinimumFractionDigits(); 684 assertTrue("setMinimumFractionDigits set " + result 685 + " instead of Integer.MAX_VALUE", result == Integer.MAX_VALUE); 686 nf1.setMinimumFractionDigits(0); 687 result = nf1.getMinimumFractionDigits(); 688 assertTrue("setMinimumFractionDigits set " + result + " instead of 0", 689 result == 0); 690 nf1.setMinimumFractionDigits(5); 691 result = nf1.getMinimumFractionDigits(); 692 assertTrue("setMinimumFractionDigits set " + result + " instead of 5", 693 result == 5); 694 assertEquals( 695 "format of 1234567890.0987654321 returns incorrect value with MinimumFractionDigits = 5", 696 "1,234,567,890.09000", nf1.format(1234567890.09)); 697 assertEquals( 698 "format of -1234567890 returns incorrect value with MinimumFractionDigits = 5", 699 "-1,234,567,890.00000", nf1.format(-1234567890)); 700 nf1.setMinimumFractionDigits(Integer.MIN_VALUE); 701 result = nf1.getMinimumFractionDigits(); 702 assertTrue("setMinimumFractionDigits set " + result 703 + " instead of Integer.MIN_VALUE", result == 0); 704 assertEquals( 705 "format of 1234567890.098 returns incorrect value with MinimumFractionDigits = 5", 706 "1,234,567,890.098", nf1.format(1234567890.098)); 707 } 708 709 public void test_setMinimumIntegerDigits() { 710 711 NumberFormat nf1 = NumberFormat.getInstance(Locale.US); 712 nf1.setMinimumIntegerDigits(Integer.MAX_VALUE); 713 int result = nf1.getMinimumIntegerDigits(); 714 assertTrue("setMinimumIntegerDigits set " + result 715 + " instead of Integer.MAX_VALUE", result == Integer.MAX_VALUE); 716 nf1.setMinimumIntegerDigits(0); 717 result = nf1.getMinimumIntegerDigits(); 718 assertTrue("setMinimumIntegerDigits set " + result + " instead of 0", 719 result == 0); 720 nf1.setMinimumIntegerDigits(5); 721 result = nf1.getMinimumIntegerDigits(); 722 assertTrue("setMinimumIntegerDigits set " + result + " instead of 5", 723 result == 5); 724 assertEquals( 725 "format of 123.09 returns incorrect value with MinimumIntegerDigits = 5", 726 "00,123.09", nf1.format(123.09)); 727 assertEquals( 728 "format of -123 returns incorrect value with MinimumIntegerDigits = 5", 729 "-00,123", nf1.format(-123)); 730 nf1.setMinimumIntegerDigits(Integer.MIN_VALUE); 731 result = nf1.getMinimumIntegerDigits(); 732 assertTrue("setMinimumIntegerDigits set " + result 733 + " instead of Integer.MIN_VALUE", result == 0); 734 } 735 736 // Broken Test: Fails in CTS, passes in CoreTestRunner 737 public void test_parseLjava_lang_String() { 738 NumberFormat nf1 = NumberFormat.getInstance(); 739 try { 740 assertEquals( 741 "Test1: NumberFormat.getInstance().parse(\"1234567890.1\") returned wrong number", 742 new Double(1234567890.1), nf1.parse("1234567890.1")); 743 } catch (java.text.ParseException pe) { 744 fail("java.text.ParseException is thrown for 1234567890.1"); 745 } 746 747 try { 748 assertEquals( 749 "Test2: NumberFormat.getInstance().parse(\"-1234567890.1\") returned wrong number", 750 new Double(-1234567890.1), nf1.parse("-1,234,567,890.1")); 751 } catch (java.text.ParseException pe) { 752 fail("java.text.ParseException is thrown for -1,234,567,890.1"); 753 } 754 755 try { 756 nf1.parse("@1,234,567,8901"); 757 fail("java.text.ParseException is not thrown for 1,234,567,890z1"); 758 } catch (java.text.ParseException pe) { 759 // expected 760 } 761 762 nf1 = NumberFormat.getPercentInstance(); 763 try { 764 assertEquals( 765 "Test3: NumberFormat.getPercentInstance().parse(\"-123%\") returned wrong number", 766 new Double(-1.23), nf1.parse("-123%")); 767 } catch (java.text.ParseException pe) { 768 fail("java.text.ParseException is thrown for -123%"); 769 } 770 771 nf1 = NumberFormat.getCurrencyInstance(); 772 try { 773 assertEquals( 774 "Test4: NumberFormat.getCurrencyInstance().parse(\"$123\") returned wrong number", 775 new Long(123), nf1.parse("$123")); 776 } catch (java.text.ParseException pe) { 777 fail("java.text.ParseException is thrown for $123"); 778 } 779 780 try { 781 assertEquals( 782 "Test4: NumberFormat.getCurrencyInstance().parse(\"$123abc\") returned wrong number", 783 new Long(123), nf1.parse("$123abc")); 784 } catch (java.text.ParseException pe) { 785 fail("java.text.ParseException is thrown for $123"); 786 } 787 788 nf1 = NumberFormat.getIntegerInstance(); 789 try { 790 assertEquals( 791 "Test5: NumberFormat.getIntegerInstance().parse(\"-123.123\") returned wrong number", 792 nf1.parseObject("-123.123"), nf1.parse("-123.123")); 793 } catch (java.text.ParseException pe) { 794 fail("java.text.ParseException is thrown for $123"); 795 } 796 } 797 798 public void test_constructor() { 799 MyNumberFormat mf = new MyNumberFormat(); 800 assertFalse("Greated NumberFormat object is null", mf == null); 801 assertTrue( 802 "Greated NumberFormat object is not instance of NumberFormat", 803 mf instanceof NumberFormat); 804 } 805 806 class MyNumberFormat extends NumberFormat { 807 static final long serialVersionUID = 1L; 808 809 public MyNumberFormat() { 810 super(); 811 } 812 813 public StringBuffer format(double number, StringBuffer toAppendTo, 814 FieldPosition pos) { 815 816 return new StringBuffer(); 817 } 818 819 public Number parse(String source, ParsePosition parsePosition) { 820 821 return new Double(0); 822 } 823 824 public StringBuffer format(long number, StringBuffer toAppendTo, 825 FieldPosition pos) { 826 return new StringBuffer(); 827 } 828 829 } 830 } 831