GUI -- added getAsString() to TableModel.Row.
- Moved cell-related classes to new package.
- Created base classes for cell comparator and formatter.
- Created IntComparator and HexFormatter.
Change-Id: I3861bf3e0ca738a2d6eab9c70174db53f22960f8
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 33b8fb0..40a9672 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
@@ -18,6 +18,9 @@
import org.junit.Test;
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.*;
@@ -26,22 +29,13 @@
*/
public class TableModelTest {
- private static final String UNEX_SORT_ORDER = "unexpected sort: index ";
+ private static final String UNEX_SORT = "unexpected sort: index ";
private static final String FOO = "foo";
private static final String BAR = "bar";
private static final String ZOO = "zoo";
- private static class TestCmpr implements CellComparator {
- @Override
- public int compare(Object o1, Object o2) {
- int i1 = (int) o1;
- int i2 = (int) o2;
- return i1 - i2;
- }
- }
-
- private static class TestFmtr implements CellFormatter {
+ private static class ParenFormatter implements CellFormatter {
@Override
public String format(Object value) {
return "(" + value + ")";
@@ -95,14 +89,14 @@
@Test
public void altFormatter() {
tm = new TableModel(FOO, BAR);
- tm.setFormatter(BAR, new TestFmtr());
+ tm.setFormatter(BAR, new ParenFormatter());
fmt = tm.getFormatter(FOO);
assertTrue("Wrong formatter", fmt instanceof DefaultCellFormatter);
assertEquals("Wrong result", "2", fmt.format(2));
fmt = tm.getFormatter(BAR);
- assertTrue("Wrong formatter", fmt instanceof TestFmtr);
+ assertTrue("Wrong formatter", fmt instanceof ParenFormatter);
assertEquals("Wrong result", "(2)", fmt.format(2));
}
@@ -174,6 +168,10 @@
1, 2, 3, 4, 11, 12, 20, 30
};
+ private static final String[] SORTED_HEX = {
+ "0x1", "0x2", "0x3", "0x4", "0xb", "0xc", "0x14", "0x1e"
+ };
+
@Test
public void verifyTestData() {
// not a unit test per se, but will fail if we don't keep
@@ -206,7 +204,7 @@
int nr = rows.length;
assertEquals("row count", NAMES.length, nr);
for (int i = 0; i < nr; i++) {
- assertEquals(UNEX_SORT_ORDER + i, SORTED_NAMES[i], rows[i].get(FOO));
+ assertEquals(UNEX_SORT + i, SORTED_NAMES[i], rows[i].get(FOO));
}
// now the other way
@@ -217,7 +215,7 @@
nr = rows.length;
assertEquals("row count", NAMES.length, nr);
for (int i = 0; i < nr; i++) {
- assertEquals(UNEX_SORT_ORDER + i,
+ assertEquals(UNEX_SORT + i,
SORTED_NAMES[nr - 1 - i], rows[i].get(FOO));
}
}
@@ -227,7 +225,7 @@
initUnsortedTable();
// first, tell the table to use an integer-based comparator
- tm.setComparator(BAR, new TestCmpr());
+ tm.setComparator(BAR, new IntComparator());
// sort by number
tm.sort(BAR, SortDir.ASC);
@@ -237,7 +235,7 @@
int nr = rows.length;
assertEquals("row count", NUMBERS.length, nr);
for (int i = 0; i < nr; i++) {
- assertEquals(UNEX_SORT_ORDER + i, SORTED_NUMBERS[i], rows[i].get(BAR));
+ assertEquals(UNEX_SORT + i, SORTED_NUMBERS[i], rows[i].get(BAR));
}
// now the other way
@@ -248,12 +246,33 @@
nr = rows.length;
assertEquals("row count", NUMBERS.length, nr);
for (int i = 0; i < nr; i++) {
- assertEquals(UNEX_SORT_ORDER + i,
+ assertEquals(UNEX_SORT + i,
SORTED_NUMBERS[nr - 1 - i], rows[i].get(BAR));
}
}
@Test
+ public void sortAndFormat() {
+ initUnsortedTable();
+
+ // set integer-based comparator and hex formatter
+ tm.setComparator(BAR, new IntComparator());
+ tm.setFormatter(BAR, new HexFormatter());
+
+ // sort by number
+ tm.sort(BAR, SortDir.ASC);
+
+ // verify results
+ rows = tm.getRows();
+ int nr = rows.length;
+ assertEquals("row count", SORTED_HEX.length, nr);
+ for (int i = 0; i < nr; i++) {
+ assertEquals(UNEX_SORT + i, SORTED_HEX[i], rows[i].getAsString(BAR));
+ }
+ }
+
+
+ @Test
public void sortDirAsc() {
assertEquals("asc sort dir", SortDir.ASC, TableModel.sortDir("asc"));
}
diff --git a/core/api/src/test/java/org/onosproject/ui/table/DefaultCellComparatorTest.java b/core/api/src/test/java/org/onosproject/ui/table/cell/DefaultCellComparatorTest.java
similarity index 95%
rename from core/api/src/test/java/org/onosproject/ui/table/DefaultCellComparatorTest.java
rename to core/api/src/test/java/org/onosproject/ui/table/cell/DefaultCellComparatorTest.java
index 86add93..d4cd8ed 100644
--- a/core/api/src/test/java/org/onosproject/ui/table/DefaultCellComparatorTest.java
+++ b/core/api/src/test/java/org/onosproject/ui/table/cell/DefaultCellComparatorTest.java
@@ -15,9 +15,10 @@
*
*/
-package org.onosproject.ui.table;
+package org.onosproject.ui.table.cell;
import org.junit.Test;
+import org.onosproject.ui.table.CellComparator;
import static org.junit.Assert.assertTrue;
diff --git a/core/api/src/test/java/org/onosproject/ui/table/DefaultCellFormatterTest.java b/core/api/src/test/java/org/onosproject/ui/table/cell/DefaultCellFormatterTest.java
similarity index 95%
rename from core/api/src/test/java/org/onosproject/ui/table/DefaultCellFormatterTest.java
rename to core/api/src/test/java/org/onosproject/ui/table/cell/DefaultCellFormatterTest.java
index bbb0104..8934f48 100644
--- a/core/api/src/test/java/org/onosproject/ui/table/DefaultCellFormatterTest.java
+++ b/core/api/src/test/java/org/onosproject/ui/table/cell/DefaultCellFormatterTest.java
@@ -15,9 +15,10 @@
*
*/
-package org.onosproject.ui.table;
+package org.onosproject.ui.table.cell;
import org.junit.Test;
+import org.onosproject.ui.table.CellFormatter;
import static org.junit.Assert.assertEquals;
diff --git a/core/api/src/test/java/org/onosproject/ui/table/cell/HexFormatterTest.java b/core/api/src/test/java/org/onosproject/ui/table/cell/HexFormatterTest.java
new file mode 100644
index 0000000..738fbdd
--- /dev/null
+++ b/core/api/src/test/java/org/onosproject/ui/table/cell/HexFormatterTest.java
@@ -0,0 +1,56 @@
+/*
+ * 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 static org.junit.Assert.assertEquals;
+
+/**
+ * Unit tests for {@link HexFormatter}.
+ */
+public class HexFormatterTest {
+
+ private HexFormatter fmt = new HexFormatter();
+
+ @Test
+ public void nullValue() {
+ assertEquals("null value", "", fmt.format(null));
+ }
+
+ @Test
+ public void zero() {
+ assertEquals("zero", "0x0", fmt.format(0));
+ }
+
+ @Test
+ public void one() {
+ assertEquals("one", "0x1", fmt.format(1));
+ }
+
+ @Test
+ public void ten() {
+ assertEquals("ten", "0xa", fmt.format(10));
+ }
+
+ @Test
+ public void twenty() {
+ assertEquals("twenty", "0x14", fmt.format(20));
+ }
+
+}
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
new file mode 100644
index 0000000..2b45b3f
--- /dev/null
+++ b/core/api/src/test/java/org/onosproject/ui/table/cell/IntComparatorTest.java
@@ -0,0 +1,72 @@
+/*
+ * 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 = new IntComparator();
+
+ @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);
+ }
+}