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 */ 17package org.apache.harmony.text.tests.java.text; 18 19import java.math.RoundingMode; 20import java.text.ChoiceFormat; 21import java.text.DecimalFormat; 22import java.text.FieldPosition; 23import java.text.NumberFormat; 24import java.text.ParseException; 25import java.text.ParsePosition; 26import java.util.Currency; 27import java.util.Locale; 28 29public class NumberFormatTest extends junit.framework.TestCase { 30 31 /** 32 * @tests java.text.NumberFormat#format(java.lang.Object, 33 * java.lang.StringBuffer, java.text.FieldPosition) 34 */ 35 public void test_formatLjava_lang_ObjectLjava_lang_StringBufferLjava_text_FieldPosition() { 36 FieldPosition pos; 37 StringBuffer out; 38 DecimalFormat format = (DecimalFormat) NumberFormat 39 .getInstance(Locale.US); 40 41 pos = new FieldPosition(0); 42 out = format.format(new Long(Long.MAX_VALUE), new StringBuffer(), pos); 43 assertEquals("Wrong result L1: " + out, "9,223,372,036,854,775,807", 44 out.toString()); 45 46 pos = new FieldPosition(0); 47 out = format.format(new Long(Long.MIN_VALUE), new StringBuffer(), pos); 48 assertEquals("Wrong result L2: " + out, "-9,223,372,036,854,775,808", 49 out.toString()); 50 51 pos = new FieldPosition(0); 52 out = format.format(new java.math.BigInteger(String 53 .valueOf(Long.MAX_VALUE)), new StringBuffer(), pos); 54 assertEquals("Wrong result BI1: " + out, "9,223,372,036,854,775,807", 55 out.toString()); 56 57 pos = new FieldPosition(0); 58 out = format.format(new java.math.BigInteger(String 59 .valueOf(Long.MIN_VALUE)), new StringBuffer(), pos); 60 assertEquals("Wrong result BI2: " + out, "-9,223,372,036,854,775,808", 61 out.toString()); 62 63 java.math.BigInteger big; 64 pos = new FieldPosition(0); 65 big = new java.math.BigInteger(String.valueOf(Long.MAX_VALUE)) 66 .add(new java.math.BigInteger("1")); 67 out = format.format(big, new StringBuffer(), pos); 68 assertEquals("Wrong result BI3: " + out, "9,223,372,036,854,775,808", 69 out.toString()); 70 71 pos = new FieldPosition(0); 72 big = new java.math.BigInteger(String.valueOf(Long.MIN_VALUE)) 73 .add(new java.math.BigInteger("-1")); 74 out = format.format(big, new StringBuffer(), pos); 75 assertEquals("Wrong result BI4: " + out, "-9,223,372,036,854,775,809", 76 out.toString()); 77 78 pos = new FieldPosition(0); 79 out = format.format(new java.math.BigDecimal("51.348"), 80 new StringBuffer(), pos); 81 assertEquals("Wrong result BD1: " + out, "51.348", out.toString()); 82 83 pos = new FieldPosition(0); 84 out = format.format(new java.math.BigDecimal("51"), new StringBuffer(), 85 pos); 86 assertEquals("Wrong result BD2: " + out, "51", out.toString()); 87 88 try { 89 format.format(this, new StringBuffer(), pos); 90 fail("Should throw IllegalArgumentException"); 91 } catch (IllegalArgumentException e) { 92 // Expected 93 } 94 95 try { 96 format.format(null, new StringBuffer(), pos); 97 fail("Should throw IllegalArgumentException"); 98 } catch (IllegalArgumentException e) { 99 // Expected 100 } 101 102 try { 103 format.format(new Long(0), null, pos); 104 fail("Should throw NullPointerException"); 105 } catch (NullPointerException e) { 106 // Expected 107 } 108 109 try { 110 format.format(new Long(0), new StringBuffer(), null); 111 fail("Should throw NullPointerException"); 112 } catch (NullPointerException e) { 113 // Expected 114 } 115 } 116 117 /** 118 * @tests java.text.NumberFormat#getIntegerInstance() 119 */ 120 public void test_getIntegerInstance() throws ParseException { 121 // Test for method java.text.NumberFormat getIntegerInstance() 122 Locale origLocale = Locale.getDefault(); 123 Locale.setDefault(Locale.US); 124 125 DecimalFormat format = (DecimalFormat) NumberFormat 126 .getIntegerInstance(); 127 128 assertEquals( 129 "Test1: NumberFormat.getIntegerInstance().toPattern() returned wrong pattern", 130 "#,##0", format.toPattern()); 131 assertEquals( 132 "Test2: NumberFormat.getIntegerInstance().format(35.76) returned wrong value", 133 "36", format.format(35.76)); 134 assertEquals( 135 "Test3: NumberFormat.getIntegerInstance().parse(\"35.76\") returned wrong number", 136 new Long(35), format.parse("35.76")); 137 assertEquals( 138 "Test4: NumberFormat.getIntegerInstance().parseObject(\"35.76\") returned wrong number", 139 new Long(35), format.parseObject("35.76")); 140 Locale.setDefault(origLocale); 141 } 142 143 /** 144 * @tests java.text.NumberFormat#getIntegerInstance(java.util.Locale) 145 */ 146 public void test_getIntegerInstanceLjava_util_Locale() 147 throws ParseException { 148 // Test for method java.text.NumberFormat 149 // getIntegerInstance(java.util.Locale) 150 Locale usLocale = Locale.US; 151 Locale arLocale = new Locale("ar", "AE"); 152 153 DecimalFormat format = (DecimalFormat) NumberFormat 154 .getIntegerInstance(usLocale); 155 assertEquals( 156 "Test1: NumberFormat.getIntegerInstance().toPattern() returned wrong pattern", 157 "#,##0", format.toPattern()); 158 assertEquals( 159 "Test2: NumberFormat.getIntegerInstance().format(-35.76) returned wrong value", 160 "-36", format.format(-35.76)); 161 assertEquals( 162 "Test3: NumberFormat.getIntegerInstance().parse(\"-36\") returned wrong number", 163 new Long(-36), format.parse("-36")); 164 assertEquals( 165 "Test4: NumberFormat.getIntegerInstance().parseObject(\"-36\") returned wrong number", 166 new Long(-36), format.parseObject("-36")); 167 assertEquals( 168 "Test5: NumberFormat.getIntegerInstance().getMaximumFractionDigits() returned wrong value", 169 0, format.getMaximumFractionDigits()); 170 assertTrue("Test6: NumberFormat.getIntegerInstance().isParseIntegerOnly() returned wrong value", 171 format.isParseIntegerOnly()); 172 173 // try with a locale that has a different integer pattern 174 format = (DecimalFormat) NumberFormat.getIntegerInstance(arLocale); 175 assertEquals( 176 "Test7: NumberFormat.getIntegerInstance(new Locale(\"ar\", \"AE\")).toPattern() returned wrong pattern", 177 "#,##0;#,##0-", format.toPattern()); 178 assertEquals( 179 "Test8: NumberFormat.getIntegerInstance(new Locale(\"ar\", \"AE\")).format(-35.76) returned wrong value", 180 "\u0666-", format.format(-6)); 181 assertEquals( 182 "Test9: NumberFormat.getIntegerInstance(new Locale(\"ar\", \"AE\")).parse(\"-36-\") returned wrong number", 183 new Long(-36), format.parse("36-")); 184 assertEquals( 185 "Test10: NumberFormat.getIntegerInstance(new Locale(\"ar\", \"AE\")).parseObject(\"36-\") returned wrong number", 186 new Long(-36), format.parseObject("36-")); 187 188 assertEquals( 189 "Test11: NumberFormat.getIntegerInstance(new Locale(\"ar\", \"AE\")).getMaximumFractionDigits() returned wrong value", 190 0, format.getMaximumFractionDigits()); 191 assertTrue( 192 "Test12: NumberFormat.getIntegerInstance(new Locale(\"ar\", \"AE\")).isParseIntegerOnly() returned wrong value", 193 format.isParseIntegerOnly()); 194 } 195 196 /** 197 * @tests java.text.NumberFormat#getCurrency() 198 */ 199 public void test_getCurrency() { 200 // Test for method java.util.Currency getCurrency() 201 202 // a subclass that supports currency formatting 203 Currency currH = Currency.getInstance("HUF"); 204 NumberFormat format = NumberFormat.getInstance(new Locale("hu", "HU")); 205 assertSame("Returned incorrect currency", currH, format.getCurrency()); 206 207 // a subclass that doesn't support currency formatting 208 ChoiceFormat cformat = new ChoiceFormat( 209 "0#Less than one|1#one|1<Between one and two|2<Greater than two"); 210 try { 211 ((NumberFormat) cformat).getCurrency(); 212 fail("Expected UnsupportedOperationException"); 213 } catch (UnsupportedOperationException e) { 214 } 215 } 216 217 /** 218 * @tests java.text.NumberFormat#getMaximumIntegerDigits() 219 */ 220 public void test_getMaximumIntegerDigits() { 221 NumberFormat format = NumberFormat.getInstance(); 222 format.setMaximumIntegerDigits(2); 223 assertEquals("Wrong result", "23", format.format(123)); 224 } 225 226 /** 227 * @tests java.text.NumberFormat#setCurrency(java.util.Currency) 228 */ 229 public void test_setCurrencyLjava_util_Currency() { 230 // Test for method void setCurrency(java.util.Currency) 231 // a subclass that supports currency formatting 232 Currency currA = Currency.getInstance("ARS"); 233 NumberFormat format = NumberFormat.getInstance(new Locale("hu", "HU")); 234 format.setCurrency(currA); 235 assertSame("Returned incorrect currency", currA, format.getCurrency()); 236 237 // a subclass that doesn't support currency formatting 238 ChoiceFormat cformat = new ChoiceFormat( 239 "0#Less than one|1#one|1<Between one and two|2<Greater than two"); 240 try { 241 ((NumberFormat) cformat).setCurrency(currA); 242 fail("Expected UnsupportedOperationException"); 243 } catch (UnsupportedOperationException e) { 244 } 245 } 246 /** 247 * @tests java.text.NumberFormat#parseObject(java.lang.String, java.text.ParsePosition) 248 */ 249 public void test_parseObjectLjava_lang_StringLjava_text_ParsePosition() { 250 // regression test for HARMONY-1003 251 assertNull(NumberFormat.getInstance().parseObject("0", new ParsePosition(-1))); 252 253 // Regression for HARMONY-1685 254 try { 255 NumberFormat.getInstance().parseObject("test", null); 256 fail("NullPointerException expected"); 257 } catch (NullPointerException e) { 258 //expected 259 } 260 } 261 262 protected void setUp() { 263 } 264 265 protected void tearDown() { 266 } 267 268 /** 269 * @test java.text.NumberFormat#setRoundingMode(java.math.RoundingMode) 270 */ 271 public void test_setRoundingMode_NullRoundingMode() { 272 try { 273 // Create a subclass ChoiceFormat which doesn't support 274 // RoundingMode 275 ChoiceFormat choiceFormat = new ChoiceFormat( 276 "0#Less than one|1#one|1<Between one and two|2<Greater than two"); 277 ((NumberFormat) choiceFormat).setRoundingMode(null); 278 // Follow the behavior of RI 279 fail("UnsupportedOperationException expected"); 280 } catch (UnsupportedOperationException e) { 281 // expected 282 } 283 } 284 285 /** 286 * @test java.text.NumberFormat#setRoundingMode(java.math.RoundingMode) 287 */ 288 public void test_setRoundingMode_Normal() { 289 try { 290 // Create a subclass ChoiceFormat which doesn't support 291 // RoundingMode 292 ChoiceFormat choiceFormat = new ChoiceFormat( 293 "0#Less than one|1#one|1<Between one and two|2<Greater than two"); 294 ((NumberFormat) choiceFormat).setRoundingMode(RoundingMode.CEILING); 295 fail("UnsupportedOperationException expected"); 296 } catch (UnsupportedOperationException e) { 297 // expected 298 } 299 } 300 301} 302