aboutsummaryrefslogtreecommitdiffstats
path: root/compatibility-server/src/test/java/com/vaadin/v7/data/fieldgroup/FieldGroupTest.java
blob: 219bd07815713071587f2ab73d139c465b165c0f (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
package com.vaadin.v7.data.fieldgroup;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsNull.nullValue;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;

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

import com.vaadin.v7.data.Property;
import com.vaadin.v7.data.Property.Transactional;
import com.vaadin.v7.data.util.BeanItem;
import com.vaadin.v7.data.util.TransactionalPropertyWrapper;
import com.vaadin.v7.ui.Field;
import com.vaadin.v7.ui.TextField;

public class FieldGroupTest {

    private FieldGroup sut;
    private Field field;

    @Before
    public void setup() {
        sut = new FieldGroup();
        field = mock(Field.class);
    }

    @Test
    public void fieldIsBound() {
        sut.bind(field, "foobar");

        assertThat(sut.getField("foobar"), is(field));
    }

    @Test(expected = FieldGroup.BindException.class)
    public void cannotBindToAlreadyBoundProperty() {
        sut.bind(field, "foobar");
        sut.bind(mock(Field.class), "foobar");
    }

    @Test(expected = FieldGroup.BindException.class)
    public void cannotBindNullField() {
        sut.bind(null, "foobar");
    }

    public void canUnbindWithoutItem() {
        sut.bind(field, "foobar");

        sut.unbind(field);
        assertThat(sut.getField("foobar"), is(nullValue()));
    }

    @Test
    public void wrapInTransactionalProperty_provideCustomImpl_customTransactionalWrapperIsUsed() {
        Bean bean = new Bean();
        FieldGroup group = new FieldGroup() {
            @Override
            protected <T> Transactional<T> wrapInTransactionalProperty(
                    Property<T> itemProperty) {
                return new TransactionalPropertyImpl(itemProperty);
            }
        };
        group.setItemDataSource(new BeanItem<Bean>(bean));
        TextField field = new TextField();
        group.bind(field, "name");

        Property propertyDataSource = field.getPropertyDataSource();
        assertTrue(
                "Custom implementation of transactional property "
                        + "has not been used",
                propertyDataSource instanceof TransactionalPropertyImpl);
    }

    public static class TransactionalPropertyImpl<T>
            extends TransactionalPropertyWrapper<T> {

        public TransactionalPropertyImpl(Property<T> wrappedProperty) {
            super(wrappedProperty);
        }

    }

    public static class Bean {
        private String name;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }
}