001: /* ===========================================================
002: * JFreeChart : a free chart library for the Java(tm) platform
003: * ===========================================================
004: *
005: * (C) Copyright 2000-2007, by Object Refinery Limited and Contributors.
006: *
007: * Project Info: http://www.jfree.org/jfreechart/index.html
008: *
009: * This library is free software; you can redistribute it and/or modify it
010: * under the terms of the GNU Lesser General Public License as published by
011: * the Free Software Foundation; either version 2.1 of the License, or
012: * (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful, but
015: * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
016: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
017: * License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library; if not, write to the Free Software
021: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
022: * USA.
023: *
024: * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
025: * in the United States and other countries.]
026: *
027: * ----------------------------
028: * DefaultKeyedValuesTests.java
029: * ----------------------------
030: * (C) Copyright 2003-2007, by Object Refinery Limited.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: DefaultKeyedValuesTests.java,v 1.1.2.2 2007/04/30 15:28:04 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 05-Mar-2003 : Version 1 (DG);
040: * 27-Aug-2003 : Moved SortOrder from org.jfree.data --> org.jfree.util (DG);
041: * 31-Jul-2006 : Added test for new clear() method (DG);
042: * 01-Aug-2006 : Extended testGetIndex() method (DG);
043: * 30-Apr-2007 : Added some new tests (DG);
044: *
045: */
046:
047: package org.jfree.data.junit;
048:
049: import java.io.ByteArrayInputStream;
050: import java.io.ByteArrayOutputStream;
051: import java.io.ObjectInput;
052: import java.io.ObjectInputStream;
053: import java.io.ObjectOutput;
054: import java.io.ObjectOutputStream;
055: import java.util.List;
056:
057: import junit.framework.Test;
058: import junit.framework.TestCase;
059: import junit.framework.TestSuite;
060:
061: import org.jfree.data.DefaultKeyedValues;
062: import org.jfree.data.UnknownKeyException;
063: import org.jfree.util.SortOrder;
064:
065: /**
066: * Tests for the {@link DefaultKeyedValues} class.
067: */
068: public class DefaultKeyedValuesTests extends TestCase {
069:
070: /**
071: * Returns the tests as a test suite.
072: *
073: * @return The test suite.
074: */
075: public static Test suite() {
076: return new TestSuite(DefaultKeyedValuesTests.class);
077: }
078:
079: /**
080: * Constructs a new set of tests.
081: *
082: * @param name the name of the tests.
083: */
084: public DefaultKeyedValuesTests(String name) {
085: super (name);
086: }
087:
088: /**
089: * Common test setup.
090: */
091: protected void setUp() {
092: // no setup required
093: }
094:
095: /**
096: * Checks that a new instance is empty.
097: */
098: public void testConstructor() {
099: DefaultKeyedValues d = new DefaultKeyedValues();
100: assertEquals(0, d.getItemCount());
101: }
102:
103: /**
104: * Some checks for the getItemCount() method.
105: */
106: public void testGetItemCount() {
107: DefaultKeyedValues d = new DefaultKeyedValues();
108: assertEquals(0, d.getItemCount());
109: d.addValue("A", 1.0);
110: assertEquals(1, d.getItemCount());
111: d.addValue("B", 2.0);
112: assertEquals(2, d.getItemCount());
113: d.clear();
114: assertEquals(0, d.getItemCount());
115: }
116:
117: /**
118: * Some checks for the getKeys() method.
119: */
120: public void testGetKeys() {
121: DefaultKeyedValues d = new DefaultKeyedValues();
122: List keys = d.getKeys();
123: assertTrue(keys.isEmpty());
124: d.addValue("A", 1.0);
125: keys = d.getKeys();
126: assertEquals(1, keys.size());
127: assertTrue(keys.contains("A"));
128: d.addValue("B", 2.0);
129: keys = d.getKeys();
130: assertEquals(2, keys.size());
131: assertTrue(keys.contains("A"));
132: assertTrue(keys.contains("B"));
133: d.clear();
134: keys = d.getKeys();
135: assertEquals(0, keys.size());
136: }
137:
138: /**
139: * A simple test for the clear() method.
140: */
141: public void testClear() {
142: DefaultKeyedValues v1 = new DefaultKeyedValues();
143: v1.addValue("A", 1.0);
144: v1.addValue("B", 2.0);
145: assertEquals(2, v1.getItemCount());
146: v1.clear();
147: assertEquals(0, v1.getItemCount());
148: }
149:
150: /**
151: * Some checks for the getValue() methods.
152: */
153: public void testGetValue() {
154: DefaultKeyedValues v1 = new DefaultKeyedValues();
155: try {
156: /* Number n = */v1.getValue(-1);
157: assertTrue(false);
158: } catch (IndexOutOfBoundsException e) {
159: // expected
160: }
161: try {
162: /* Number n = */v1.getValue(0);
163: assertTrue(false);
164: } catch (IndexOutOfBoundsException e) {
165: // expected
166: }
167: DefaultKeyedValues v2 = new DefaultKeyedValues();
168: v2.addValue("K1", new Integer(1));
169: v2.addValue("K2", new Integer(2));
170: v2.addValue("K3", new Integer(3));
171: assertEquals(new Integer(3), v2.getValue(2));
172:
173: boolean pass = false;
174: try {
175: /* Number n = */v2.getValue("KK");
176: } catch (UnknownKeyException e) {
177: pass = true;
178: }
179: assertTrue(pass);
180: }
181:
182: /**
183: * Some checks for the getKey() methods.
184: */
185: public void testGetKey() {
186: DefaultKeyedValues v1 = new DefaultKeyedValues();
187: try {
188: /* Comparable k = */v1.getKey(-1);
189: assertTrue(false);
190: } catch (IndexOutOfBoundsException e) {
191: // expected
192: }
193: try {
194: /* Comparable k = */v1.getKey(0);
195: assertTrue(false);
196: } catch (IndexOutOfBoundsException e) {
197: // expected
198: }
199: DefaultKeyedValues v2 = new DefaultKeyedValues();
200: v2.addValue("K1", new Integer(1));
201: v2.addValue("K2", new Integer(2));
202: v2.addValue("K3", new Integer(3));
203: assertEquals("K2", v2.getKey(1));
204: }
205:
206: /**
207: * Some checks for the getIndex() methods.
208: */
209: public void testGetIndex() {
210: DefaultKeyedValues v1 = new DefaultKeyedValues();
211: assertEquals(-1, v1.getIndex("K1"));
212:
213: DefaultKeyedValues v2 = new DefaultKeyedValues();
214: v2.addValue("K1", new Integer(1));
215: v2.addValue("K2", new Integer(2));
216: v2.addValue("K3", new Integer(3));
217: assertEquals(2, v2.getIndex("K3"));
218:
219: // try null
220: boolean pass = false;
221: try {
222: v2.getIndex(null);
223: } catch (IllegalArgumentException e) {
224: pass = true;
225: }
226: assertTrue(pass);
227: }
228:
229: /**
230: * Some checks for the addValue() method.
231: */
232: public void testAddValue() {
233: DefaultKeyedValues v1 = new DefaultKeyedValues();
234: v1.addValue("A", 1.0);
235: assertEquals(new Double(1.0), v1.getValue("A"));
236: v1.addValue("B", 2.0);
237: assertEquals(new Double(2.0), v1.getValue("B"));
238: v1.addValue("B", 3.0);
239: assertEquals(new Double(3.0), v1.getValue("B"));
240: assertEquals(2, v1.getItemCount());
241: v1.addValue("A", null);
242: assertNull(v1.getValue("A"));
243: assertEquals(2, v1.getItemCount());
244:
245: boolean pass = false;
246: try {
247: v1.addValue(null, 99.9);
248: } catch (IllegalArgumentException e) {
249: pass = true;
250: }
251: assertTrue(pass);
252: }
253:
254: /**
255: * Some checks for the insertValue() method.
256: */
257: public void testInsertValue() {
258: DefaultKeyedValues v1 = new DefaultKeyedValues();
259: v1.insertValue(0, "A", 1.0);
260: assertEquals(new Double(1.0), v1.getValue(0));
261: v1.insertValue(0, "B", 2.0);
262: assertEquals(new Double(2.0), v1.getValue(0));
263: assertEquals(new Double(1.0), v1.getValue(1));
264:
265: // it's OK to use an index equal to the size of the list
266: v1.insertValue(2, "C", 3.0);
267: assertEquals(new Double(2.0), v1.getValue(0));
268: assertEquals(new Double(1.0), v1.getValue(1));
269: assertEquals(new Double(3.0), v1.getValue(2));
270:
271: // try replacing an existing value
272: v1.insertValue(2, "B", 4.0);
273: assertEquals(new Double(1.0), v1.getValue(0));
274: assertEquals(new Double(3.0), v1.getValue(1));
275: assertEquals(new Double(4.0), v1.getValue(2));
276: }
277:
278: /**
279: * Some checks for the clone() method.
280: */
281: public void testCloning() {
282: DefaultKeyedValues v1 = new DefaultKeyedValues();
283: v1.addValue("V1", new Integer(1));
284: v1.addValue("V2", null);
285: v1.addValue("V3", new Integer(3));
286: DefaultKeyedValues v2 = null;
287: try {
288: v2 = (DefaultKeyedValues) v1.clone();
289: } catch (CloneNotSupportedException e) {
290: System.err.println("Failed to clone.");
291: }
292: assertTrue(v1 != v2);
293: assertTrue(v1.getClass() == v2.getClass());
294: assertTrue(v1.equals(v2));
295:
296: // confirm that the clone is independent of the original
297: v2.setValue("V1", new Integer(44));
298: assertFalse(v1.equals(v2));
299: }
300:
301: /**
302: * Check that inserting and retrieving values works as expected.
303: */
304: public void testInsertAndRetrieve() {
305:
306: DefaultKeyedValues data = new DefaultKeyedValues();
307: data.addValue("A", new Double(1.0));
308: data.addValue("B", new Double(2.0));
309: data.addValue("C", new Double(3.0));
310: data.addValue("D", null);
311:
312: // check key order
313: assertEquals(data.getKey(0), "A");
314: assertEquals(data.getKey(1), "B");
315: assertEquals(data.getKey(2), "C");
316: assertEquals(data.getKey(3), "D");
317:
318: // check retrieve value by key
319: assertEquals(data.getValue("A"), new Double(1.0));
320: assertEquals(data.getValue("B"), new Double(2.0));
321: assertEquals(data.getValue("C"), new Double(3.0));
322: assertEquals(data.getValue("D"), null);
323:
324: // check retrieve value by index
325: assertEquals(data.getValue(0), new Double(1.0));
326: assertEquals(data.getValue(1), new Double(2.0));
327: assertEquals(data.getValue(2), new Double(3.0));
328: assertEquals(data.getValue(3), null);
329:
330: }
331:
332: /**
333: * Some tests for the removeValue() method.
334: */
335: public void testRemoveValue() {
336: DefaultKeyedValues data = new DefaultKeyedValues();
337: data.addValue("A", new Double(1.0));
338: data.addValue("B", null);
339: data.addValue("C", new Double(3.0));
340: data.addValue("D", new Double(2.0));
341: assertEquals(1, data.getIndex("B"));
342: data.removeValue("B");
343: assertEquals(-1, data.getIndex("B"));
344:
345: boolean pass = true;
346: try {
347: data.removeValue("XXX");
348: } catch (Exception e) {
349: pass = false;
350: }
351: assertTrue(pass);
352: }
353:
354: /**
355: * Tests sorting of data by key (ascending).
356: */
357: public void testSortByKeyAscending() {
358:
359: DefaultKeyedValues data = new DefaultKeyedValues();
360: data.addValue("C", new Double(1.0));
361: data.addValue("B", null);
362: data.addValue("D", new Double(3.0));
363: data.addValue("A", new Double(2.0));
364:
365: data.sortByKeys(SortOrder.ASCENDING);
366:
367: // check key order
368: assertEquals(data.getKey(0), "A");
369: assertEquals(data.getKey(1), "B");
370: assertEquals(data.getKey(2), "C");
371: assertEquals(data.getKey(3), "D");
372:
373: // check retrieve value by key
374: assertEquals(data.getValue("A"), new Double(2.0));
375: assertEquals(data.getValue("B"), null);
376: assertEquals(data.getValue("C"), new Double(1.0));
377: assertEquals(data.getValue("D"), new Double(3.0));
378:
379: // check retrieve value by index
380: assertEquals(data.getValue(0), new Double(2.0));
381: assertEquals(data.getValue(1), null);
382: assertEquals(data.getValue(2), new Double(1.0));
383: assertEquals(data.getValue(3), new Double(3.0));
384:
385: }
386:
387: /**
388: * Tests sorting of data by key (descending).
389: */
390: public void testSortByKeyDescending() {
391:
392: DefaultKeyedValues data = new DefaultKeyedValues();
393: data.addValue("C", new Double(1.0));
394: data.addValue("B", null);
395: data.addValue("D", new Double(3.0));
396: data.addValue("A", new Double(2.0));
397:
398: data.sortByKeys(SortOrder.DESCENDING);
399:
400: // check key order
401: assertEquals(data.getKey(0), "D");
402: assertEquals(data.getKey(1), "C");
403: assertEquals(data.getKey(2), "B");
404: assertEquals(data.getKey(3), "A");
405:
406: // check retrieve value by key
407: assertEquals(data.getValue("A"), new Double(2.0));
408: assertEquals(data.getValue("B"), null);
409: assertEquals(data.getValue("C"), new Double(1.0));
410: assertEquals(data.getValue("D"), new Double(3.0));
411:
412: // check retrieve value by index
413: assertEquals(data.getValue(0), new Double(3.0));
414: assertEquals(data.getValue(1), new Double(1.0));
415: assertEquals(data.getValue(2), null);
416: assertEquals(data.getValue(3), new Double(2.0));
417:
418: }
419:
420: /**
421: * Tests sorting of data by value (ascending).
422: */
423: public void testSortByValueAscending() {
424:
425: DefaultKeyedValues data = new DefaultKeyedValues();
426: data.addValue("C", new Double(1.0));
427: data.addValue("B", null);
428: data.addValue("D", new Double(3.0));
429: data.addValue("A", new Double(2.0));
430:
431: data.sortByValues(SortOrder.ASCENDING);
432:
433: // check key order
434: assertEquals(data.getKey(0), "C");
435: assertEquals(data.getKey(1), "A");
436: assertEquals(data.getKey(2), "D");
437: assertEquals(data.getKey(3), "B");
438:
439: // check retrieve value by key
440: assertEquals(data.getValue("A"), new Double(2.0));
441: assertEquals(data.getValue("B"), null);
442: assertEquals(data.getValue("C"), new Double(1.0));
443: assertEquals(data.getValue("D"), new Double(3.0));
444:
445: // check retrieve value by index
446: assertEquals(data.getValue(0), new Double(1.0));
447: assertEquals(data.getValue(1), new Double(2.0));
448: assertEquals(data.getValue(2), new Double(3.0));
449: assertEquals(data.getValue(3), null);
450:
451: }
452:
453: /**
454: * Tests sorting of data by key (descending).
455: */
456: public void testSortByValueDescending() {
457:
458: DefaultKeyedValues data = new DefaultKeyedValues();
459: data.addValue("C", new Double(1.0));
460: data.addValue("B", null);
461: data.addValue("D", new Double(3.0));
462: data.addValue("A", new Double(2.0));
463:
464: data.sortByValues(SortOrder.DESCENDING);
465:
466: // check key order
467: assertEquals(data.getKey(0), "D");
468: assertEquals(data.getKey(1), "A");
469: assertEquals(data.getKey(2), "C");
470: assertEquals(data.getKey(3), "B");
471:
472: // check retrieve value by key
473: assertEquals(data.getValue("A"), new Double(2.0));
474: assertEquals(data.getValue("B"), null);
475: assertEquals(data.getValue("C"), new Double(1.0));
476: assertEquals(data.getValue("D"), new Double(3.0));
477:
478: // check retrieve value by index
479: assertEquals(data.getValue(0), new Double(3.0));
480: assertEquals(data.getValue(1), new Double(2.0));
481: assertEquals(data.getValue(2), new Double(1.0));
482: assertEquals(data.getValue(3), null);
483:
484: }
485:
486: /**
487: * Serialize an instance, restore it, and check for equality.
488: */
489: public void testSerialization() {
490:
491: DefaultKeyedValues v1 = new DefaultKeyedValues();
492: v1.addValue("Key 1", new Double(23));
493: v1.addValue("Key 2", null);
494: v1.addValue("Key 3", new Double(42));
495:
496: DefaultKeyedValues v2 = null;
497:
498: try {
499: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
500: ObjectOutput out = new ObjectOutputStream(buffer);
501: out.writeObject(v1);
502: out.close();
503:
504: ObjectInput in = new ObjectInputStream(
505: new ByteArrayInputStream(buffer.toByteArray()));
506: v2 = (DefaultKeyedValues) in.readObject();
507: in.close();
508: } catch (Exception e) {
509: System.out.println(e.toString());
510: }
511: assertEquals(v1, v2);
512:
513: }
514:
515: }
|