GUI -- Enhanced DefaultCellComparator to utilize Comparable implementation if available on the objects being compared, defaulting to string comparison otherwise.
- Deleted obsolete comparators.
Change-Id: Ifeb74c05f73bd974d1afb3dc27527a5c7ad1bc12
diff --git a/core/api/src/test/java/org/onosproject/ui/table/TableModelTest.java b/core/api/src/test/java/org/onosproject/ui/table/TableModelTest.java
index 7e3c651..77326ce 100644
--- a/core/api/src/test/java/org/onosproject/ui/table/TableModelTest.java
+++ b/core/api/src/test/java/org/onosproject/ui/table/TableModelTest.java
@@ -20,7 +20,6 @@
import org.onosproject.ui.table.TableModel.SortDir;
import org.onosproject.ui.table.cell.DefaultCellFormatter;
import org.onosproject.ui.table.cell.HexFormatter;
-import org.onosproject.ui.table.cell.IntComparator;
import static org.junit.Assert.*;
@@ -35,6 +34,10 @@
private static final String BAR = "bar";
private static final String ZOO = "zoo";
+ private enum StarWars {
+ LUKE_SKYWALKER, LEIA_ORGANA, HAN_SOLO, C3PO, R2D2, JABBA_THE_HUTT
+ }
+
private static class ParenFormatter implements CellFormatter {
@Override
public String format(Object value) {
@@ -220,9 +223,6 @@
public void tableNumberSort() {
initUnsortedTable();
- // first, tell the table to use an integer-based comparator
- tm.setComparator(BAR, IntComparator.INSTANCE);
-
// sort by number
tm.sort(BAR, SortDir.ASC);
@@ -251,8 +251,7 @@
public void sortAndFormat() {
initUnsortedTable();
- // set integer-based comparator and hex formatter
- tm.setComparator(BAR, IntComparator.INSTANCE);
+ // set hex formatter
tm.setFormatter(BAR, HexFormatter.INSTANCE);
// sort by number
@@ -320,4 +319,26 @@
assertEquals("null sort dir", SortDir.ASC, TableModel.sortDir(null));
}
+
+ @Test
+ public void enumSort() {
+ tm = new TableModel(FOO);
+ tm.addRow().cell(FOO, StarWars.HAN_SOLO);
+ tm.addRow().cell(FOO, StarWars.C3PO);
+ tm.addRow().cell(FOO, StarWars.JABBA_THE_HUTT);
+ tm.addRow().cell(FOO, StarWars.LEIA_ORGANA);
+ tm.addRow().cell(FOO, StarWars.R2D2);
+ tm.addRow().cell(FOO, StarWars.LUKE_SKYWALKER);
+
+ tm.sort(FOO, SortDir.ASC);
+
+ // verify expected results
+ StarWars[] ordered = StarWars.values();
+ TableModel.Row[] rows = tm.getRows();
+ assertEquals("wrong length?", ordered.length, rows.length);
+ int nr = rows.length;
+ for (int i = 0; i < nr; i++) {
+ assertEquals(UNEX_SORT + i, ordered[i], rows[i].get(FOO));
+ }
+ }
}
diff --git a/core/api/src/test/java/org/onosproject/ui/table/cell/DefaultCellComparatorTest.java b/core/api/src/test/java/org/onosproject/ui/table/cell/DefaultCellComparatorTest.java
index 38c6bed..87c9528 100644
--- a/core/api/src/test/java/org/onosproject/ui/table/cell/DefaultCellComparatorTest.java
+++ b/core/api/src/test/java/org/onosproject/ui/table/cell/DefaultCellComparatorTest.java
@@ -27,20 +27,13 @@
*/
public class DefaultCellComparatorTest {
- private static class TestClass {
- @Override
- public String toString() {
- return SOME;
- }
- }
-
private static final String SOME = "SoMeStRiNg";
private static final String OTHER = "OtherSTRING";
- private static final int NUMBER = 42;
- private static final TestClass OBJECT = new TestClass();
private CellComparator cmp = DefaultCellComparator.INSTANCE;
+ // default comparator should detect Comparable<T> impls and use that
+
@Test
public void sameString() {
assertTrue("same string", cmp.compare(SOME, SOME) == 0);
@@ -57,21 +50,6 @@
}
@Test
- public void someVsObject() {
- assertTrue("some vs object", cmp.compare(SOME, OBJECT) == 0);
- }
-
- @Test
- public void otherVsObject() {
- assertTrue("other vs object", cmp.compare(OTHER, OBJECT) < 0);
- }
-
- @Test
- public void otherVsNumber() {
- assertTrue("other vs 42", cmp.compare(OTHER, NUMBER) > 0);
- }
-
- @Test
public void someVsNull() {
assertTrue("some vs null", cmp.compare(SOME, null) > 0);
}
@@ -81,4 +59,89 @@
assertTrue("null vs some", cmp.compare(null, SOME) < 0);
}
+ @Test(expected = ClassCastException.class)
+ public void mismatch() {
+ cmp.compare(42, SOME);
+ }
+
+
+ @Test
+ public void strElevenTwo() {
+ assertTrue("str 11 vs 2", cmp.compare("11", "2") < 0);
+ }
+
+ @Test
+ public void intElevenTwo() {
+ assertTrue("int 11 vs 2", cmp.compare(11, 2) > 0);
+ }
+
+
+ @Test
+ public void intSmallBig() {
+ assertTrue("int 2 vs 4", cmp.compare(2, 4) < 0);
+ }
+
+ @Test
+ public void intBigSmall() {
+ assertTrue("int 4 vs 2", cmp.compare(4, 2) > 0);
+ }
+
+ @Test
+ public void intEqual() {
+ assertTrue("int 4 vs 4", cmp.compare(4, 4) == 0);
+ }
+
+ @Test
+ public void longSmallBig() {
+ assertTrue("long 2 vs 4", cmp.compare(2L, 4L) < 0);
+ }
+
+ @Test
+ public void longBigSmall() {
+ assertTrue("long 4 vs 2", cmp.compare(4L, 2L) > 0);
+ }
+
+ @Test
+ public void longEqual() {
+ assertTrue("long 4 vs 4", cmp.compare(4L, 4L) == 0);
+ }
+
+
+ private enum SmallStarWars { C3PO, R2D2, LUKE }
+
+ @Test
+ public void swEpisodeI() {
+ assertTrue("c3po r2d2",
+ cmp.compare(SmallStarWars.C3PO, SmallStarWars.R2D2) < 0);
+ }
+
+ @Test
+ public void swEpisodeII() {
+ assertTrue("r2d2 c3po",
+ cmp.compare(SmallStarWars.R2D2, SmallStarWars.C3PO) > 0);
+ }
+
+ @Test
+ public void swEpisodeIII() {
+ assertTrue("luke c3po",
+ cmp.compare(SmallStarWars.LUKE, SmallStarWars.C3PO) > 0);
+ }
+
+ @Test
+ public void swEpisodeIV() {
+ assertTrue("c3po luke",
+ cmp.compare(SmallStarWars.C3PO, SmallStarWars.LUKE) < 0);
+ }
+
+ @Test
+ public void swEpisodeV() {
+ assertTrue("luke r2d2",
+ cmp.compare(SmallStarWars.LUKE, SmallStarWars.R2D2) > 0);
+ }
+
+ @Test
+ public void swEpisodeVI() {
+ assertTrue("r2d2 luke",
+ cmp.compare(SmallStarWars.R2D2, SmallStarWars.LUKE) < 0);
+ }
}
diff --git a/core/api/src/test/java/org/onosproject/ui/table/cell/IntComparatorTest.java b/core/api/src/test/java/org/onosproject/ui/table/cell/IntComparatorTest.java
deleted file mode 100644
index 9455329..0000000
--- a/core/api/src/test/java/org/onosproject/ui/table/cell/IntComparatorTest.java
+++ /dev/null
@@ -1,72 +0,0 @@
-/*
- * Copyright 2015 Open Networking Laboratory
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- *
- */
-
-package org.onosproject.ui.table.cell;
-
-import org.junit.Test;
-import org.onosproject.ui.table.CellComparator;
-
-import static org.junit.Assert.assertTrue;
-
-/**
- * Unit tests for {@link IntComparator}.
- */
-public class IntComparatorTest {
-
- private CellComparator cmp = IntComparator.INSTANCE;
-
- @Test
- public void twoNulls() {
- assertTrue("two nulls", cmp.compare(null, null) == 0);
- }
-
- @Test
- public void nullVsNegValue() {
- assertTrue("null vs neg value", cmp.compare(null, -5) < 0);
- }
-
- @Test
- public void nullVsPosValue() {
- assertTrue("null vs pos value", cmp.compare(null, 5) < 0);
- }
-
- @Test
- public void negValueVsNull() {
- assertTrue("neg value vs null", cmp.compare(-5, null) > 0);
- }
-
- @Test
- public void posValueVsNull() {
- assertTrue("pos value vs null", cmp.compare(5, null) > 0);
- }
-
-
- @Test
- public void smallVsBig() {
- assertTrue("small vs big", cmp.compare(25, 75) < 0);
- }
-
- @Test
- public void bigVsSmall() {
- assertTrue("big vs small", cmp.compare(75, 25) > 0);
- }
-
- @Test
- public void sameValue() {
- assertTrue("same value", cmp.compare(50, 50) == 0);
- }
-}
diff --git a/core/api/src/test/java/org/onosproject/ui/table/cell/LongComparatorTest.java b/core/api/src/test/java/org/onosproject/ui/table/cell/LongComparatorTest.java
deleted file mode 100644
index f016540..0000000
--- a/core/api/src/test/java/org/onosproject/ui/table/cell/LongComparatorTest.java
+++ /dev/null
@@ -1,72 +0,0 @@
-/*
- * Copyright 2015 Open Networking Laboratory
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- *
- */
-
-package org.onosproject.ui.table.cell;
-
-import org.junit.Test;
-import org.onosproject.ui.table.CellComparator;
-
-import static org.junit.Assert.assertTrue;
-
-/**
- * Unit tests for {@link LongComparator}.
- */
-public class LongComparatorTest {
-
- private CellComparator cmp = LongComparator.INSTANCE;
-
- @Test
- public void twoNulls() {
- assertTrue("two nulls", cmp.compare(null, null) == 0);
- }
-
- @Test
- public void nullVsNegValue() {
- assertTrue("null vs neg value", cmp.compare(null, -5L) < 0);
- }
-
- @Test
- public void nullVsPosValue() {
- assertTrue("null vs pos value", cmp.compare(null, 5L) < 0);
- }
-
- @Test
- public void negValueVsNull() {
- assertTrue("neg value vs null", cmp.compare(-5L, null) > 0);
- }
-
- @Test
- public void posValueVsNull() {
- assertTrue("pos value vs null", cmp.compare(5L, null) > 0);
- }
-
-
- @Test
- public void smallVsBig() {
- assertTrue("small vs big", cmp.compare(25L, 75L) < 0);
- }
-
- @Test
- public void bigVsSmall() {
- assertTrue("big vs small", cmp.compare(75L, 25L) > 0);
- }
-
- @Test
- public void sameValue() {
- assertTrue("same value", cmp.compare(50L, 50L) == 0);
- }
-}