NumericDDMFormFieldValueValidator implements DDMFormFieldValueValidator { @Override public void validate(DDMFormField ddmFormField, Value value) throws DDMFormFieldValueValidationException { for (Locale availableLocale : value.getAvailableLocales()) { String valueString = value.getString(availableLocale); if (Validator.isNotNull(valueString) && !isNumber(valueString, availableLocale)) { throw new DDMFormFieldValueValidationException( String.format( "\"%s\" is not a %s", valueString, ddmFormField.getDataType())); } } }  @Override void validate(DDMFormField ddmFormField, Value value);  }
@Test(expected = DDMFormFieldValueValidationException.class) public void testValidationWithInvalidNumber() throws Exception { DDMFormField ddmFormField = DDMFormTestUtil.createDDMFormField( "integer", "integer", "numeric", "integer", true, false, false); LocalizedValue localizedValue = new LocalizedValue(LocaleUtil.US); localizedValue.addString(LocaleUtil.US, "invalid number"); DDMFormFieldValue ddmFormFieldValue = DDMFormValuesTestUtil.createDDMFormFieldValue( "integer", localizedValue); _numericDDMFormFieldValueValidator.validate( ddmFormField, ddmFormFieldValue.getValue()); }
@Test public void testValidationWithNonrequiredFieldAndEmptyValue() throws Exception { DDMFormField ddmFormField = DDMFormTestUtil.createDDMFormField( "integer", "integer", "numeric", "integer", true, false, false); LocalizedValue localizedValue = new LocalizedValue(LocaleUtil.US); localizedValue.addString(LocaleUtil.US, ""); DDMFormFieldValue ddmFormFieldValue = DDMFormValuesTestUtil.createDDMFormFieldValue( "integer", localizedValue); _numericDDMFormFieldValueValidator.validate( ddmFormField, ddmFormFieldValue.getValue()); }
@Test public void testValidationWithValidDouble() throws Exception { DDMFormField ddmFormField = DDMFormTestUtil.createDDMFormField( "double", "double", "numeric", "double", true, false, false); LocalizedValue localizedValue = new LocalizedValue(LocaleUtil.US); localizedValue.addString(LocaleUtil.US, "2"); DDMFormFieldValue ddmFormFieldValue = DDMFormValuesTestUtil.createDDMFormFieldValue( "double", localizedValue); _numericDDMFormFieldValueValidator.validate( ddmFormField, ddmFormFieldValue.getValue()); }
@Test public void testValidationWithValidInteger() throws Exception { DDMFormField ddmFormField = DDMFormTestUtil.createDDMFormField( "integer", "integer", "numeric", "integer", true, false, false); LocalizedValue localizedValue = new LocalizedValue(LocaleUtil.US); localizedValue.addString(LocaleUtil.US, "1"); localizedValue.addString(LocaleUtil.BRAZIL, "2"); DDMFormFieldValue ddmFormFieldValue = DDMFormValuesTestUtil.createDDMFormFieldValue( "integer", localizedValue); _numericDDMFormFieldValueValidator.validate( ddmFormField, ddmFormFieldValue.getValue()); }