DateRangeValidationMatcher implements ValidationMatcher { @Override public void validate(String fieldName, String value, List<String> params, TestContext context) throws ValidationException { LOG.debug(String.format( "Validating date range for date '%s' using control data: %s", value, ValidationMatcherUtils.getParameterListAsString(params))); try { String dateFromParam = params.get(0); String dateToParam = params.get(1); String datePatternParam = FALLBACK_DATE_PATTERN; if (params.size() == 3) { datePatternParam = params.get(2); } Calendar dateFrom = toCalender(dateFromParam, datePatternParam); Calendar dateTo = toCalender(dateToParam, datePatternParam); Calendar dateToValidate = toCalender(value, datePatternParam); if (!checkInRange(dateFrom, dateTo, dateToValidate)) { String validationErr = String.format("%s failed for field '%s'. Date '%s' not in range: %s - %s", this.getClass().getSimpleName(), fieldName, value, dateFromParam, dateToParam ); throw new ValidationException(validationErr); } } catch (Exception e) { if (e instanceof ValidationException) { throw e; } else { String validationErr = String.format("%s failed for field '%s'", this.getClass().getSimpleName(), fieldName ); throw new ValidationException(validationErr, e); } } }  @Override void validate(String fieldName, String value, List<String> params, TestContext context);  }
@Test(dataProvider = "validateData") public void validateDateRanges(String value, List<String> params, String expectedErrorMessage) { try { testling.validate("xyz", value, params, null); if (expectedErrorMessage != null) { fail("Was expecting exception with error message " + expectedErrorMessage); } } catch (Exception e) { if (expectedErrorMessage == null) { fail("Was not expecting exception but got one", e); } Assert.assertTrue(e.getMessage().indexOf(expectedErrorMessage) > -1, String.format("Expected error '%s' not found in '%s'", expectedErrorMessage, e.getMessage())); } }