Example usage for org.apache.wicket.validation Validatable Validatable

List of usage examples for org.apache.wicket.validation Validatable Validatable

Introduction

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

Prototype

public Validatable() 

Source Link

Document

Constructor.

Usage

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

License:Open Source License

@Test
public void testDataValidatorNormalBehavior() {
    Validatable value = new Validatable();
    value.setValue(DataBuilder.buildInteger(10));

    // We should match the argument to make sure we obtain the proper value
    mockValidator.validate((IValidatable) EasyMock.anyObject());

    DataValidator validator = new DataValidator(mockValidator, DataType.INTEGER);
    EasyMock.replay(mockValidator);/*from   www  .  j a  va  2s . c  om*/
    validator.validate(value);
    EasyMock.verify(mockValidator);
}

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

License:Open Source License

@SuppressWarnings("unchecked")
@Test//w  w w .  j  av a2  s . c om
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);
}