FieldExpressionValidator extends FieldValidatorSupport { public void validate(Object object) throws ValidationException { String fieldName = getFieldName(); Boolean answer = Boolean.FALSE; Object obj = null; try { OgnlBasicValueHolder valueHolder = new OgnlBasicValueHolder(); valueHolder.setRoot(object); obj = valueHolder.findValue(expression); } catch (Exception e) { } if ((obj != null) && (obj instanceof Boolean)) { answer = (Boolean) obj; } else { logger.warn("Got result of " + obj + " when trying to get Boolean."); } if (!answer.booleanValue()) { addFieldValidationError(fieldName, object); } }  void setExpression(String expression); String getExpression(); void validate(Object object);  }
@Test public void testSuccess() { HashMap<String, Object> map = new HashMap<String, Object>(); Account account = new Account(); account.setId(1L); account.setCity("Shanghai"); map.put("account", account); try { this.validate(map); } catch (ValidationException e) { Assert.fail(e.getMessage()); } }
@Test public void testFailure() { boolean success = false; HashMap<String, Object> map = new HashMap<String, Object>(); Account account = new Account(); account.setId(1L); account.setCity("heaven"); map.put("account", account); try { this.validate(map); } catch (ValidationException e) { success = true; } Assert.assertTrue(success); }