diff options
author | Ashlee Young <ashlee@onosfw.com> | 2015-09-09 22:15:21 -0700 |
---|---|---|
committer | Ashlee Young <ashlee@onosfw.com> | 2015-09-09 22:15:21 -0700 |
commit | 13d05bc8458758ee39cb829098241e89616717ee (patch) | |
tree | 22a4d1ce65f15952f07a3df5af4b462b4697cb3a /framework/src/onos/core/api/src/test/java/org/onosproject/ui/table | |
parent | 6139282e1e93c2322076de4b91b1c85d0bc4a8b3 (diff) |
ONOS checkin based on commit tag e796610b1f721d02f9b0e213cf6f7790c10ecd60
Change-Id: Ife8810491034fe7becdba75dda20de4267bd15cd
Diffstat (limited to 'framework/src/onos/core/api/src/test/java/org/onosproject/ui/table')
12 files changed, 1022 insertions, 0 deletions
diff --git a/framework/src/onos/core/api/src/test/java/org/onosproject/ui/table/TableModelTest.java b/framework/src/onos/core/api/src/test/java/org/onosproject/ui/table/TableModelTest.java new file mode 100644 index 00000000..7524bcb3 --- /dev/null +++ b/framework/src/onos/core/api/src/test/java/org/onosproject/ui/table/TableModelTest.java @@ -0,0 +1,338 @@ +/* + * 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; + +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 static org.junit.Assert.*; + +/** + * Unit tests for {@link TableModel}. + */ +public class TableModelTest { + + 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 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) { + return "(" + value + ")"; + } + } + + private TableModel tm; + private TableModel.Row[] rows; + private TableModel.Row row; + private CellFormatter fmt; + + @Test(expected = NullPointerException.class) + public void guardAgainstNull() { + tm = new TableModel(null); + } + + @Test(expected = IllegalArgumentException.class) + public void guardAgainstEmpty() { + tm = new TableModel(); + } + + @Test(expected = IllegalArgumentException.class) + public void guardAgainstDuplicateCols() { + tm = new TableModel(FOO, BAR, FOO); + } + + @Test + public void basic() { + tm = new TableModel(FOO, BAR); + assertEquals("column count", 2, tm.columnCount()); + assertEquals("row count", 0, tm.rowCount()); + } + + @Test + public void defaultFormatter() { + tm = new TableModel(FOO); + fmt = tm.getFormatter(FOO); + assertTrue("Wrong formatter", fmt instanceof DefaultCellFormatter); + } + + @Test(expected = IllegalArgumentException.class) + public void formatterBadColumn() { + tm = new TableModel(FOO); + fmt = tm.getFormatter(BAR); + } + + @Test + public void altFormatter() { + tm = new TableModel(FOO, BAR); + 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 ParenFormatter); + assertEquals("Wrong result", "(2)", fmt.format(2)); + } + + @Test + public void emptyRow() { + tm = new TableModel(FOO, BAR); + tm.addRow(); + assertEquals("bad row count", 1, tm.rowCount()); + } + + @Test(expected = IllegalArgumentException.class) + public void rowBadColumn() { + tm = new TableModel(FOO, BAR); + tm.addRow().cell(ZOO, 2); + } + + @Test + public void simpleRow() { + tm = new TableModel(FOO, BAR); + tm.addRow().cell(FOO, 3).cell(BAR, true); + assertEquals("bad row count", 1, tm.rowCount()); + row = tm.getRows()[0]; + assertEquals("bad cell", 3, row.get(FOO)); + assertEquals("bad cell", true, row.get(BAR)); + } + + + private static final String ONE = "one"; + private static final String TWO = "two"; + private static final String THREE = "three"; + private static final String FOUR = "four"; + private static final String ELEVEN = "eleven"; + private static final String TWELVE = "twelve"; + private static final String TWENTY = "twenty"; + private static final String THIRTY = "thirty"; + + private static final String[] NAMES = { + FOUR, + THREE, + TWO, + ONE, + ELEVEN, + TWELVE, + THIRTY, + TWENTY, + }; + private static final String[] SORTED_NAMES = { + ELEVEN, + FOUR, + ONE, + THIRTY, + THREE, + TWELVE, + TWENTY, + TWO, + }; + + private static final int[] NUMBERS = { + 4, 3, 2, 1, 11, 12, 30, 20 + }; + + private static final int[] SORTED_NUMBERS = { + 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 + // the three test arrays in sync + int nalen = NAMES.length; + int snlen = SORTED_NAMES.length; + int nulen = NUMBERS.length; + + if (nalen != snlen || nalen != nulen) { + fail("test data array size discrepancy"); + } + } + + private void initUnsortedTable() { + tm = new TableModel(FOO, BAR); + for (int i = 0; i < NAMES.length; i++) { + tm.addRow().cell(FOO, NAMES[i]).cell(BAR, NUMBERS[i]); + } + } + + @Test + public void tableStringSort() { + initUnsortedTable(); + + // sort by name + tm.sort(FOO, SortDir.ASC); + + // verify results + rows = tm.getRows(); + int nr = rows.length; + assertEquals("row count", NAMES.length, nr); + for (int i = 0; i < nr; i++) { + assertEquals(UNEX_SORT + i, SORTED_NAMES[i], rows[i].get(FOO)); + } + + // now the other way + tm.sort(FOO, SortDir.DESC); + + // verify results + rows = tm.getRows(); + nr = rows.length; + assertEquals("row count", NAMES.length, nr); + for (int i = 0; i < nr; i++) { + assertEquals(UNEX_SORT + i, + SORTED_NAMES[nr - 1 - i], rows[i].get(FOO)); + } + } + + @Test + public void tableNumberSort() { + initUnsortedTable(); + + // sort by number + tm.sort(BAR, SortDir.ASC); + + // verify results + rows = tm.getRows(); + int nr = rows.length; + assertEquals("row count", NUMBERS.length, nr); + for (int i = 0; i < nr; i++) { + assertEquals(UNEX_SORT + i, SORTED_NUMBERS[i], rows[i].get(BAR)); + } + + // now the other way + tm.sort(BAR, SortDir.DESC); + + // verify results + rows = tm.getRows(); + nr = rows.length; + assertEquals("row count", NUMBERS.length, nr); + for (int i = 0; i < nr; i++) { + assertEquals(UNEX_SORT + i, + SORTED_NUMBERS[nr - 1 - i], rows[i].get(BAR)); + } + } + + @Test + public void sortAndFormat() { + initUnsortedTable(); + + // set hex formatter + tm.setFormatter(BAR, HexFormatter.INSTANCE); + + // 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)); + } + } + + private static final String[][] SORTED_NAMES_AND_HEX = { + {ELEVEN, "0xb"}, + {FOUR, "0x4"}, + {ONE, "0x1"}, + {THIRTY, "0x1e"}, + {THREE, "0x3"}, + {TWELVE, "0xc"}, + {TWENTY, "0x14"}, + {TWO, "0x2"}, + }; + + @Test + public void sortAndFormatTwo() { + initUnsortedTable(); + tm.setFormatter(BAR, HexFormatter.INSTANCE); + tm.sort(FOO, SortDir.ASC); + rows = tm.getRows(); + int nr = rows.length; + for (int i = 0; i < nr; i++) { + String[] exp = SORTED_NAMES_AND_HEX[i]; + String[] act = rows[i].getAsFormattedStrings(); + assertArrayEquals(UNEX_SORT + i, exp, act); + } + } + + private static final String[] FBZ = {FOO, BAR, ZOO}; + + @Test + public void getColumnIds() { + tm = new TableModel(FOO, BAR, ZOO); + assertArrayEquals("col IDs", FBZ, tm.getColumnIds()); + } + + @Test + public void sortDirAsc() { + assertEquals("asc sort dir", SortDir.ASC, TableModel.sortDir("asc")); + } + + @Test + public void sortDirDesc() { + assertEquals("desc sort dir", SortDir.DESC, TableModel.sortDir("desc")); + } + + @Test + public void sortDirOther() { + assertEquals("other sort dir", SortDir.ASC, TableModel.sortDir("other")); + } + + @Test + public void sortDirNull() { + 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/framework/src/onos/core/api/src/test/java/org/onosproject/ui/table/TableUtilsTest.java b/framework/src/onos/core/api/src/test/java/org/onosproject/ui/table/TableUtilsTest.java new file mode 100644 index 00000000..7c2f2d73 --- /dev/null +++ b/framework/src/onos/core/api/src/test/java/org/onosproject/ui/table/TableUtilsTest.java @@ -0,0 +1,45 @@ +/* + * 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; + +import com.fasterxml.jackson.databind.node.ArrayNode; +import org.junit.Assert; +import org.junit.Test; + +/** + * Unit tests for {@link TableUtils}. + */ +public class TableUtilsTest { + + private static final String FOO = "foo"; + private static final String BAR = "bar"; + + private static final String ARRAY_AS_STRING = + "[{\"foo\":\"1\",\"bar\":\"2\"},{\"foo\":\"3\",\"bar\":\"4\"}]"; + + @Test + public void basic() { + TableModel tm = new TableModel(FOO, BAR); + tm.addRow().cell(FOO, 1).cell(BAR, 2); + tm.addRow().cell(FOO, 3).cell(BAR, 4); + + ArrayNode array = TableUtils.generateArrayNode(tm); + Assert.assertEquals("wrong results", ARRAY_AS_STRING, array.toString()); + } + +} diff --git a/framework/src/onos/core/api/src/test/java/org/onosproject/ui/table/cell/AbstractCellComparatorTest.java b/framework/src/onos/core/api/src/test/java/org/onosproject/ui/table/cell/AbstractCellComparatorTest.java new file mode 100644 index 00000000..0b9af23d --- /dev/null +++ b/framework/src/onos/core/api/src/test/java/org/onosproject/ui/table/cell/AbstractCellComparatorTest.java @@ -0,0 +1,59 @@ +/* + * 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 AbstractCellComparator}. + */ +public class AbstractCellComparatorTest { + + private static class Concrete extends AbstractCellComparator { + @Override + protected int nonNullCompare(Object o1, Object o2) { + return 42; + } + } + + private CellComparator cmp = new Concrete(); + + @Test + public void twoNullArgs() { + assertTrue("two nulls", cmp.compare(null, null) == 0); + } + + @Test + public void nullArgOne() { + assertTrue("null one", cmp.compare(null, 1) < 0); + } + + @Test + public void nullArgTwo() { + assertTrue("null two", cmp.compare(1, null) > 0); + } + + // mock output, but check that our method was invoked... + @Test + public void noNulls() { + assertTrue("no Nulls", cmp.compare(1, 2) == 42); + } +} diff --git a/framework/src/onos/core/api/src/test/java/org/onosproject/ui/table/cell/AbstractCellFormatterTest.java b/framework/src/onos/core/api/src/test/java/org/onosproject/ui/table/cell/AbstractCellFormatterTest.java new file mode 100644 index 00000000..7a3c34bc --- /dev/null +++ b/framework/src/onos/core/api/src/test/java/org/onosproject/ui/table/cell/AbstractCellFormatterTest.java @@ -0,0 +1,52 @@ +/* + * 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.CellFormatter; + +import static org.junit.Assert.assertEquals; + +/** + * Unit tests for {@link AbstractCellFormatter}. + */ +public class AbstractCellFormatterTest { + + private static final String MOCK_OUTPUT = "Mock!!"; + + private static class Concrete extends AbstractCellFormatter { + @Override + protected String nonNullFormat(Object value) { + return MOCK_OUTPUT; + } + } + + private CellFormatter frm = new Concrete(); + + @Test + public void nullInput() { + assertEquals("wrong result", "", frm.format(null)); + } + + // mock output, but check that our method was invoked... + @Test + public void nonNullInput() { + assertEquals("what?", MOCK_OUTPUT, frm.format(1)); + } + +} diff --git a/framework/src/onos/core/api/src/test/java/org/onosproject/ui/table/cell/AppIdFormatterTest.java b/framework/src/onos/core/api/src/test/java/org/onosproject/ui/table/cell/AppIdFormatterTest.java new file mode 100644 index 00000000..e74fb47c --- /dev/null +++ b/framework/src/onos/core/api/src/test/java/org/onosproject/ui/table/cell/AppIdFormatterTest.java @@ -0,0 +1,50 @@ +/* + * 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.core.ApplicationId; +import org.onosproject.ui.table.CellFormatter; + +import static org.junit.Assert.assertEquals; + +/** + * Unit tests for {@link AppIdFormatter}. + */ +public class AppIdFormatterTest { + + private static final ApplicationId APP_ID = new ApplicationId() { + @Override + public short id() { + return 25; + } + + @Override + public String name() { + return "some app"; + } + }; + + private CellFormatter fmt = AppIdFormatter.INSTANCE; + + @Test + public void basic() { + assertEquals("wrong format", "25 : some app", fmt.format(APP_ID)); + } + +} diff --git a/framework/src/onos/core/api/src/test/java/org/onosproject/ui/table/cell/ConnectPointFormatterTest.java b/framework/src/onos/core/api/src/test/java/org/onosproject/ui/table/cell/ConnectPointFormatterTest.java new file mode 100644 index 00000000..65fd7843 --- /dev/null +++ b/framework/src/onos/core/api/src/test/java/org/onosproject/ui/table/cell/ConnectPointFormatterTest.java @@ -0,0 +1,45 @@ +/* + * 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.net.ConnectPoint; +import org.onosproject.net.DeviceId; +import org.onosproject.net.PortNumber; +import org.onosproject.ui.table.CellFormatter; + +import static org.junit.Assert.assertEquals; + +/** + * Unit tests for {@link ConnectPointFormatter}. + */ +public class ConnectPointFormatterTest { + + private static final DeviceId DEVID = DeviceId.deviceId("foobar"); + private static final PortNumber PORT = PortNumber.portNumber(42); + + private static final ConnectPoint CP = new ConnectPoint(DEVID, PORT); + + private CellFormatter fmt = ConnectPointFormatter.INSTANCE; + + @Test + public void basic() { + assertEquals("wrong format", "foobar/42", fmt.format(CP)); + } + +} diff --git a/framework/src/onos/core/api/src/test/java/org/onosproject/ui/table/cell/DefaultCellComparatorTest.java b/framework/src/onos/core/api/src/test/java/org/onosproject/ui/table/cell/DefaultCellComparatorTest.java new file mode 100644 index 00000000..87c95288 --- /dev/null +++ b/framework/src/onos/core/api/src/test/java/org/onosproject/ui/table/cell/DefaultCellComparatorTest.java @@ -0,0 +1,147 @@ +/* + * 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 DefaultCellComparator}. + */ +public class DefaultCellComparatorTest { + + private static final String SOME = "SoMeStRiNg"; + private static final String OTHER = "OtherSTRING"; + + 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); + } + + @Test + public void someVsOther() { + assertTrue("some vs other", cmp.compare(SOME, OTHER) > 0); + } + + @Test + public void otherVsSome() { + assertTrue("other vs some", cmp.compare(OTHER, SOME) < 0); + } + + @Test + public void someVsNull() { + assertTrue("some vs null", cmp.compare(SOME, null) > 0); + } + + @Test + public void nullVsSome() { + 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/framework/src/onos/core/api/src/test/java/org/onosproject/ui/table/cell/DefaultCellFormatterTest.java b/framework/src/onos/core/api/src/test/java/org/onosproject/ui/table/cell/DefaultCellFormatterTest.java new file mode 100644 index 00000000..6351a1f4 --- /dev/null +++ b/framework/src/onos/core/api/src/test/java/org/onosproject/ui/table/cell/DefaultCellFormatterTest.java @@ -0,0 +1,71 @@ +/* + * 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.CellFormatter; + +import static org.junit.Assert.assertEquals; + +/** + * Unit tests for {@link DefaultCellFormatter}. + */ +public class DefaultCellFormatterTest { + + private static final String UNEX = "Unexpected result"; + private static final String SOME_STRING = "SoMeStRiNg"; + + private static class TestClass { + @Override + public String toString() { + return SOME_STRING; + } + } + + private CellFormatter fmt = DefaultCellFormatter.INSTANCE; + + @Test + public void formatNull() { + assertEquals(UNEX, "", fmt.format(null)); + } + + @Test + public void formatInteger() { + assertEquals(UNEX, "3", fmt.format(3)); + } + + @Test + public void formatTrue() { + assertEquals(UNEX, "true", fmt.format(true)); + } + + @Test + public void formatFalse() { + assertEquals(UNEX, "false", fmt.format(false)); + } + + @Test + public void formatString() { + assertEquals(UNEX, "FOO", fmt.format("FOO")); + } + + @Test + public void formatObject() { + assertEquals(UNEX, SOME_STRING, fmt.format(new TestClass())); + } +} diff --git a/framework/src/onos/core/api/src/test/java/org/onosproject/ui/table/cell/EnumFormatterTest.java b/framework/src/onos/core/api/src/test/java/org/onosproject/ui/table/cell/EnumFormatterTest.java new file mode 100644 index 00000000..68833c90 --- /dev/null +++ b/framework/src/onos/core/api/src/test/java/org/onosproject/ui/table/cell/EnumFormatterTest.java @@ -0,0 +1,60 @@ +/* + * 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.CellFormatter; + +import static org.junit.Assert.assertEquals; + +/** + * Unit tests for {@link EnumFormatter}. + */ +public class EnumFormatterTest { + + enum TestEnum { + ADDED, + PENDING_ADD, + WAITING_AUDIT_COMPLETE + } + + private CellFormatter fmt = EnumFormatter.INSTANCE; + + @Test + public void nullValue() { + assertEquals("null value", "", fmt.format(null)); + } + + @Test + public void noUnderscores() { + assertEquals("All caps", "Added", fmt.format(TestEnum.ADDED)); + } + + @Test + public void underscores() { + assertEquals("All caps with underscores", + "Pending Add", fmt.format(TestEnum.PENDING_ADD)); + } + + @Test + public void multiUnderscores() { + assertEquals("All caps with underscores", + "Waiting Audit Complete", + fmt.format(TestEnum.WAITING_AUDIT_COMPLETE)); + } + +} diff --git a/framework/src/onos/core/api/src/test/java/org/onosproject/ui/table/cell/HexFormatterTest.java b/framework/src/onos/core/api/src/test/java/org/onosproject/ui/table/cell/HexFormatterTest.java new file mode 100644 index 00000000..ad23b02c --- /dev/null +++ b/framework/src/onos/core/api/src/test/java/org/onosproject/ui/table/cell/HexFormatterTest.java @@ -0,0 +1,57 @@ +/* + * 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.CellFormatter; + +import static org.junit.Assert.assertEquals; + +/** + * Unit tests for {@link HexFormatter}. + */ +public class HexFormatterTest { + + private CellFormatter fmt = HexFormatter.INSTANCE; + + @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/framework/src/onos/core/api/src/test/java/org/onosproject/ui/table/cell/HostLocationFormatterTest.java b/framework/src/onos/core/api/src/test/java/org/onosproject/ui/table/cell/HostLocationFormatterTest.java new file mode 100644 index 00000000..bfbe4541 --- /dev/null +++ b/framework/src/onos/core/api/src/test/java/org/onosproject/ui/table/cell/HostLocationFormatterTest.java @@ -0,0 +1,46 @@ +/* + * 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.net.DeviceId; +import org.onosproject.net.HostLocation; +import org.onosproject.net.PortNumber; +import org.onosproject.ui.table.CellFormatter; + +import static org.junit.Assert.assertEquals; + +/** + * Unit tests for {@link HostLocationFormatter}. + */ +public class HostLocationFormatterTest { + + private static final DeviceId DEVID = DeviceId.deviceId("foobar"); + private static final PortNumber PORT = PortNumber.portNumber(42); + private static final long TIME = 12345; + + private static final HostLocation LOC = new HostLocation(DEVID, PORT, TIME); + + private CellFormatter fmt = HostLocationFormatter.INSTANCE; + + @Test + public void basic() { + assertEquals("wrong format", "foobar/42", fmt.format(LOC)); + } + +} diff --git a/framework/src/onos/core/api/src/test/java/org/onosproject/ui/table/cell/TimeFormatterTest.java b/framework/src/onos/core/api/src/test/java/org/onosproject/ui/table/cell/TimeFormatterTest.java new file mode 100644 index 00000000..f41d82b3 --- /dev/null +++ b/framework/src/onos/core/api/src/test/java/org/onosproject/ui/table/cell/TimeFormatterTest.java @@ -0,0 +1,52 @@ +/* + * 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.joda.time.DateTime; +import org.joda.time.DateTimeZone; +import org.junit.Test; +import org.onosproject.ui.table.CellFormatter; + +import java.util.Locale; + +import static org.junit.Assert.assertTrue; + +/** + * Unit tests for {@link TimeFormatter}. + */ +public class TimeFormatterTest { + + private static final Locale LOCALE = Locale.ENGLISH; + private static final DateTimeZone ZONE = DateTimeZone.UTC; + + private static final DateTime TIME = new DateTime(2015, 5, 4, 15, 30, ZONE); + private static final String EXP_ZONE_NAME = "3:30:00 PM UTC"; + private static final String EXP_ZONE_OFFSET = "3:30:00 PM +00:00"; + + // Have to use explicit Locale and TimeZone for the unit test, so that + // irrespective of which locale and time zone the test is run in, it + // always produces the same result... + private CellFormatter fmt = + new TimeFormatter().withLocale(LOCALE).withZone(ZONE); + + @Test + public void basic() { + assertTrue("wrong format", (EXP_ZONE_NAME.equals(fmt.format(TIME)) || + EXP_ZONE_OFFSET.equals(fmt.format(TIME)))); + } +} |