aboutsummaryrefslogtreecommitdiffstats
path: root/server/src/test/java/com/vaadin/data/BinderTestBase.java
blob: ca3e2d07794cf5a112f477a3f3cd2611f4be9962 (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
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);
    }
}