Jian Li | b6859e1 | 2016-03-01 09:29:24 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 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 | package org.onosproject.ui.chart; |
| 17 | |
| 18 | import org.junit.Test; |
| 19 | |
| 20 | import static org.hamcrest.Matchers.is; |
| 21 | import static org.junit.Assert.assertArrayEquals; |
| 22 | import static org.junit.Assert.assertEquals; |
| 23 | import static org.junit.Assert.assertThat; |
| 24 | |
| 25 | /** |
| 26 | * Unit tests for {@link ChartModel}. |
| 27 | */ |
| 28 | public class ChartModelTest { |
| 29 | |
| 30 | private static final String FOO = "foo"; |
| 31 | private static final String BAR = "bar"; |
| 32 | private static final String ZOO = "zoo"; |
| 33 | |
| 34 | private static final Double[] VALUES1 = {0D, 1D, 2D}; |
| 35 | private static final Double[] VALUES2 = {3D, 4D, 5D}; |
| 36 | private static final Double[] VALUES3 = {6D, 7D, 8D}; |
| 37 | |
| 38 | private static final String[] SERIES = {FOO, BAR, ZOO}; |
| 39 | |
| 40 | private ChartModel cm; |
| 41 | |
| 42 | @Test(expected = NullPointerException.class) |
| 43 | public void guardAgainstNullSeries() { |
| 44 | cm = new ChartModel(1, null); |
| 45 | } |
| 46 | |
| 47 | @Test(expected = IllegalArgumentException.class) |
| 48 | public void guardAgainstWrongDpNumber() { |
| 49 | cm = new ChartModel(0, FOO); |
| 50 | } |
| 51 | |
| 52 | @Test |
| 53 | public void testSeriesCount() { |
| 54 | cm = new ChartModel(1, FOO, BAR, ZOO); |
| 55 | assertEquals("Wrong series count", 3, cm.seriesCount()); |
| 56 | } |
| 57 | |
| 58 | @Test |
| 59 | public void testAddDataPoint() { |
| 60 | cm = new ChartModel(2, FOO, BAR, ZOO); |
| 61 | |
| 62 | cm.addDataPoint("1", VALUES1); |
| 63 | cm.addDataPoint("2", VALUES2); |
| 64 | |
| 65 | assertEquals("Wrong result", "1", cm.getDataPoints()[0].getLabel()); |
| 66 | assertEquals("Wrong result", "2", cm.getDataPoints()[1].getLabel()); |
| 67 | |
| 68 | cm.addDataPoint("3", VALUES3); |
| 69 | |
| 70 | assertEquals("Wrong result", "2", cm.getDataPoints()[0].getLabel()); |
| 71 | assertEquals("Wrong result", "3", cm.getDataPoints()[1].getLabel()); |
| 72 | } |
| 73 | |
| 74 | @Test |
| 75 | public void testGetData() { |
| 76 | cm = new ChartModel(2, FOO, BAR, ZOO); |
| 77 | |
| 78 | cm.addDataPoint("1", VALUES1); |
| 79 | assertThat(cm.getLastDataPoint().getValue(ZOO), is(2D)); |
| 80 | |
| 81 | cm.addDataPoint("2", VALUES2); |
| 82 | assertThat(cm.getLastDataPoint().getValue(BAR), is(4D)); |
| 83 | } |
| 84 | |
| 85 | @Test |
| 86 | public void testGetSeries() { |
| 87 | cm = new ChartModel(1, FOO, BAR, ZOO); |
| 88 | |
| 89 | assertArrayEquals("series", SERIES, cm.getSeries()); |
| 90 | } |
| 91 | } |