1 16 17 package org.apache.commons.beanutils.locale; 18 19 20 import java.lang.reflect.InvocationTargetException ; 21 import java.sql.Date ; 22 import java.sql.Time ; 23 import java.sql.Timestamp ; 24 import java.util.HashMap ; 25 import java.util.Locale ; 26 import java.text.DecimalFormat ; 27 import java.text.NumberFormat ; 28 29 import junit.framework.TestCase; 30 import junit.framework.Test; 31 import junit.framework.TestSuite; 32 import org.apache.commons.beanutils.ConvertUtils; 33 import org.apache.commons.beanutils.ConversionException; 34 35 36 46 47 public class LocaleConvertUtilsTestCase extends TestCase { 48 49 51 private char m_decimalSeparator; 52 53 55 56 61 public LocaleConvertUtilsTestCase(String name) { 62 super(name); 63 } 64 65 66 68 69 72 public void setUp() { 73 74 LocaleConvertUtils.deregister(); 75 76 NumberFormat nf = DecimalFormat.getNumberInstance(); 77 String result = nf.format(1.1); 78 79 m_decimalSeparator = result.charAt(1); 81 82 83 } 84 85 86 89 public static Test suite() { 90 return (new TestSuite(LocaleConvertUtilsTestCase.class)); 91 } 92 93 94 97 public void tearDown() { 98 ; } 100 101 102 104 105 108 public void fixmetestNegativeIntegerArray() { 109 110 fail("Array conversions not implemented yet."); 111 112 Object value = null; 113 int intArray[] = new int[0]; 114 115 value = LocaleConvertUtils.convert((String ) null, intArray.getClass()); 116 checkIntegerArray(value, intArray); 117 value = LocaleConvertUtils.convert("a", intArray.getClass()); 118 checkIntegerArray(value, intArray); 119 value = LocaleConvertUtils.convert("{ a }", intArray.getClass()); 120 checkIntegerArray(value, intArray); 121 value = LocaleConvertUtils.convert("1a3", intArray.getClass()); 122 checkIntegerArray(value, intArray); 123 value = LocaleConvertUtils.convert("{ 1a3 }", intArray.getClass()); 124 checkIntegerArray(value, intArray); 125 value = LocaleConvertUtils.convert("0,1a3", intArray.getClass()); 126 checkIntegerArray(value, intArray); 127 value = LocaleConvertUtils.convert("{ 0, 1a3 }", intArray.getClass()); 128 checkIntegerArray(value, intArray); 129 130 } 131 132 133 137 public void fixmetestNegativeScalar() { 138 139 Object value = null; 140 141 148 149 150 try { 151 value = LocaleConvertUtils.convert("foo", Byte.TYPE); 152 fail("Should have thrown conversion exception (1)"); 153 } catch (ConversionException e) { 154 ; } 156 157 try { 158 value = LocaleConvertUtils.convert("foo", Byte .class); 159 fail("Should have thrown conversion exception (2)"); 160 } catch (ConversionException e) { 161 ; } 163 164 172 173 try { 174 value = LocaleConvertUtils.convert("foo", Double.TYPE); 175 fail("Should have thrown conversion exception (3)"); 176 } catch (ConversionException e) { 177 ; } 179 180 try { 181 value = LocaleConvertUtils.convert("foo", Double .class); 182 fail("Should have thrown conversion exception (4)"); 183 } catch (ConversionException e) { 184 ; } 186 187 try { 188 value = LocaleConvertUtils.convert("foo", Float.TYPE); 189 fail("Should have thrown conversion exception (5)"); 190 } catch (ConversionException e) { 191 ; } 193 194 try { 195 value = LocaleConvertUtils.convert("foo", Float .class); 196 fail("Should have thrown conversion exception (6)"); 197 } catch (ConversionException e) { 198 ; } 200 201 try { 202 value = LocaleConvertUtils.convert("foo", Integer.TYPE); 203 fail("Should have thrown conversion exception (7)"); 204 } catch (ConversionException e) { 205 ; } 207 208 try { 209 value = LocaleConvertUtils.convert("foo", Integer .class); 210 fail("Should have thrown conversion exception (8)"); 211 } catch (ConversionException e) { 212 ; } 214 215 try { 216 value = LocaleConvertUtils.convert("foo", Byte.TYPE); 217 fail("Should have thrown conversion exception (9)"); 218 } catch (ConversionException e) { 219 ; } 221 222 try { 223 value = LocaleConvertUtils.convert("foo", Long .class); 224 fail("Should have thrown conversion exception (10)"); 225 } catch (ConversionException e) { 226 ; } 228 229 try { 230 value = LocaleConvertUtils.convert("foo", Short.TYPE); 231 fail("Should have thrown conversion exception (11)"); 232 } catch (ConversionException e) { 233 ; } 235 236 try { 237 value = LocaleConvertUtils.convert("foo", Short .class); 238 fail("Should have thrown conversion exception (12)"); 239 } catch (ConversionException e) { 240 ; } 242 243 } 244 245 246 249 public void fixmetestNegativeStringArray() { 250 251 fail("Array conversions not implemented yet."); 252 253 Object value = null; 254 String stringArray[] = new String [0]; 255 256 value = LocaleConvertUtils.convert((String ) null, stringArray.getClass()); 257 checkStringArray(value, stringArray); 258 } 259 260 261 264 public void fixmetestObjectToStringArray() { 265 266 fail("Array conversions not implemented yet."); 267 int intArray0[] = new int[0]; 268 int intArray1[] = {123}; 269 int intArray2[] = {123, 456}; 270 String stringArray0[] = new String [0]; 271 String stringArray1[] = {"abc"}; 272 String stringArray2[] = {"abc", "def"}; 273 274 assertEquals("intArray0", null, 275 LocaleConvertUtils.convert(intArray0)); 276 assertEquals("intArray1", "123", 277 LocaleConvertUtils.convert(intArray1)); 278 assertEquals("intArray2", "123", 279 LocaleConvertUtils.convert(intArray2)); 280 281 assertEquals("stringArray0", null, 282 LocaleConvertUtils.convert(stringArray0)); 283 assertEquals("stringArray1", "abc", 284 LocaleConvertUtils.convert(stringArray1)); 285 assertEquals("stringArray2", "abc", 286 LocaleConvertUtils.convert(stringArray2)); 287 288 } 289 290 291 294 public void testObjectToStringScalar() { 295 296 assertEquals("Boolean->String", "false", 297 LocaleConvertUtils.convert(Boolean.FALSE)); 298 assertEquals("Boolean->String", "true", 299 LocaleConvertUtils.convert(Boolean.TRUE)); 300 assertEquals("Byte->String", "123", 301 LocaleConvertUtils.convert(new Byte ((byte) 123))); 302 assertEquals("Character->String", "a", 303 LocaleConvertUtils.convert(new Character ('a'))); 304 assertEquals("Double->String", "123" + m_decimalSeparator + "4", 305 LocaleConvertUtils.convert(new Double ((double) 123.4))); 306 assertEquals("Float->String", "123" + m_decimalSeparator + "4", 307 LocaleConvertUtils.convert(new Float ((float) 123.4))); 308 assertEquals("Integer->String", "123", 309 LocaleConvertUtils.convert(new Integer ((int) 123))); 310 assertEquals("Long->String", "123", 311 LocaleConvertUtils.convert(new Long ((long) 123))); 312 assertEquals("Short->String", "123", 313 LocaleConvertUtils.convert(new Short ((short) 123))); 314 assertEquals("String->String", "abc", 315 LocaleConvertUtils.convert("abc")); 316 assertEquals("String->String null", null, 317 LocaleConvertUtils.convert(null)); 318 319 } 320 321 322 325 public void fixmetestPositiveArray() { 326 327 fail("Array conversions not implemented yet."); 328 329 String values1[] = {"10", "20", "30"}; 330 Object value = LocaleConvertUtils.convert(values1, Integer.TYPE); 331 int shape[] = new int[0]; 332 assertEquals(shape.getClass(), value.getClass()); 333 int results1[] = (int[]) value; 334 assertEquals(results1[0], 10); 335 assertEquals(results1[1], 20); 336 assertEquals(results1[2], 30); 337 338 String values2[] = {"100", "200", "300"}; 339 value = LocaleConvertUtils.convert(values2, shape.getClass()); 340 assertEquals(shape.getClass(), value.getClass()); 341 int results2[] = (int[]) value; 342 assertEquals(results2[0], 100); 343 assertEquals(results2[1], 200); 344 assertEquals(results2[2], 300); 345 } 346 347 348 351 public void fixmetestPositiveIntegerArray() { 352 353 fail("Array conversions not implemented yet."); 354 355 Object value = null; 356 int intArray[] = new int[0]; 357 int intArray1[] = new int[]{0}; 358 int intArray2[] = new int[]{0, 10}; 359 360 value = LocaleConvertUtils.convert("{ }", intArray.getClass()); 361 checkIntegerArray(value, intArray); 362 363 value = LocaleConvertUtils.convert("0", intArray.getClass()); 364 checkIntegerArray(value, intArray1); 365 value = LocaleConvertUtils.convert(" 0 ", intArray.getClass()); 366 checkIntegerArray(value, intArray1); 367 value = LocaleConvertUtils.convert("{ 0 }", intArray.getClass()); 368 checkIntegerArray(value, intArray1); 369 370 value = LocaleConvertUtils.convert("0,10", intArray.getClass()); 371 checkIntegerArray(value, intArray2); 372 value = LocaleConvertUtils.convert("0 10", intArray.getClass()); 373 checkIntegerArray(value, intArray2); 374 value = LocaleConvertUtils.convert("{0,10}", intArray.getClass()); 375 checkIntegerArray(value, intArray2); 376 value = LocaleConvertUtils.convert("{0 10}", intArray.getClass()); 377 checkIntegerArray(value, intArray2); 378 value = LocaleConvertUtils.convert("{ 0, 10 }", intArray.getClass()); 379 checkIntegerArray(value, intArray2); 380 value = LocaleConvertUtils.convert("{ 0 10 }", intArray.getClass()); 381 checkIntegerArray(value, intArray2); 382 } 383 384 385 388 public void testPositiveScalar() { 389 Object value = null; 390 391 456 457 value = LocaleConvertUtils.convert("123", Byte.TYPE); 458 assertTrue(value instanceof Byte ); 459 assertEquals(((Byte ) value).byteValue(), (byte) 123); 460 461 value = LocaleConvertUtils.convert("123", Byte .class); 462 assertTrue(value instanceof Byte ); 463 assertEquals(((Byte ) value).byteValue(), (byte) 123); 464 465 474 479 480 value = LocaleConvertUtils.convert("123" + m_decimalSeparator + "456", Double.TYPE); 481 assertTrue(value instanceof Double ); 482 assertEquals(((Double ) value).doubleValue(), (double) 123.456, 483 (double) 0.005); 484 485 value = LocaleConvertUtils.convert("123" + m_decimalSeparator + "456", Double .class); 486 assertTrue(value instanceof Double ); 487 assertEquals(((Double ) value).doubleValue(), (double) 123.456, 488 (double) 0.005); 489 490 value = LocaleConvertUtils.convert("123" + m_decimalSeparator + "456", Float.TYPE); 491 assertTrue(value instanceof Float ); 492 assertEquals(((Float ) value).floatValue(), (float) 123.456, 493 (float) 0.005); 494 495 value = LocaleConvertUtils.convert("123" + m_decimalSeparator + "456", Float .class); 496 assertTrue(value instanceof Float ); 497 assertEquals(((Float ) value).floatValue(), (float) 123.456, 498 (float) 0.005); 499 500 value = LocaleConvertUtils.convert("123", Integer.TYPE); 501 assertTrue(value instanceof Integer ); 502 assertEquals(((Integer ) value).intValue(), (int) 123); 503 504 value = LocaleConvertUtils.convert("123", Integer .class); 505 assertTrue(value instanceof Integer ); 506 assertEquals(((Integer ) value).intValue(), (int) 123); 507 508 value = LocaleConvertUtils.convert("123", Long.TYPE); 509 assertTrue(value instanceof Long ); 510 assertEquals(((Long ) value).longValue(), (long) 123); 511 512 value = LocaleConvertUtils.convert("123456", Long .class); 513 assertTrue(value instanceof Long ); 514 assertEquals(((Long ) value).longValue(), (long) 123456); 515 516 525 526 String input = null; 527 528 input = "2002-03-17"; 529 value = LocaleConvertUtils.convert(input, Date .class); 530 assertTrue(value instanceof Date ); 531 assertEquals(input, value.toString()); 532 533 input = "20:30:40"; 534 value = LocaleConvertUtils.convert(input, Time .class); 535 assertTrue(value instanceof Time ); 536 assertEquals(input, value.toString()); 537 538 input = "2002-03-17 20:30:40.0"; 539 value = LocaleConvertUtils.convert(input, Timestamp .class); 540 assertTrue(value instanceof Timestamp ); 541 assertEquals(input, value.toString()); 542 543 } 544 545 546 549 public void fixmetestPositiveStringArray() { 550 551 fail("Array conversions not implemented yet."); 552 553 Object value = null; 554 String stringArray[] = new String [0]; 555 String stringArray1[] = new String [] 556 {"abc"}; 557 String stringArray2[] = new String [] 558 {"abc", "de,f"}; 559 560 value = LocaleConvertUtils.convert("", stringArray.getClass()); 561 checkStringArray(value, stringArray); 562 value = LocaleConvertUtils.convert(" ", stringArray.getClass()); 563 checkStringArray(value, stringArray); 564 value = LocaleConvertUtils.convert("{}", stringArray.getClass()); 565 checkStringArray(value, stringArray); 566 value = LocaleConvertUtils.convert("{ }", stringArray.getClass()); 567 checkStringArray(value, stringArray); 568 569 value = LocaleConvertUtils.convert("abc", stringArray.getClass()); 570 checkStringArray(value, stringArray1); 571 value = LocaleConvertUtils.convert("{abc}", stringArray.getClass()); 572 checkStringArray(value, stringArray1); 573 value = LocaleConvertUtils.convert("\"abc\"", stringArray.getClass()); 574 checkStringArray(value, stringArray1); 575 value = LocaleConvertUtils.convert("{\"abc\"}", stringArray.getClass()); 576 checkStringArray(value, stringArray1); 577 value = LocaleConvertUtils.convert("'abc'", stringArray.getClass()); 578 checkStringArray(value, stringArray1); 579 value = LocaleConvertUtils.convert("{'abc'}", stringArray.getClass()); 580 checkStringArray(value, stringArray1); 581 582 value = LocaleConvertUtils.convert("abc 'de,f'", 583 stringArray.getClass()); 584 checkStringArray(value, stringArray2); 585 value = LocaleConvertUtils.convert("{abc, 'de,f'}", 586 stringArray.getClass()); 587 checkStringArray(value, stringArray2); 588 value = LocaleConvertUtils.convert("\"abc\",\"de,f\"", 589 stringArray.getClass()); 590 checkStringArray(value, stringArray2); 591 value = LocaleConvertUtils.convert("{\"abc\" 'de,f'}", 592 stringArray.getClass()); 593 checkStringArray(value, stringArray2); 594 value = LocaleConvertUtils.convert("'abc' 'de,f'", 595 stringArray.getClass()); 596 checkStringArray(value, stringArray2); 597 value = LocaleConvertUtils.convert("{'abc', \"de,f\"}", 598 stringArray.getClass()); 599 checkStringArray(value, stringArray2); 600 601 } 602 603 604 606 607 private void checkIntegerArray(Object value, int intArray[]) { 608 609 assertNotNull("Returned value is not null", value); 610 assertEquals("Returned value is int[]", 611 intArray.getClass(), value.getClass()); 612 int results[] = (int[]) value; 613 assertEquals("Returned array length", intArray.length, results.length); 614 for (int i = 0; i < intArray.length; i++) { 615 assertEquals("Returned array value " + i, 616 intArray[i], results[i]); 617 } 618 619 } 620 621 622 private void checkStringArray(Object value, String stringArray[]) { 623 624 assertNotNull("Returned value is not null", value); 625 assertEquals("Returned value is String[]", 626 stringArray.getClass(), value.getClass()); 627 String results[] = (String []) value; 628 assertEquals("Returned array length", 629 stringArray.length, results.length); 630 for (int i = 0; i < stringArray.length; i++) { 631 assertEquals("Returned array value " + i, 632 stringArray[i], results[i]); 633 } 634 635 } 636 637 638 } 639 640 | Popular Tags |