DateType implements Type<DateHolder> { @Override public DateHolder decode(String text) { Date date = EngineUtil.hasText(text) ? guess(text) : null; return new DateHolder(date); }  @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 testDecode() { Date date = d("27-04-2012"); Object[][] testCases = { {"27-04-2012", date}, {"27.04.2012", date}, {"27/04/2012", date}, {"2012-04-27", date}, {"2012.04.27", date}, {"2012/04/27", date}, {" 2012/04/27 ", date}, {" ", null}, {null, null} }; for (Object[] testCase : testCases) { String text = (String) testCase[0]; Date expectedDate = (Date) testCase[1]; DateHolder result = type.decode(text); assertEquals(expectedDate, result.getValue()); assertEquals(expectedDate, result.getDate()); assertEquals(new DateHolder(expectedDate), result); } }
@Test public void testDecode__illegalArgument() { String[] illegals = { "30-02-2012", "32-01-2012", "27 kwietnia 2012", "01-01.2012", "01-01/2012", "01.01-2012", "01.01/2012", "01/01.2012", "01/01-2012", "2012-01.01", "2012-01/01", "2012.01-01", "2012.01/01", "2012/01-01", "2012/01.01" }; for (String text : illegals) { try { type.decode(text); fail(); } catch (RuntimeException e) { System.out.println("OK: " + e.getMessage()); } } }