DateConverter implements Converter { @SuppressWarnings("unchecked") public Object convert(final Class type, final Object value) { if (value == null) { return null; } else if (type == Timestamp.class) { return convertToDate(type, value, DateUtil.getDateTimePattern()); } else if (type == Date.class) { return convertToDate(type, value, DateUtil.getDatePattern()); } else if (type == String.class) { return convertToString(value); } throw new ConversionException("Could not convert " + value.getClass().getName() + " to " + type.getName()); }  @SuppressWarnings("unchecked") Object convert(final Class type, final Object value);  }
@Test public void testConvertStringToDate() throws Exception { final Date today = new Date(); final Calendar todayCalendar = new GregorianCalendar(); todayCalendar.setTime(today); final String datePart = DateUtil.convertDateToString(today); Date date = (Date) converter.convert(Date.class, ""); assertNull(date); date = (Date) converter.convert(Date.class, datePart); final Calendar cal = new GregorianCalendar(); cal.setTime(date); assertEquals(todayCalendar.get(Calendar.YEAR), cal.get(Calendar.YEAR)); assertEquals(todayCalendar.get(Calendar.MONTH), cal.get(Calendar.MONTH)); assertEquals(todayCalendar.get(Calendar.DAY_OF_MONTH), cal.get(Calendar.DAY_OF_MONTH)); }
@Test public void testConvertDateToString() throws Exception { final Calendar cal = new GregorianCalendar(2005, 0, 16); final String date = (String) converter.convert(String.class, cal.getTime()); assertEquals(DateUtil.convertDateToString(cal.getTime()), date); }
@Test public void testConvertStringToString() throws Exception { assertEquals(converter.convert(String.class, "anystring"), "anystring"); }
@Test public void testConvertDateWithNull() throws Exception { final String date = (String) converter.convert(String.class, null); assertNull(date); }
@Test public void testConvertStringToTimestamp() throws Exception { final Date today = new Date(); final Calendar todayCalendar = new GregorianCalendar(); todayCalendar.setTime(today); final String datePart = DateUtil.convertDateToString(today); final Timestamp time = (Timestamp) converter.convert(Timestamp.class, datePart + " 01:02:03.4"); final Calendar cal = new GregorianCalendar(); cal.setTimeInMillis(time.getTime()); assertEquals(todayCalendar.get(Calendar.YEAR), cal.get(Calendar.YEAR)); assertEquals(todayCalendar.get(Calendar.MONTH), cal.get(Calendar.MONTH)); assertEquals(todayCalendar.get(Calendar.DAY_OF_MONTH), cal.get(Calendar.DAY_OF_MONTH)); }
@Test public void testConvertTimestampToString() throws Exception { final Timestamp timestamp = Timestamp.valueOf("2005-03-10 01:02:03.4"); final String time = (String) converter.convert(String.class, timestamp); assertEquals(DateUtil.getDateTime(DateUtil.getDateTimePattern(), timestamp), time); }