blob: 92edd734eb9634a84b6352ca3deaf02637afd234 (
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
|
/*
* 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.io.ByteArrayOutputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Locale;
import org.junit.After;
import org.junit.Before;
import com.vaadin.ui.TextField;
/**
* A base class for {@code Binder} unit tests.
*
* @author Vaadin Ltd.
*
* @since 8.0
*/
public abstract class BinderTestBase<BINDER extends Binder<ITEM>, ITEM> implements Serializable {
protected static final String NEGATIVE_ERROR_MESSAGE = "Value must be non-negative";
protected static final String NOT_NUMBER_ERROR_MESSAGE = "Value must be a number";
protected static final String EMPTY_ERROR_MESSAGE = "Value cannot be empty";
protected BINDER binder;
protected ITEM item;
protected TextField nameField;
protected TextField ageField;
protected Validator<String> notEmpty = Validator.from(val -> !val.isEmpty(),
EMPTY_ERROR_MESSAGE);
protected Converter<String, Integer> stringToInteger = Converter.from(
Integer::valueOf, String::valueOf, e -> NOT_NUMBER_ERROR_MESSAGE);
protected Validator<Integer> notNegative = Validator.from(x -> x >= 0,
NEGATIVE_ERROR_MESSAGE);
public static void testSerialization(Object toSerialize) {
try (ObjectOutputStream objectOutputStream = new ObjectOutputStream(new ByteArrayOutputStream())) {
objectOutputStream.writeObject(toSerialize);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Before
public void setUpBase() {
nameField = new TextField();
nameField.setLocale(Locale.US);
ageField = new TextField();
ageField.setLocale(Locale.US);
}
@After
public void testBinderSerialization() {
testSerialization(binder);
}
}
|