blob: 8c79a058928b3f9922deb0a4d5996f0238edd023 [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
Jian Li69f66632016-01-15 12:27:42 -080024import java.util.Collection;
25
Simon Hunt933b1a82015-05-04 19:07:24 -070026import static org.junit.Assert.*;
Simon Hunte9828152015-05-01 17:54:25 -070027
28/**
29 * Unit tests for {@link TableModel}.
30 */
31public class TableModelTest {
32
Simon Hunt3ee7f742015-05-05 10:18:20 -070033 private static final String UNEX_SORT = "unexpected sort: index ";
Simon Hunt933b1a82015-05-04 19:07:24 -070034
Simon Hunte9828152015-05-01 17:54:25 -070035 private static final String FOO = "foo";
36 private static final String BAR = "bar";
Simon Hunte9828152015-05-01 17:54:25 -070037 private static final String ZOO = "zoo";
38
Simon Hunt27bf0792015-05-07 10:50:29 -070039 private enum StarWars {
40 LUKE_SKYWALKER, LEIA_ORGANA, HAN_SOLO, C3PO, R2D2, JABBA_THE_HUTT
41 }
42
Simon Hunt3ee7f742015-05-05 10:18:20 -070043 private static class ParenFormatter implements CellFormatter {
Simon Hunte9828152015-05-01 17:54:25 -070044 @Override
45 public String format(Object value) {
46 return "(" + value + ")";
47 }
48 }
49
50 private TableModel tm;
Simon Hunt933b1a82015-05-04 19:07:24 -070051 private TableModel.Row[] rows;
52 private TableModel.Row row;
Simon Hunte9828152015-05-01 17:54:25 -070053 private CellFormatter fmt;
54
55 @Test(expected = NullPointerException.class)
56 public void guardAgainstNull() {
57 tm = new TableModel(null);
58 }
59
60 @Test(expected = IllegalArgumentException.class)
61 public void guardAgainstEmpty() {
62 tm = new TableModel();
63 }
64
65 @Test(expected = IllegalArgumentException.class)
66 public void guardAgainstDuplicateCols() {
67 tm = new TableModel(FOO, BAR, FOO);
68 }
69
70 @Test
71 public void basic() {
72 tm = new TableModel(FOO, BAR);
73 assertEquals("column count", 2, tm.columnCount());
74 assertEquals("row count", 0, tm.rowCount());
Simon Hunte9828152015-05-01 17:54:25 -070075 }
76
77 @Test
78 public void defaultFormatter() {
79 tm = new TableModel(FOO);
80 fmt = tm.getFormatter(FOO);
81 assertTrue("Wrong formatter", fmt instanceof DefaultCellFormatter);
82 }
83
84 @Test(expected = IllegalArgumentException.class)
85 public void formatterBadColumn() {
86 tm = new TableModel(FOO);
87 fmt = tm.getFormatter(BAR);
88 }
89
90 @Test
91 public void altFormatter() {
92 tm = new TableModel(FOO, BAR);
Simon Hunt3ee7f742015-05-05 10:18:20 -070093 tm.setFormatter(BAR, new ParenFormatter());
Simon Hunte9828152015-05-01 17:54:25 -070094
95 fmt = tm.getFormatter(FOO);
96 assertTrue("Wrong formatter", fmt instanceof DefaultCellFormatter);
97 assertEquals("Wrong result", "2", fmt.format(2));
98
99 fmt = tm.getFormatter(BAR);
Simon Hunt3ee7f742015-05-05 10:18:20 -0700100 assertTrue("Wrong formatter", fmt instanceof ParenFormatter);
Simon Hunte9828152015-05-01 17:54:25 -0700101 assertEquals("Wrong result", "(2)", fmt.format(2));
102 }
103
104 @Test
105 public void emptyRow() {
106 tm = new TableModel(FOO, BAR);
107 tm.addRow();
108 assertEquals("bad row count", 1, tm.rowCount());
109 }
110
111 @Test(expected = IllegalArgumentException.class)
112 public void rowBadColumn() {
113 tm = new TableModel(FOO, BAR);
114 tm.addRow().cell(ZOO, 2);
115 }
116
Simon Hunte9828152015-05-01 17:54:25 -0700117 @Test
118 public void simpleRow() {
119 tm = new TableModel(FOO, BAR);
120 tm.addRow().cell(FOO, 3).cell(BAR, true);
121 assertEquals("bad row count", 1, tm.rowCount());
Simon Hunt933b1a82015-05-04 19:07:24 -0700122 row = tm.getRows()[0];
123 assertEquals("bad cell", 3, row.get(FOO));
124 assertEquals("bad cell", true, row.get(BAR));
Simon Hunte9828152015-05-01 17:54:25 -0700125 }
Simon Hunt933b1a82015-05-04 19:07:24 -0700126
Simon Hunt933b1a82015-05-04 19:07:24 -0700127 private static final String ONE = "one";
128 private static final String TWO = "two";
129 private static final String THREE = "three";
130 private static final String FOUR = "four";
131 private static final String ELEVEN = "eleven";
132 private static final String TWELVE = "twelve";
133 private static final String TWENTY = "twenty";
134 private static final String THIRTY = "thirty";
135
136 private static final String[] NAMES = {
137 FOUR,
138 THREE,
139 TWO,
140 ONE,
141 ELEVEN,
142 TWELVE,
143 THIRTY,
144 TWENTY,
145 };
146 private static final String[] SORTED_NAMES = {
147 ELEVEN,
148 FOUR,
149 ONE,
150 THIRTY,
151 THREE,
152 TWELVE,
153 TWENTY,
154 TWO,
155 };
156
157 private static final int[] NUMBERS = {
158 4, 3, 2, 1, 11, 12, 30, 20
159 };
160
161 private static final int[] SORTED_NUMBERS = {
162 1, 2, 3, 4, 11, 12, 20, 30
163 };
164
Simon Hunt3ee7f742015-05-05 10:18:20 -0700165 private static final String[] SORTED_HEX = {
166 "0x1", "0x2", "0x3", "0x4", "0xb", "0xc", "0x14", "0x1e"
167 };
168
Simon Hunt933b1a82015-05-04 19:07:24 -0700169 @Test
170 public void verifyTestData() {
171 // not a unit test per se, but will fail if we don't keep
172 // the three test arrays in sync
173 int nalen = NAMES.length;
174 int snlen = SORTED_NAMES.length;
175 int nulen = NUMBERS.length;
176
177 if (nalen != snlen || nalen != nulen) {
178 fail("test data array size discrepancy");
179 }
180 }
181
182 private void initUnsortedTable() {
183 tm = new TableModel(FOO, BAR);
184 for (int i = 0; i < NAMES.length; i++) {
185 tm.addRow().cell(FOO, NAMES[i]).cell(BAR, NUMBERS[i]);
186 }
187 }
188
189 @Test
190 public void tableStringSort() {
191 initUnsortedTable();
192
193 // sort by name
194 tm.sort(FOO, SortDir.ASC);
195
196 // verify results
197 rows = tm.getRows();
198 int nr = rows.length;
199 assertEquals("row count", NAMES.length, nr);
200 for (int i = 0; i < nr; i++) {
Simon Hunt3ee7f742015-05-05 10:18:20 -0700201 assertEquals(UNEX_SORT + i, SORTED_NAMES[i], rows[i].get(FOO));
Simon Hunt933b1a82015-05-04 19:07:24 -0700202 }
203
204 // now the other way
205 tm.sort(FOO, SortDir.DESC);
206
207 // verify results
208 rows = tm.getRows();
209 nr = rows.length;
210 assertEquals("row count", NAMES.length, nr);
211 for (int i = 0; i < nr; i++) {
Simon Hunt3ee7f742015-05-05 10:18:20 -0700212 assertEquals(UNEX_SORT + i,
Simon Hunt933b1a82015-05-04 19:07:24 -0700213 SORTED_NAMES[nr - 1 - i], rows[i].get(FOO));
214 }
215 }
216
217 @Test
218 public void tableNumberSort() {
219 initUnsortedTable();
220
Simon Hunt933b1a82015-05-04 19:07:24 -0700221 // sort by number
222 tm.sort(BAR, SortDir.ASC);
223
224 // verify results
225 rows = tm.getRows();
226 int nr = rows.length;
227 assertEquals("row count", NUMBERS.length, nr);
228 for (int i = 0; i < nr; i++) {
Simon Hunt3ee7f742015-05-05 10:18:20 -0700229 assertEquals(UNEX_SORT + i, SORTED_NUMBERS[i], rows[i].get(BAR));
Simon Hunt933b1a82015-05-04 19:07:24 -0700230 }
231
232 // now the other way
233 tm.sort(BAR, SortDir.DESC);
234
235 // verify results
236 rows = tm.getRows();
237 nr = rows.length;
238 assertEquals("row count", NUMBERS.length, nr);
239 for (int i = 0; i < nr; i++) {
Simon Hunt3ee7f742015-05-05 10:18:20 -0700240 assertEquals(UNEX_SORT + i,
Simon Hunt933b1a82015-05-04 19:07:24 -0700241 SORTED_NUMBERS[nr - 1 - i], rows[i].get(BAR));
242 }
243 }
244
245 @Test
Simon Hunt3ee7f742015-05-05 10:18:20 -0700246 public void sortAndFormat() {
247 initUnsortedTable();
248
Simon Hunt27bf0792015-05-07 10:50:29 -0700249 // set hex formatter
Simon Hunt3d1b0652015-05-05 17:27:24 -0700250 tm.setFormatter(BAR, HexFormatter.INSTANCE);
Simon Hunt3ee7f742015-05-05 10:18:20 -0700251
252 // sort by number
253 tm.sort(BAR, SortDir.ASC);
254
255 // verify results
256 rows = tm.getRows();
257 int nr = rows.length;
258 assertEquals("row count", SORTED_HEX.length, nr);
259 for (int i = 0; i < nr; i++) {
260 assertEquals(UNEX_SORT + i, SORTED_HEX[i], rows[i].getAsString(BAR));
261 }
262 }
263
Simon Hunt9ffbd8d2015-05-06 12:02:32 -0700264 private static final String[][] SORTED_NAMES_AND_HEX = {
265 {ELEVEN, "0xb"},
266 {FOUR, "0x4"},
267 {ONE, "0x1"},
268 {THIRTY, "0x1e"},
269 {THREE, "0x3"},
270 {TWELVE, "0xc"},
271 {TWENTY, "0x14"},
272 {TWO, "0x2"},
273 };
274
275 @Test
276 public void sortAndFormatTwo() {
277 initUnsortedTable();
278 tm.setFormatter(BAR, HexFormatter.INSTANCE);
279 tm.sort(FOO, SortDir.ASC);
280 rows = tm.getRows();
281 int nr = rows.length;
282 for (int i = 0; i < nr; i++) {
283 String[] exp = SORTED_NAMES_AND_HEX[i];
284 String[] act = rows[i].getAsFormattedStrings();
285 assertArrayEquals(UNEX_SORT + i, exp, act);
286 }
287 }
288
289 private static final String[] FBZ = {FOO, BAR, ZOO};
290
291 @Test
292 public void getColumnIds() {
293 tm = new TableModel(FOO, BAR, ZOO);
294 assertArrayEquals("col IDs", FBZ, tm.getColumnIds());
295 }
Simon Hunt3ee7f742015-05-05 10:18:20 -0700296
297 @Test
Simon Hunt933b1a82015-05-04 19:07:24 -0700298 public void sortDirAsc() {
299 assertEquals("asc sort dir", SortDir.ASC, TableModel.sortDir("asc"));
300 }
301
302 @Test
303 public void sortDirDesc() {
304 assertEquals("desc sort dir", SortDir.DESC, TableModel.sortDir("desc"));
305 }
306
307 @Test
308 public void sortDirOther() {
309 assertEquals("other sort dir", SortDir.ASC, TableModel.sortDir("other"));
310 }
311
312 @Test
313 public void sortDirNull() {
314 assertEquals("null sort dir", SortDir.ASC, TableModel.sortDir(null));
315 }
316
Simon Hunt27bf0792015-05-07 10:50:29 -0700317 @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 }
Jian Li69f66632016-01-15 12:27:42 -0800338
339 @Test
340 public void stringAnnotation() {
341 tm = new TableModel(FOO);
342 tm.addAnnotation(BAR, ZOO);
343 Collection<TableModel.Annot> annots = tm.getAnnotations();
344 assertEquals("wrong size", 1, annots.size());
345
346 TableModel.Annot annot = annots.iterator().next();
347 assertEquals("wrong key", BAR, annot.key());
348 assertEquals("wrong value", ZOO, annot.value());
349 }
350
351 private static final String K_INT = "int";
352 private static final String K_BOOL = "bool";
353 private static final String K_FLOAT = "float";
354 private static final String K_DOUBLE = "double";
355 private static final String K_ENUM = "enum";
356
357 private TableModel.Annot getAnnotation(Collection<TableModel.Annot> annots, String key) {
358 final TableModel.Annot[] annot = {null};
359 annots.forEach(a -> {
360 if (a.key().equals(key)) {
361 annot[0] = a;
362 }
363 });
364 return annot[0];
365 }
366
367 private void verifyCollectionContains(Collection<TableModel.Annot> annots,
368 String key, int i) {
369 TableModel.Annot a = getAnnotation(annots, key);
370 assertEquals("wrong int value", i, a.value());
371 }
372
373 private void verifyCollectionContains(Collection<TableModel.Annot> annots,
374 String key, boolean b) {
375 TableModel.Annot a = getAnnotation(annots, key);
376 assertEquals("wrong boolean value", b, a.value());
377 }
378
379 private void verifyCollectionContains(Collection<TableModel.Annot> annots,
380 String key, float f) {
381 TableModel.Annot a = getAnnotation(annots, key);
382 assertEquals("wrong float value", f, a.value());
383 }
384
385 private void verifyCollectionContains(Collection<TableModel.Annot> annots,
386 String key, double d) {
387 TableModel.Annot a = getAnnotation(annots, key);
388 assertEquals("wrong double value", d, a.value());
389 }
390
391 private void verifyCollectionContains(Collection<TableModel.Annot> annots,
392 String key, Enum<?> e) {
393 TableModel.Annot a = getAnnotation(annots, key);
394 assertEquals("wrong double value", e, a.value());
395 }
396
397 @Test
398 public void primitivesAnnotation() {
399 tm = new TableModel(FOO);
400 tm.addAnnotation(K_INT, 1);
401 tm.addAnnotation(K_BOOL, true);
402 tm.addAnnotation(K_FLOAT, 3.14f);
403 tm.addAnnotation(K_DOUBLE, 2.71828);
404 tm.addAnnotation(K_ENUM, StarWars.LUKE_SKYWALKER);
405
406 Collection<TableModel.Annot> annots = tm.getAnnotations();
407 assertEquals("wrong size", 5, annots.size());
408
409 verifyCollectionContains(annots, K_INT, 1);
410 verifyCollectionContains(annots, K_BOOL, true);
411 verifyCollectionContains(annots, K_FLOAT, 3.14f);
412 verifyCollectionContains(annots, K_DOUBLE, 2.71828);
413 verifyCollectionContains(annots, K_ENUM, StarWars.LUKE_SKYWALKER);
414 }
415
416 // TODO: add support for compound object value
Simon Hunte9828152015-05-01 17:54:25 -0700417}