1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
|
/* ===========================================================
* JFreeChart : a free chart library for the Java(tm) platform
* ===========================================================
*
* (C) Copyright 2000-2013, by Object Refinery Limited and Contributors.
*
* Project Info: http://www.jfree.org/jfreechart/index.html
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
* USA.
*
* [Oracle and Java are registered trademarks of Oracle and/or its affiliates.
* Other names may be trademarks of their respective owners.]
*
* -------------------------------
* ComparableObjectSeriesTest.java
* -------------------------------
* (C) Copyright 2006-2013, by Object Refinery Limited and Contributors.
*
* Original Author: David Gilbert (for Object Refinery Limited);
* Contributor(s): -;
*
* Changes
* -------
* 20-Oct-2006 : Version 1 (DG);
* 31-Oct-2007 : New hashCode() test (DG);
*
*/
package org.jfree.data;
import org.jfree.chart.TestUtilities;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertNull;
/**
* Tests for the {@link ComparableObjectSeries} class.
*/
public class ComparableObjectSeriesTest {
static class MyComparableObjectSeries extends ComparableObjectSeries {
/**
* Creates a new instance.
*
* @param key the series key.
*/
public MyComparableObjectSeries(Comparable key) {
super(key);
}
/**
* Creates a new instance.
*
* @param key the series key.
* @param autoSort automatically sort by x-value?
* @param allowDuplicateXValues allow duplicate values?
*/
public MyComparableObjectSeries(Comparable key, boolean autoSort,
boolean allowDuplicateXValues) {
super(key, autoSort, allowDuplicateXValues);
}
@Override
public void add(Comparable x, Object y) {
super.add(x, y);
}
@Override
public ComparableObjectItem remove(Comparable x) {
return super.remove(x);
}
}
/**
* Some checks for the constructor.
*/
@Test
public void testConstructor1() {
ComparableObjectSeries s1 = new ComparableObjectSeries("s1");
assertEquals("s1", s1.getKey());
assertNull(s1.getDescription());
assertTrue(s1.getAllowDuplicateXValues());
assertTrue(s1.getAutoSort());
assertEquals(0, s1.getItemCount());
assertEquals(Integer.MAX_VALUE, s1.getMaximumItemCount());
// try null key
boolean pass = false;
try {
/*s1 = */new ComparableObjectSeries(null);
}
catch (IllegalArgumentException e) {
pass = true;
}
assertTrue(pass);
}
/**
* Confirm that the equals method can distinguish all the required fields.
*/
@Test
public void testEquals() {
MyComparableObjectSeries s1 = new MyComparableObjectSeries("A");
MyComparableObjectSeries s2 = new MyComparableObjectSeries("A");
assertTrue(s1.equals(s2));
assertTrue(s2.equals(s1));
// key
s1 = new MyComparableObjectSeries("B");
assertNotEquals(s1, s2);
s2 = new MyComparableObjectSeries("B");
assertTrue(s1.equals(s2));
// autoSort
s1 = new MyComparableObjectSeries("B", false, true);
assertNotEquals(s1, s2);
s2 = new MyComparableObjectSeries("B", false, true);
assertTrue(s1.equals(s2));
// allowDuplicateXValues
s1 = new MyComparableObjectSeries("B", false, false);
assertNotEquals(s1, s2);
s2 = new MyComparableObjectSeries("B", false, false);
assertTrue(s1.equals(s2));
// add a value
s1.add(new Integer(1), "ABC");
assertNotEquals(s1, s2);
s2.add(new Integer(1), "ABC");
assertTrue(s1.equals(s2));
// add another value
s1.add(new Integer(0), "DEF");
assertNotEquals(s1, s2);
s2.add(new Integer(0), "DEF");
assertTrue(s1.equals(s2));
// remove an item
s1.remove(new Integer(1));
assertNotEquals(s1, s2);
s2.remove(new Integer(1));
assertTrue(s1.equals(s2));
}
/**
* Some checks for the clone() method.
*/
@Test
public void testCloning() throws CloneNotSupportedException {
MyComparableObjectSeries s1 = new MyComparableObjectSeries("A");
s1.add(new Integer(1), "ABC");
MyComparableObjectSeries s2 = (MyComparableObjectSeries) s1.clone();
assertTrue(s1 != s2);
assertTrue(s1.getClass() == s2.getClass());
assertTrue(s1.equals(s2));
}
/**
* Serialize an instance, restore it, and check for equality.
*/
@Test
public void testSerialization() {
MyComparableObjectSeries s1 = new MyComparableObjectSeries("A");
s1.add(new Integer(1), "ABC");
MyComparableObjectSeries s2 = (MyComparableObjectSeries)
TestUtilities.serialised(s1);
assertEquals(s1, s2);
}
/**
* Some simple checks for the hashCode() method.
*/
@Test
public void testHashCode() {
MyComparableObjectSeries s1 = new MyComparableObjectSeries("Test");
MyComparableObjectSeries s2 = new MyComparableObjectSeries("Test");
assertEquals(s1, s2);
assertEquals(s1.hashCode(), s2.hashCode());
s1.add("A", "1");
s2.add("A", "1");
assertEquals(s1, s2);
assertEquals(s1.hashCode(), s2.hashCode());
s1.add("B", null);
s2.add("B", null);
assertEquals(s1, s2);
assertEquals(s1.hashCode(), s2.hashCode());
s1.add("C", "3");
s2.add("C", "3");
assertEquals(s1, s2);
assertEquals(s1.hashCode(), s2.hashCode());
s1.add("D", "4");
s2.add("D", "4");
assertEquals(s1, s2);
assertEquals(s1.hashCode(), s2.hashCode());
}
}
|