summaryrefslogtreecommitdiffstats
path: root/server/src/test/java/com/vaadin/data/BeanBinderInstanceFieldTest.java
blob: d04789f35398c1746ee19820d275412490f24f2c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/*
 * 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.util.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;

/**
 * Unit tests for {@link BeanBinder#bindInstanceFields(Object)} method.
 * 
 * @author Vaadin Ltd
 *
 */
public class BeanBinderInstanceFieldTest {

    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();
        BeanBinder<Person> binder = new BeanBinder<>(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
    public void bindInstanceFields_bindOnlyOneFields() {
        BindOnlyOneField form = new BindOnlyOneField();
        BeanBinder<Person> binder = new BeanBinder<>(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();
        BeanBinder<Person> binder = new BeanBinder<>(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();
        BeanBinder<Person> binder = new BeanBinder<>(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();
        BeanBinder<Person> binder = new BeanBinder<>(Person.class);
        binder.bindInstanceFields(form);
    }

    @Test(expected = IllegalStateException.class)
    public void bindInstanceFields_generic() {
        BindGeneric<String> form = new BindGeneric<>();
        BeanBinder<Person> binder = new BeanBinder<>(Person.class);
        binder.bindInstanceFields(form);
    }

    @Test(expected = IllegalStateException.class)
    public void bindInstanceFields_rawFieldType() {
        BindRaw form = new BindRaw();
        BeanBinder<Person> binder = new BeanBinder<>(Person.class);
        binder.bindInstanceFields(form);
    }

    @Test(expected = IllegalStateException.class)
    public void bindInstanceFields_abstractFieldType() {
        BindAbstract form = new BindAbstract();
        BeanBinder<Person> binder = new BeanBinder<>(Person.class);
        binder.bindInstanceFields(form);
    }

    @Test(expected = IllegalStateException.class)
    public void bindInstanceFields_noInstantiatableFieldType() {
        BindNonInstantiatableType form = new BindNonInstantiatableType();
        BeanBinder<Person> binder = new BeanBinder<>(Person.class);
        binder.bindInstanceFields(form);
    }

    @Test(expected = IllegalStateException.class)
    public void bindInstanceFields_wrongFieldType() {
        BindWrongTypeParameterField form = new BindWrongTypeParameterField();
        BeanBinder<Person> binder = new BeanBinder<>(Person.class);
        binder.bindInstanceFields(form);
    }

    @Test
    public void bindInstanceFields_complexGenericHierarchy() {
        BindComplextHierarchyGenericType form = new BindComplextHierarchyGenericType();
        BeanBinder<Person> binder = new BeanBinder<>(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();
        BeanBinder<Person> binder = new BeanBinder<>(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();
        BeanBinder<Person> binder = new BeanBinder<>(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();
        BeanBinder<Person> binder = new BeanBinder<>(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();
        BeanBinder<Person> binder = new BeanBinder<>(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());
    }
}