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 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
|
/* ===========================================================
* 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.]
*
* ---------------------
* KeyedObjectsTest.java
* ---------------------
* (C) Copyright 2004-2013, by Object Refinery Limited.
*
* Original Author: David Gilbert (for Object Refinery Limited);
* Contributor(s): -;
*
* Changes
* -------
* 27-Jan-2004 : Version 1 (DG);
* 28-Sep-2007 : Added testCloning2() (DG);
* 03-Oct-2007 : New tests (DG);
*
*/
package org.jfree.data;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertNull;
import java.util.ArrayList;
import org.jfree.chart.TestUtilities;
import org.jfree.data.general.DefaultPieDataset;
import org.junit.Test;
/**
* Tests for the {@link KeyedObjects} class.
*/
public class KeyedObjectsTest {
/**
* Confirm that cloning works.
*/
@Test
public void testCloning() throws CloneNotSupportedException {
KeyedObjects ko1 = new KeyedObjects();
ko1.addObject("V1", new Integer(1));
ko1.addObject("V2", null);
ko1.addObject("V3", new Integer(3));
KeyedObjects ko2 = (KeyedObjects) ko1.clone();
assertTrue(ko1 != ko2);
assertTrue(ko1.getClass() == ko2.getClass());
assertTrue(ko1.equals(ko2));
}
/**
* Confirm special features of cloning.
*/
@Test
public void testCloning2() throws CloneNotSupportedException {
// case 1 - object is mutable but not PublicCloneable
Object obj1 = new ArrayList();
KeyedObjects ko1 = new KeyedObjects();
ko1.addObject("K1", obj1);
KeyedObjects ko2 = (KeyedObjects) ko1.clone();
assertTrue(ko1 != ko2);
assertTrue(ko1.getClass() == ko2.getClass());
assertTrue(ko1.equals(ko2));
// the clone contains a reference to the original object
assertTrue(ko2.getObject("K1") == obj1);
// CASE 2 - object is mutable AND PublicCloneable
obj1 = new DefaultPieDataset();
ko1 = new KeyedObjects();
ko1.addObject("K1", obj1);
ko2 = (KeyedObjects) ko1.clone();
assertTrue(ko1 != ko2);
assertTrue(ko1.getClass() == ko2.getClass());
assertTrue(ko1.equals(ko2));
// the clone contains a reference to a CLONE of the original object
assertTrue(ko2.getObject("K1") != obj1);
}
/**
* Check that inserting and retrieving values works as expected.
*/
@Test
public void testInsertAndRetrieve() {
KeyedObjects data = new KeyedObjects();
data.addObject("A", new Double(1.0));
data.addObject("B", new Double(2.0));
data.addObject("C", new Double(3.0));
data.addObject("D", null);
// check key order
assertEquals(data.getKey(0), "A");
assertEquals(data.getKey(1), "B");
assertEquals(data.getKey(2), "C");
assertEquals(data.getKey(3), "D");
// check retrieve value by key
assertEquals(data.getObject("A"), new Double(1.0));
assertEquals(data.getObject("B"), new Double(2.0));
assertEquals(data.getObject("C"), new Double(3.0));
assertEquals(data.getObject("D"), null);
boolean pass = false;
try {
data.getObject("Not a key");
}
catch (UnknownKeyException e) {
pass = true;
}
assertTrue(pass);
// check retrieve value by index
assertEquals(data.getObject(0), new Double(1.0));
assertEquals(data.getObject(1), new Double(2.0));
assertEquals(data.getObject(2), new Double(3.0));
assertEquals(data.getObject(3), null);
}
/**
* Serialize an instance, restore it, and check for equality.
*/
@Test
public void testSerialization() {
KeyedObjects ko1 = new KeyedObjects();
ko1.addObject("Key 1", "Object 1");
ko1.addObject("Key 2", null);
ko1.addObject("Key 3", "Object 2");
KeyedObjects ko2 = (KeyedObjects) TestUtilities.serialised(ko1);
assertEquals(ko1, ko2);
}
/**
* Simple checks for the getObject(int) method.
*/
@Test
public void testGetObject() {
// retrieve an item
KeyedObjects ko1 = new KeyedObjects();
ko1.addObject("Key 1", "Object 1");
ko1.addObject("Key 2", null);
ko1.addObject("Key 3", "Object 2");
assertEquals("Object 1", ko1.getObject(0));
assertNull(ko1.getObject(1));
assertEquals("Object 2", ko1.getObject(2));
// request with a negative index
boolean pass = false;
try {
ko1.getObject(-1);
}
catch (IndexOutOfBoundsException e) {
pass = true;
}
assertTrue(pass);
// request width index == itemCount
pass = false;
try {
ko1.getObject(3);
}
catch (IndexOutOfBoundsException e) {
pass = true;
}
assertTrue(pass);
}
/**
* Simple checks for the getKey(int) method.
*/
@Test
public void testGetKey() {
// retrieve an item
KeyedObjects ko1 = new KeyedObjects();
ko1.addObject("Key 1", "Object 1");
ko1.addObject("Key 2", null);
ko1.addObject("Key 3", "Object 2");
assertEquals("Key 1", ko1.getKey(0));
assertEquals("Key 2", ko1.getKey(1));
assertEquals("Key 3", ko1.getKey(2));
// request with a negative index
boolean pass = false;
try {
ko1.getKey(-1);
}
catch (IndexOutOfBoundsException e) {
pass = true;
}
assertTrue(pass);
// request width index == itemCount
pass = false;
try {
ko1.getKey(3);
}
catch (IndexOutOfBoundsException e) {
pass = true;
}
assertTrue(pass);
}
/**
* Simple checks for the getIndex(Comparable) method.
*/
@Test
public void testGetIndex() {
KeyedObjects ko1 = new KeyedObjects();
ko1.addObject("Key 1", "Object 1");
ko1.addObject("Key 2", null);
ko1.addObject("Key 3", "Object 2");
assertEquals(0, ko1.getIndex("Key 1"));
assertEquals(1, ko1.getIndex("Key 2"));
assertEquals(2, ko1.getIndex("Key 3"));
// check null argument
boolean pass = false;
try {
ko1.getIndex(null);
}
catch (IllegalArgumentException e) {
pass = true;
}
assertTrue(pass);
}
/**
* Some checks for the setObject(Comparable, Object) method.
*/
@Test
public void testSetObject() {
KeyedObjects ko1 = new KeyedObjects();
ko1.setObject("Key 1", "Object 1");
ko1.setObject("Key 2", null);
ko1.setObject("Key 3", "Object 2");
assertEquals("Object 1", ko1.getObject("Key 1"));
assertEquals(null, ko1.getObject("Key 2"));
assertEquals("Object 2", ko1.getObject("Key 3"));
// replace an existing value
ko1.setObject("Key 2", "AAA");
ko1.setObject("Key 3", "BBB");
assertEquals("AAA", ko1.getObject("Key 2"));
assertEquals("BBB", ko1.getObject("Key 3"));
// try a null key - should throw an exception
boolean pass = false;
try {
ko1.setObject(null, "XX");
}
catch (IllegalArgumentException e) {
pass = true;
}
assertTrue(pass);
}
/**
* Some checks for the removeValue() methods.
*/
@Test
public void testRemoveValue() {
KeyedObjects ko1 = new KeyedObjects();
ko1.setObject("Key 1", "Object 1");
ko1.setObject("Key 2", null);
ko1.setObject("Key 3", "Object 2");
ko1.removeValue(1);
assertEquals(2, ko1.getItemCount());
assertEquals(1, ko1.getIndex("Key 3"));
ko1.removeValue("Key 1");
assertEquals(1, ko1.getItemCount());
assertEquals(0, ko1.getIndex("Key 3"));
// try unknown key
boolean pass = false;
try {
ko1.removeValue("UNKNOWN");
}
catch (UnknownKeyException e) {
pass = true;
}
assertTrue(pass);
// try null argument
pass = false;
try {
ko1.removeValue(null);
}
catch (IllegalArgumentException e) {
pass = true;
}
assertTrue(pass);
}
/**
* Some checks for the removeValue(int) method.
*/
@Test
public void testRemoveValueInt() {
KeyedObjects ko1 = new KeyedObjects();
ko1.setObject("Key 1", "Object 1");
ko1.setObject("Key 2", null);
ko1.setObject("Key 3", "Object 2");
ko1.removeValue(1);
assertEquals(2, ko1.getItemCount());
assertEquals(1, ko1.getIndex("Key 3"));
// try negative key index
boolean pass = false;
try {
ko1.removeValue(-1);
}
catch (IndexOutOfBoundsException e) {
pass = true;
}
assertTrue(pass);
// try key index == itemCount
pass = false;
try {
ko1.removeValue(2);
}
catch (IndexOutOfBoundsException e) {
pass = true;
}
assertTrue(pass);
}
}
|