blob: 7524bcb3321f01da041058c4f9aa6b8e75f38fc2 [file] [log] [blame]
Simon Hunte9828152015-05-01 17:54:25 -07001/*
2 * Copyright 2015 Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.onosproject.ui.table;
18
19import org.junit.Test;
Simon Hunt933b1a82015-05-04 19:07:24 -070020import org.onosproject.ui.table.TableModel.SortDir;
Simon Hunt3ee7f742015-05-05 10:18:20 -070021import org.onosproject.ui.table.cell.DefaultCellFormatter;
22import org.onosproject.ui.table.cell.HexFormatter;
Simon Hunte9828152015-05-01 17:54:25 -070023
Simon Hunt933b1a82015-05-04 19:07:24 -070024import static org.junit.Assert.*;
Simon Hunte9828152015-05-01 17:54:25 -070025
26/**
27 * Unit tests for {@link TableModel}.
28 */
29public class TableModelTest {
30
Simon Hunt3ee7f742015-05-05 10:18:20 -070031 private static final String UNEX_SORT = "unexpected sort: index ";
Simon Hunt933b1a82015-05-04 19:07:24 -070032
Simon Hunte9828152015-05-01 17:54:25 -070033 private static final String FOO = "foo";
34 private static final String BAR = "bar";
Simon Hunte9828152015-05-01 17:54:25 -070035 private static final String ZOO = "zoo";
36
Simon Hunt27bf0792015-05-07 10:50:29 -070037 private enum StarWars {
38 LUKE_SKYWALKER, LEIA_ORGANA, HAN_SOLO, C3PO, R2D2, JABBA_THE_HUTT
39 }
40
Simon Hunt3ee7f742015-05-05 10:18:20 -070041 private static class ParenFormatter implements CellFormatter {
Simon Hunte9828152015-05-01 17:54:25 -070042 @Override
43 public String format(Object value) {
44 return "(" + value + ")";
45 }
46 }
47
48 private TableModel tm;
Simon Hunt933b1a82015-05-04 19:07:24 -070049 private TableModel.Row[] rows;
50 private TableModel.Row row;
Simon Hunte9828152015-05-01 17:54:25 -070051 private CellFormatter fmt;
52
53 @Test(expected = NullPointerException.class)
54 public void guardAgainstNull() {
55 tm = new TableModel(null);
56 }
57
58 @Test(expected = IllegalArgumentException.class)
59 public void guardAgainstEmpty() {
60 tm = new TableModel();
61 }
62
63 @Test(expected = IllegalArgumentException.class)
64 public void guardAgainstDuplicateCols() {
65 tm = new TableModel(FOO, BAR, FOO);
66 }
67
68 @Test
69 public void basic() {
70 tm = new TableModel(FOO, BAR);
71 assertEquals("column count", 2, tm.columnCount());
72 assertEquals("row count", 0, tm.rowCount());
Simon Hunte9828152015-05-01 17:54:25 -070073 }
74
75 @Test
76 public void defaultFormatter() {
77 tm = new TableModel(FOO);
78 fmt = tm.getFormatter(FOO);
79 assertTrue("Wrong formatter", fmt instanceof DefaultCellFormatter);
80 }
81
82 @Test(expected = IllegalArgumentException.class)
83 public void formatterBadColumn() {
84 tm = new TableModel(FOO);
85 fmt = tm.getFormatter(BAR);
86 }
87
88 @Test
89 public void altFormatter() {
90 tm = new TableModel(FOO, BAR);
Simon Hunt3ee7f742015-05-05 10:18:20 -070091 tm.setFormatter(BAR, new ParenFormatter());
Simon Hunte9828152015-05-01 17:54:25 -070092
93 fmt = tm.getFormatter(FOO);
94 assertTrue("Wrong formatter", fmt instanceof DefaultCellFormatter);
95 assertEquals("Wrong result", "2", fmt.format(2));
96
97 fmt = tm.getFormatter(BAR);
Simon Hunt3ee7f742015-05-05 10:18:20 -070098 assertTrue("Wrong formatter", fmt instanceof ParenFormatter);
Simon Hunte9828152015-05-01 17:54:25 -070099 assertEquals("Wrong result", "(2)", fmt.format(2));
100 }
101
102 @Test
103 public void emptyRow() {
104 tm = new TableModel(FOO, BAR);
105 tm.addRow();
106 assertEquals("bad row count", 1, tm.rowCount());
107 }
108
109 @Test(expected = IllegalArgumentException.class)
110 public void rowBadColumn() {
111 tm = new TableModel(FOO, BAR);
112 tm.addRow().cell(ZOO, 2);
113 }
114
Simon Hunte9828152015-05-01 17:54:25 -0700115 @Test
116 public void simpleRow() {
117 tm = new TableModel(FOO, BAR);
118 tm.addRow().cell(FOO, 3).cell(BAR, true);
119 assertEquals("bad row count", 1, tm.rowCount());
Simon Hunt933b1a82015-05-04 19:07:24 -0700120 row = tm.getRows()[0];
121 assertEquals("bad cell", 3, row.get(FOO));
122 assertEquals("bad cell", true, row.get(BAR));
Simon Hunte9828152015-05-01 17:54:25 -0700123 }
Simon Hunt933b1a82015-05-04 19:07:24 -0700124
125
126 private static final String ONE = "one";
127 private static final String TWO = "two";
128 private static final String THREE = "three";
129 private static final String FOUR = "four";
130 private static final String ELEVEN = "eleven";
131 private static final String TWELVE = "twelve";
132 private static final String TWENTY = "twenty";
133 private static final String THIRTY = "thirty";
134
135 private static final String[] NAMES = {
136 FOUR,
137 THREE,
138 TWO,
139 ONE,
140 ELEVEN,
141 TWELVE,
142 THIRTY,
143 TWENTY,
144 };
145 private static final String[] SORTED_NAMES = {
146 ELEVEN,
147 FOUR,
148 ONE,
149 THIRTY,
150 THREE,
151 TWELVE,
152 TWENTY,
153 TWO,
154 };
155
156 private static final int[] NUMBERS = {
157 4, 3, 2, 1, 11, 12, 30, 20
158 };
159
160 private static final int[] SORTED_NUMBERS = {
161 1, 2, 3, 4, 11, 12, 20, 30
162 };
163
Simon Hunt3ee7f742015-05-05 10:18:20 -0700164 private static final String[] SORTED_HEX = {
165 "0x1", "0x2", "0x3", "0x4", "0xb", "0xc", "0x14", "0x1e"
166 };
167
Simon Hunt933b1a82015-05-04 19:07:24 -0700168 @Test
169 public void verifyTestData() {
170 // not a unit test per se, but will fail if we don't keep
171 // the three test arrays in sync
172 int nalen = NAMES.length;
173 int snlen = SORTED_NAMES.length;
174 int nulen = NUMBERS.length;
175
176 if (nalen != snlen || nalen != nulen) {
177 fail("test data array size discrepancy");
178 }
179 }
180
181 private void initUnsortedTable() {
182 tm = new TableModel(FOO, BAR);
183 for (int i = 0; i < NAMES.length; i++) {
184 tm.addRow().cell(FOO, NAMES[i]).cell(BAR, NUMBERS[i]);
185 }
186 }
187
188 @Test
189 public void tableStringSort() {
190 initUnsortedTable();
191
192 // sort by name
193 tm.sort(FOO, SortDir.ASC);
194
195 // verify results
196 rows = tm.getRows();
197 int nr = rows.length;
198 assertEquals("row count", NAMES.length, nr);
199 for (int i = 0; i < nr; i++) {
Simon Hunt3ee7f742015-05-05 10:18:20 -0700200 assertEquals(UNEX_SORT + i, SORTED_NAMES[i], rows[i].get(FOO));
Simon Hunt933b1a82015-05-04 19:07:24 -0700201 }
202
203 // now the other way
204 tm.sort(FOO, SortDir.DESC);
205
206 // verify results
207 rows = tm.getRows();
208 nr = rows.length;
209 assertEquals("row count", NAMES.length, nr);
210 for (int i = 0; i < nr; i++) {
Simon Hunt3ee7f742015-05-05 10:18:20 -0700211 assertEquals(UNEX_SORT + i,
Simon Hunt933b1a82015-05-04 19:07:24 -0700212 SORTED_NAMES[nr - 1 - i], rows[i].get(FOO));
213 }
214 }
215
216 @Test
217 public void tableNumberSort() {
218 initUnsortedTable();
219
Simon Hunt933b1a82015-05-04 19:07:24 -0700220 // sort by number
221 tm.sort(BAR, SortDir.ASC);
222
223 // verify results
224 rows = tm.getRows();
225 int nr = rows.length;
226 assertEquals("row count", NUMBERS.length, nr);
227 for (int i = 0; i < nr; i++) {
Simon Hunt3ee7f742015-05-05 10:18:20 -0700228 assertEquals(UNEX_SORT + i, SORTED_NUMBERS[i], rows[i].get(BAR));
Simon Hunt933b1a82015-05-04 19:07:24 -0700229 }
230
231 // now the other way
232 tm.sort(BAR, SortDir.DESC);
233
234 // verify results
235 rows = tm.getRows();
236 nr = rows.length;
237 assertEquals("row count", NUMBERS.length, nr);
238 for (int i = 0; i < nr; i++) {
Simon Hunt3ee7f742015-05-05 10:18:20 -0700239 assertEquals(UNEX_SORT + i,
Simon Hunt933b1a82015-05-04 19:07:24 -0700240 SORTED_NUMBERS[nr - 1 - i], rows[i].get(BAR));
241 }
242 }
243
244 @Test
Simon Hunt3ee7f742015-05-05 10:18:20 -0700245 public void sortAndFormat() {
246 initUnsortedTable();
247
Simon Hunt27bf0792015-05-07 10:50:29 -0700248 // set hex formatter
Simon Hunt3d1b0652015-05-05 17:27:24 -0700249 tm.setFormatter(BAR, HexFormatter.INSTANCE);
Simon Hunt3ee7f742015-05-05 10:18:20 -0700250
251 // sort by number
252 tm.sort(BAR, SortDir.ASC);
253
254 // verify results
255 rows = tm.getRows();
256 int nr = rows.length;
257 assertEquals("row count", SORTED_HEX.length, nr);
258 for (int i = 0; i < nr; i++) {
259 assertEquals(UNEX_SORT + i, SORTED_HEX[i], rows[i].getAsString(BAR));
260 }
261 }
262
Simon Hunt9ffbd8d2015-05-06 12:02:32 -0700263 private static final String[][] SORTED_NAMES_AND_HEX = {
264 {ELEVEN, "0xb"},
265 {FOUR, "0x4"},
266 {ONE, "0x1"},
267 {THIRTY, "0x1e"},
268 {THREE, "0x3"},
269 {TWELVE, "0xc"},
270 {TWENTY, "0x14"},
271 {TWO, "0x2"},
272 };
273
274 @Test
275 public void sortAndFormatTwo() {
276 initUnsortedTable();
277 tm.setFormatter(BAR, HexFormatter.INSTANCE);
278 tm.sort(FOO, SortDir.ASC);
279 rows = tm.getRows();
280 int nr = rows.length;
281 for (int i = 0; i < nr; i++) {
282 String[] exp = SORTED_NAMES_AND_HEX[i];
283 String[] act = rows[i].getAsFormattedStrings();
284 assertArrayEquals(UNEX_SORT + i, exp, act);
285 }
286 }
287
288 private static final String[] FBZ = {FOO, BAR, ZOO};
289
290 @Test
291 public void getColumnIds() {
292 tm = new TableModel(FOO, BAR, ZOO);
293 assertArrayEquals("col IDs", FBZ, tm.getColumnIds());
294 }
Simon Hunt3ee7f742015-05-05 10:18:20 -0700295
296 @Test
Simon Hunt933b1a82015-05-04 19:07:24 -0700297 public void sortDirAsc() {
298 assertEquals("asc sort dir", SortDir.ASC, TableModel.sortDir("asc"));
299 }
300
301 @Test
302 public void sortDirDesc() {
303 assertEquals("desc sort dir", SortDir.DESC, TableModel.sortDir("desc"));
304 }
305
306 @Test
307 public void sortDirOther() {
308 assertEquals("other sort dir", SortDir.ASC, TableModel.sortDir("other"));
309 }
310
311 @Test
312 public void sortDirNull() {
313 assertEquals("null sort dir", SortDir.ASC, TableModel.sortDir(null));
314 }
315
Simon Hunt27bf0792015-05-07 10:50:29 -0700316
317 @Test
318 public void enumSort() {
319 tm = new TableModel(FOO);
320 tm.addRow().cell(FOO, StarWars.HAN_SOLO);
321 tm.addRow().cell(FOO, StarWars.C3PO);
322 tm.addRow().cell(FOO, StarWars.JABBA_THE_HUTT);
323 tm.addRow().cell(FOO, StarWars.LEIA_ORGANA);
324 tm.addRow().cell(FOO, StarWars.R2D2);
325 tm.addRow().cell(FOO, StarWars.LUKE_SKYWALKER);
326
327 tm.sort(FOO, SortDir.ASC);
328
329 // verify expected results
330 StarWars[] ordered = StarWars.values();
331 TableModel.Row[] rows = tm.getRows();
332 assertEquals("wrong length?", ordered.length, rows.length);
333 int nr = rows.length;
334 for (int i = 0; i < nr; i++) {
335 assertEquals(UNEX_SORT + i, ordered[i], rows[i].get(FOO));
336 }
337 }
Simon Hunte9828152015-05-01 17:54:25 -0700338}