aboutsummaryrefslogtreecommitdiffstats
path: root/compatibility-server/src/test/java/com/vaadin/tests/server/component/label/LabelConvertersTest.java
blob: bc9e30ceed9435e9bc2957f4c0313cb4db4198fa (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
package com.vaadin.tests.server.component.label;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail;

import org.junit.Before;
import org.junit.Test;

import com.vaadin.server.VaadinSession;
import com.vaadin.tests.data.bean.Person;
import com.vaadin.tests.util.AlwaysLockedVaadinSession;
import com.vaadin.util.CurrentInstance;
import com.vaadin.v7.data.Property;
import com.vaadin.v7.data.util.MethodProperty;
import com.vaadin.v7.ui.Label;

public class LabelConvertersTest {
    @Before
    public void clearExistingThreadLocals() {
        // Ensure no previous test left some thread locals hanging
        CurrentInstance.clearAll();
    }

    @Test
    public void testLabelSetDataSourceLaterOn() {
        Person p = Person.createTestPerson1();
        Label l = new Label("My label");
        assertEquals("My label", l.getValue());
        assertNull(l.getConverter());
        l.setPropertyDataSource(new MethodProperty<String>(p, "firstName"));
        assertEquals(p.getFirstName(), l.getValue());
        p.setFirstName("123");
        assertEquals("123", l.getValue());
    }

    @Test
    public void testIntegerDataSource() {
        VaadinSession.setCurrent(new AlwaysLockedVaadinSession(null));
        Label l = new Label("Foo");
        Property ds = new MethodProperty<Integer>(Person.createTestPerson1(),
                "age");
        l.setPropertyDataSource(ds);
        assertEquals(String.valueOf(Person.createTestPerson1().getAge()),
                l.getValue());
    }

    @Test
    public void testSetValueWithDataSource() {
        try {
            MethodProperty<String> property = new MethodProperty<String>(
                    Person.createTestPerson1(), "firstName");
            Label l = new Label(property);
            l.setValue("Foo");
            fail("setValue should throw an exception when a data source is set");
        } catch (Exception e) {
        }

    }

    @Test
    public void testLabelWithoutDataSource() {
        Label l = new Label("My label");
        assertEquals("My label", l.getValue());
        assertNull(l.getConverter());
        assertNull(l.getPropertyDataSource());
        l.setValue("New value");
        assertEquals("New value", l.getValue());
        assertNull(l.getConverter());
        assertNull(l.getPropertyDataSource());
    }
}