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

import static org.junit.Assert.assertEquals;
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.StringToDoubleConverter;

public class StringToDoubleConverterTest extends AbstractConverterTest {

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

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

    @Test
    public void testValueConversion() {
        Result<Double> value = getConverter().convertToModel("10",
                new ValueContext());
        assertValue(10.0d, value);
    }

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

    @Test
    public void customEmptyValue() {
        StringToDoubleConverter converter = new StringToDoubleConverter(0.0,
                getErrorMessage());

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

}