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 org.apache.harmony.tests.java.text; 18 19 import java.math.RoundingMode; 20 import java.text.ChoiceFormat; 21 import java.text.DecimalFormat; 22 import java.text.FieldPosition; 23 import java.text.NumberFormat; 24 import java.text.ParseException; 25 import java.text.ParsePosition; 26 import java.util.Currency; 27 import java.util.Locale; 28 29 public 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 // Previous versions of android use just the positive format string (ICU4C) although now we 176 // use '<positive_format>;<negative_format>' because of ICU4J denormalization. 177 String variant = (format.toPattern().indexOf(';') > 0) ? "#,##0;-#,##0" : "#,##0"; 178 assertEquals( 179 "Test7: NumberFormat.getIntegerInstance(new Locale(\"ar\", \"AE\")).toPattern() returned wrong pattern", 180 variant, format.toPattern()); 181 assertEquals( 182 "Test8: NumberFormat.getIntegerInstance(new Locale(\"ar\", \"AE\")).format(-6) returned wrong value", 183 "-\u0666", format.format(-6)); 184 assertEquals( 185 "Test9: NumberFormat.getIntegerInstance(new Locale(\"ar\", \"AE\")).parse(\"-36-\") returned wrong number", 186 new Long(36), format.parse("36-")); 187 assertEquals( 188 "Test10: NumberFormat.getIntegerInstance(new Locale(\"ar\", \"AE\")).parseObject(\"36-\") returned wrong number", 189 new Long(36), format.parseObject("36-")); 190 191 assertEquals( 192 "Test11: NumberFormat.getIntegerInstance(new Locale(\"ar\", \"AE\")).getMaximumFractionDigits() returned wrong value", 193 0, format.getMaximumFractionDigits()); 194 assertTrue( 195 "Test12: NumberFormat.getIntegerInstance(new Locale(\"ar\", \"AE\")).isParseIntegerOnly() returned wrong value", 196 format.isParseIntegerOnly()); 197 } 198 199 /** 200 * @tests java.text.NumberFormat#getCurrency() 201 */ 202 public void test_getCurrency() { 203 // Test for method java.util.Currency getCurrency() 204 205 // a subclass that supports currency formatting 206 Currency currH = Currency.getInstance("HUF"); 207 NumberFormat format = NumberFormat.getInstance(new Locale("hu", "HU")); 208 assertSame("Returned incorrect currency", currH, format.getCurrency()); 209 210 // a subclass that doesn't support currency formatting 211 ChoiceFormat cformat = new ChoiceFormat( 212 "0#Less than one|1#one|1<Between one and two|2<Greater than two"); 213 try { 214 ((NumberFormat) cformat).getCurrency(); 215 fail("Expected UnsupportedOperationException"); 216 } catch (UnsupportedOperationException e) { 217 } 218 } 219 220 /** 221 * @tests java.text.NumberFormat#getMaximumIntegerDigits() 222 */ 223 public void test_getMaximumIntegerDigits() { 224 NumberFormat format = NumberFormat.getInstance(); 225 format.setMaximumIntegerDigits(2); 226 assertEquals("Wrong result", "23", format.format(123)); 227 } 228 229 /** 230 * @tests java.text.NumberFormat#setCurrency(java.util.Currency) 231 */ 232 public void test_setCurrencyLjava_util_Currency() { 233 // Test for method void setCurrency(java.util.Currency) 234 // a subclass that supports currency formatting 235 Currency currA = Currency.getInstance("ARS"); 236 NumberFormat format = NumberFormat.getInstance(new Locale("hu", "HU")); 237 format.setCurrency(currA); 238 assertSame("Returned incorrect currency", currA, format.getCurrency()); 239 240 // a subclass that doesn't support currency formatting 241 ChoiceFormat cformat = new ChoiceFormat( 242 "0#Less than one|1#one|1<Between one and two|2<Greater than two"); 243 try { 244 ((NumberFormat) cformat).setCurrency(currA); 245 fail("Expected UnsupportedOperationException"); 246 } catch (UnsupportedOperationException e) { 247 } 248 } 249 /** 250 * @tests java.text.NumberFormat#parseObject(java.lang.String, java.text.ParsePosition) 251 */ 252 public void test_parseObjectLjava_lang_StringLjava_text_ParsePosition() { 253 // regression test for HARMONY-1003 254 assertNull(NumberFormat.getInstance().parseObject("0", new ParsePosition(-1))); 255 256 // Regression for HARMONY-1685 257 try { 258 NumberFormat.getInstance().parseObject("test", null); 259 fail("NullPointerException expected"); 260 } catch (NullPointerException e) { 261 //expected 262 } 263 } 264 265 protected void setUp() { 266 } 267 268 protected void tearDown() { 269 } 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 public void test_setRoundingMode_Normal() { 286 try { 287 // Create a subclass ChoiceFormat which doesn't support 288 // RoundingMode 289 ChoiceFormat choiceFormat = new ChoiceFormat( 290 "0#Less than one|1#one|1<Between one and two|2<Greater than two"); 291 ((NumberFormat) choiceFormat).setRoundingMode(RoundingMode.CEILING); 292 fail("UnsupportedOperationException expected"); 293 } catch (UnsupportedOperationException e) { 294 // expected 295 } 296 } 297 298 } 299