DateConverter implements Converter { public Object convert(Class destClass, Object srcObj) { final Class srcFieldClass = srcObj.getClass(); long time; int nanos = 0; if (Calendar.class.isAssignableFrom(srcFieldClass)) { Calendar inVal = (Calendar)srcObj; time = inVal.getTime().getTime(); } else if (Timestamp.class.isAssignableFrom(srcFieldClass)) { Timestamp timestamp = (Timestamp)srcObj; time = timestamp.getTime(); nanos = timestamp.getNanos(); } else if (java.util.Date.class.isAssignableFrom(srcFieldClass)) { time = ((java.util.Date)srcObj).getTime(); } else if (XMLGregorianCalendar.class.isAssignableFrom(srcFieldClass)) { time = ((XMLGregorianCalendar)srcObj).toGregorianCalendar().getTimeInMillis(); } else if (dateFormat != null && String.class.isAssignableFrom(srcObj.getClass())) { try { if ("".equals(srcObj)) { return null; } time = dateFormat.parse((String)srcObj).getTime(); } catch (ParseException e) { throw new ConversionException("Unable to parse source object using specified date format", e); } } else { try { time = Long.parseLong(srcObj.toString()); } catch (NumberFormatException e) { throw new ConversionException("Unable to determine time in millis of source object", e); } } try { if (Calendar.class.isAssignableFrom(destClass)) { Constructor constructor = destClass.getConstructor(); Calendar result = (Calendar)constructor.newInstance(); result.setTimeInMillis(time); return result; } if (dateFormat != null && String.class.isAssignableFrom(destClass)) { return dateFormat.format(new java.util.Date(time)); } Constructor constructor = destClass.getConstructor(Long.TYPE); Object result = constructor.newInstance(time); if (nanos != 0 && (Timestamp.class.isAssignableFrom(destClass))) { ((Timestamp)result).setNanos(nanos); } return result; } catch (Exception e) { throw new ConversionException(e); } } DateConverter(DateFormat dateFormat); Object convert(Class destClass, Object srcObj);  }
@Test public void testTimestampConversion() { Timestamp timestamp = new java.sql.Timestamp(1L); timestamp.setNanos(12345); Timestamp result = (Timestamp)converter.convert(Timestamp.class, timestamp); assertEquals(timestamp, result); }
@Test public void testSqlDateConversion() { Date date = new Date(1L); Date result = (Date)converter.convert(Date.class, date); assertEquals(date, result); }
@Test public void testUtilDateConversion() { java.util.Date date = new java.util.Date(1L); java.util.Date result = (java.util.Date)converter.convert(java.util.Date.class, date); assertEquals(date, result); }
@Test public void testCalendarConversion() { GregorianCalendar calendar = new GregorianCalendar(1, 2, 3); GregorianCalendar result = (GregorianCalendar)converter.convert(GregorianCalendar.class, calendar); assertEquals(calendar, result); }
@Test public void testTimeConversion() { Time time = new Time(1L); Time result = (Time)converter.convert(Time.class, time); assertEquals(result, time); }
@Test public void testEmptyString() { assertNull(converter.convert(Date.class, "")); }
@Test public void testGoodString() { GregorianCalendar calendar = new GregorianCalendar(2001, 1, 1); java.util.Date expected = calendar.getTime(); assertEquals(expected, converter.convert(java.util.Date.class, "01.02.2001")); }
@Test public void testBadString() { try { converter.convert(Date.class, "xyz"); fail(); } catch (ConversionException e) { } }
@Test public void testXmlGregorianCalendar() { XMLGregorianCalendar xmlCalendar = mock(XMLGregorianCalendar.class); GregorianCalendar expected = new GregorianCalendar(); when(xmlCalendar.toGregorianCalendar()).thenReturn(expected); Date date = new Date(expected.getTimeInMillis()); assertEquals(date, converter.convert(Date.class, xmlCalendar)); }
@Test public void testConvert_Format() { GregorianCalendar calendar = new GregorianCalendar(2001, 1, 1); Object result = converter.convert(String.class, calendar); String stringCalendar = (String)result; assertEquals(stringCalendar, "01.02.2001"); }