aboutsummaryrefslogtreecommitdiffstats
path: root/compatibility-server/src/test/java/com/vaadin/v7/data/util/sqlcontainer/ColumnPropertyTest.java
blob: c4fd631ad0fbb0eea9e4aa0b80d0d92d6d923111 (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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
package com.vaadin.v7.data.util.sqlcontainer;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.easymock.EasyMock;
import org.junit.Test;

import com.vaadin.v7.data.Property.ReadOnlyException;
import com.vaadin.v7.data.util.sqlcontainer.ColumnProperty.NotNullableException;
import com.vaadin.v7.data.util.sqlcontainer.query.QueryDelegate;

public class ColumnPropertyTest {

    @Test
    public void constructor_legalParameters_shouldSucceed() {
        ColumnProperty cp = new ColumnProperty("NAME", false, true, true, false,
                "Ville", String.class);
        assertNotNull(cp);
    }

    @Test(expected = IllegalArgumentException.class)
    public void constructor_missingPropertyId_shouldFail() {
        new ColumnProperty(null, false, true, true, false, "Ville",
                String.class);
    }

    @Test(expected = IllegalArgumentException.class)
    public void constructor_missingType_shouldFail() {
        new ColumnProperty("NAME", false, true, true, false, "Ville", null);
    }

    @Test
    public void getValue_defaultValue_returnsVille() {
        ColumnProperty cp = new ColumnProperty("NAME", false, true, true, false,
                "Ville", String.class);
        assertEquals("Ville", cp.getValue());
    }

    @Test
    public void setValue_readWriteNullable_returnsKalle() {
        ColumnProperty cp = new ColumnProperty("NAME", false, true, true, false,
                "Ville", String.class);
        SQLContainer container = EasyMock.createMock(SQLContainer.class);
        RowItem owner = new RowItem(container, new RowId(new Object[] { 1 }),
                Arrays.asList(cp));
        container.itemChangeNotification(owner);
        EasyMock.replay(container);
        cp.setValue("Kalle");
        assertEquals("Kalle", cp.getValue());
        EasyMock.verify(container);
    }

    @Test(expected = ReadOnlyException.class)
    public void setValue_readOnlyNullable_shouldFail() {
        ColumnProperty cp = new ColumnProperty("NAME", true, true, true, false,
                "Ville", String.class);
        SQLContainer container = EasyMock.createMock(SQLContainer.class);
        new RowItem(container, new RowId(new Object[] { 1 }),
                Arrays.asList(cp));
        EasyMock.replay(container);
        cp.setValue("Kalle");
        EasyMock.verify(container);
    }

    @Test
    public void setValue_readWriteNullable_nullShouldWork() {
        ColumnProperty cp = new ColumnProperty("NAME", false, true, true, false,
                "Ville", String.class);
        SQLContainer container = EasyMock.createMock(SQLContainer.class);
        RowItem owner = new RowItem(container, new RowId(new Object[] { 1 }),
                Arrays.asList(cp));
        container.itemChangeNotification(owner);
        EasyMock.replay(container);
        cp.setValue(null);
        assertNull(cp.getValue());
        EasyMock.verify(container);
    }

    @Test(expected = NotNullableException.class)
    public void setValue_readWriteNotNullable_nullShouldFail() {
        ColumnProperty cp = new ColumnProperty("NAME", false, true, false,
                false, "Ville", String.class);
        SQLContainer container = EasyMock.createMock(SQLContainer.class);
        RowItem owner = new RowItem(container, new RowId(new Object[] { 1 }),
                Arrays.asList(cp));
        container.itemChangeNotification(owner);
        EasyMock.replay(container);
        cp.setValue(null);
        assertNotNull(cp.getValue());
        EasyMock.verify(container);
    }

    @Test
    public void getType_normal_returnsStringClass() {
        ColumnProperty cp = new ColumnProperty("NAME", false, true, true, false,
                "Ville", String.class);
        assertSame(String.class, cp.getType());
    }

    @Test
    public void isReadOnly_readWriteNullable_returnsTrue() {
        ColumnProperty cp = new ColumnProperty("NAME", false, true, true, false,
                "Ville", String.class);
        assertFalse(cp.isReadOnly());
    }

    @Test
    public void isReadOnly_readOnlyNullable_returnsTrue() {
        ColumnProperty cp = new ColumnProperty("NAME", true, true, true, false,
                "Ville", String.class);
        assertTrue(cp.isReadOnly());
    }

    @Test
    public void setReadOnly_readOnlyChangeAllowed_shouldSucceed() {
        ColumnProperty cp = new ColumnProperty("NAME", false, true, true, false,
                "Ville", String.class);
        cp.setReadOnly(true);
        assertTrue(cp.isReadOnly());
    }

    @Test
    public void setReadOnly_readOnlyChangeDisallowed_shouldFail() {
        ColumnProperty cp = new ColumnProperty("NAME", false, false, true,
                false, "Ville", String.class);
        cp.setReadOnly(true);
        assertFalse(cp.isReadOnly());
    }

    @Test
    public void getPropertyId_normal_returnsNAME() {
        ColumnProperty cp = new ColumnProperty("NAME", false, false, true,
                false, "Ville", String.class);
        assertEquals("NAME", cp.getPropertyId());
    }

    @Test
    public void isModified_valueModified_returnsTrue() {
        ColumnProperty cp = new ColumnProperty("NAME", false, true, true, false,
                "Ville", String.class);
        SQLContainer container = EasyMock.createMock(SQLContainer.class);
        RowItem owner = new RowItem(container, new RowId(new Object[] { 1 }),
                Arrays.asList(cp));
        container.itemChangeNotification(owner);
        EasyMock.replay(container);
        cp.setValue("Kalle");
        assertEquals("Kalle", cp.getValue());
        assertTrue(cp.isModified());
        EasyMock.verify(container);
    }

    @Test
    public void isModified_valueNotModified_returnsFalse() {
        ColumnProperty cp = new ColumnProperty("NAME", false, false, true,
                false, "Ville", String.class);
        assertFalse(cp.isModified());
    }

    @Test
    public void setValue_nullOnNullable_shouldWork() {
        ColumnProperty cp = new ColumnProperty("NAME", false, true, true, false,
                "asdf", String.class);
        SQLContainer container = EasyMock.createMock(SQLContainer.class);
        new RowItem(container, new RowId(new Object[] { 1 }),
                Arrays.asList(cp));
        cp.setValue(null);
        assertNull(cp.getValue());
    }

    @Test
    public void setValue_resetTonullOnNullable_shouldWork() {
        ColumnProperty cp = new ColumnProperty("NAME", false, true, true, false,
                null, String.class);
        SQLContainer container = EasyMock.createMock(SQLContainer.class);
        new RowItem(container, new RowId(new Object[] { 1 }),
                Arrays.asList(cp));
        cp.setValue("asdf");
        assertEquals("asdf", cp.getValue());
        cp.setValue(null);
        assertNull(cp.getValue());
    }

    @Test
    public void setValue_sendsItemChangeNotification() throws SQLException {

        class TestContainer extends SQLContainer {
            Object value = null;
            boolean modified = false;

            public TestContainer(QueryDelegate delegate) throws SQLException {
                super(delegate);
            }

            @Override
            public void itemChangeNotification(RowItem changedItem) {
                ColumnProperty cp = (ColumnProperty) changedItem
                        .getItemProperty("NAME");
                value = cp.getValue();
                modified = cp.isModified();
            }
        }

        ColumnProperty property = new ColumnProperty("NAME", false, true, true,
                false, "Ville", String.class);

        Statement statement = EasyMock.createNiceMock(Statement.class);
        EasyMock.replay(statement);

        ResultSetMetaData metadata = EasyMock
                .createNiceMock(ResultSetMetaData.class);
        EasyMock.replay(metadata);

        ResultSet resultSet = EasyMock.createNiceMock(ResultSet.class);
        EasyMock.expect(resultSet.getStatement()).andReturn(statement);
        EasyMock.expect(resultSet.getMetaData()).andReturn(metadata);
        EasyMock.replay(resultSet);

        QueryDelegate delegate = EasyMock.createNiceMock(QueryDelegate.class);
        EasyMock.expect(delegate.getResults(0, 1)).andReturn(resultSet);
        EasyMock.replay(delegate);

        TestContainer container = new TestContainer(delegate);

        new RowItem(container, new RowId(new Object[] { 1 }),
                Arrays.asList(property));

        property.setValue("Kalle");
        assertEquals("Kalle", container.value);
        assertTrue(container.modified);
    }

    @Test
    public void versionColumnsShouldNotBeInValueMap_shouldReturnFalse() {
        ColumnProperty property = new ColumnProperty("NAME", false, true, true,
                false, "Ville", String.class);
        property.setVersionColumn(true);

        assertFalse(property.isPersistent());
    }

    @Test
    public void neverWritableColumnsShouldNotBeInValueMap_shouldReturnFalse() {
        ColumnProperty property = new ColumnProperty("NAME", true, false, true,
                false, "Ville", String.class);

        assertFalse(property.isPersistent());
    }

    @Test
    public void writableColumnsShouldBeInValueMap_shouldReturnTrue() {
        ColumnProperty property = new ColumnProperty("NAME", false, true, true,
                false, "Ville", String.class);

        assertTrue(property.isPersistent());
    }

    @Test
    public void writableButReadOnlyColumnsShouldNotBeInValueMap_shouldReturnFalse() {
        ColumnProperty property = new ColumnProperty("NAME", true, true, true,
                false, "Ville", String.class);

        assertFalse(property.isPersistent());
    }

    @Test
    public void primKeysShouldBeRowIdentifiers_shouldReturnTrue() {
        ColumnProperty property = new ColumnProperty("NAME", false, true, true,
                true, "Ville", String.class);

        assertTrue(property.isRowIdentifier());
    }

    @Test
    public void versionColumnsShouldBeRowIdentifiers_shouldReturnTrue() {
        ColumnProperty property = new ColumnProperty("NAME", false, true, true,
                false, "Ville", String.class);
        property.setVersionColumn(true);

        assertTrue(property.isRowIdentifier());
    }

    @Test
    public void nonPrimKeyOrVersionColumnsShouldBeNotRowIdentifiers_shouldReturnFalse() {
        ColumnProperty property = new ColumnProperty("NAME", false, true, true,
                false, "Ville", String.class);

        assertFalse(property.isRowIdentifier());
    }

    @Test
    public void getOldValueShouldReturnPreviousValue_shouldReturnVille() {
        ColumnProperty property = new ColumnProperty("NAME", false, true, true,
                false, "Ville", String.class);

        // Here we really don't care about the container management, but in
        // order to set the value for a column the owner (RowItem) must be set
        // and to create the owner we must have a container...
        List<ColumnProperty> properties = new ArrayList<ColumnProperty>();
        properties.add(property);

        SQLContainer container = EasyMock.createNiceMock(SQLContainer.class);
        RowItem rowItem = new RowItem(container, new RowId(new Object[] { 1 }),
                Arrays.asList(property));

        property.setValue("Kalle");
        // Just check that the new value was actually set...
        assertEquals("Kalle", property.getValue());
        // Assert that old value is the original value...
        assertEquals("Ville", property.getOldValue());
    }

}