Search in sources :

Example 1 with Assert.assertSame

use of org.junit.Assert.assertSame in project flow by vaadin.

the class BinderTest method setValidationErrorHandler_handlerIsSet_handlerMethodsAreCalled.

@Test
public void setValidationErrorHandler_handlerIsSet_handlerMethodsAreCalled() {
    TestTextField testField = new TestTextField();
    class TestErrorHandler implements BinderValidationErrorHandler {

        private ValidationResult result;

        private boolean clearIsCalled;

        @Override
        public void handleError(HasValue<?, ?> field, ValidationResult result) {
            Assert.assertSame(testField, field);
            this.result = result;
            clearIsCalled = false;
        }

        @Override
        public void clearError(HasValue<?, ?> field) {
            Assert.assertSame(testField, field);
            result = null;
            clearIsCalled = true;
        }
    }
    ;
    TestErrorHandler handler = new TestErrorHandler();
    binder.setValidationErrorHandler(handler);
    binder.forField(testField).asRequired().withValidator((val, context) -> {
        if ("bar".equals(val)) {
            return ValidationResult.error("foo");
        }
        return ValidationResult.ok();
    }).bind(Person::getFirstName, Person::setFirstName);
    binder.setBean(new Person());
    testField.setValue("bar");
    Assert.assertTrue(handler.result.isError());
    Assert.assertFalse(handler.clearIsCalled);
    testField.setValue("foo");
    Assert.assertNull(handler.result);
    Assert.assertTrue(handler.clearIsCalled);
    Assert.assertSame(handler, binder.getValidationErrorHandler());
}
Also used : CoreMatchers(org.hamcrest.CoreMatchers) HasValue(com.vaadin.flow.component.HasValue) CurrentInstance(com.vaadin.flow.internal.CurrentInstance) Person(com.vaadin.flow.tests.data.bean.Person) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) StringToDoubleConverter(com.vaadin.flow.data.converter.StringToDoubleConverter) AtomicReference(java.util.concurrent.atomic.AtomicReference) StringUtils(org.apache.commons.lang3.StringUtils) NumberFormat(java.text.NumberFormat) Assert.assertSame(org.junit.Assert.assertSame) BigDecimal(java.math.BigDecimal) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Locale(java.util.Locale) IntegerRangeValidator(com.vaadin.flow.data.validator.IntegerRangeValidator) Map(java.util.Map) After(org.junit.After) BindingBuilder(com.vaadin.flow.data.binder.Binder.BindingBuilder) UI(com.vaadin.flow.component.UI) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) ExpectedException(org.junit.rules.ExpectedException) Before(org.junit.Before) StringToIntegerConverter(com.vaadin.flow.data.converter.StringToIntegerConverter) Assert.assertNotNull(org.junit.Assert.assertNotNull) DecimalFormat(java.text.DecimalFormat) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Matchers.isEmptyString(org.hamcrest.Matchers.isEmptyString) StringToBigDecimalConverter(com.vaadin.flow.data.converter.StringToBigDecimalConverter) Assert.assertNotEquals(org.junit.Assert.assertNotEquals) Serializable(java.io.Serializable) Objects(java.util.Objects) Converter(com.vaadin.flow.data.converter.Converter) StringLengthValidator(com.vaadin.flow.data.validator.StringLengthValidator) Stream(java.util.stream.Stream) Rule(org.junit.Rule) Assert.assertNull(org.junit.Assert.assertNull) Assert.assertFalse(org.junit.Assert.assertFalse) Optional(java.util.Optional) NotEmptyValidator(com.vaadin.flow.data.validator.NotEmptyValidator) Assert(org.junit.Assert) Binding(com.vaadin.flow.data.binder.Binder.Binding) TestTextField(com.vaadin.flow.data.binder.testcomponents.TestTextField) Sex(com.vaadin.flow.tests.data.bean.Sex) Matchers.containsString(org.hamcrest.Matchers.containsString) Assert.assertEquals(org.junit.Assert.assertEquals) TestTextField(com.vaadin.flow.data.binder.testcomponents.TestTextField) Person(com.vaadin.flow.tests.data.bean.Person) HasValue(com.vaadin.flow.component.HasValue) Test(org.junit.Test)

Aggregations

HasValue (com.vaadin.flow.component.HasValue)1 UI (com.vaadin.flow.component.UI)1 Binding (com.vaadin.flow.data.binder.Binder.Binding)1 BindingBuilder (com.vaadin.flow.data.binder.Binder.BindingBuilder)1 TestTextField (com.vaadin.flow.data.binder.testcomponents.TestTextField)1 Converter (com.vaadin.flow.data.converter.Converter)1 StringToBigDecimalConverter (com.vaadin.flow.data.converter.StringToBigDecimalConverter)1 StringToDoubleConverter (com.vaadin.flow.data.converter.StringToDoubleConverter)1 StringToIntegerConverter (com.vaadin.flow.data.converter.StringToIntegerConverter)1 IntegerRangeValidator (com.vaadin.flow.data.validator.IntegerRangeValidator)1 NotEmptyValidator (com.vaadin.flow.data.validator.NotEmptyValidator)1 StringLengthValidator (com.vaadin.flow.data.validator.StringLengthValidator)1 CurrentInstance (com.vaadin.flow.internal.CurrentInstance)1 Person (com.vaadin.flow.tests.data.bean.Person)1 Sex (com.vaadin.flow.tests.data.bean.Sex)1 Serializable (java.io.Serializable)1 BigDecimal (java.math.BigDecimal)1 DecimalFormat (java.text.DecimalFormat)1 NumberFormat (java.text.NumberFormat)1 HashMap (java.util.HashMap)1