aboutsummaryrefslogtreecommitdiffstats
path: root/uitest/src/com/vaadin/tests/components/datefield/DateFieldUnparsableDate.java
blob: bbe2423a3f7d886b0be0ad14d8d6b02ba7853ce4 (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
93
94
95
96
97
98
99
100
101
102
103
104
105
package com.vaadin.tests.components.datefield;

import java.util.Date;

import com.vaadin.data.Property;
import com.vaadin.data.util.converter.Converter;
import com.vaadin.tests.components.TestBase;
import com.vaadin.ui.DateField;

public class DateFieldUnparsableDate extends TestBase {

    public class MyDateField extends DateField {
        Date oldDate = null;

        public MyDateField(String caption) {
            super(caption);
            addListener(new Property.ValueChangeListener() {
                @Override
                public void valueChange(
                        com.vaadin.data.Property.ValueChangeEvent event) {
                    oldDate = getValue();
                }
            });
        }

        @Override
        protected Date handleUnparsableDateString(String dateString)
                throws Converter.ConversionException {
            return oldDate;
        }
    }

    public class MyDateField2 extends DateField {
        public MyDateField2(String caption) {
            super(caption);
        }

        @Override
        protected Date handleUnparsableDateString(String dateString)
                throws Converter.ConversionException {
            return null;
        }
    }

    public class MyDateField3 extends DateField {
        public MyDateField3(String caption) {
            super(caption);
        }

        @Override
        protected Date handleUnparsableDateString(String dateString)
                throws Converter.ConversionException {
            throw new Converter.ConversionException(
                    "You should not enter invalid dates!");
        }
    }

    public class MyDateField4 extends DateField {
        public MyDateField4(String caption) {
            super(caption);
        }

        @Override
        protected Date handleUnparsableDateString(String dateString)
                throws Converter.ConversionException {
            if (dateString != null && dateString.equals("today")) {
                return new Date();
            }
            throw new Converter.ConversionException(
                    "You should not enter invalid dates!");
        }
    }

    @Override
    protected void setup() {
        MyDateField df = new MyDateField(
                "Returns the old value for invalid dates");
        df.setImmediate(true);
        addComponent(df);

        MyDateField2 df2 = new MyDateField2("Returns empty for invalid dates");
        df2.setImmediate(true);
        addComponent(df2);

        MyDateField3 df3 = new MyDateField3(
                "Throws an exception for invalid dates");
        df3.setImmediate(true);
        addComponent(df3);

        MyDateField4 df4 = new MyDateField4("Can convert 'today'");
        df4.setImmediate(true);
        addComponent(df4);

    }

    @Override
    protected String getDescription() {
        return "DateFields in various configurations (according to caption). All handle unparsable dates differently";
    }

    @Override
    protected Integer getTicketNumber() {
        return 4236;
    }
}