1 42 43 package org.jfree.data.junit; 44 45 import java.io.ByteArrayInputStream ; 46 import java.io.ByteArrayOutputStream ; 47 import java.io.ObjectInput ; 48 import java.io.ObjectInputStream ; 49 import java.io.ObjectOutput ; 50 import java.io.ObjectOutputStream ; 51 52 import junit.framework.Test; 53 import junit.framework.TestCase; 54 import junit.framework.TestSuite; 55 56 import org.jfree.data.DefaultKeyedValues; 57 import org.jfree.util.SortOrder; 58 59 62 public class DefaultKeyedValuesTests extends TestCase { 63 64 69 public static Test suite() { 70 return new TestSuite(DefaultKeyedValuesTests.class); 71 } 72 73 78 public DefaultKeyedValuesTests(String name) { 79 super(name); 80 } 81 82 85 protected void setUp() { 86 } 88 89 92 public void testGetValue() { 93 DefaultKeyedValues v1 = new DefaultKeyedValues(); 94 try { 95 v1.getValue(-1); 96 assertTrue(false); 97 } 98 catch (IndexOutOfBoundsException e) { 99 } 101 try { 102 v1.getValue(0); 103 assertTrue(false); 104 } 105 catch (IndexOutOfBoundsException e) { 106 } 108 DefaultKeyedValues v2 = new DefaultKeyedValues(); 109 v2.addValue("K1", new Integer (1)); 110 v2.addValue("K2", new Integer (2)); 111 v2.addValue("K3", new Integer (3)); 112 assertEquals(new Integer (3), v2.getValue(2)); 113 } 114 115 118 public void testGetKey() { 119 DefaultKeyedValues v1 = new DefaultKeyedValues(); 120 try { 121 v1.getKey(-1); 122 assertTrue(false); 123 } 124 catch (IndexOutOfBoundsException e) { 125 } 127 try { 128 v1.getKey(0); 129 assertTrue(false); 130 } 131 catch (IndexOutOfBoundsException e) { 132 } 134 DefaultKeyedValues v2 = new DefaultKeyedValues(); 135 v2.addValue("K1", new Integer (1)); 136 v2.addValue("K2", new Integer (2)); 137 v2.addValue("K3", new Integer (3)); 138 assertEquals("K2", v2.getKey(1)); 139 } 140 141 144 public void testGetIndex() { 145 DefaultKeyedValues v1 = new DefaultKeyedValues(); 146 assertEquals(-1, v1.getIndex("K1")); 147 148 DefaultKeyedValues v2 = new DefaultKeyedValues(); 149 v2.addValue("K1", new Integer (1)); 150 v2.addValue("K2", new Integer (2)); 151 v2.addValue("K3", new Integer (3)); 152 assertEquals(2, v2.getIndex("K3")); 153 } 154 155 158 public void testCloning() { 159 DefaultKeyedValues v1 = new DefaultKeyedValues(); 160 v1.addValue("V1", new Integer (1)); 161 v1.addValue("V2", null); 162 v1.addValue("V3", new Integer (3)); 163 DefaultKeyedValues v2 = null; 164 try { 165 v2 = (DefaultKeyedValues) v1.clone(); 166 } 167 catch (CloneNotSupportedException e) { 168 System.err.println("Failed to clone."); 169 } 170 assertTrue(v1 != v2); 171 assertTrue(v1.getClass() == v2.getClass()); 172 assertTrue(v1.equals(v2)); 173 174 v2.setValue("V1", new Integer (44)); 176 assertFalse(v1.equals(v2)); 177 } 178 179 182 public void testInsertAndRetrieve() { 183 184 DefaultKeyedValues data = new DefaultKeyedValues(); 185 data.addValue("A", new Double (1.0)); 186 data.addValue("B", new Double (2.0)); 187 data.addValue("C", new Double (3.0)); 188 data.addValue("D", null); 189 190 assertEquals(data.getKey(0), "A"); 192 assertEquals(data.getKey(1), "B"); 193 assertEquals(data.getKey(2), "C"); 194 assertEquals(data.getKey(3), "D"); 195 196 assertEquals(data.getValue("A"), new Double (1.0)); 198 assertEquals(data.getValue("B"), new Double (2.0)); 199 assertEquals(data.getValue("C"), new Double (3.0)); 200 assertEquals(data.getValue("D"), null); 201 202 assertEquals(data.getValue(0), new Double (1.0)); 204 assertEquals(data.getValue(1), new Double (2.0)); 205 assertEquals(data.getValue(2), new Double (3.0)); 206 assertEquals(data.getValue(3), null); 207 208 } 209 210 213 public void testSortByKeyAscending() { 214 215 DefaultKeyedValues data = new DefaultKeyedValues(); 216 data.addValue("C", new Double (1.0)); 217 data.addValue("B", null); 218 data.addValue("D", new Double (3.0)); 219 data.addValue("A", new Double (2.0)); 220 221 data.sortByKeys(SortOrder.ASCENDING); 222 223 assertEquals(data.getKey(0), "A"); 225 assertEquals(data.getKey(1), "B"); 226 assertEquals(data.getKey(2), "C"); 227 assertEquals(data.getKey(3), "D"); 228 229 assertEquals(data.getValue("A"), new Double (2.0)); 231 assertEquals(data.getValue("B"), null); 232 assertEquals(data.getValue("C"), new Double (1.0)); 233 assertEquals(data.getValue("D"), new Double (3.0)); 234 235 assertEquals(data.getValue(0), new Double (2.0)); 237 assertEquals(data.getValue(1), null); 238 assertEquals(data.getValue(2), new Double (1.0)); 239 assertEquals(data.getValue(3), new Double (3.0)); 240 241 } 242 243 246 public void testSortByKeyDescending() { 247 248 DefaultKeyedValues data = new DefaultKeyedValues(); 249 data.addValue("C", new Double (1.0)); 250 data.addValue("B", null); 251 data.addValue("D", new Double (3.0)); 252 data.addValue("A", new Double (2.0)); 253 254 data.sortByKeys(SortOrder.DESCENDING); 255 256 assertEquals(data.getKey(0), "D"); 258 assertEquals(data.getKey(1), "C"); 259 assertEquals(data.getKey(2), "B"); 260 assertEquals(data.getKey(3), "A"); 261 262 assertEquals(data.getValue("A"), new Double (2.0)); 264 assertEquals(data.getValue("B"), null); 265 assertEquals(data.getValue("C"), new Double (1.0)); 266 assertEquals(data.getValue("D"), new Double (3.0)); 267 268 assertEquals(data.getValue(0), new Double (3.0)); 270 assertEquals(data.getValue(1), new Double (1.0)); 271 assertEquals(data.getValue(2), null); 272 assertEquals(data.getValue(3), new Double (2.0)); 273 274 } 275 276 279 public void testSortByValueAscending() { 280 281 DefaultKeyedValues data = new DefaultKeyedValues(); 282 data.addValue("C", new Double (1.0)); 283 data.addValue("B", null); 284 data.addValue("D", new Double (3.0)); 285 data.addValue("A", new Double (2.0)); 286 287 data.sortByValues(SortOrder.ASCENDING); 288 289 assertEquals(data.getKey(0), "C"); 291 assertEquals(data.getKey(1), "A"); 292 assertEquals(data.getKey(2), "D"); 293 assertEquals(data.getKey(3), "B"); 294 295 assertEquals(data.getValue("A"), new Double (2.0)); 297 assertEquals(data.getValue("B"), null); 298 assertEquals(data.getValue("C"), new Double (1.0)); 299 assertEquals(data.getValue("D"), new Double (3.0)); 300 301 assertEquals(data.getValue(0), new Double (1.0)); 303 assertEquals(data.getValue(1), new Double (2.0)); 304 assertEquals(data.getValue(2), new Double (3.0)); 305 assertEquals(data.getValue(3), null); 306 307 } 308 309 312 public void testSortByValueDescending() { 313 314 DefaultKeyedValues data = new DefaultKeyedValues(); 315 data.addValue("C", new Double (1.0)); 316 data.addValue("B", null); 317 data.addValue("D", new Double (3.0)); 318 data.addValue("A", new Double (2.0)); 319 320 data.sortByValues(SortOrder.DESCENDING); 321 322 assertEquals(data.getKey(0), "D"); 324 assertEquals(data.getKey(1), "A"); 325 assertEquals(data.getKey(2), "C"); 326 assertEquals(data.getKey(3), "B"); 327 328 assertEquals(data.getValue("A"), new Double (2.0)); 330 assertEquals(data.getValue("B"), null); 331 assertEquals(data.getValue("C"), new Double (1.0)); 332 assertEquals(data.getValue("D"), new Double (3.0)); 333 334 assertEquals(data.getValue(0), new Double (3.0)); 336 assertEquals(data.getValue(1), new Double (2.0)); 337 assertEquals(data.getValue(2), new Double (1.0)); 338 assertEquals(data.getValue(3), null); 339 340 } 341 342 345 public void testSerialization() { 346 347 DefaultKeyedValues v1 = new DefaultKeyedValues(); 348 v1.addValue("Key 1", new Double (23)); 349 v1.addValue("Key 2", null); 350 v1.addValue("Key 3", new Double (42)); 351 352 DefaultKeyedValues v2 = null; 353 354 try { 355 ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 356 ObjectOutput out = new ObjectOutputStream (buffer); 357 out.writeObject(v1); 358 out.close(); 359 360 ObjectInput in = new ObjectInputStream ( 361 new ByteArrayInputStream (buffer.toByteArray()) 362 ); 363 v2 = (DefaultKeyedValues) in.readObject(); 364 in.close(); 365 } 366 catch (Exception e) { 367 System.out.println(e.toString()); 368 } 369 assertEquals(v1, v2); 370 371 } 372 373 } 374 | Popular Tags |