123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506 |
- /*
- * Copyright 2000-2016 Vaadin Ltd.
- *
- * Licensed under the Apache License, Version 2.0 (the "License"); you may not
- * use this file except in compliance with the License. You may obtain a copy of
- * the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
- * License for the specific language governing permissions and limitations under
- * the License.
- */
- package com.vaadin.data;
-
- import java.util.List;
- import java.util.concurrent.atomic.AtomicReference;
-
- import org.junit.Assert;
- import org.junit.Before;
- import org.junit.Test;
-
- import com.vaadin.data.Binder.Binding;
- import com.vaadin.data.Binder.BindingBuilder;
- import com.vaadin.data.ValidationStatus.Status;
- import com.vaadin.tests.data.bean.Person;
- import com.vaadin.ui.Label;
-
- public class BinderValidationStatusTest
- extends BinderTestBase<Binder<Person>, Person> {
-
- protected final static ValidationStatusHandler NOOP = event -> {
- };
-
- @Before
- public void setUp() {
- binder = new Binder<>();
- item = new Person();
- item.setFirstName("Johannes");
- item.setAge(32);
- }
-
- //
- // Binding-level status handler
- //
-
- @Test
- public void bindingWithStatusHandler_handlerGetsEvents() {
- AtomicReference<ValidationStatus<?>> statusCapture = new AtomicReference<>();
- BindingBuilder<Person, String> binding = binder.forField(nameField)
- .withValidator(notEmpty).withValidationStatusHandler(evt -> {
- Assert.assertNull(statusCapture.get());
- statusCapture.set(evt);
- });
- binding.bind(Person::getFirstName, Person::setFirstName);
-
- nameField.setValue("");
-
- // First validation fails => should be event with ERROR status and
- // message
- binder.validate();
-
- Assert.assertNotNull(statusCapture.get());
- ValidationStatus<?> evt = statusCapture.get();
- Assert.assertEquals(Status.ERROR, evt.getStatus());
- Assert.assertEquals(EMPTY_ERROR_MESSAGE, evt.getMessage().get());
- Assert.assertEquals(nameField, evt.getField());
-
- statusCapture.set(null);
- nameField.setValue("foo");
-
- statusCapture.set(null);
- // Second validation succeeds => should be event with OK status and
- // no message
- binder.validate();
-
- evt = statusCapture.get();
- Assert.assertNotNull(evt);
- Assert.assertEquals(Status.OK, evt.getStatus());
- Assert.assertFalse(evt.getMessage().isPresent());
- Assert.assertEquals(nameField, evt.getField());
- }
-
- @Test
- public void bindingWithStatusHandler_defaultStatusHandlerIsReplaced() {
- Binding<Person, String> binding = binder.forField(nameField)
- .withValidator(notEmpty).withValidationStatusHandler(evt -> {
- }).bind(Person::getFirstName, Person::setFirstName);
-
- Assert.assertNull(nameField.getComponentError());
-
- nameField.setValue("");
-
- // First validation fails => should be event with ERROR status and
- // message
- binding.validate();
-
- // default behavior should update component error for the nameField
- Assert.assertNull(nameField.getComponentError());
- }
-
- @Test
- public void bindingWithStatusLabel_labelIsUpdatedAccordingStatus() {
- Label label = new Label();
-
- Binding<Person, String> binding = binder.forField(nameField)
- .withValidator(notEmpty).withStatusLabel(label)
- .bind(Person::getFirstName, Person::setFirstName);
-
- nameField.setValue("");
-
- // First validation fails => should be event with ERROR status and
- // message
- binding.validate();
-
- Assert.assertTrue(label.isVisible());
- Assert.assertEquals(EMPTY_ERROR_MESSAGE, label.getValue());
-
- nameField.setValue("foo");
-
- // Second validation succeeds => should be event with OK status and
- // no message
- binding.validate();
-
- Assert.assertFalse(label.isVisible());
- Assert.assertEquals("", label.getValue());
- }
-
- @Test
- public void bindingWithStatusLabel_defaultStatusHandlerIsReplaced() {
- Label label = new Label();
-
- Binding<Person, String> binding = binder.forField(nameField)
- .withValidator(notEmpty).withStatusLabel(label)
- .bind(Person::getFirstName, Person::setFirstName);
-
- Assert.assertNull(nameField.getComponentError());
-
- nameField.setValue("");
-
- // First validation fails => should be event with ERROR status and
- // message
- binding.validate();
-
- // default behavior should update component error for the nameField
- Assert.assertNull(nameField.getComponentError());
- }
-
- @Test(expected = IllegalStateException.class)
- public void bindingWithStatusHandler_addAfterBound() {
- BindingBuilder<Person, String> binding = binder.forField(nameField)
- .withValidator(notEmpty);
- binding.bind(Person::getFirstName, Person::setFirstName);
-
- binding.withValidationStatusHandler(evt -> Assert.fail());
- }
-
- @Test(expected = IllegalStateException.class)
- public void bindingWithStatusLabel_addAfterBound() {
- Label label = new Label();
-
- BindingBuilder<Person, String> binding = binder.forField(nameField)
- .withValidator(notEmpty);
- binding.bind(Person::getFirstName, Person::setFirstName);
-
- binding.withStatusLabel(label);
- }
-
- @Test(expected = IllegalStateException.class)
- public void bindingWithStatusLabel_setAfterHandler() {
- Label label = new Label();
-
- BindingBuilder<Person, String> binding = binder.forField(nameField);
-
- binding.withValidationStatusHandler(NOOP);
-
- binding.withStatusLabel(label);
- }
-
- @Test(expected = IllegalStateException.class)
- public void bindingWithStatusHandler_setAfterLabel() {
- Label label = new Label();
-
- BindingBuilder<Person, String> binding = binder.forField(nameField);
-
- binding.withStatusLabel(label);
-
- binding.withValidationStatusHandler(NOOP);
- }
-
- @Test(expected = IllegalStateException.class)
- public void bindingWithStatusHandler_setAfterOtherHandler() {
-
- BindingBuilder<Person, String> binding = binder.forField(nameField);
-
- binding.withValidationStatusHandler(NOOP);
-
- binding.withValidationStatusHandler(NOOP);
- }
-
- //
- // Binder-level status handler
- //
-
- @Test
- public void binderWithStatusHandler_fieldValidationNoBeanValidation_handlerGetsStatusUpdates() {
- AtomicReference<BinderValidationStatus<?>> statusCapture = new AtomicReference<>();
- binder.forField(nameField).withValidator(notEmpty)
- .withValidationStatusHandler(evt -> {
- Assert.fail(
- "Using a custom status change handler so no change should end up here");
- }).bind(Person::getFirstName, Person::setFirstName);
- binder.forField(ageField).withConverter(stringToInteger)
- .withValidator(notNegative).withValidationStatusHandler(evt -> {
- Assert.fail(
- "Using a custom status change handler so no change should end up here");
- }).bind(Person::getAge, Person::setAge);
-
- binder.setValidationStatusHandler(r -> {
- statusCapture.set(r);
- });
- binder.setBean(item);
- Assert.assertNull(nameField.getComponentError());
-
- nameField.setValue("");
- ageField.setValue("5");
-
- // First binding validation fails => should be result with ERROR status
- // and message
- BinderValidationStatus<Person> status2 = binder.validate();
- BinderValidationStatus<?> status = statusCapture.get();
- Assert.assertSame(status2, status);
-
- Assert.assertNull(nameField.getComponentError());
-
- List<ValidationStatus<?>> bindingStatuses = status
- .getFieldValidationStatuses();
- Assert.assertNotNull(bindingStatuses);
- Assert.assertEquals(1, status.getFieldValidationErrors().size());
- Assert.assertEquals(2, bindingStatuses.size());
-
- ValidationStatus<?> r = bindingStatuses.get(0);
- Assert.assertTrue(r.isError());
- Assert.assertEquals(EMPTY_ERROR_MESSAGE, r.getMessage().get());
- Assert.assertEquals(nameField, r.getField());
-
- r = bindingStatuses.get(1);
- Assert.assertFalse(r.isError());
- Assert.assertFalse(r.getMessage().isPresent());
- Assert.assertEquals(ageField, r.getField());
-
- Assert.assertEquals(0, status.getBeanValidationResults().size());
- Assert.assertEquals(0, status.getBeanValidationErrors().size());
-
- nameField.setValue("foo");
- ageField.setValue("");
-
- statusCapture.set(null);
- // Second validation succeeds => should be result with OK status and
- // no message, and error result for age
- binder.validate();
-
- status = statusCapture.get();
- bindingStatuses = status.getFieldValidationStatuses();
- Assert.assertEquals(1, status.getFieldValidationErrors().size());
- Assert.assertEquals(2, bindingStatuses.size());
-
- r = bindingStatuses.get(0);
- Assert.assertFalse(r.isError());
- Assert.assertFalse(r.getMessage().isPresent());
- Assert.assertEquals(nameField, r.getField());
-
- r = bindingStatuses.get(1);
- Assert.assertTrue(r.isError());
- Assert.assertEquals("Value must be a number", r.getMessage().get());
- Assert.assertEquals(ageField, r.getField());
-
- Assert.assertEquals(0, status.getBeanValidationResults().size());
- Assert.assertEquals(0, status.getBeanValidationErrors().size());
-
- statusCapture.set(null);
- // binding validations pass, binder validation fails
- ageField.setValue("0");
- binder.validate();
-
- status = statusCapture.get();
- bindingStatuses = status.getFieldValidationStatuses();
- Assert.assertEquals(0, status.getFieldValidationErrors().size());
- Assert.assertEquals(2, bindingStatuses.size());
-
- Assert.assertEquals(0, status.getBeanValidationResults().size());
- Assert.assertEquals(0, status.getBeanValidationErrors().size());
- }
-
- @Test
- public void binderWithStatusHandler_fieldAndBeanLevelValidation_handlerGetsStatusUpdates() {
- AtomicReference<BinderValidationStatus<?>> statusCapture = new AtomicReference<>();
- binder.forField(nameField).withValidator(notEmpty)
- .withValidationStatusHandler(evt -> {
- Assert.fail(
- "Using a custom status change handler so no change should end up here");
- }).bind(Person::getFirstName, Person::setFirstName);
- binder.forField(ageField).withConverter(stringToInteger)
- .withValidator(notNegative).withValidationStatusHandler(evt -> {
- Assert.fail(
- "Using a custom status change handler so no change should end up here");
- }).bind(Person::getAge, Person::setAge);
- binder.withValidator(
- bean -> !bean.getFirstName().isEmpty() && bean.getAge() > 0,
- "Need first name and age");
-
- binder.setValidationStatusHandler(r -> {
- statusCapture.set(r);
- });
- binder.setBean(item);
- Assert.assertNull(nameField.getComponentError());
-
- nameField.setValue("");
- ageField.setValue("5");
-
- // First binding validation fails => should be result with ERROR status
- // and message
- BinderValidationStatus<Person> status2 = binder.validate();
- BinderValidationStatus<?> status = statusCapture.get();
- Assert.assertSame(status2, status);
-
- Assert.assertNull(nameField.getComponentError());
-
- List<ValidationStatus<?>> bindingStatuses = status
- .getFieldValidationStatuses();
- Assert.assertNotNull(bindingStatuses);
- Assert.assertEquals(1, status.getFieldValidationErrors().size());
- Assert.assertEquals(2, bindingStatuses.size());
-
- ValidationStatus<?> r = bindingStatuses.get(0);
- Assert.assertTrue(r.isError());
- Assert.assertEquals(EMPTY_ERROR_MESSAGE, r.getMessage().get());
- Assert.assertEquals(nameField, r.getField());
-
- r = bindingStatuses.get(1);
- Assert.assertFalse(r.isError());
- Assert.assertFalse(r.getMessage().isPresent());
- Assert.assertEquals(ageField, r.getField());
-
- Assert.assertEquals(0, status.getBeanValidationResults().size());
- Assert.assertEquals(0, status.getBeanValidationErrors().size());
-
- nameField.setValue("foo");
- ageField.setValue("");
-
- statusCapture.set(null);
- // Second validation succeeds => should be result with OK status and
- // no message, and error result for age
- binder.validate();
-
- status = statusCapture.get();
- bindingStatuses = status.getFieldValidationStatuses();
- Assert.assertEquals(1, status.getFieldValidationErrors().size());
- Assert.assertEquals(2, bindingStatuses.size());
-
- r = bindingStatuses.get(0);
- Assert.assertFalse(r.isError());
- Assert.assertFalse(r.getMessage().isPresent());
- Assert.assertEquals(nameField, r.getField());
-
- r = bindingStatuses.get(1);
- Assert.assertTrue(r.isError());
- Assert.assertEquals("Value must be a number", r.getMessage().get());
- Assert.assertEquals(ageField, r.getField());
-
- Assert.assertEquals(0, status.getBeanValidationResults().size());
- Assert.assertEquals(0, status.getBeanValidationErrors().size());
-
- statusCapture.set(null);
- // binding validations pass, binder validation fails
- ageField.setValue("0");
- binder.validate();
-
- status = statusCapture.get();
- bindingStatuses = status.getFieldValidationStatuses();
- Assert.assertEquals(0, status.getFieldValidationErrors().size());
- Assert.assertEquals(2, bindingStatuses.size());
-
- Assert.assertEquals(1, status.getBeanValidationResults().size());
- Assert.assertEquals(1, status.getBeanValidationErrors().size());
-
- Assert.assertEquals("Need first name and age",
- status.getBeanValidationErrors().get(0).getErrorMessage());
- }
-
- @Test
- public void binderWithStatusHandler_defaultStatusHandlerIsReplaced() {
- Binding<Person, String> binding = binder.forField(nameField)
- .withValidator(notEmpty).withValidationStatusHandler(evt -> {
- }).bind(Person::getFirstName, Person::setFirstName);
-
- Assert.assertNull(nameField.getComponentError());
-
- nameField.setValue("");
-
- // First validation fails => should be event with ERROR status and
- // message
- binding.validate();
-
- // no component error since default handler is replaced
- Assert.assertNull(nameField.getComponentError());
- }
-
- @Test
- public void binderWithStatusLabel_defaultStatusHandlerIsReplaced() {
- Label label = new Label();
-
- Binding<Person, String> binding = binder.forField(nameField)
- .withValidator(notEmpty).withStatusLabel(label)
- .bind(Person::getFirstName, Person::setFirstName);
-
- Assert.assertNull(nameField.getComponentError());
-
- nameField.setValue("");
-
- // First validation fails => should be event with ERROR status and
- // message
- binding.validate();
-
- // default behavior should update component error for the nameField
- Assert.assertNull(nameField.getComponentError());
- }
-
- @Test(expected = IllegalStateException.class)
- public void binderWithStatusHandler_addAfterBound() {
- BindingBuilder<Person, String> binding = binder.forField(nameField)
- .withValidator(notEmpty);
- binding.bind(Person::getFirstName, Person::setFirstName);
-
- binding.withValidationStatusHandler(evt -> Assert.fail());
- }
-
- @Test(expected = IllegalStateException.class)
- public void binderWithStatusLabel_addAfterBound() {
- Label label = new Label();
-
- BindingBuilder<Person, String> binding = binder.forField(nameField)
- .withValidator(notEmpty);
- binding.bind(Person::getFirstName, Person::setFirstName);
-
- binding.withStatusLabel(label);
- }
-
- @Test(expected = IllegalStateException.class)
- public void binderWithStatusLabel_setAfterHandler() {
- Label label = new Label();
-
- BindingBuilder<Person, String> binding = binder.forField(nameField);
- binding.bind(Person::getFirstName, Person::setFirstName);
-
- binder.setValidationStatusHandler(event -> {
- });
-
- binder.setStatusLabel(label);
- }
-
- @Test(expected = IllegalStateException.class)
- public void binderWithStatusHandler_setAfterLabel() {
- Label label = new Label();
-
- BindingBuilder<Person, String> binding = binder.forField(nameField);
- binding.bind(Person::getFirstName, Person::setFirstName);
-
- binder.setStatusLabel(label);
-
- binder.setValidationStatusHandler(event -> {
- });
- }
-
- @Test(expected = NullPointerException.class)
- public void binderWithNullStatusHandler_throws() {
- binder.setValidationStatusHandler(null);
- }
-
- @Test
- public void binderWithStatusHandler_replaceHandler() {
- AtomicReference<BinderValidationStatus<?>> capture = new AtomicReference<>();
-
- BindingBuilder<Person, String> binding = binder.forField(nameField);
- binding.bind(Person::getFirstName, Person::setFirstName);
-
- binder.setValidationStatusHandler(results -> {
- Assert.fail();
- });
-
- binder.setValidationStatusHandler(results -> {
- capture.set(results);
- });
-
- nameField.setValue("foo");
- binder.validate();
-
- List<ValidationStatus<?>> results = capture.get()
- .getFieldValidationStatuses();
- Assert.assertNotNull(results);
- Assert.assertEquals(1, results.size());
- Assert.assertFalse(results.get(0).isError());
- }
- }
|