DateType implements Type<DateHolder> { @Override public DateHolder convert(Object obj) { if (obj instanceof Date) { return new DateHolder((Date) obj); } if (obj instanceof Calendar) { Calendar cal = (Calendar) obj; return new DateHolder(cal.getTime()); } if (obj == null) { return new DateHolder(null); } if (obj instanceof String) { return decode((String) obj); } throw new IllegalArgumentException("conversion not supported for: " + obj.getClass()); }  @Override String encode(DateHolder holder); @Override DateHolder decode(String text); @Override DateHolder convert(Object obj); @Override DateHolder[] newArray(int size); static void setDefaultOutputPattern(String pattern); static String getDefaultOutputPattern(); static final String TYPE_NAME; }
@Test public void testConvert() { Date d = d("27-04-2012"); Date d2 = dlong("27-04-2012 16:45:55"); Object[][] testCases = { {d("27-04-2012"), d}, {dlong("27-04-2012 16:45:55"), d2}, {new Timestamp(d.getTime()), d}, {new java.sql.Date(d.getTime()), d}, {new GregorianCalendar(2012, 4-1, 27), d}, {"2012/04/27", d}, {"27.04.2012", d}, {" ", null}, {null, null} }; for (Object[] testCase : testCases) { Object obj = testCase[0]; Date expectedValue = (Date) testCase[1]; assertEquals(expectedValue, type.convert(obj).getDate()); } }
@Test public void testConvert__illegalArgument() { Object[] illegals = {new BigDecimal(7), new Integer(1), "abc"}; for (Object obj : illegals) { try { type.convert(obj); fail(); } catch (IllegalArgumentException e) { } } }