@Test public void testConvert() throws Exception { System.out.println("convert"); DateField instance; instance = new DateField(); Calendar calendar = Calendar.getInstance(); calendar.set(2014, 0, 1); final Date expectedResult = new Date(calendar.getTimeInMillis()); assertEquals(expectedResult.toString(), instance.convert("2014-01-01").toString()); assertEquals(expectedResult.toString(), instance.convert("2014-1-01").toString()); assertEquals(expectedResult.toString(), instance.convert("2014-01-1").toString()); assertEquals(expectedResult.toString(), instance.convert("2014-1-1").toString()); try { instance.convert("2014/01/01"); } catch (OperationException ex) { assertEquals(ErrorCode.INVALID_DATE_FORMAT, ex.getErrorCode()); } try { instance.convert("2014-22-01"); } catch (OperationException ex) { assertEquals(ErrorCode.INVALID_DATE_FORMAT, ex.getErrorCode()); } try { instance.convert("01-01-2014"); } catch (OperationException ex) { assertEquals(ErrorCode.INVALID_DATE_FORMAT, ex.getErrorCode()); } try { instance.convert(null); fail("null value not checked"); } catch (OperationException ex) { assertEquals(ErrorCode.DATATYPE_MISMATCH, ex.getErrorCode()); } }
@Override public Date convert(Object objValue) throws OperationException { if (objValue == null) { throw new OperationException(ErrorCode.DATATYPE_MISMATCH); } if (objValue instanceof String) { try { return Date.valueOf(objValue.toString()); } catch (IllegalArgumentException ex) { throw new OperationException(ErrorCode.INVALID_DATE_FORMAT, "Could not parse " + objValue.toString() + " to a valid date. Supported format is yyyy-[m]m-[d]d"); } } else if (objValue instanceof Long) { return new Date((Long) objValue); } else if (objValue instanceof Date) { return (Date) objValue; } else { throw new OperationException(ErrorCode.DATATYPE_MISMATCH, "Cannot cast to a valid Date field. Value: " + objValue); } }
DateField implements FieldType<Date> { @Override public Date convert(Object objValue) throws OperationException { if (objValue == null) { throw new OperationException(ErrorCode.DATATYPE_MISMATCH); } if (objValue instanceof String) { try { return Date.valueOf(objValue.toString()); } catch (IllegalArgumentException ex) { throw new OperationException(ErrorCode.INVALID_DATE_FORMAT, "Could not parse " + objValue.toString() + " to a valid date. Supported format is yyyy-[m]m-[d]d"); } } else if (objValue instanceof Long) { return new Date((Long) objValue); } else if (objValue instanceof Date) { return (Date) objValue; } else { throw new OperationException(ErrorCode.DATATYPE_MISMATCH, "Cannot cast to a valid Date field. Value: " + objValue); } } }
DateField implements FieldType<Date> { @Override public Date convert(Object objValue) throws OperationException { if (objValue == null) { throw new OperationException(ErrorCode.DATATYPE_MISMATCH); } if (objValue instanceof String) { try { return Date.valueOf(objValue.toString()); } catch (IllegalArgumentException ex) { throw new OperationException(ErrorCode.INVALID_DATE_FORMAT, "Could not parse " + objValue.toString() + " to a valid date. Supported format is yyyy-[m]m-[d]d"); } } else if (objValue instanceof Long) { return new Date((Long) objValue); } else if (objValue instanceof Date) { return (Date) objValue; } else { throw new OperationException(ErrorCode.DATATYPE_MISMATCH, "Cannot cast to a valid Date field. Value: " + objValue); } } DateField();  DateField(final Date defaultValue); }
DateField implements FieldType<Date> { @Override public Date convert(Object objValue) throws OperationException { if (objValue == null) { throw new OperationException(ErrorCode.DATATYPE_MISMATCH); } if (objValue instanceof String) { try { return Date.valueOf(objValue.toString()); } catch (IllegalArgumentException ex) { throw new OperationException(ErrorCode.INVALID_DATE_FORMAT, "Could not parse " + objValue.toString() + " to a valid date. Supported format is yyyy-[m]m-[d]d"); } } else if (objValue instanceof Long) { return new Date((Long) objValue); } else if (objValue instanceof Date) { return (Date) objValue; } else { throw new OperationException(ErrorCode.DATATYPE_MISMATCH, "Cannot cast to a valid Date field. Value: " + objValue); } } DateField();  DateField(final Date defaultValue); @Override Types getType(); @Override Date getDefaultValue(); @Override boolean hasDefaultValue(); @Override Date convert(Object objValue); @Override JSONObject toJson(); }
DateField implements FieldType<Date> { @Override public Date convert(Object objValue) throws OperationException { if (objValue == null) { throw new OperationException(ErrorCode.DATATYPE_MISMATCH); } if (objValue instanceof String) { try { return Date.valueOf(objValue.toString()); } catch (IllegalArgumentException ex) { throw new OperationException(ErrorCode.INVALID_DATE_FORMAT, "Could not parse " + objValue.toString() + " to a valid date. Supported format is yyyy-[m]m-[d]d"); } } else if (objValue instanceof Long) { return new Date((Long) objValue); } else if (objValue instanceof Date) { return (Date) objValue; } else { throw new OperationException(ErrorCode.DATATYPE_MISMATCH, "Cannot cast to a valid Date field. Value: " + objValue); } } DateField();  DateField(final Date defaultValue); @Override Types getType(); @Override Date getDefaultValue(); @Override boolean hasDefaultValue(); @Override Date convert(Object objValue); @Override JSONObject toJson();  }