aboutsummaryrefslogtreecommitdiffstats
path: root/compatibility-server/src/test/java/com/vaadin/tests/server/PropertyFormatterTest.java
blob: 2d49ea52750e1a813360843548a1c8b6604ac068 (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
package com.vaadin.tests.server;

import static org.junit.Assert.assertTrue;

import java.util.Date;

import org.junit.Test;

import com.vaadin.v7.data.util.ObjectProperty;
import com.vaadin.v7.data.util.PropertyFormatter;

@SuppressWarnings("unchecked")
public class PropertyFormatterTest {

    class TestFormatter extends PropertyFormatter {

        @Override
        public String format(Object value) {
            boolean isCorrectType = getExpectedClass()
                    .isAssignableFrom(value.getClass());
            assertTrue(isCorrectType);
            return "FOO";
        }

        @Override
        public Object parse(String formattedValue) throws Exception {
            return getExpectedClass().newInstance();
        }
    }

    @SuppressWarnings("rawtypes")
    private Class expectedClass;

    @SuppressWarnings("rawtypes")
    private Class getExpectedClass() {
        return expectedClass;
    }

    /**
     * The object passed to format should be same as property's type.
     *
     * @throws IllegalAccessException
     * @throws InstantiationException
     */
    @Test
    @SuppressWarnings({ "rawtypes" })
    public void testCorrectTypeForFormat()
            throws InstantiationException, IllegalAccessException {
        Class[] testedTypes = { Integer.class, Boolean.class,
                Double.class, String.class, Date.class };
        Object[] testValues = { 3, false,
                3.3D, "bar", new Date() };

        int i = 0;
        for (Class class1 : testedTypes) {
            expectedClass = class1;

            TestFormatter formatter = new TestFormatter();

            // Should just return null, without formatting
            Object value = formatter.getValue();

            // test with property which value is null
            formatter.setPropertyDataSource(
                    new ObjectProperty(null, expectedClass));
            formatter.getValue(); // calls format

            // test with a value
            formatter.setPropertyDataSource(
                    new ObjectProperty(testValues[i++], expectedClass));
            formatter.getValue(); // calls format
        }

    }
}