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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
|
package com.vaadin.data;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertSame;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import com.vaadin.data.Binder.Binding;
import com.vaadin.data.util.converter.Converter;
import com.vaadin.server.AbstractErrorMessage;
import com.vaadin.server.ErrorMessage;
import com.vaadin.server.UserError;
import com.vaadin.tests.data.bean.Person;
import com.vaadin.ui.AbstractField;
public class BinderTest {
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 StatusBean {
private String status;
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
}
Binder<Person> binder;
TextField nameField;
TextField ageField;
Person p = new Person();
Validator<String> notEmpty = Validator.from(val -> !val.isEmpty(),
"Value cannot be empty");
Converter<String, Integer> stringToInteger = Converter.from(
Integer::valueOf, String::valueOf, e -> "Value must be a number");
Validator<Integer> notNegative = Validator.from(x -> x >= 0,
"Value must be positive");
@Before
public void setUp() {
binder = new Binder<>();
p.setFirstName("Johannes");
p.setAge(32);
nameField = new TextField();
ageField = new TextField();
}
@Test(expected = NullPointerException.class)
public void bindingNullBeanThrows() {
binder.bind(null);
}
@Test(expected = NullPointerException.class)
public void bindingNullFieldThrows() {
binder.forField(null);
}
@Test(expected = NullPointerException.class)
public void bindingNullGetterThrows() {
binder.bind(nameField, null, Person::setFirstName);
}
@Test
public void fieldValueUpdatedOnBeanBind() {
binder.forField(nameField).bind(Person::getFirstName,
Person::setFirstName);
binder.bind(p);
assertEquals("Johannes", nameField.getValue());
}
@Test
public void fieldValueUpdatedWithShortcutBind() {
bindName();
assertEquals("Johannes", nameField.getValue());
}
@Test
public void fieldValueUpdatedIfBeanAlreadyBound() {
binder.bind(p);
binder.bind(nameField, Person::getFirstName, Person::setFirstName);
assertEquals("Johannes", nameField.getValue());
nameField.setValue("Artur");
assertEquals("Artur", p.getFirstName());
}
@Test
public void getBeanReturnsBoundBeanOrNothing() {
assertFalse(binder.getBean().isPresent());
binder.bind(p);
assertSame(p, binder.getBean().get());
binder.unbind();
assertFalse(binder.getBean().isPresent());
}
@Test
public void fieldValueSavedToPropertyOnChange() {
bindName();
nameField.setValue("Henri");
assertEquals("Henri", p.getFirstName());
}
@Test
public void fieldValueNotSavedAfterUnbind() {
bindName();
nameField.setValue("Henri");
binder.unbind();
nameField.setValue("Aleksi");
assertEquals("Henri", p.getFirstName());
}
@Test
public void bindNullSetterIgnoresValueChange() {
binder.bind(nameField, Person::getFirstName, null);
binder.bind(p);
nameField.setValue("Artur");
assertEquals(p.getFirstName(), "Johannes");
}
@Test
public void bindToAnotherBeanStopsUpdatingOriginalBean() {
bindName();
nameField.setValue("Leif");
Person p2 = new Person();
p2.setFirstName("Marlon");
binder.bind(p2);
assertEquals("Marlon", nameField.getValue());
assertEquals("Leif", p.getFirstName());
assertSame(p2, binder.getBean().get());
nameField.setValue("Ilia");
assertEquals("Ilia", p2.getFirstName());
assertEquals("Leif", p.getFirstName());
}
@Test
public void save_unbound_noChanges() {
Binder<Person> binder = new Binder<>();
Person person = new Person();
int age = 10;
person.setAge(age);
binder.save(person);
Assert.assertEquals(age, person.getAge());
}
@Test
public void save_bound_beanIsUpdated() {
Binder<Person> binder = new Binder<>();
binder.bind(nameField, Person::getFirstName, Person::setFirstName);
Person person = new Person();
String fieldValue = "bar";
nameField.setValue(fieldValue);
person.setFirstName("foo");
binder.save(person);
Assert.assertEquals(fieldValue, person.getFirstName());
}
@Test
public void load_bound_fieldValueIsUpdated() {
Binder<Person> binder = new Binder<>();
binder.bind(nameField, Person::getFirstName, Person::setFirstName);
Person person = new Person();
String name = "bar";
person.setFirstName(name);
binder.load(person);
Assert.assertEquals(name, nameField.getValue());
}
@Test
public void load_unbound_noChanges() {
Binder<Person> binder = new Binder<>();
nameField.setValue("");
Person person = new Person();
String name = "bar";
person.setFirstName(name);
binder.load(person);
Assert.assertEquals("", nameField.getValue());
}
@Test
public void validate_notBound_noErrors() {
Binder<Person> binder = new Binder<>();
List<ValidationError<?>> errors = binder.validate();
Assert.assertTrue(errors.isEmpty());
}
@Test
public void bound_validatorsAreOK_noErrors() {
Binder<Person> binder = new Binder<>();
Binding<Person, String, String> binding = binder.forField(nameField);
binding.withValidator(Validator.alwaysPass()).bind(Person::getFirstName,
Person::setFirstName);
nameField.setComponentError(new UserError(""));
List<ValidationError<?>> errors = binder.validate();
Assert.assertTrue(errors.isEmpty());
Assert.assertNull(nameField.getComponentError());
}
@SuppressWarnings("serial")
@Test
public void bound_validatorsFail_errors() {
Binder<Person> binder = new Binder<>();
Binding<Person, String, String> binding = binder.forField(nameField);
binding.withValidator(Validator.alwaysPass());
String msg1 = "foo";
String msg2 = "bar";
binding.withValidator(new Validator<String>() {
@Override
public Result<String> apply(String value) {
return new SimpleResult<>(null, msg1);
}
});
binding.withValidator(value -> false, msg2);
binding.bind(Person::getFirstName, Person::setFirstName);
List<ValidationError<?>> errors = binder.validate();
Assert.assertEquals(1, errors.size());
Set<String> errorMessages = errors.stream()
.map(ValidationError::getMessage).collect(Collectors.toSet());
Assert.assertTrue(errorMessages.contains(msg1));
Set<?> fields = errors.stream().map(ValidationError::getField)
.collect(Collectors.toSet());
Assert.assertEquals(1, fields.size());
Assert.assertTrue(fields.contains(nameField));
ErrorMessage componentError = nameField.getComponentError();
Assert.assertNotNull(componentError);
Assert.assertEquals("foo",
((AbstractErrorMessage) componentError).getMessage());
}
private void bindName() {
binder.bind(nameField, Person::getFirstName, Person::setFirstName);
binder.bind(p);
}
private void bindAgeWithValidatorConverterValidator() {
binder.forField(ageField).withValidator(notEmpty)
.withConverter(stringToInteger).withValidator(notNegative)
.bind(Person::getAge, Person::setAge);
binder.bind(p);
}
@Test
public void validatorForSuperTypeCanBeUsed() {
// Validates that a validator for a super type can be used, e.g.
// validator for Number can be used on a Double
TextField salaryField = new TextField();
Binder<Person> binder = new Binder<>();
Validator<Number> positiveNumberValidator = value -> {
if (value.doubleValue() >= 0) {
return Result.ok(value);
} else {
return Result.error("Number must be positive");
}
};
binder.forField(salaryField)
.withConverter(Double::valueOf, String::valueOf)
.withValidator(positiveNumberValidator)
.bind(Person::getSalaryDouble, Person::setSalaryDouble);
Person person = new Person();
binder.bind(person);
salaryField.setValue("10");
Assert.assertEquals(10, person.getSalaryDouble(), 0);
salaryField.setValue("-1"); // Does not pass validator
Assert.assertEquals(10, person.getSalaryDouble(), 0);
}
@Test
public void convertInitialValue() {
bindAgeWithValidatorConverterValidator();
assertEquals("32", ageField.getValue());
}
@Test
public void convertToModelValidAge() {
bindAgeWithValidatorConverterValidator();
ageField.setValue("33");
assertEquals(33, p.getAge());
}
@Test
public void convertToModelNegativeAgeFailsOnFirstValidator() {
bindAgeWithValidatorConverterValidator();
ageField.setValue("");
assertEquals(32, p.getAge());
assertValidationErrors(binder.validate(), "Value cannot be empty");
}
private void assertValidationErrors(
List<ValidationError<?>> validationErrors,
String... errorMessages) {
Assert.assertEquals(errorMessages.length, validationErrors.size());
for (int i = 0; i < errorMessages.length; i++) {
Assert.assertEquals(errorMessages[i],
validationErrors.get(i).getMessage());
}
}
@Test
public void convertToModelConversionFails() {
bindAgeWithValidatorConverterValidator();
ageField.setValue("abc");
assertEquals(32, p.getAge());
assertValidationErrors(binder.validate(), "Value must be a number");
}
@Test
public void convertToModelNegativeAgeFailsOnIntegerValidator() {
bindAgeWithValidatorConverterValidator();
ageField.setValue("-5");
assertEquals(32, p.getAge());
assertValidationErrors(binder.validate(), "Value must be positive");
}
@Test
public void convertDataToField() {
bindAgeWithValidatorConverterValidator();
binder.getBean().get().setAge(12);
binder.load(binder.getBean().get());
Assert.assertEquals("12", ageField.getValue());
}
@Test
public void convertNotValidatableDataToField() {
bindAgeWithValidatorConverterValidator();
binder.getBean().get().setAge(-12);
binder.load(binder.getBean().get());
Assert.assertEquals("-12", ageField.getValue());
}
@Test(expected = IllegalArgumentException.class)
public void convertInvalidDataToField() {
TextField field = new TextField();
StatusBean bean = new StatusBean();
bean.setStatus("1");
Binder<StatusBean> binder = new Binder<StatusBean>();
Binding<StatusBean, String, String> binding = binder.forField(field)
.withConverter(presentation -> {
if (presentation.equals("OK")) {
return "1";
} else if (presentation.equals("NOTOK")) {
return "2";
}
throw new IllegalArgumentException(
"Value must be OK or NOTOK");
}, model -> {
if (model.equals("1")) {
return "OK";
} else if (model.equals("2")) {
return "NOTOK";
} else {
throw new IllegalArgumentException(
"Value in model must be 1 or 2");
}
});
binding.bind(StatusBean::getStatus, StatusBean::setStatus);
binder.bind(bean);
bean.setStatus("3");
binder.load(bean);
}
}
|