@Test public void testConvertNotSupported() throws Exception { final Calendar cal = new GregorianCalendar(2005, 0, 16); try { converter.convert(Object.class, cal.getTime()); fail("Object.class is not supported"); } catch (final Exception e) { // expected } try { converter.convertToDate(Object.class, cal.getTime(), ""); fail("Object.class is not supported"); } catch (final Exception e) { // expected } }
@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); // test empty time 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 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 testConvertDateWithNull() throws Exception { final String date = (String) converter.convert(String.class, null); assertNull(date); }
@Test public void testConvertStringToString() throws Exception { assertEquals(converter.convert(String.class, "anystring"), "anystring"); }
@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 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); }