Browse Source

Start unit test for GenericTableModel.

pull/357/head
Greg Holmes 9 years ago
parent
commit
d4f2c96131

+ 11
- 9
ui_swing/src/com/dmdirc/addons/ui_swing/components/GenericTableModel.java View File

@@ -79,13 +79,21 @@ public class GenericTableModel<T> extends AbstractTableModel {
79 79
      * @param getterValues Names of the getters to display in the table
80 80
      */
81 81
     public GenericTableModel(final Class<T> type, final String... getterValues) {
82
-        this(type, (i1, i2) -> false, (i1, i2, i3) -> {}, getterValues);
82
+        this(type, (row, column) -> false, (value, row, column) -> {}, getterValues);
83 83
     }
84 84
 
85 85
     /**
86 86
      * Creates a new generic table model.
87 87
      *
88 88
      * @param type         Type to be displayed, used to verify getters
89
+     * @param isEditable   Function to decide if a cell is editable or not
90
+     *                          First parameter is the row
91
+     *                          Second parameter is the column
92
+     *                          Third parameter is the return type
93
+     * @param editFunction The function to be called when editing a particular cell.
94
+     *                          First parameter is the object
95
+     *                          Second parameter is the row
96
+     *                          Third parameter is the column
89 97
      * @param getterValues Names of the getters to display in the table
90 98
      */
91 99
     public GenericTableModel(final Class<T> type,
@@ -187,7 +195,7 @@ public class GenericTableModel<T> extends AbstractTableModel {
187 195
     public void setHeaderNames(final String... headerValues) {
188 196
         checkArgument(headerValues.length == getters.length,
189 197
                 "There must be as many headers as columns");
190
-        System.arraycopy(headerValues, 0, this.headers, 0, headerValues.length);
198
+        System.arraycopy(headerValues, 0, headers, 0, headerValues.length);
191 199
         fireTableStructureChanged();
192 200
     }
193 201
 
@@ -219,8 +227,7 @@ public class GenericTableModel<T> extends AbstractTableModel {
219 227
     public void replaceValueAt(final T value, final int rowIndex) {
220 228
         checkNotNull(value, "Value must not be null");
221 229
         checkElementIndex(rowIndex, values.size(), "RowIndex must be valid");
222
-        values.remove(rowIndex);
223
-        values.add(rowIndex, value);
230
+        values.set(rowIndex, value);
224 231
         fireTableRowsUpdated(rowIndex, rowIndex);
225 232
     }
226 233
 
@@ -264,11 +271,6 @@ public class GenericTableModel<T> extends AbstractTableModel {
264 271
         return Collections.unmodifiableCollection(values);
265 272
     }
266 273
 
267
-    @SuppressWarnings("unchecked")
268
-    private T castObjectToType(final Object value) {
269
-        return (T) value;
270
-    }
271
-
272 274
     /**
273 275
      * Returns an optional method for the specified name.
274 276
      *

+ 292
- 0
ui_swing/test/com/dmdirc/addons/ui_swing/components/GenericTableModelTest.java View File

@@ -0,0 +1,292 @@
1
+/*
2
+ * Copyright (c) 2006-2015 DMDirc Developers
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the "Software"), to deal
6
+ * in the Software without restriction, including without limitation the rights
7
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ * copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in
12
+ * all copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ * SOFTWARE.
21
+ */
22
+
23
+package com.dmdirc.addons.ui_swing.components;
24
+
25
+import com.google.common.base.MoreObjects;
26
+
27
+import java.util.ArrayList;
28
+import java.util.List;
29
+import java.util.Objects;
30
+
31
+import javax.swing.event.TableModelEvent;
32
+import javax.swing.event.TableModelListener;
33
+
34
+import org.junit.Before;
35
+import org.junit.Test;
36
+import org.junit.runner.RunWith;
37
+import org.mockito.ArgumentCaptor;
38
+import org.mockito.Captor;
39
+import org.mockito.Mock;
40
+import org.mockito.runners.MockitoJUnitRunner;
41
+
42
+import static junit.framework.Assert.assertEquals;
43
+import static junit.framework.Assert.assertSame;
44
+import static org.junit.Assert.assertFalse;
45
+import static org.junit.Assert.assertTrue;
46
+import static org.mockito.Mockito.verify;
47
+
48
+@RunWith(MockitoJUnitRunner.class)
49
+public class GenericTableModelTest {
50
+
51
+    @Mock private TableModelListener listener;
52
+    @Captor private ArgumentCaptor<TableModelEvent> tableModelEvent;
53
+    private List<TestObject> values;
54
+    private TestObject newObject;
55
+    private GenericTableModel<TestObject> instance;
56
+    private String[] headers;
57
+
58
+    @Before
59
+    public void setUp() throws Exception {
60
+        values = new ArrayList<>();
61
+        values.add(new TestObject("one", new NotAString("value-one")));
62
+        values.add(new TestObject("two", new NotAString("value-two")));
63
+        values.add(new TestObject("three", new NotAString("value-three")));
64
+        values.add(new TestObject("four", new NotAString("value-four")));
65
+        values.add(new TestObject("five", new NotAString("value-five")));
66
+
67
+        newObject = new TestObject("RAR", new NotAString("RAR"));
68
+
69
+        headers = new String[]{"getName", "getValue"};
70
+
71
+        instance = new GenericTableModel<>(TestObject.class, headers);
72
+        values.forEach(instance::addValue);
73
+        instance.addTableModelListener(listener);
74
+    }
75
+
76
+    @Test
77
+    public void testGetRowCount() throws Exception {
78
+        assertEquals(values.size(), instance.getRowCount());
79
+    }
80
+
81
+    /// TODO: Row out of bounds
82
+
83
+    @Test
84
+    public void testGetColumnCount() throws Exception {
85
+        assertEquals(headers.length, instance.getColumnCount());
86
+    }
87
+
88
+    // TODO: Column out of bounds
89
+
90
+    @Test
91
+    public void testGetColumnName() throws Exception {
92
+        for (int i = 0; i < headers.length; i++) {
93
+            assertEquals(headers[i], instance.getColumnName(i));
94
+        }
95
+    }
96
+
97
+    // TODO: Get out of bounds column names
98
+
99
+    @Test
100
+    public void testGetColumnClass() throws Exception {
101
+        assertSame(String.class, instance.getColumnClass(0));
102
+        assertSame(NotAString.class, instance.getColumnClass(1));
103
+    }
104
+
105
+    // TODO: Get column out of bounds
106
+
107
+    @Test
108
+    public void testGetValueAt() throws Exception {
109
+        for (int i = 0; i < values.size(); i++) {
110
+            assertEquals(values.get(i).getName(), instance.getValueAt(i, 0));
111
+            assertEquals(values.get(i).getValue(), instance.getValueAt(i, 1));
112
+        }
113
+    }
114
+
115
+    // TODO: Get row and column out of bounds
116
+
117
+    @Test
118
+    public void testIsCellEditable() throws Exception {
119
+        assertFalse(instance.isCellEditable(0, 0));
120
+        assertFalse(instance.isCellEditable(0, 1));
121
+        assertFalse(instance.isCellEditable(1, 0));
122
+        assertFalse(instance.isCellEditable(1, 1));
123
+    }
124
+
125
+    // TODO: Test non default isEditable
126
+
127
+    @Test
128
+    public void testGetValue() throws Exception {
129
+        for (int i = 0; i < values.size(); i++) {
130
+            assertEquals(values.get(i), instance.getValue(i));
131
+        }
132
+    }
133
+
134
+    // TODO: Test out of bounds
135
+
136
+    @Test
137
+    public void testGetIndex() throws Exception {
138
+        for (int i = 0; i < values.size(); i++) {
139
+            assertEquals(i, instance.getIndex(values.get(i)));
140
+        }
141
+        assertEquals(-1, instance.getIndex(new TestObject("", new NotAString(""))));
142
+        assertEquals(-1, instance.getIndex(null));
143
+    }
144
+
145
+    @Test
146
+    public void testSetHeaderNames() throws Exception {
147
+        for (int i = 0; i < headers.length; i++) {
148
+            assertEquals(headers[i], instance.getColumnName(i));
149
+        }
150
+        final String[] setHeaders = {"name", "value"};
151
+        instance.setHeaderNames(setHeaders);
152
+        for (int i = 0; i < setHeaders.length; i++) {
153
+            assertEquals(setHeaders[i], instance.getColumnName(i));
154
+        }
155
+        // TODO: Check listener fired appropriately
156
+    }
157
+
158
+    // TODO: Set longer and shorter than
159
+
160
+    @Test
161
+    public void testSetValueAt() throws Exception {
162
+        assertEquals(values.get(0).getName(), instance.getValueAt(0, 0));
163
+        instance.setValueAt(newObject, 0, 0);
164
+        assertEquals(values.get(0).getName(), instance.getValueAt(0, 0));
165
+        verify(listener).tableChanged(tableModelEvent.capture());
166
+        assertEquals(0, tableModelEvent.getValue().getType());
167
+        assertEquals(0, tableModelEvent.getValue().getFirstRow());
168
+        assertEquals(0, tableModelEvent.getValue().getLastRow());
169
+        assertEquals(-1, tableModelEvent.getValue().getColumn());
170
+    }
171
+
172
+    // TODO: Test with non default edit function
173
+    // TODO: Test with incorrect value type
174
+
175
+    @Test
176
+    public void testReplaceValueAt() throws Exception {
177
+        assertEquals(values.get(0), instance.getValue(0));
178
+        instance.replaceValueAt(newObject, 0);
179
+        assertEquals(newObject, instance.getValue(0));
180
+        // TODO: Check listener fired appropriately
181
+    }
182
+
183
+    @Test
184
+    public void testRemoveValue() throws Exception {
185
+        assertEquals(values.size(), instance.getRowCount());
186
+        assertTrue(instance.getIndex(values.get(0)) != -1);
187
+        instance.removeValue(values.get(0));
188
+        assertEquals(values.size() -1, instance.getRowCount());
189
+        assertEquals(-1, instance.getIndex(values.get(0)));
190
+        // TODO: Check listener fired appropriately
191
+    }
192
+
193
+    // TODO: Test invalid value
194
+
195
+    @Test
196
+    public void testRemoveAll() throws Exception {
197
+        assertEquals(values.size(), instance.getRowCount());
198
+        instance.removeAll();
199
+        // TODO: Check listener fired appropriately
200
+    }
201
+
202
+    @Test
203
+    public void testAddValue() throws Exception {
204
+        assertEquals(values.size(), instance.getRowCount());
205
+        assertEquals(-1, instance.getIndex(newObject));
206
+        instance.addValue(newObject);
207
+        assertEquals(values.size() + 1, instance.getRowCount());
208
+        assertEquals(values.size(), instance.getIndex(newObject));
209
+        // TODO: Check listener fired appropriately
210
+    }
211
+
212
+    @Test
213
+    public void testElements() throws Exception {
214
+        assertEquals(values, new ArrayList<>(instance.elements()));
215
+    }
216
+
217
+    private static class TestObject {
218
+        private String name;
219
+        private NotAString value;
220
+
221
+        public TestObject(final String name, final NotAString value) {
222
+            this.name = name;
223
+            this.value = value;
224
+        }
225
+
226
+        public String getName() {
227
+            return name;
228
+        }
229
+
230
+        public void setName(final String name) {
231
+            this.name = name;
232
+        }
233
+
234
+        public NotAString getValue() {
235
+            return value;
236
+        }
237
+
238
+        public void setValue(final NotAString value) {
239
+            this.value = value;
240
+        }
241
+
242
+        @Override
243
+        public String toString() {
244
+            return MoreObjects.toStringHelper(this)
245
+                    .add("Name", getName())
246
+                    .add("Value", getValue())
247
+                    .toString();
248
+        }
249
+
250
+        @Override
251
+        public boolean equals(final Object object) {
252
+            return object instanceof TestObject &&
253
+                    Objects.equals(getName(), ((TestObject) object).getName())
254
+                    && Objects.equals(getValue(), ((TestObject) object).getValue());
255
+        }
256
+
257
+        @Override
258
+        public int hashCode() {
259
+            return Objects.hash(getName(), getValue());
260
+        }
261
+    }
262
+
263
+    private static class NotAString {
264
+        private final String value;
265
+
266
+        public NotAString(final String value) {
267
+            this.value = value;
268
+        }
269
+
270
+        public String get() {
271
+            return value;
272
+        }
273
+
274
+        @Override
275
+        public String toString() {
276
+            return MoreObjects.toStringHelper(this)
277
+                    .add("Value", get())
278
+                    .toString();
279
+        }
280
+
281
+        @Override
282
+        public boolean equals(final Object object) {
283
+            return object instanceof NotAString
284
+                    && Objects.equals(get(), ((NotAString) object).get());
285
+        }
286
+
287
+        @Override
288
+        public int hashCode() {
289
+            return Objects.hash(get());
290
+        }
291
+    }
292
+}

Loading…
Cancel
Save