aboutsummaryrefslogtreecommitdiffstats
path: root/server/src/test/java/com/vaadin/tests/data/converter/StringToBooleanConverterTest.java
blob: f1ab5f19f693dff9e2af91248bcd2fd62bfaa507 (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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package com.vaadin.tests.data.converter;

import static org.junit.Assert.assertEquals;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import org.junit.Test;

import com.vaadin.data.ValueContext;
import com.vaadin.data.converter.StringToBooleanConverter;

public class StringToBooleanConverterTest extends AbstractStringConverterTest {

    @Override
    protected StringToBooleanConverter getConverter() {
        return new StringToBooleanConverter(getErrorMessage());
    }

    private StringToBooleanConverter yesNoConverter = new StringToBooleanConverter(
            getErrorMessage(), "yes", "no");
    private StringToBooleanConverter emptyTrueConverter = new StringToBooleanConverter(
            getErrorMessage(), "", "ABSENT");
    private final StringToBooleanConverter localeConverter = new StringToBooleanConverter(
            getErrorMessage()) {
        @Override
        public String getFalseString(Locale locale) {
            Date d = new Date(3000000000000L);
            return SimpleDateFormat
                    .getDateInstance(SimpleDateFormat.LONG, locale)
                    .format(d.getTime()
                            + (d.getTimezoneOffset() + 120) * 60 * 1000L);
        }

        @Override
        public String getTrueString(Locale locale) {
            Date d = new Date(2000000000000L);
            return SimpleDateFormat
                    .getDateInstance(SimpleDateFormat.LONG, locale)
                    .format(d.getTime()
                            + (d.getTimezoneOffset() + 120) * 60 * 1000L);
        }
    };

    @Test
    public void testValueConversion() {
        assertValue(true,
                getConverter().convertToModel("true", new ValueContext()));
        assertValue(false,
                getConverter().convertToModel("false", new ValueContext()));
    }

    @Test
    public void testYesNoValueConversion() {
        assertValue(true,
                yesNoConverter.convertToModel("yes", new ValueContext()));
        assertValue(false,
                yesNoConverter.convertToModel("no", new ValueContext()));

        assertEquals("yes",
                yesNoConverter.convertToPresentation(true, new ValueContext()));
        assertEquals("no", yesNoConverter.convertToPresentation(false,
                new ValueContext()));
    }

    @Test
    public void testEmptyTrueValueConversion() {
        assertValue(true,
                emptyTrueConverter.convertToModel("", new ValueContext()));
        assertValue(false, emptyTrueConverter.convertToModel("ABSENT",
                new ValueContext()));

        assertEquals("", emptyTrueConverter.convertToPresentation(true,
                new ValueContext()));
        assertEquals("ABSENT", emptyTrueConverter.convertToPresentation(false,
                new ValueContext()));
    }

    @Test
    public void testLocale() {
        assertEquals("May 18, 2033", localeConverter.convertToPresentation(true,
                new ValueContext(Locale.US)));
        assertEquals("January 24, 2065", localeConverter
                .convertToPresentation(false, new ValueContext(Locale.US)));

        assertEquals("18. Mai 2033", localeConverter.convertToPresentation(true,
                new ValueContext(Locale.GERMANY)));
        assertEquals("24. Januar 2065", localeConverter.convertToPresentation(
                false, new ValueContext(Locale.GERMANY)));
    }
}