aboutsummaryrefslogtreecommitdiffstats
path: root/server/src/test/java/com/vaadin/tests/data/converter/StringToIntegerConverterTest.java
blob: db050233cff52220b03a09161eff8a9e43af2f4d (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
package com.vaadin.tests.data.converter;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import org.junit.Test;

import com.vaadin.data.Result;
import com.vaadin.data.ValueContext;
import com.vaadin.data.converter.StringToIntegerConverter;

public class StringToIntegerConverterTest extends AbstractConverterTest {

    @Override
    protected StringToIntegerConverter getConverter() {
        return new StringToIntegerConverter("Failed");
    }

    @Test
    public void testEmptyStringConversion() {
        assertValue(null,
                getConverter().convertToModel("", new ValueContext()));
    }

    @Test
    public void testValueOutOfRange() {
        Double[] values = { Integer.MAX_VALUE * 2.0, Integer.MIN_VALUE * 2.0,
                Long.MAX_VALUE * 2.0, Long.MIN_VALUE * 2.0 };

        boolean accepted = false;
        for (Number value : values) {
            try {
                getConverter().convertToModel(String.format("%.0f", value),
                        new ValueContext());
            } catch (Exception e) {
                accepted = true;
            }
        }
        assertFalse("Accepted value outside range of int", accepted);
    }

    @Test
    public void testValueConversion() {
        assertValue(Integer.valueOf(10),
                getConverter().convertToModel("10", new ValueContext()));
    }

    @Test
    public void testErrorMessage() {
        Result<Integer> result = getConverter().convertToModel("abc",
                new ValueContext());
        assertTrue(result.isError());
        assertEquals("Failed", result.getMessage().get());
    }

    @Test
    public void customEmptyValue() {
        StringToIntegerConverter converter = new StringToIntegerConverter(0,
                getErrorMessage());

        assertValue(0, converter.convertToModel("", new ValueContext()));
        assertEquals("0",
                converter.convertToPresentation(0, new ValueContext()));
    }
}