/** * @see java.beans.PropertyEditorSupport#setAsText(java.lang.String) * @should handle date * @should handle date and time * @should fail on partial date * @should fail on partial date and time */ @Override public void setAsText(String asString) throws IllegalArgumentException { if (StringUtils.isEmpty(asString)) { setValue(null); return; } try { // first try date+time setValue(Context.getDateTimeFormat().parse(asString)); } catch (ParseException dateTimeEx) { // next try just date try { setValue(Context.getDateFormat().parse(asString)); } catch (ParseException dateEx) { // those were the only two options, so we fail throw new IllegalArgumentException(dateTimeEx); } } }
@Before public void before() { ed = new DateOrDatetimeEditor(); }
/** * @see java.beans.PropertyEditorSupport#getAsText() * @should print date without time * @should print date and time with time */ @Override public String getAsText() { Date date = (Date) getValue(); if (date == null) { return ""; } if ("0000".equals(new SimpleDateFormat("HmsS").format(date))) { return Context.getDateFormat().format(date); } else { return Context.getDateTimeFormat().format(date); } }
/** * @see DateOrDatetimeEditor#setAsText(String) */ @Test(expected = IllegalArgumentException.class) public void setAsText_shouldFailOnPartialDate() { ed.setAsText("27/10"); }
wdb.registerCustomEditor(java.lang.Integer.class, new CustomNumberEditor(java.lang.Integer.class, NumberFormat .getInstance(Context.getLocale()), true)); wdb.registerCustomEditor(Date.class, new DateOrDatetimeEditor()); wdb.registerCustomEditor(PatientIdentifierType.class, new PatientIdentifierTypeEditor()); wdb.registerCustomEditor(ConceptMapType.class, new ConceptMapTypeEditor());
/** * @see DateOrDatetimeEditor#setAsText(String) */ @Test(expected = IllegalArgumentException.class) public void setAsText_shouldFailOnPartialDateAndTime() { ed.setAsText("27/10/2011 17"); } }