123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388 |
- /*
- * 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.time.LocalDate;
-
- import org.junit.Assert;
- import org.junit.Test;
-
- import com.vaadin.annotations.PropertyId;
- import com.vaadin.data.converter.StringToIntegerConverter;
- import com.vaadin.data.validator.StringLengthValidator;
- import com.vaadin.tests.data.bean.Person;
- import com.vaadin.ui.AbstractField;
- import com.vaadin.ui.AbstractTextField;
- import com.vaadin.ui.DateField;
- import com.vaadin.ui.FormLayout;
- import com.vaadin.ui.TextField;
-
- public class BinderInstanceFieldTest {
-
- public static class BindAllFields extends FormLayout {
- private TextField firstName;
- private DateField birthDate;
- }
-
- public static class BindFieldsUsingAnnotation extends FormLayout {
- @PropertyId("firstName")
- private TextField nameField;
-
- @PropertyId("birthDate")
- private DateField birthDateField;
- }
-
- public static class BindOnlyOneField extends FormLayout {
- private TextField firstName;
- private TextField noFieldInPerson;
- }
-
- public static class BindNoHasValueField extends FormLayout {
- private String firstName;
- }
-
- public static class BindGenericField extends FormLayout {
- private CustomField<String> firstName;
- }
-
- public static class BindGenericWrongTypeParameterField extends FormLayout {
- private CustomField<Boolean> firstName;
- }
-
- public static class BindWrongTypeParameterField extends FormLayout {
- private IntegerTextField firstName;
- }
-
- public static class BindGeneric<T> extends FormLayout {
- private CustomField<T> firstName;
- }
-
- public static class BindRaw extends FormLayout {
- private CustomField firstName;
- }
-
- public static class BindAbstract extends FormLayout {
- private AbstractTextField firstName;
- }
-
- public static class BindNonInstantiatableType extends FormLayout {
- private NoDefaultCtor firstName;
- }
-
- public static class BindComplextHierarchyGenericType extends FormLayout {
- private ComplexHierarchy firstName;
- }
-
- public static class NoDefaultCtor extends TextField {
- public NoDefaultCtor(int arg) {
- }
- }
-
- public static class IntegerTextField extends CustomField<Integer> {
-
- }
-
- public static class ComplexHierarchy extends Generic<Long> {
-
- }
-
- public static class Generic<T> extends ComplexGeneric<Boolean, String, T> {
-
- }
-
- public static class ComplexGeneric<U, V, S> extends CustomField<V> {
-
- }
-
- public static class CustomField<T> extends AbstractField<T> {
-
- private T value;
-
- @Override
- public T getValue() {
- return value;
- }
-
- @Override
- protected void doSetValue(T value) {
- this.value = value;
- }
-
- }
-
- @Test
- public void bindInstanceFields_bindAllFields() {
- BindAllFields form = new BindAllFields();
- Binder<Person> binder = new Binder<>(Person.class);
- binder.bindInstanceFields(form);
-
- Person person = new Person();
- person.setFirstName("foo");
- person.setBirthDate(LocalDate.now());
-
- binder.setBean(person);
-
- Assert.assertEquals(person.getFirstName(), form.firstName.getValue());
- Assert.assertEquals(person.getBirthDate(), form.birthDate.getValue());
-
- form.firstName.setValue("bar");
- form.birthDate.setValue(person.getBirthDate().plusDays(345));
-
- Assert.assertEquals(form.firstName.getValue(), person.getFirstName());
- Assert.assertEquals(form.birthDate.getValue(), person.getBirthDate());
- }
-
- @Test(expected = IllegalStateException.class)
- public void bind_instanceFields_noArgsConstructor() {
- BindAllFields form = new BindAllFields();
- Binder<Person> binder = new Binder<>();
- binder.bindInstanceFields(form);
- }
-
- @Test
- public void bindInstanceFields_bindOnlyOneFields() {
- BindOnlyOneField form = new BindOnlyOneField();
- Binder<Person> binder = new Binder<>(Person.class);
- binder.bindInstanceFields(form);
-
- Person person = new Person();
- person.setFirstName("foo");
-
- binder.setBean(person);
-
- Assert.assertEquals(person.getFirstName(), form.firstName.getValue());
-
- Assert.assertNull(form.noFieldInPerson);
-
- form.firstName.setValue("bar");
-
- Assert.assertEquals(form.firstName.getValue(), person.getFirstName());
- }
-
- @Test
- public void bindInstanceFields_bindNotHasValueField_fieldIsNull() {
- BindNoHasValueField form = new BindNoHasValueField();
- Binder<Person> binder = new Binder<>(Person.class);
- binder.bindInstanceFields(form);
-
- Person person = new Person();
- person.setFirstName("foo");
-
- binder.setBean(person);
-
- Assert.assertNull(form.firstName);
- }
-
- @Test
- public void bindInstanceFields_genericField() {
- BindGenericField form = new BindGenericField();
- Binder<Person> binder = new Binder<>(Person.class);
- binder.bindInstanceFields(form);
-
- Person person = new Person();
- person.setFirstName("foo");
-
- binder.setBean(person);
-
- Assert.assertEquals(person.getFirstName(), form.firstName.getValue());
-
- form.firstName.setValue("bar");
-
- Assert.assertEquals(form.firstName.getValue(), person.getFirstName());
- }
-
- @Test(expected = IllegalStateException.class)
- public void bindInstanceFields_genericFieldWithWrongTypeParameter() {
- BindGenericWrongTypeParameterField form = new BindGenericWrongTypeParameterField();
- Binder<Person> binder = new Binder<>(Person.class);
- binder.bindInstanceFields(form);
- }
-
- @Test(expected = IllegalStateException.class)
- public void bindInstanceFields_generic() {
- BindGeneric<String> form = new BindGeneric<>();
- Binder<Person> binder = new Binder<>(Person.class);
- binder.bindInstanceFields(form);
- }
-
- @Test(expected = IllegalStateException.class)
- public void bindInstanceFields_rawFieldType() {
- BindRaw form = new BindRaw();
- Binder<Person> binder = new Binder<>(Person.class);
- binder.bindInstanceFields(form);
- }
-
- @Test(expected = IllegalStateException.class)
- public void bindInstanceFields_abstractFieldType() {
- BindAbstract form = new BindAbstract();
- Binder<Person> binder = new Binder<>(Person.class);
- binder.bindInstanceFields(form);
- }
-
- @Test(expected = IllegalStateException.class)
- public void bindInstanceFields_noInstantiatableFieldType() {
- BindNonInstantiatableType form = new BindNonInstantiatableType();
- Binder<Person> binder = new Binder<>(Person.class);
- binder.bindInstanceFields(form);
- }
-
- @Test(expected = IllegalStateException.class)
- public void bindInstanceFields_wrongFieldType() {
- BindWrongTypeParameterField form = new BindWrongTypeParameterField();
- Binder<Person> binder = new Binder<>(Person.class);
- binder.bindInstanceFields(form);
- }
-
- @Test
- public void bindInstanceFields_complexGenericHierarchy() {
- BindComplextHierarchyGenericType form = new BindComplextHierarchyGenericType();
- Binder<Person> binder = new Binder<>(Person.class);
- binder.bindInstanceFields(form);
-
- Person person = new Person();
- person.setFirstName("foo");
-
- binder.setBean(person);
-
- Assert.assertEquals(person.getFirstName(), form.firstName.getValue());
-
- form.firstName.setValue("bar");
-
- Assert.assertEquals(form.firstName.getValue(), person.getFirstName());
- }
-
- @Test
- public void bindInstanceFields_bindNotHasValueField_fieldIsNotReplaced() {
- BindNoHasValueField form = new BindNoHasValueField();
- Binder<Person> binder = new Binder<>(Person.class);
-
- String name = "foo";
- form.firstName = name;
-
- binder.bindInstanceFields(form);
-
- Person person = new Person();
- person.setFirstName("foo");
-
- binder.setBean(person);
-
- Assert.assertEquals(name, form.firstName);
- }
-
- @Test
- public void bindInstanceFields_bindAllFieldsUsingAnnotations() {
- BindFieldsUsingAnnotation form = new BindFieldsUsingAnnotation();
- Binder<Person> binder = new Binder<>(Person.class);
- binder.bindInstanceFields(form);
-
- Person person = new Person();
- person.setFirstName("foo");
- person.setBirthDate(LocalDate.now());
-
- binder.setBean(person);
-
- Assert.assertEquals(person.getFirstName(), form.nameField.getValue());
- Assert.assertEquals(person.getBirthDate(),
- form.birthDateField.getValue());
-
- form.nameField.setValue("bar");
- form.birthDateField.setValue(person.getBirthDate().plusDays(345));
-
- Assert.assertEquals(form.nameField.getValue(), person.getFirstName());
- Assert.assertEquals(form.birthDateField.getValue(),
- person.getBirthDate());
- }
-
- @Test
- public void bindInstanceFields_bindNotBoundFieldsOnly_customBindingIsNotReplaced() {
- BindAllFields form = new BindAllFields();
- Binder<Person> binder = new Binder<>(Person.class);
-
- TextField name = new TextField();
- form.firstName = name;
- binder.forField(form.firstName)
- .withValidator(
- new StringLengthValidator("Name is invalid", 3, 10))
- .bind("firstName");
-
- binder.bindInstanceFields(form);
-
- Person person = new Person();
- String personName = "foo";
- person.setFirstName(personName);
- person.setBirthDate(LocalDate.now());
-
- binder.setBean(person);
-
- Assert.assertEquals(person.getFirstName(), form.firstName.getValue());
- Assert.assertEquals(person.getBirthDate(), form.birthDate.getValue());
- // the instance is not overridden
- Assert.assertEquals(name, form.firstName);
-
- form.firstName.setValue("aa");
- form.birthDate.setValue(person.getBirthDate().plusDays(345));
-
- Assert.assertEquals(personName, person.getFirstName());
- Assert.assertEquals(form.birthDate.getValue(), person.getBirthDate());
-
- Assert.assertFalse(binder.validate().isOk());
- }
-
- @Test
- public void bindInstanceFields_fieldsAreConfigured_customBindingIsNotReplaced() {
- BindOnlyOneField form = new BindOnlyOneField();
- Binder<Person> binder = new Binder<>(Person.class);
-
- TextField name = new TextField();
- form.firstName = name;
- binder.forField(form.firstName)
- .withValidator(
- new StringLengthValidator("Name is invalid", 3, 10))
- .bind("firstName");
- TextField ageField = new TextField();
- form.noFieldInPerson = ageField;
- binder.forField(form.noFieldInPerson)
- .withConverter(new StringToIntegerConverter(""))
- .bind(Person::getAge, Person::setAge);
-
- binder.bindInstanceFields(form);
-
- Person person = new Person();
- String personName = "foo";
- int age = 11;
- person.setFirstName(personName);
- person.setAge(age);
-
- binder.setBean(person);
-
- Assert.assertEquals(person.getFirstName(), form.firstName.getValue());
- Assert.assertEquals(String.valueOf(person.getAge()),
- form.noFieldInPerson.getValue());
- // the instances are not overridden
- Assert.assertEquals(name, form.firstName);
- Assert.assertEquals(ageField, form.noFieldInPerson);
-
- form.firstName.setValue("aa");
- age += 56;
- form.noFieldInPerson.setValue(String.valueOf(age));
-
- Assert.assertEquals(personName, person.getFirstName());
- Assert.assertEquals(form.noFieldInPerson.getValue(),
- String.valueOf(person.getAge()));
-
- Assert.assertFalse(binder.validate().isOk());
- }
- }
|