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
|
package com.vaadin.data;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import com.vaadin.tests.data.bean.BeanToValidate;
public class BeanBinderTest extends
BinderTestBase<BeanBinder<BeanToValidate>, BeanToValidate> {
@Before
public void setUp() {
binder = new BeanBinder<>(BeanToValidate.class);
item = new BeanToValidate();
item.setFirstname("Johannes");
item.setAge(32);
}
@Test
public void fieldBound_bindBean_fieldValueUpdated() {
binder.bind(nameField, "firstname");
binder.bind(item);
assertEquals("Johannes", nameField.getValue());
}
@Test
public void beanBound_bindField_fieldValueUpdated() {
binder.bind(item);
binder.bind(nameField, "firstname");
assertEquals("Johannes", nameField.getValue());
}
@Test(expected = IllegalArgumentException.class)
public void bindInvalidPropertyName_throws() {
binder.bind(nameField, "firstnaem");
}
@Test(expected = NullPointerException.class)
public void bindNullPropertyName_throws() {
binder.bind(nameField, null);
}
@Test(expected = IllegalArgumentException.class)
public void bindNonReadableProperty_throws() {
binder.bind(nameField, "writeOnlyProperty");
}
@Test
public void beanBound_setValidFieldValue_propertyValueChanged() {
binder.bind(item);
binder.bind(nameField, "firstname");
nameField.setValue("Henri");
assertEquals("Henri", item.getFirstname());
}
@Test
public void readOnlyPropertyBound_setFieldValue_ignored() {
binder.bind(nameField, "readOnlyProperty");
binder.bind(item);
String propertyValue = item.getReadOnlyProperty();
nameField.setValue("Foo");
assertEquals(propertyValue, item.getReadOnlyProperty());
}
@Test
public void beanBound_setInvalidFieldValue_validationError() {
binder.bind(item);
binder.bind(nameField, "firstname");
nameField.setValue("H"); // too short
assertEquals("Johannes", item.getFirstname());
assertInvalid(nameField, "size must be between 3 and 16");
}
@Test
public void beanNotBound_setInvalidFieldValue_validationError() {
binder.bind(nameField, "firstname");
nameField.setValue("H"); // too short
assertInvalid(nameField, "size must be between 3 and 16");
}
@Test
public void explicitValidatorAdded_setInvalidFieldValue_explicitValidatorRunFirst() {
binder.forField(nameField).withValidator(name -> name.startsWith("J"),
"name must start with J").bind("firstname");
nameField.setValue("A");
assertInvalid(nameField, "name must start with J");
}
@Test
public void explicitValidatorAdded_setInvalidFieldValue_beanValidatorRun() {
binder.forField(nameField).withValidator(name -> name.startsWith("J"),
"name must start with J").bind("firstname");
nameField.setValue("J");
assertInvalid(nameField, "size must be between 3 and 16");
}
@Test(expected = ClassCastException.class)
public void fieldWithIncompatibleTypeBound_bindBean_throws() {
binder.bind(ageField, "age");
binder.bind(item);
}
@Test(expected = ClassCastException.class)
public void fieldWithIncompatibleTypeBound_loadBean_throws() {
binder.bind(ageField, "age");
binder.load(item);
}
@Test(expected = ClassCastException.class)
public void fieldWithIncompatibleTypeBound_saveBean_throws()
throws Throwable {
try {
binder.bind(ageField, "age");
binder.save(item);
} catch (RuntimeException e) {
throw e.getCause();
}
}
@Test
public void fieldWithConverterBound_bindBean_fieldValueUpdated() {
binder.forField(ageField)
.withConverter(Integer::valueOf, String::valueOf).bind("age");
binder.bind(item);
assertEquals("32", ageField.getValue());
}
@Test(expected = ClassCastException.class)
public void fieldWithInvalidConverterBound_bindBean_fieldValueUpdated() {
binder.forField(ageField).withConverter(Float::valueOf, String::valueOf)
.bind("age");
binder.bind(item);
assertEquals("32", ageField.getValue());
}
private void assertInvalid(HasValue<?> field, String message) {
BinderValidationStatus<?> status = binder.validate();
List<ValidationStatus<?>> errors = status.getFieldValidationErrors();
assertEquals(1, errors.size());
assertSame(field, errors.get(0).getField());
assertEquals(message, errors.get(0).getMessage().get());
}
}
|