123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241 |
- /*
- * 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 org.junit.Assert;
- import org.junit.Before;
- import org.junit.Test;
-
- import com.vaadin.data.Binder.Binding;
- import com.vaadin.data.util.converter.StringToIntegerConverter;
- import com.vaadin.data.validator.EmailValidator;
- import com.vaadin.server.AbstractErrorMessage;
- import com.vaadin.ui.AbstractField;
- import com.vaadin.ui.Slider;
-
- /**
- * Book of Vaadin tests.
- *
- * @author Vaadin Ltd
- *
- */
- public class BinderBookOfVaadinTest {
-
- static class TextField extends AbstractField<String> {
-
- String value = "";
-
- @Override
- public String getValue() {
- return value;
- }
-
- @Override
- protected void doSetValue(String value) {
- this.value = value;
- }
- }
-
- private static class BookPerson {
- private String lastName;
- private String email;
- private int yearOfBirth, salaryLevel;
-
- public String getLastName() {
- return lastName;
- }
-
- public void setLastName(String lastName) {
- this.lastName = lastName;
- }
-
- public BookPerson(int yearOfBirth, int salaryLevel) {
- this.yearOfBirth = yearOfBirth;
- this.salaryLevel = salaryLevel;
- }
-
- public int getYearOfBirth() {
- return yearOfBirth;
- }
-
- public void setYearOfBirth(int yearOfBirth) {
- this.yearOfBirth = yearOfBirth;
- }
-
- public int getSalaryLevel() {
- return salaryLevel;
- }
-
- public void setSalaryLevel(int salaryLevel) {
- this.salaryLevel = salaryLevel;
- }
-
- public String getEmail() {
- return email;
- }
-
- public void setEmail(String email) {
- this.email = email;
- }
-
- }
-
- private Binder<BookPerson> binder;
-
- private TextField field;
-
- @Before
- public void setUp() {
- binder = new Binder<>();
- field = new TextField();
- }
-
- @Test
- public void simpleEmailValidator() {
- binder.forField(field)
- // Explicit validator instance
- .withValidator(new EmailValidator(
- "This doesn't look like a valid email address"))
- .bind(BookPerson::getEmail, BookPerson::setEmail);
-
- field.setValue("not-email");
- List<ValidationError<?>> errors = binder.validate();
- Assert.assertEquals(1, errors.size());
- Assert.assertEquals("This doesn't look like a valid email address",
- errors.get(0).getMessage());
- Assert.assertEquals("This doesn't look like a valid email address",
- ((AbstractErrorMessage) field.getErrorMessage()).getMessage());
-
- field.setValue("abc@vaadin.com");
- errors = binder.validate();
- Assert.assertEquals(0, errors.size());
- Assert.assertNull(field.getErrorMessage());
- }
-
- @Test
- public void nameLengthTest() {
- binder.forField(field)
- // Validator defined based on a lambda and an error message
- .withValidator(name -> name.length() >= 3,
- "Last name must contain at least three characters")
- .bind(BookPerson::getLastName, BookPerson::setLastName);
-
- field.setValue("a");
- List<ValidationError<?>> errors = binder.validate();
- Assert.assertEquals(1, errors.size());
- Assert.assertEquals("Last name must contain at least three characters",
- errors.get(0).getMessage());
- Assert.assertEquals("Last name must contain at least three characters",
- ((AbstractErrorMessage) field.getErrorMessage()).getMessage());
-
- field.setValue("long last name");
- errors = binder.validate();
- Assert.assertEquals(0, errors.size());
- Assert.assertNull(field.getErrorMessage());
- }
-
- @Test
- public void chainedEmailValidator() {
- binder.forField(field)
- // Explicit validator instance
- .withValidator(new EmailValidator(
- "This doesn't look like a valid email address"))
- .withValidator(email -> email.endsWith("@acme.com"),
- "Only acme.com email addresses are allowed")
- .bind(BookPerson::getEmail, BookPerson::setEmail);
-
- field.setValue("not-email");
- List<ValidationError<?>> errors = binder.validate();
- // Only one error per field should be reported
- Assert.assertEquals(1, errors.size());
- Assert.assertEquals("This doesn't look like a valid email address",
- errors.get(0).getMessage());
- Assert.assertEquals("This doesn't look like a valid email address",
- ((AbstractErrorMessage) field.getErrorMessage()).getMessage());
-
- field.setValue("abc@vaadin.com");
- errors = binder.validate();
- Assert.assertEquals(1, errors.size());
- Assert.assertEquals("Only acme.com email addresses are allowed",
- errors.get(0).getMessage());
- Assert.assertEquals("Only acme.com email addresses are allowed",
- ((AbstractErrorMessage) field.getErrorMessage()).getMessage());
-
- field.setValue("abc@acme.com");
- errors = binder.validate();
- Assert.assertEquals(0, errors.size());
- Assert.assertNull(field.getErrorMessage());
- }
-
- @Test
- public void converterBookOfVaadinExample1() {
- TextField yearOfBirthField = new TextField();
- // Slider for integers between 1 and 10
- Slider salaryLevelField = new Slider("Salary level", 1, 10);
-
- Binding<BookPerson, String, String> b1 = binder
- .forField(yearOfBirthField);
- Binding<BookPerson, String, Integer> b2 = b1.withConverter(
- new StringToIntegerConverter("Must enter a number"));
- b2.bind(BookPerson::getYearOfBirth, BookPerson::setYearOfBirth);
-
- Binding<BookPerson, Double, Double> salaryBinding1 = binder
- .forField(salaryLevelField);
- Binding<BookPerson, Double, Integer> salaryBinding2 = salaryBinding1
- .withConverter(Double::intValue, Integer::doubleValue);
- salaryBinding2.bind(BookPerson::getSalaryLevel,
- BookPerson::setSalaryLevel);
-
- // Test that the book code works
- BookPerson bookPerson = new BookPerson(1972, 4);
- binder.bind(bookPerson);
- Assert.assertEquals(4.0, salaryLevelField.getValue().doubleValue(), 0);
- Assert.assertEquals("1,972", yearOfBirthField.getValue());
-
- bookPerson.setSalaryLevel(8);
- binder.load(bookPerson);
- Assert.assertEquals(8.0, salaryLevelField.getValue().doubleValue(), 0);
- bookPerson.setYearOfBirth(123);
- binder.load(bookPerson);
- Assert.assertEquals("123", yearOfBirthField.getValue());
-
- yearOfBirthField.setValue("2016");
- salaryLevelField.setValue(1.0);
- Assert.assertEquals(2016, bookPerson.getYearOfBirth());
- Assert.assertEquals(1, bookPerson.getSalaryLevel());
- }
-
- @Test
- public void converterBookOfVaadinExample2() {
- TextField yearOfBirthField = new TextField();
-
- binder.forField(yearOfBirthField)
- .withConverter(Integer::valueOf, String::valueOf,
- // Text to use instead of the NumberFormatException
- // message
- "Please enter a number")
- .bind(BookPerson::getYearOfBirth, BookPerson::setYearOfBirth);
-
- binder.bind(new BookPerson(1900, 5));
- yearOfBirthField.setValue("abc");
- binder.validate();
- Assert.assertEquals("Please enter a number",
- yearOfBirthField.getComponentError().getFormattedHtmlMessage());
- }
-
- }
|