Example usage for org.apache.wicket.validation ValidationError getErrorMessage

List of usage examples for org.apache.wicket.validation ValidationError getErrorMessage

Introduction

In this page you can find the example usage for org.apache.wicket.validation ValidationError getErrorMessage.

Prototype

@Override
public final Serializable getErrorMessage(IErrorMessageSource messageSource) 

Source Link

Usage

From source file:org.obiba.onyx.wicket.data.DataValidatorTest.java

License:Open Source License

@SuppressWarnings("unchecked")
@Test/* w ww  . j  a v  a2s  . c o m*/
public void testDataValidatorWithInvalidType() {
    Date testDate = new Date();
    Validatable value = new Validatable();
    value.setValue(DataBuilder.buildDate(testDate));

    // Expect a DECIMAL, but obtain an DATE
    DataValidator validator = new DataValidator(mockValidator, DataType.DECIMAL);
    // Expect no calls on mockValidator
    EasyMock.replay(mockValidator);
    validator.validate(value);
    EasyMock.verify(mockValidator);

    List<IValidationError> errors = value.getErrors();
    Assert.assertNotNull(errors);
    Assert.assertEquals(1, errors.size());
    IValidationError ierror = errors.get(0);
    Assert.assertTrue(ierror instanceof ValidationError);
    ValidationError error = (ValidationError) ierror;
    Map<String, Object> variables = error.getVariables();
    Assert.assertTrue(variables.containsKey("expectedType"));
    Assert.assertEquals(DataType.DECIMAL, variables.get("expectedType"));

    Assert.assertTrue(variables.containsKey("actualType"));
    Assert.assertEquals(DataType.DATE, variables.get("actualType"));

    Assert.assertTrue(variables.containsKey("value"));
    Assert.assertEquals(testDate, variables.get("value"));

    // We can only get at the list of keys by asking the error for its message
    IErrorMessageSource mockSource = EasyMock.createMock(IErrorMessageSource.class);
    EasyMock.expect(mockSource.getMessage(EasyMock.eq("DataValidator.incompatibleTypes")))
            .andReturn("theMessage");
    EasyMock.expectLastCall().once();
    // We are not testing the IValidationError implementation, but we need to expect this call anyway. Make it as
    // flexible as possible. The important expectation is the first which expects the proper message key.
    EasyMock.expect(mockSource.substitute((String) EasyMock.anyObject(), (Map) EasyMock.anyObject()))
            .andReturn("theSubistutedMessage");
    EasyMock.expectLastCall().anyTimes();

    EasyMock.replay(mockSource);
    error.getErrorMessage(mockSource);
    EasyMock.verify(mockSource);
}