1 16 package net.sf.dozer.util.mapping.converters; 17 18 import java.math.BigDecimal ; 19 import java.math.BigInteger ; 20 import java.text.DateFormat ; 21 import java.text.SimpleDateFormat ; 22 import java.util.Calendar ; 23 import java.util.GregorianCalendar ; 24 25 import junit.framework.Assert; 26 import net.sf.dozer.util.mapping.DozerTestBase; 27 import net.sf.dozer.util.mapping.util.DateFormatContainer; 28 29 32 public class PrimitiveOrWrapperConverterTest extends DozerTestBase { 33 34 private PrimitiveOrWrapperConverter converter = new PrimitiveOrWrapperConverter(); 35 36 public void testConvertPrimitiveOrWrapperEmptyString() throws Exception { 37 String input = ""; 39 Object output = (String ) converter.convert(input, String .class, null); 40 41 assertNotNull("output object should not be null", output); 42 assertEquals("invalid output value", input, output); 43 } 44 45 public void testConvertPrimitiveOrWrapperEmptyString2() throws Exception { 46 String input = ""; 48 Integer output = (Integer ) converter.convert(input, Integer .class, null); 49 50 assertNull("output object should be null", output); 51 } 52 53 public void testConvertInteger() throws Exception { 54 Object [] input = { String.valueOf(Integer.MIN_VALUE), "-17", "-1", "0", "1", "17", 55 String.valueOf(Integer.MAX_VALUE), new Byte ((byte) 7), new Short ((short) 8), new Integer (9), new Long (10), 56 new Float (11.1), new Double (12.2) }; 57 Integer [] expected = { new Integer (Integer.MIN_VALUE), new Integer (-17), new Integer (-1), new Integer (0), 58 new Integer (1), new Integer (17), new Integer (Integer.MAX_VALUE), new Integer (7), new Integer (8), 59 new Integer (9), new Integer (10), new Integer (11), new Integer (12) }; 60 61 for (int i = 0; i < expected.length; i++) { 62 assertEquals(input[i].getClass().getName() + " to Integer", expected[i], converter.convert( 63 input[i], Integer .class, null)); 64 assertEquals(input[i].getClass().getName() + " to int", expected[i], converter.convert( 65 input[i], Integer.TYPE, null)); 66 } 67 } 68 69 public void testConvertIntegerWithFailure() throws Exception { 70 Object input = "three"; 71 try { 72 converter.convert(input, Integer .class, null); 73 fail("Should have thrown conversion exception"); 74 } catch (ConversionException e) { 75 assertTrue(true); 76 } 77 } 78 79 public void testConvertDouble() throws Exception { 80 Object [] input = { String.valueOf(Double.MIN_VALUE), "-17.2", "-1.1", "0.0", "1.1", "17.2", 81 String.valueOf(Double.MAX_VALUE), new Byte ((byte) 7), new Short ((short) 8), new Integer (9), new Long (10), 82 new Float (11.1), new Double (12.2) }; 83 Double [] expected = { new Double (Double.MIN_VALUE), new Double (-17.2), new Double (-1.1), new Double (0.0), 84 new Double (1.1), new Double (17.2), new Double (Double.MAX_VALUE), new Double (7), new Double (8), new Double (9), 85 new Double (10), new Double (11.1), new Double (12.2) }; 86 87 for (int i = 0; i < expected.length; i++) { 88 assertEquals(input[i].getClass().getName() + " to Double", expected[i].doubleValue(), ((Double ) (converter 89 .convert(input[i], Double .class, null))).doubleValue(), 0.00001D); 90 assertEquals(input[i].getClass().getName() + " to double", expected[i].doubleValue(), ((Double ) (converter 91 .convert(input[i], Double.TYPE, null))).doubleValue(), 0.00001D); 92 } 93 } 94 95 public void testConvertFloat() throws Exception { 96 Object [] input = { String.valueOf(Float.MIN_VALUE), "-17.2", "-1.1", "0.0", "1.1", "17.2", 97 String.valueOf(Float.MAX_VALUE), new Byte ((byte) 7), new Short ((short) 8), new Integer (9), new Long (10), 98 new Float (11.1), new Double (12.2) }; 99 Float [] expected = { new Float (Float.MIN_VALUE), new Float (-17.2), new Float (-1.1), new Float (0.0), new Float (1.1), 100 new Float (17.2), new Float (Float.MAX_VALUE), new Float (7), new Float (8), new Float (9), new Float (10), 101 new Float (11.1), new Float (12.2) }; 102 103 for (int i = 0; i < expected.length; i++) { 104 assertEquals(input[i].getClass().getName() + " to Float", expected[i].floatValue(), ((Float ) (converter 105 .convert(input[i], Float .class, null))).floatValue(), 0.00001); 106 assertEquals(input[i].getClass().getName() + " to float", expected[i].floatValue(), ((Float ) (converter 107 .convert(input[i], Float.TYPE, null))).floatValue(), 0.00001); 108 } 109 } 110 111 public void testConvertLong() throws Exception { 112 Object [] input = { String.valueOf(Long.MIN_VALUE), "-17", "-1", "0", "1", "17", String.valueOf(Long.MAX_VALUE), 113 new Byte ((byte) 7), new Short ((short) 8), new Integer (9), new Long (10), new Float (11.1), new Double (12.2) }; 114 Long [] expected = { new Long (Long.MIN_VALUE), new Long (-17), new Long (-1), new Long (0), new Long (1), new Long (17), 115 new Long (Long.MAX_VALUE), new Long (7), new Long (8), new Long (9), new Long (10), new Long (11), new Long (12) }; 116 117 for (int i = 0; i < expected.length; i++) { 118 assertEquals(input[i].getClass().getName() + " to Long", expected[i], converter.convert( 119 input[i], Long .class, null)); 120 assertEquals(input[i].getClass().getName() + " to long", expected[i], converter.convert( 121 input[i], Long.TYPE, null)); 122 } 123 } 124 125 public void testConvertBigDecimal() throws Exception { 126 Object [] input = { String.valueOf(Integer.MIN_VALUE), "-17", "-1", "0", "1", "17", 127 String.valueOf(Integer.MAX_VALUE), new Byte ((byte) 7), new Short ((short) 8), new Integer (9), new Long (10), }; 128 BigDecimal [] expected = { new BigDecimal (Integer.MIN_VALUE), new BigDecimal (-17), new BigDecimal (-1), 129 new BigDecimal (0), new BigDecimal (1), new BigDecimal (17), new BigDecimal (Integer.MAX_VALUE), new BigDecimal (7), 130 new BigDecimal (8), new BigDecimal (9), new BigDecimal (10) }; 131 132 for (int i = 0; i < expected.length; i++) { 133 assertEquals(input[i].getClass().getName() + " to BigDecimal", expected[i], converter 134 .convert(input[i], BigDecimal .class, null)); 135 } 136 } 137 138 public void testConvertBigInteger() throws Exception { 139 Object [] input = { String.valueOf(Integer.MIN_VALUE), "-17", "-1", "0", "1", "17", 140 String.valueOf(Integer.MAX_VALUE), new Byte ((byte) 7), new Short ((short) 8), new Integer (9), new Long (10), }; 141 BigInteger [] expected = { new BigInteger (String.valueOf(Integer.MIN_VALUE)), new BigInteger ("-17"), new BigInteger ("-1"), 142 new BigInteger ("0"), new BigInteger ("1"), new BigInteger ("17"), new BigInteger (String.valueOf(Integer.MAX_VALUE)), new BigInteger ("7"), 143 new BigInteger ("8"), new BigInteger ("9"), new BigInteger ("10")}; 144 145 for (int i = 0; i < expected.length; i++) { 146 assertEquals(input[i].getClass().getName() + " to BigDecimal", expected[i], converter 147 .convert(input[i], BigInteger .class, null)); 148 } 149 } 150 151 public void testConvertShort() throws Exception { 152 Object [] input = { String.valueOf(Short.MIN_VALUE), "-17", "-1", "0", "1", "17", String.valueOf(Short.MAX_VALUE), 153 new Byte ((byte) 7), new Short ((short) 8), new Integer (9), new Long (10), new Float (11.1), new Double (12.2) }; 154 Short [] expected = { new Short (Short.MIN_VALUE), new Short ((short) -17), new Short ((short) -1), 155 new Short ((short) 0), new Short ((short) 1), new Short ((short) 17), new Short (Short.MAX_VALUE), 156 new Short ((short) 7), new Short ((short) 8), new Short ((short) 9), new Short ((short) 10), new Short ((short) 11), 157 new Short ((short) 12) }; 158 159 for (int i = 0; i < expected.length; i++) { 160 assertEquals(input[i].getClass().getName() + " to Short", expected[i], converter.convert( 161 input[i], Short .class, null)); 162 assertEquals(input[i].getClass().getName() + " to short", expected[i], converter.convert( 163 input[i], Short.TYPE, null)); 164 } 165 } 166 167 public void testConvertDate() throws Exception { 168 long time = Calendar.getInstance().getTimeInMillis(); 169 170 java.util.Date date = new java.util.Date (time); 171 172 Calendar cal = Calendar.getInstance(); 173 cal.setTimeInMillis(time); 174 175 GregorianCalendar gregCal = new GregorianCalendar (); 176 gregCal.setTimeInMillis(time); 177 178 DateFormat [] dateFormats = new DateFormat [] {DateFormat.getDateInstance(DateFormat.FULL), 179 DateFormat.getDateInstance(DateFormat.LONG), DateFormat.getDateInstance(DateFormat.MEDIUM), 180 new SimpleDateFormat ("MM/dd/yyyy"), new SimpleDateFormat ("MM/dd/yyyy HH:mm:ss:SS")}; 181 182 Object [] input = { new java.sql.Time (time), new java.sql.Timestamp (time), new java.sql.Date (time), cal, gregCal, 184 String.valueOf(time) }; 185 Object expected = new java.util.Date (time); 186 Object result = null; 187 188 for (int i = 0; i < input.length; i++) { 189 DateFormatContainer dfc = new DateFormatContainer(null, null); 190 dfc.setDateFormat(null); 191 result = converter.convert(input[i], java.util.Date .class, dfc); 192 assertTrue("result should be instance of java.util.Date", result instanceof java.util.Date ); 193 assertEquals(input[i].getClass().getName() + " to java.util.Date", expected, result); 194 } 195 196 for (int i = 0; i < dateFormats.length; i++) { 197 String dateStr = dateFormats[i].format(date); 198 DateFormatContainer dfc = new DateFormatContainer(null, null); 199 dfc.setDateFormat(dateFormats[i]); 200 result = converter.convert(dateStr, java.util.Date .class, dfc); 201 assertEquals("String to java.util.Date for input: " + dateStr,dateFormats[i].parse(dateStr), result); 202 assertEquals("String to java.util.Date for input: " + dateStr, dateStr,dateFormats[i].format(result)); 203 } 204 205 input = new Object [] { new java.util.Date (time), new java.sql.Time (time), new java.sql.Timestamp (time), cal, 207 gregCal, String.valueOf(time) }; 208 expected = new java.sql.Date (time); 209 210 for (int i = 0; i < input.length; i++) { 211 DateFormatContainer dfc = new DateFormatContainer(null, null); 212 dfc.setDateFormat(null); 213 result = converter.convert(input[i], java.sql.Date .class, dfc); 214 assertTrue("result should be instance of java.sql.Date", result instanceof java.sql.Date ); 215 assertEquals(input[i].getClass().getName() + " to java.sql.Date", expected, result); 216 } 217 218 for (int i = 0; i < dateFormats.length; i++) { 219 String dateStr = dateFormats[i].format(date); 220 DateFormatContainer dfc = new DateFormatContainer(null, null); 221 dfc.setDateFormat(dateFormats[i]); 222 result = converter.convert(dateStr, java.sql.Date .class, dfc); 223 assertEquals("String to java.sql.Date for input: " + dateStr, dateFormats[i].parse(dateStr), result); 224 assertEquals("String to java.sql.Date for input: " + dateStr, dateStr,dateFormats[i].format(result)); 225 } 226 227 input = new Object [] { new java.util.Date (time), new java.sql.Date (time), new java.sql.Timestamp (time), cal, 229 gregCal, String.valueOf(time) }; 230 expected = new java.sql.Time (time); 231 232 for (int i = 0; i < input.length; i++) { 233 DateFormatContainer dfc = new DateFormatContainer(null, null); 234 dfc.setDateFormat(null); 235 result = converter.convert(input[i], java.sql.Time .class, dfc); 236 assertTrue("result should be instance of java.sql.Time", result instanceof java.sql.Time ); 237 assertEquals(input[i].getClass().getName() + " to java.sql.Time", expected, result); 238 } 239 240 for (int i = 0; i < dateFormats.length; i++) { 241 String dateStr = dateFormats[i].format(date); 242 DateFormatContainer dfc = new DateFormatContainer(null, null); 243 dfc.setDateFormat(dateFormats[i]); 244 result = converter.convert(dateStr, java.sql.Time .class, dfc); 245 assertEquals("String to java.sql.Time for input: " + dateStr, dateFormats[i].parse(dateStr), result); 246 assertEquals("String to java.sql.Time for input: " + dateStr, dateStr,dateFormats[i].format(result)); 247 } 248 249 input = new Object [] { new java.util.Date (time), new java.sql.Date (time), new java.sql.Time (time), cal, gregCal, 251 String.valueOf(time) }; 252 253 for (int i = 0; i < input.length; i++) { 254 DateFormatContainer dfc = new DateFormatContainer(null, null); 255 dfc.setDateFormat(null); 256 result = converter.convert(input[i], java.sql.Timestamp .class, dfc); 257 assertTrue("result should be instance of java.sql.Timestamp", result instanceof java.sql.Timestamp ); 258 assertEquals(input[i].getClass().getName() + " to java.sql.Timestamp", time, ((java.sql.Timestamp ) result) 259 .getTime()); 260 } 261 262 for (int i = 0; i < dateFormats.length; i++) { 263 String dateStr = dateFormats[i].format(date); 264 DateFormatContainer dfc = new DateFormatContainer(null, null); 265 dfc.setDateFormat(dateFormats[i]); 266 result = converter.convert(dateStr, java.sql.Timestamp .class, dfc); 267 assertEquals("String to java.sql.Timestamp for input: " + dateStr, dateFormats[i].parse(dateStr), result); 268 assertEquals("String to java.sql.Timestamp for input: " + dateStr, dateStr,dateFormats[i].format(result)); 269 } 270 271 input = new Object [] { new java.util.Date (time), new java.sql.Date (time), new java.sql.Time (time), 273 new java.sql.Timestamp (time), gregCal, String.valueOf(time) }; 274 275 for (int i = 0; i < input.length; i++) { 276 DateFormatContainer dfc = new DateFormatContainer(null, null); 277 dfc.setDateFormat(null); 278 result = converter.convert(input[i], java.util.Calendar .class, dfc); 279 assertTrue("result should be instance of java.util.Calendar", result instanceof java.util.Calendar ); 280 assertEquals(input[i].getClass().getName() + " to java.util.Calendar", time, ((java.util.Calendar ) result) 281 .getTimeInMillis()); 282 } 283 284 for (int i = 0; i < dateFormats.length; i++) { 285 String dateStr = dateFormats[i].format(date); 286 DateFormatContainer dfc = new DateFormatContainer(null, null); 287 dfc.setDateFormat(dateFormats[i]); 288 result = converter.convert(dateStr, java.util.Calendar .class, dfc); 289 assertEquals("String to java.util.Calendar for input: " + dateStr, dateFormats[i].parse(dateStr), ((Calendar )result).getTime()); 290 } 291 292 input = new Object [] { new java.util.Date (time), new java.sql.Date (time), new java.sql.Time (time), 294 new java.sql.Timestamp (time), cal, String.valueOf(time) }; 295 296 for (int i = 0; i < input.length; i++) { 297 DateFormatContainer dfc = new DateFormatContainer(null, null); 298 dfc.setDateFormat(null); 299 result = converter.convert(input[i], java.util.GregorianCalendar .class, dfc); 300 assertTrue("result should be instance of java.util.GregorianCalendar", 301 result instanceof java.util.GregorianCalendar ); 302 assertEquals(input[i].getClass().getName() + " to java.util.GregorianCalendar", time, 303 ((java.util.GregorianCalendar ) result).getTimeInMillis()); 304 } 305 306 for (int i = 0; i < dateFormats.length; i++) { 307 String dateStr = dateFormats[i].format(date); 308 DateFormatContainer dfc = new DateFormatContainer(null, null); 309 dfc.setDateFormat(dateFormats[i]); 310 result = converter.convert(dateStr, java.util.GregorianCalendar .class, dfc); 311 assertEquals("String to java.util.GregorianCalendar for input: " + dateStr, dateFormats[i].parse(dateStr), ((GregorianCalendar )result).getTime()); 312 } 313 314 Class [] classes = new Class [] { java.util.Date .class, java.sql.Date .class, java.sql.Time .class, 316 java.sql.Timestamp .class, java.util.Calendar .class, java.util.GregorianCalendar .class }; 317 String invalidInputStr = "dflksjf"; 318 for (int i = 0; i < classes.length; i++) { 319 try { 320 converter.convert(invalidInputStr, classes[i], null); 321 Assert.fail("mapping value " + invalidInputStr + "to class " + classes[i].getName() 322 + " should have thrown a mapping ex"); 323 } catch (Throwable e) { 324 assertTrue(true); 325 } 326 327 assertNull("mapping null value to class " + classes[i].getName() + " should result in null", converter 328 .convert(null, classes[i], null)); 329 } 330 } 331 332 public void testConvertStringPositiveScalar() { 333 Object value = converter.convert("true", Boolean.TYPE, null); 334 335 assertTrue(value instanceof Boolean ); 336 assertEquals(((Boolean ) value).booleanValue(), true); 337 338 value = converter.convert("true", Boolean .class, null); 339 340 assertTrue(value instanceof Boolean ); 341 assertEquals(((Boolean ) value).booleanValue(), true); 342 343 value = converter.convert("yes", Boolean.TYPE, null); 344 345 assertTrue(value instanceof Boolean ); 346 assertEquals(((Boolean ) value).booleanValue(), true); 347 348 value = converter.convert("yes", Boolean .class, null); 349 350 assertTrue(value instanceof Boolean ); 351 assertEquals(((Boolean ) value).booleanValue(), true); 352 353 value = converter.convert("y", Boolean.TYPE, null); 354 355 assertTrue(value instanceof Boolean ); 356 assertEquals(((Boolean ) value).booleanValue(), true); 357 358 value = converter.convert("y", Boolean .class, null); 359 360 assertTrue(value instanceof Boolean ); 361 assertEquals(((Boolean ) value).booleanValue(), true); 362 363 value = converter.convert("on", Boolean.TYPE, null); 364 365 assertTrue(value instanceof Boolean ); 366 assertEquals(((Boolean ) value).booleanValue(), true); 367 368 value = converter.convert("on", Boolean .class, null); 369 370 assertTrue(value instanceof Boolean ); 371 assertEquals(((Boolean ) value).booleanValue(), true); 372 373 value = converter.convert("1", Boolean .class, null); 374 375 assertTrue(value instanceof Boolean ); 376 assertEquals(((Boolean ) value).booleanValue(), true); 377 378 value = converter.convert("0", Boolean .class, null); 379 380 assertTrue(value instanceof Boolean ); 381 assertEquals(((Boolean ) value).booleanValue(), false); 382 383 value = converter.convert("false", Boolean.TYPE, null); 384 385 assertTrue(value instanceof Boolean ); 386 assertEquals(((Boolean ) value).booleanValue(), false); 387 388 value = converter.convert("false", Boolean .class, null); 389 390 assertTrue(value instanceof Boolean ); 391 assertEquals(((Boolean ) value).booleanValue(), false); 392 393 value = converter.convert("no", Boolean.TYPE, null); 394 395 assertTrue(value instanceof Boolean ); 396 assertEquals(((Boolean ) value).booleanValue(), false); 397 398 value = converter.convert("no", Boolean .class, null); 399 400 assertTrue(value instanceof Boolean ); 401 assertEquals(((Boolean ) value).booleanValue(), false); 402 403 value = converter.convert("n", Boolean.TYPE, null); 404 405 assertTrue(value instanceof Boolean ); 406 assertEquals(((Boolean ) value).booleanValue(), false); 407 408 value = converter.convert("n", Boolean .class, null); 409 410 assertTrue(value instanceof Boolean ); 411 assertEquals(((Boolean ) value).booleanValue(), false); 412 413 value = converter.convert("off", Boolean.TYPE, null); 414 415 assertTrue(value instanceof Boolean ); 416 assertEquals(((Boolean ) value).booleanValue(), false); 417 418 value = converter.convert("off", Boolean .class, null); 419 420 assertTrue(value instanceof Boolean ); 421 assertEquals(((Boolean ) value).booleanValue(), false); 422 423 value = converter.convert("123", Byte.TYPE, null); 424 425 assertTrue(value instanceof Byte ); 426 assertEquals(((Byte ) value).byteValue(), (byte) 123); 427 428 value = converter.convert("123", Byte .class, null); 429 430 assertTrue(value instanceof Byte ); 431 assertEquals(((Byte ) value).byteValue(), (byte) 123); 432 433 value = converter.convert("a", Character.TYPE, null); 434 435 assertTrue(value instanceof Character ); 436 assertEquals(((Character ) value).charValue(), 'a'); 437 438 value = converter.convert("a", Character .class, null); 439 440 assertTrue(value instanceof Character ); 441 assertEquals(((Character ) value).charValue(), 'a'); 442 443 value = converter.convert("123.456", Double.TYPE, null); 444 445 assertTrue(value instanceof Double ); 446 assertEquals(((Double ) value).doubleValue(), (double) 123.456, (double) 0.005); 447 448 value = converter.convert("123.456", Double .class, null); 449 450 assertTrue(value instanceof Double ); 451 assertEquals(((Double ) value).doubleValue(), (double) 123.456, (double) 0.005); 452 453 value = converter.convert("123.456", Float.TYPE, null); 454 455 assertTrue(value instanceof Float ); 456 assertEquals(((Float ) value).floatValue(), (float) 123.456, (float) 0.005); 457 458 value = converter.convert("123.456", Float .class, null); 459 460 assertTrue(value instanceof Float ); 461 assertEquals(((Float ) value).floatValue(), (float) 123.456, (float) 0.005); 462 463 value = converter.convert("123", Integer.TYPE, null); 464 465 assertTrue(value instanceof Integer ); 466 assertEquals(((Integer ) value).intValue(), (int) 123); 467 468 value = converter.convert("123", Integer .class, null); 469 470 assertTrue(value instanceof Integer ); 471 assertEquals(((Integer ) value).intValue(), (int) 123); 472 473 value = converter.convert("123", Long.TYPE, null); 474 475 assertTrue(value instanceof Long ); 476 assertEquals(((Long ) value).longValue(), (long) 123); 477 478 value = converter.convert("123", Long .class, null); 479 480 assertTrue(value instanceof Long ); 481 assertEquals(((Long ) value).longValue(), (long) 123); 482 483 value = converter.convert("123", Short.TYPE, null); 484 485 assertTrue(value instanceof Short ); 486 assertEquals(((Short ) value).shortValue(), (short) 123); 487 488 value = converter.convert("123", Short .class, null); 489 490 assertTrue(value instanceof Short ); 491 assertEquals(((Short ) value).shortValue(), (short) 123); 492 } 493 494 public void testConvertStringNegativeScalar() { 495 Object value = null; 496 try { 497 value = converter.convert("foo", Boolean.TYPE, null); 498 fail("Should have thrown conversion exception"); 499 } catch (ConversionException e) { 500 assertTrue(true); 501 } 502 try { 503 value = converter.convert("foo", Boolean .class, null); 504 fail("Should have thrown conversion exception"); 505 } catch (ConversionException e) { 506 assertTrue(true); 507 } 508 value = converter.convert("org.apache.commons.beanutils.Undefined", Class .class, null); 509 assertEquals("org.apache.commons.beanutils.Undefined", value); 510 } 511 } 512 | Popular Tags |