Simon Hunt | e982815 | 2015-05-01 17:54:25 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Simon Hunt | e982815 | 2015-05-01 17:54:25 -0700 | [diff] [blame] | 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 | |
| 17 | package org.onosproject.ui.table; |
| 18 | |
| 19 | import org.junit.Test; |
Simon Hunt | 933b1a8 | 2015-05-04 19:07:24 -0700 | [diff] [blame] | 20 | import org.onosproject.ui.table.TableModel.SortDir; |
Simon Hunt | 3ee7f74 | 2015-05-05 10:18:20 -0700 | [diff] [blame] | 21 | import org.onosproject.ui.table.cell.DefaultCellFormatter; |
| 22 | import org.onosproject.ui.table.cell.HexFormatter; |
Simon Hunt | e982815 | 2015-05-01 17:54:25 -0700 | [diff] [blame] | 23 | |
Jian Li | 69f6663 | 2016-01-15 12:27:42 -0800 | [diff] [blame] | 24 | import java.util.Collection; |
| 25 | |
Simon Hunt | 933b1a8 | 2015-05-04 19:07:24 -0700 | [diff] [blame] | 26 | import static org.junit.Assert.*; |
Simon Hunt | e982815 | 2015-05-01 17:54:25 -0700 | [diff] [blame] | 27 | |
| 28 | /** |
| 29 | * Unit tests for {@link TableModel}. |
| 30 | */ |
| 31 | public class TableModelTest { |
| 32 | |
Simon Hunt | 3ee7f74 | 2015-05-05 10:18:20 -0700 | [diff] [blame] | 33 | private static final String UNEX_SORT = "unexpected sort: index "; |
Simon Hunt | 933b1a8 | 2015-05-04 19:07:24 -0700 | [diff] [blame] | 34 | |
Simon Hunt | e982815 | 2015-05-01 17:54:25 -0700 | [diff] [blame] | 35 | private static final String FOO = "foo"; |
| 36 | private static final String BAR = "bar"; |
Simon Hunt | e982815 | 2015-05-01 17:54:25 -0700 | [diff] [blame] | 37 | private static final String ZOO = "zoo"; |
Simon Hunt | 051e9fa | 2016-01-19 15:54:22 -0800 | [diff] [blame] | 38 | private static final String ID = "id"; |
| 39 | private static final String ALPHA = "alpha"; |
| 40 | private static final String NUMBER = "number"; |
Simon Hunt | e982815 | 2015-05-01 17:54:25 -0700 | [diff] [blame] | 41 | |
Simon Hunt | 27bf079 | 2015-05-07 10:50:29 -0700 | [diff] [blame] | 42 | private enum StarWars { |
| 43 | LUKE_SKYWALKER, LEIA_ORGANA, HAN_SOLO, C3PO, R2D2, JABBA_THE_HUTT |
| 44 | } |
| 45 | |
Simon Hunt | 3ee7f74 | 2015-05-05 10:18:20 -0700 | [diff] [blame] | 46 | private static class ParenFormatter implements CellFormatter { |
Simon Hunt | e982815 | 2015-05-01 17:54:25 -0700 | [diff] [blame] | 47 | @Override |
| 48 | public String format(Object value) { |
| 49 | return "(" + value + ")"; |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | private TableModel tm; |
Simon Hunt | 933b1a8 | 2015-05-04 19:07:24 -0700 | [diff] [blame] | 54 | private TableModel.Row[] rows; |
| 55 | private TableModel.Row row; |
Simon Hunt | e982815 | 2015-05-01 17:54:25 -0700 | [diff] [blame] | 56 | private CellFormatter fmt; |
| 57 | |
| 58 | @Test(expected = NullPointerException.class) |
| 59 | public void guardAgainstNull() { |
| 60 | tm = new TableModel(null); |
| 61 | } |
| 62 | |
| 63 | @Test(expected = IllegalArgumentException.class) |
| 64 | public void guardAgainstEmpty() { |
| 65 | tm = new TableModel(); |
| 66 | } |
| 67 | |
| 68 | @Test(expected = IllegalArgumentException.class) |
| 69 | public void guardAgainstDuplicateCols() { |
| 70 | tm = new TableModel(FOO, BAR, FOO); |
| 71 | } |
| 72 | |
| 73 | @Test |
| 74 | public void basic() { |
| 75 | tm = new TableModel(FOO, BAR); |
| 76 | assertEquals("column count", 2, tm.columnCount()); |
| 77 | assertEquals("row count", 0, tm.rowCount()); |
Simon Hunt | e982815 | 2015-05-01 17:54:25 -0700 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | @Test |
| 81 | public void defaultFormatter() { |
| 82 | tm = new TableModel(FOO); |
| 83 | fmt = tm.getFormatter(FOO); |
| 84 | assertTrue("Wrong formatter", fmt instanceof DefaultCellFormatter); |
| 85 | } |
| 86 | |
| 87 | @Test(expected = IllegalArgumentException.class) |
| 88 | public void formatterBadColumn() { |
| 89 | tm = new TableModel(FOO); |
| 90 | fmt = tm.getFormatter(BAR); |
| 91 | } |
| 92 | |
| 93 | @Test |
| 94 | public void altFormatter() { |
| 95 | tm = new TableModel(FOO, BAR); |
Simon Hunt | 3ee7f74 | 2015-05-05 10:18:20 -0700 | [diff] [blame] | 96 | tm.setFormatter(BAR, new ParenFormatter()); |
Simon Hunt | e982815 | 2015-05-01 17:54:25 -0700 | [diff] [blame] | 97 | |
| 98 | fmt = tm.getFormatter(FOO); |
| 99 | assertTrue("Wrong formatter", fmt instanceof DefaultCellFormatter); |
| 100 | assertEquals("Wrong result", "2", fmt.format(2)); |
| 101 | |
| 102 | fmt = tm.getFormatter(BAR); |
Simon Hunt | 3ee7f74 | 2015-05-05 10:18:20 -0700 | [diff] [blame] | 103 | assertTrue("Wrong formatter", fmt instanceof ParenFormatter); |
Simon Hunt | e982815 | 2015-05-01 17:54:25 -0700 | [diff] [blame] | 104 | assertEquals("Wrong result", "(2)", fmt.format(2)); |
| 105 | } |
| 106 | |
| 107 | @Test |
| 108 | public void emptyRow() { |
| 109 | tm = new TableModel(FOO, BAR); |
| 110 | tm.addRow(); |
| 111 | assertEquals("bad row count", 1, tm.rowCount()); |
| 112 | } |
| 113 | |
| 114 | @Test(expected = IllegalArgumentException.class) |
| 115 | public void rowBadColumn() { |
| 116 | tm = new TableModel(FOO, BAR); |
| 117 | tm.addRow().cell(ZOO, 2); |
| 118 | } |
| 119 | |
Simon Hunt | e982815 | 2015-05-01 17:54:25 -0700 | [diff] [blame] | 120 | @Test |
| 121 | public void simpleRow() { |
| 122 | tm = new TableModel(FOO, BAR); |
| 123 | tm.addRow().cell(FOO, 3).cell(BAR, true); |
| 124 | assertEquals("bad row count", 1, tm.rowCount()); |
Simon Hunt | 933b1a8 | 2015-05-04 19:07:24 -0700 | [diff] [blame] | 125 | row = tm.getRows()[0]; |
| 126 | assertEquals("bad cell", 3, row.get(FOO)); |
| 127 | assertEquals("bad cell", true, row.get(BAR)); |
Simon Hunt | e982815 | 2015-05-01 17:54:25 -0700 | [diff] [blame] | 128 | } |
Simon Hunt | 933b1a8 | 2015-05-04 19:07:24 -0700 | [diff] [blame] | 129 | |
Simon Hunt | 933b1a8 | 2015-05-04 19:07:24 -0700 | [diff] [blame] | 130 | private static final String ONE = "one"; |
| 131 | private static final String TWO = "two"; |
| 132 | private static final String THREE = "three"; |
| 133 | private static final String FOUR = "four"; |
| 134 | private static final String ELEVEN = "eleven"; |
| 135 | private static final String TWELVE = "twelve"; |
| 136 | private static final String TWENTY = "twenty"; |
| 137 | private static final String THIRTY = "thirty"; |
| 138 | |
| 139 | private static final String[] NAMES = { |
| 140 | FOUR, |
| 141 | THREE, |
| 142 | TWO, |
| 143 | ONE, |
| 144 | ELEVEN, |
| 145 | TWELVE, |
| 146 | THIRTY, |
| 147 | TWENTY, |
| 148 | }; |
| 149 | private static final String[] SORTED_NAMES = { |
| 150 | ELEVEN, |
| 151 | FOUR, |
| 152 | ONE, |
| 153 | THIRTY, |
| 154 | THREE, |
| 155 | TWELVE, |
| 156 | TWENTY, |
| 157 | TWO, |
| 158 | }; |
| 159 | |
| 160 | private static final int[] NUMBERS = { |
| 161 | 4, 3, 2, 1, 11, 12, 30, 20 |
| 162 | }; |
| 163 | |
| 164 | private static final int[] SORTED_NUMBERS = { |
| 165 | 1, 2, 3, 4, 11, 12, 20, 30 |
| 166 | }; |
| 167 | |
Simon Hunt | 3ee7f74 | 2015-05-05 10:18:20 -0700 | [diff] [blame] | 168 | private static final String[] SORTED_HEX = { |
| 169 | "0x1", "0x2", "0x3", "0x4", "0xb", "0xc", "0x14", "0x1e" |
| 170 | }; |
| 171 | |
Simon Hunt | 933b1a8 | 2015-05-04 19:07:24 -0700 | [diff] [blame] | 172 | @Test |
| 173 | public void verifyTestData() { |
| 174 | // not a unit test per se, but will fail if we don't keep |
| 175 | // the three test arrays in sync |
| 176 | int nalen = NAMES.length; |
| 177 | int snlen = SORTED_NAMES.length; |
| 178 | int nulen = NUMBERS.length; |
| 179 | |
| 180 | if (nalen != snlen || nalen != nulen) { |
| 181 | fail("test data array size discrepancy"); |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | private void initUnsortedTable() { |
| 186 | tm = new TableModel(FOO, BAR); |
| 187 | for (int i = 0; i < NAMES.length; i++) { |
| 188 | tm.addRow().cell(FOO, NAMES[i]).cell(BAR, NUMBERS[i]); |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | @Test |
| 193 | public void tableStringSort() { |
| 194 | initUnsortedTable(); |
| 195 | |
| 196 | // sort by name |
Simon Hunt | 051e9fa | 2016-01-19 15:54:22 -0800 | [diff] [blame] | 197 | tm.sort(FOO, SortDir.ASC, null, null); |
Simon Hunt | 933b1a8 | 2015-05-04 19:07:24 -0700 | [diff] [blame] | 198 | |
| 199 | // verify results |
| 200 | rows = tm.getRows(); |
| 201 | int nr = rows.length; |
| 202 | assertEquals("row count", NAMES.length, nr); |
| 203 | for (int i = 0; i < nr; i++) { |
Simon Hunt | 3ee7f74 | 2015-05-05 10:18:20 -0700 | [diff] [blame] | 204 | assertEquals(UNEX_SORT + i, SORTED_NAMES[i], rows[i].get(FOO)); |
Simon Hunt | 933b1a8 | 2015-05-04 19:07:24 -0700 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | // now the other way |
Simon Hunt | 051e9fa | 2016-01-19 15:54:22 -0800 | [diff] [blame] | 208 | tm.sort(FOO, SortDir.DESC, null, null); |
Simon Hunt | 933b1a8 | 2015-05-04 19:07:24 -0700 | [diff] [blame] | 209 | |
| 210 | // verify results |
| 211 | rows = tm.getRows(); |
| 212 | nr = rows.length; |
| 213 | assertEquals("row count", NAMES.length, nr); |
| 214 | for (int i = 0; i < nr; i++) { |
Simon Hunt | 3ee7f74 | 2015-05-05 10:18:20 -0700 | [diff] [blame] | 215 | assertEquals(UNEX_SORT + i, |
Simon Hunt | 933b1a8 | 2015-05-04 19:07:24 -0700 | [diff] [blame] | 216 | SORTED_NAMES[nr - 1 - i], rows[i].get(FOO)); |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | @Test |
| 221 | public void tableNumberSort() { |
| 222 | initUnsortedTable(); |
| 223 | |
Simon Hunt | 933b1a8 | 2015-05-04 19:07:24 -0700 | [diff] [blame] | 224 | // sort by number |
Simon Hunt | 051e9fa | 2016-01-19 15:54:22 -0800 | [diff] [blame] | 225 | tm.sort(BAR, SortDir.ASC, null, null); |
Simon Hunt | 933b1a8 | 2015-05-04 19:07:24 -0700 | [diff] [blame] | 226 | |
| 227 | // verify results |
| 228 | rows = tm.getRows(); |
| 229 | int nr = rows.length; |
| 230 | assertEquals("row count", NUMBERS.length, nr); |
| 231 | for (int i = 0; i < nr; i++) { |
Simon Hunt | 3ee7f74 | 2015-05-05 10:18:20 -0700 | [diff] [blame] | 232 | assertEquals(UNEX_SORT + i, SORTED_NUMBERS[i], rows[i].get(BAR)); |
Simon Hunt | 933b1a8 | 2015-05-04 19:07:24 -0700 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | // now the other way |
Simon Hunt | 051e9fa | 2016-01-19 15:54:22 -0800 | [diff] [blame] | 236 | tm.sort(BAR, SortDir.DESC, null, null); |
Simon Hunt | 933b1a8 | 2015-05-04 19:07:24 -0700 | [diff] [blame] | 237 | |
| 238 | // verify results |
| 239 | rows = tm.getRows(); |
| 240 | nr = rows.length; |
| 241 | assertEquals("row count", NUMBERS.length, nr); |
| 242 | for (int i = 0; i < nr; i++) { |
Simon Hunt | 3ee7f74 | 2015-05-05 10:18:20 -0700 | [diff] [blame] | 243 | assertEquals(UNEX_SORT + i, |
Simon Hunt | 933b1a8 | 2015-05-04 19:07:24 -0700 | [diff] [blame] | 244 | SORTED_NUMBERS[nr - 1 - i], rows[i].get(BAR)); |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | @Test |
Simon Hunt | 3ee7f74 | 2015-05-05 10:18:20 -0700 | [diff] [blame] | 249 | public void sortAndFormat() { |
| 250 | initUnsortedTable(); |
| 251 | |
Simon Hunt | 27bf079 | 2015-05-07 10:50:29 -0700 | [diff] [blame] | 252 | // set hex formatter |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 253 | tm.setFormatter(BAR, HexFormatter.INSTANCE); |
Simon Hunt | 3ee7f74 | 2015-05-05 10:18:20 -0700 | [diff] [blame] | 254 | |
| 255 | // sort by number |
Simon Hunt | 051e9fa | 2016-01-19 15:54:22 -0800 | [diff] [blame] | 256 | tm.sort(BAR, SortDir.ASC, null, null); |
Simon Hunt | 3ee7f74 | 2015-05-05 10:18:20 -0700 | [diff] [blame] | 257 | |
| 258 | // verify results |
| 259 | rows = tm.getRows(); |
| 260 | int nr = rows.length; |
| 261 | assertEquals("row count", SORTED_HEX.length, nr); |
| 262 | for (int i = 0; i < nr; i++) { |
| 263 | assertEquals(UNEX_SORT + i, SORTED_HEX[i], rows[i].getAsString(BAR)); |
| 264 | } |
| 265 | } |
| 266 | |
Simon Hunt | 9ffbd8d | 2015-05-06 12:02:32 -0700 | [diff] [blame] | 267 | private static final String[][] SORTED_NAMES_AND_HEX = { |
| 268 | {ELEVEN, "0xb"}, |
| 269 | {FOUR, "0x4"}, |
| 270 | {ONE, "0x1"}, |
| 271 | {THIRTY, "0x1e"}, |
| 272 | {THREE, "0x3"}, |
| 273 | {TWELVE, "0xc"}, |
| 274 | {TWENTY, "0x14"}, |
| 275 | {TWO, "0x2"}, |
| 276 | }; |
| 277 | |
| 278 | @Test |
| 279 | public void sortAndFormatTwo() { |
| 280 | initUnsortedTable(); |
| 281 | tm.setFormatter(BAR, HexFormatter.INSTANCE); |
Simon Hunt | 051e9fa | 2016-01-19 15:54:22 -0800 | [diff] [blame] | 282 | tm.sort(FOO, SortDir.ASC, null, null); |
Simon Hunt | 9ffbd8d | 2015-05-06 12:02:32 -0700 | [diff] [blame] | 283 | rows = tm.getRows(); |
| 284 | int nr = rows.length; |
| 285 | for (int i = 0; i < nr; i++) { |
| 286 | String[] exp = SORTED_NAMES_AND_HEX[i]; |
| 287 | String[] act = rows[i].getAsFormattedStrings(); |
| 288 | assertArrayEquals(UNEX_SORT + i, exp, act); |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | private static final String[] FBZ = {FOO, BAR, ZOO}; |
| 293 | |
| 294 | @Test |
| 295 | public void getColumnIds() { |
| 296 | tm = new TableModel(FOO, BAR, ZOO); |
| 297 | assertArrayEquals("col IDs", FBZ, tm.getColumnIds()); |
| 298 | } |
Simon Hunt | 3ee7f74 | 2015-05-05 10:18:20 -0700 | [diff] [blame] | 299 | |
| 300 | @Test |
Simon Hunt | 933b1a8 | 2015-05-04 19:07:24 -0700 | [diff] [blame] | 301 | public void sortDirAsc() { |
| 302 | assertEquals("asc sort dir", SortDir.ASC, TableModel.sortDir("asc")); |
| 303 | } |
| 304 | |
| 305 | @Test |
| 306 | public void sortDirDesc() { |
| 307 | assertEquals("desc sort dir", SortDir.DESC, TableModel.sortDir("desc")); |
| 308 | } |
| 309 | |
| 310 | @Test |
| 311 | public void sortDirOther() { |
| 312 | assertEquals("other sort dir", SortDir.ASC, TableModel.sortDir("other")); |
| 313 | } |
| 314 | |
| 315 | @Test |
| 316 | public void sortDirNull() { |
| 317 | assertEquals("null sort dir", SortDir.ASC, TableModel.sortDir(null)); |
| 318 | } |
| 319 | |
Simon Hunt | 27bf079 | 2015-05-07 10:50:29 -0700 | [diff] [blame] | 320 | @Test |
| 321 | public void enumSort() { |
| 322 | tm = new TableModel(FOO); |
| 323 | tm.addRow().cell(FOO, StarWars.HAN_SOLO); |
| 324 | tm.addRow().cell(FOO, StarWars.C3PO); |
| 325 | tm.addRow().cell(FOO, StarWars.JABBA_THE_HUTT); |
| 326 | tm.addRow().cell(FOO, StarWars.LEIA_ORGANA); |
| 327 | tm.addRow().cell(FOO, StarWars.R2D2); |
| 328 | tm.addRow().cell(FOO, StarWars.LUKE_SKYWALKER); |
| 329 | |
Simon Hunt | 051e9fa | 2016-01-19 15:54:22 -0800 | [diff] [blame] | 330 | tm.sort(FOO, SortDir.ASC, null, null); |
Simon Hunt | 27bf079 | 2015-05-07 10:50:29 -0700 | [diff] [blame] | 331 | |
| 332 | // verify expected results |
| 333 | StarWars[] ordered = StarWars.values(); |
| 334 | TableModel.Row[] rows = tm.getRows(); |
| 335 | assertEquals("wrong length?", ordered.length, rows.length); |
| 336 | int nr = rows.length; |
| 337 | for (int i = 0; i < nr; i++) { |
| 338 | assertEquals(UNEX_SORT + i, ordered[i], rows[i].get(FOO)); |
| 339 | } |
| 340 | } |
Jian Li | 69f6663 | 2016-01-15 12:27:42 -0800 | [diff] [blame] | 341 | |
Simon Hunt | 051e9fa | 2016-01-19 15:54:22 -0800 | [diff] [blame] | 342 | |
| 343 | // ------------------------ |
| 344 | // Second sort column tests |
| 345 | |
| 346 | private static final String A1 = "a1"; |
| 347 | private static final String A2 = "a2"; |
| 348 | private static final String A3 = "a3"; |
| 349 | private static final String B1 = "b1"; |
| 350 | private static final String B2 = "b2"; |
| 351 | private static final String B3 = "b3"; |
| 352 | private static final String C1 = "c1"; |
| 353 | private static final String C2 = "c2"; |
| 354 | private static final String C3 = "c3"; |
| 355 | private static final String A = "A"; |
| 356 | private static final String B = "B"; |
| 357 | private static final String C = "C"; |
| 358 | |
| 359 | private static final String[] UNSORTED_IDS = { |
| 360 | A3, B2, A1, C2, A2, C3, B1, C1, B3 |
| 361 | }; |
| 362 | private static final String[] UNSORTED_ALPHAS = { |
| 363 | A, B, A, C, A, C, B, C, B |
| 364 | }; |
| 365 | private static final int[] UNSORTED_NUMBERS = { |
| 366 | 3, 2, 1, 2, 2, 3, 1, 1, 3 |
| 367 | }; |
| 368 | |
| 369 | private static final String[] ROW_ORDER_AA_NA = { |
| 370 | A1, A2, A3, B1, B2, B3, C1, C2, C3 |
| 371 | }; |
| 372 | private static final String[] ROW_ORDER_AD_NA = { |
| 373 | C1, C2, C3, B1, B2, B3, A1, A2, A3 |
| 374 | }; |
| 375 | private static final String[] ROW_ORDER_AA_ND = { |
| 376 | A3, A2, A1, B3, B2, B1, C3, C2, C1 |
| 377 | }; |
| 378 | private static final String[] ROW_ORDER_AD_ND = { |
| 379 | C3, C2, C1, B3, B2, B1, A3, A2, A1 |
| 380 | }; |
| 381 | |
| 382 | private void testAddRow(TableModel tm, int index) { |
| 383 | tm.addRow().cell(ID, UNSORTED_IDS[index]) |
| 384 | .cell(ALPHA, UNSORTED_ALPHAS[index]) |
| 385 | .cell(NUMBER, UNSORTED_NUMBERS[index]); |
| 386 | } |
| 387 | |
| 388 | private TableModel unsortedDoubleTableModel() { |
| 389 | tm = new TableModel(ID, ALPHA, NUMBER); |
| 390 | for (int i = 0; i < 9; i++) { |
| 391 | testAddRow(tm, i); |
| 392 | } |
| 393 | return tm; |
| 394 | } |
| 395 | |
| 396 | private void verifyRowOrder(String tag, TableModel tm, String[] rowOrder) { |
| 397 | int i = 0; |
| 398 | for (TableModel.Row row : tm.getRows()) { |
| 399 | assertEquals(tag + ": unexpected row id", rowOrder[i++], row.get(ID)); |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | @Test |
| 404 | public void sortAlphaAscNumberAsc() { |
| 405 | tm = unsortedDoubleTableModel(); |
| 406 | verifyRowOrder("unsorted", tm, UNSORTED_IDS); |
| 407 | tm.sort(ALPHA, SortDir.ASC, NUMBER, SortDir.ASC); |
| 408 | verifyRowOrder("aana", tm, ROW_ORDER_AA_NA); |
| 409 | } |
| 410 | |
| 411 | @Test |
| 412 | public void sortAlphaDescNumberAsc() { |
| 413 | tm = unsortedDoubleTableModel(); |
| 414 | verifyRowOrder("unsorted", tm, UNSORTED_IDS); |
| 415 | tm.sort(ALPHA, SortDir.DESC, NUMBER, SortDir.ASC); |
| 416 | verifyRowOrder("adna", tm, ROW_ORDER_AD_NA); |
| 417 | } |
| 418 | |
| 419 | @Test |
| 420 | public void sortAlphaAscNumberDesc() { |
| 421 | tm = unsortedDoubleTableModel(); |
| 422 | verifyRowOrder("unsorted", tm, UNSORTED_IDS); |
| 423 | tm.sort(ALPHA, SortDir.ASC, NUMBER, SortDir.DESC); |
| 424 | verifyRowOrder("aand", tm, ROW_ORDER_AA_ND); |
| 425 | } |
| 426 | |
| 427 | @Test |
| 428 | public void sortAlphaDescNumberDesc() { |
| 429 | tm = unsortedDoubleTableModel(); |
| 430 | verifyRowOrder("unsorted", tm, UNSORTED_IDS); |
| 431 | tm.sort(ALPHA, SortDir.DESC, NUMBER, SortDir.DESC); |
| 432 | verifyRowOrder("adnd", tm, ROW_ORDER_AD_ND); |
| 433 | } |
| 434 | |
| 435 | // ---------------- |
| 436 | // Annotation tests |
| 437 | |
Jian Li | 69f6663 | 2016-01-15 12:27:42 -0800 | [diff] [blame] | 438 | @Test |
| 439 | public void stringAnnotation() { |
| 440 | tm = new TableModel(FOO); |
| 441 | tm.addAnnotation(BAR, ZOO); |
| 442 | Collection<TableModel.Annot> annots = tm.getAnnotations(); |
| 443 | assertEquals("wrong size", 1, annots.size()); |
| 444 | |
| 445 | TableModel.Annot annot = annots.iterator().next(); |
| 446 | assertEquals("wrong key", BAR, annot.key()); |
| 447 | assertEquals("wrong value", ZOO, annot.value()); |
| 448 | } |
| 449 | |
| 450 | private static final String K_INT = "int"; |
| 451 | private static final String K_BOOL = "bool"; |
| 452 | private static final String K_FLOAT = "float"; |
| 453 | private static final String K_DOUBLE = "double"; |
| 454 | private static final String K_ENUM = "enum"; |
| 455 | |
| 456 | private TableModel.Annot getAnnotation(Collection<TableModel.Annot> annots, String key) { |
| 457 | final TableModel.Annot[] annot = {null}; |
| 458 | annots.forEach(a -> { |
| 459 | if (a.key().equals(key)) { |
| 460 | annot[0] = a; |
| 461 | } |
| 462 | }); |
| 463 | return annot[0]; |
| 464 | } |
| 465 | |
| 466 | private void verifyCollectionContains(Collection<TableModel.Annot> annots, |
| 467 | String key, int i) { |
| 468 | TableModel.Annot a = getAnnotation(annots, key); |
| 469 | assertEquals("wrong int value", i, a.value()); |
| 470 | } |
| 471 | |
| 472 | private void verifyCollectionContains(Collection<TableModel.Annot> annots, |
| 473 | String key, boolean b) { |
| 474 | TableModel.Annot a = getAnnotation(annots, key); |
| 475 | assertEquals("wrong boolean value", b, a.value()); |
| 476 | } |
| 477 | |
| 478 | private void verifyCollectionContains(Collection<TableModel.Annot> annots, |
| 479 | String key, float f) { |
| 480 | TableModel.Annot a = getAnnotation(annots, key); |
| 481 | assertEquals("wrong float value", f, a.value()); |
| 482 | } |
| 483 | |
| 484 | private void verifyCollectionContains(Collection<TableModel.Annot> annots, |
| 485 | String key, double d) { |
| 486 | TableModel.Annot a = getAnnotation(annots, key); |
| 487 | assertEquals("wrong double value", d, a.value()); |
| 488 | } |
| 489 | |
| 490 | private void verifyCollectionContains(Collection<TableModel.Annot> annots, |
| 491 | String key, Enum<?> e) { |
| 492 | TableModel.Annot a = getAnnotation(annots, key); |
| 493 | assertEquals("wrong double value", e, a.value()); |
| 494 | } |
| 495 | |
| 496 | @Test |
| 497 | public void primitivesAnnotation() { |
| 498 | tm = new TableModel(FOO); |
| 499 | tm.addAnnotation(K_INT, 1); |
| 500 | tm.addAnnotation(K_BOOL, true); |
| 501 | tm.addAnnotation(K_FLOAT, 3.14f); |
| 502 | tm.addAnnotation(K_DOUBLE, 2.71828); |
| 503 | tm.addAnnotation(K_ENUM, StarWars.LUKE_SKYWALKER); |
| 504 | |
| 505 | Collection<TableModel.Annot> annots = tm.getAnnotations(); |
| 506 | assertEquals("wrong size", 5, annots.size()); |
| 507 | |
| 508 | verifyCollectionContains(annots, K_INT, 1); |
| 509 | verifyCollectionContains(annots, K_BOOL, true); |
| 510 | verifyCollectionContains(annots, K_FLOAT, 3.14f); |
| 511 | verifyCollectionContains(annots, K_DOUBLE, 2.71828); |
| 512 | verifyCollectionContains(annots, K_ENUM, StarWars.LUKE_SKYWALKER); |
| 513 | } |
| 514 | |
| 515 | // TODO: add support for compound object value |
Simon Hunt | e982815 | 2015-05-01 17:54:25 -0700 | [diff] [blame] | 516 | } |