KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > data > junit > DefaultKeyedValuesTests


1 /* ===========================================================
2  * JFreeChart : a free chart library for the Java(tm) platform
3  * ===========================================================
4  *
5  * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
6  *
7  * Project Info: http://www.jfree.org/jfreechart/index.html
8  *
9  * This library is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17  * License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this library; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22  *
23  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
24  * in the United States and other countries.]
25  *
26  * ----------------------------
27  * DefaultKeyedValuesTests.java
28  * ----------------------------
29  * (C) Copyright 2003-2005, by Object Refinery Limited.
30  *
31  * Original Author: David Gilbert (for Object Refinery Limited);
32  * Contributor(s): -;
33  *
34  * $Id: DefaultKeyedValuesTests.java,v 1.6 2005/03/29 12:56:53 mungady Exp $
35  *
36  * Changes
37  * -------
38  * 05-Mar-2003 : Version 1 (DG);
39  * 27-Aug-2003 : Moved SortOrder from org.jfree.data --> org.jfree.util (DG);
40  *
41  */

42
43 package org.jfree.data.junit;
44
45 import java.io.ByteArrayInputStream JavaDoc;
46 import java.io.ByteArrayOutputStream JavaDoc;
47 import java.io.ObjectInput JavaDoc;
48 import java.io.ObjectInputStream JavaDoc;
49 import java.io.ObjectOutput JavaDoc;
50 import java.io.ObjectOutputStream JavaDoc;
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 /**
60  * Tests for the {@link DefaultKeyedValues} class.
61  */

62 public class DefaultKeyedValuesTests extends TestCase {
63
64     /**
65      * Returns the tests as a test suite.
66      *
67      * @return The test suite.
68      */

69     public static Test suite() {
70         return new TestSuite(DefaultKeyedValuesTests.class);
71     }
72
73     /**
74      * Constructs a new set of tests.
75      *
76      * @param name the name of the tests.
77      */

78     public DefaultKeyedValuesTests(String JavaDoc name) {
79         super(name);
80     }
81
82     /**
83      * Common test setup.
84      */

85     protected void setUp() {
86         // no setup required
87
}
88     
89     /**
90      * Some checks for the getValue() methods.
91      */

92     public void testGetValue() {
93         DefaultKeyedValues v1 = new DefaultKeyedValues();
94         try {
95             /* Number n = */ v1.getValue(-1);
96             assertTrue(false);
97         }
98         catch (IndexOutOfBoundsException JavaDoc e) {
99             // expected
100
}
101         try {
102             /* Number n = */ v1.getValue(0);
103             assertTrue(false);
104         }
105         catch (IndexOutOfBoundsException JavaDoc e) {
106             // expected
107
}
108         DefaultKeyedValues v2 = new DefaultKeyedValues();
109         v2.addValue("K1", new Integer JavaDoc(1));
110         v2.addValue("K2", new Integer JavaDoc(2));
111         v2.addValue("K3", new Integer JavaDoc(3));
112         assertEquals(new Integer JavaDoc(3), v2.getValue(2));
113     }
114
115     /**
116      * Some checks for the getKey() methods.
117      */

118     public void testGetKey() {
119         DefaultKeyedValues v1 = new DefaultKeyedValues();
120         try {
121             /* Comparable k = */ v1.getKey(-1);
122             assertTrue(false);
123         }
124         catch (IndexOutOfBoundsException JavaDoc e) {
125             // expected
126
}
127         try {
128             /* Comparable k = */ v1.getKey(0);
129             assertTrue(false);
130         }
131         catch (IndexOutOfBoundsException JavaDoc e) {
132             // expected
133
}
134         DefaultKeyedValues v2 = new DefaultKeyedValues();
135         v2.addValue("K1", new Integer JavaDoc(1));
136         v2.addValue("K2", new Integer JavaDoc(2));
137         v2.addValue("K3", new Integer JavaDoc(3));
138         assertEquals("K2", v2.getKey(1));
139     }
140
141     /**
142      * Some checks for the getKey() methods.
143      */

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 JavaDoc(1));
150         v2.addValue("K2", new Integer JavaDoc(2));
151         v2.addValue("K3", new Integer JavaDoc(3));
152         assertEquals(2, v2.getIndex("K3"));
153     }
154     
155     /**
156      * Some checks for the clone() method.
157      */

158     public void testCloning() {
159         DefaultKeyedValues v1 = new DefaultKeyedValues();
160         v1.addValue("V1", new Integer JavaDoc(1));
161         v1.addValue("V2", null);
162         v1.addValue("V3", new Integer JavaDoc(3));
163         DefaultKeyedValues v2 = null;
164         try {
165             v2 = (DefaultKeyedValues) v1.clone();
166         }
167         catch (CloneNotSupportedException JavaDoc 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         // confirm that the clone is independent of the original
175
v2.setValue("V1", new Integer JavaDoc(44));
176         assertFalse(v1.equals(v2));
177     }
178     
179     /**
180      * Check that inserting and retrieving values works as expected.
181      */

182     public void testInsertAndRetrieve() {
183
184         DefaultKeyedValues data = new DefaultKeyedValues();
185         data.addValue("A", new Double JavaDoc(1.0));
186         data.addValue("B", new Double JavaDoc(2.0));
187         data.addValue("C", new Double JavaDoc(3.0));
188         data.addValue("D", null);
189
190         // check key order
191
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         // check retrieve value by key
197
assertEquals(data.getValue("A"), new Double JavaDoc(1.0));
198         assertEquals(data.getValue("B"), new Double JavaDoc(2.0));
199         assertEquals(data.getValue("C"), new Double JavaDoc(3.0));
200         assertEquals(data.getValue("D"), null);
201
202         // check retrieve value by index
203
assertEquals(data.getValue(0), new Double JavaDoc(1.0));
204         assertEquals(data.getValue(1), new Double JavaDoc(2.0));
205         assertEquals(data.getValue(2), new Double JavaDoc(3.0));
206         assertEquals(data.getValue(3), null);
207
208     }
209
210     /**
211      * Tests sorting of data by key (ascending).
212      */

213     public void testSortByKeyAscending() {
214
215         DefaultKeyedValues data = new DefaultKeyedValues();
216         data.addValue("C", new Double JavaDoc(1.0));
217         data.addValue("B", null);
218         data.addValue("D", new Double JavaDoc(3.0));
219         data.addValue("A", new Double JavaDoc(2.0));
220
221         data.sortByKeys(SortOrder.ASCENDING);
222
223         // check key order
224
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         // check retrieve value by key
230
assertEquals(data.getValue("A"), new Double JavaDoc(2.0));
231         assertEquals(data.getValue("B"), null);
232         assertEquals(data.getValue("C"), new Double JavaDoc(1.0));
233         assertEquals(data.getValue("D"), new Double JavaDoc(3.0));
234
235         // check retrieve value by index
236
assertEquals(data.getValue(0), new Double JavaDoc(2.0));
237         assertEquals(data.getValue(1), null);
238         assertEquals(data.getValue(2), new Double JavaDoc(1.0));
239         assertEquals(data.getValue(3), new Double JavaDoc(3.0));
240
241     }
242
243     /**
244      * Tests sorting of data by key (descending).
245      */

246     public void testSortByKeyDescending() {
247
248         DefaultKeyedValues data = new DefaultKeyedValues();
249         data.addValue("C", new Double JavaDoc(1.0));
250         data.addValue("B", null);
251         data.addValue("D", new Double JavaDoc(3.0));
252         data.addValue("A", new Double JavaDoc(2.0));
253
254         data.sortByKeys(SortOrder.DESCENDING);
255
256         // check key order
257
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         // check retrieve value by key
263
assertEquals(data.getValue("A"), new Double JavaDoc(2.0));
264         assertEquals(data.getValue("B"), null);
265         assertEquals(data.getValue("C"), new Double JavaDoc(1.0));
266         assertEquals(data.getValue("D"), new Double JavaDoc(3.0));
267
268         // check retrieve value by index
269
assertEquals(data.getValue(0), new Double JavaDoc(3.0));
270         assertEquals(data.getValue(1), new Double JavaDoc(1.0));
271         assertEquals(data.getValue(2), null);
272         assertEquals(data.getValue(3), new Double JavaDoc(2.0));
273
274     }
275
276     /**
277      * Tests sorting of data by value (ascending).
278      */

279     public void testSortByValueAscending() {
280
281         DefaultKeyedValues data = new DefaultKeyedValues();
282         data.addValue("C", new Double JavaDoc(1.0));
283         data.addValue("B", null);
284         data.addValue("D", new Double JavaDoc(3.0));
285         data.addValue("A", new Double JavaDoc(2.0));
286
287         data.sortByValues(SortOrder.ASCENDING);
288
289         // check key order
290
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         // check retrieve value by key
296
assertEquals(data.getValue("A"), new Double JavaDoc(2.0));
297         assertEquals(data.getValue("B"), null);
298         assertEquals(data.getValue("C"), new Double JavaDoc(1.0));
299         assertEquals(data.getValue("D"), new Double JavaDoc(3.0));
300
301         // check retrieve value by index
302
assertEquals(data.getValue(0), new Double JavaDoc(1.0));
303         assertEquals(data.getValue(1), new Double JavaDoc(2.0));
304         assertEquals(data.getValue(2), new Double JavaDoc(3.0));
305         assertEquals(data.getValue(3), null);
306
307     }
308
309     /**
310      * Tests sorting of data by key (descending).
311      */

312     public void testSortByValueDescending() {
313
314         DefaultKeyedValues data = new DefaultKeyedValues();
315         data.addValue("C", new Double JavaDoc(1.0));
316         data.addValue("B", null);
317         data.addValue("D", new Double JavaDoc(3.0));
318         data.addValue("A", new Double JavaDoc(2.0));
319
320         data.sortByValues(SortOrder.DESCENDING);
321
322         // check key order
323
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         // check retrieve value by key
329
assertEquals(data.getValue("A"), new Double JavaDoc(2.0));
330         assertEquals(data.getValue("B"), null);
331         assertEquals(data.getValue("C"), new Double JavaDoc(1.0));
332         assertEquals(data.getValue("D"), new Double JavaDoc(3.0));
333
334         // check retrieve value by index
335
assertEquals(data.getValue(0), new Double JavaDoc(3.0));
336         assertEquals(data.getValue(1), new Double JavaDoc(2.0));
337         assertEquals(data.getValue(2), new Double JavaDoc(1.0));
338         assertEquals(data.getValue(3), null);
339
340     }
341
342     /**
343      * Serialize an instance, restore it, and check for equality.
344      */

345     public void testSerialization() {
346
347         DefaultKeyedValues v1 = new DefaultKeyedValues();
348         v1.addValue("Key 1", new Double JavaDoc(23));
349         v1.addValue("Key 2", null);
350         v1.addValue("Key 3", new Double JavaDoc(42));
351
352         DefaultKeyedValues v2 = null;
353
354         try {
355             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
356             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
357             out.writeObject(v1);
358             out.close();
359
360             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
361                 new ByteArrayInputStream JavaDoc(buffer.toByteArray())
362             );
363             v2 = (DefaultKeyedValues) in.readObject();
364             in.close();
365         }
366         catch (Exception JavaDoc e) {
367             System.out.println(e.toString());
368         }
369         assertEquals(v1, v2);
370
371     }
372
373 }
374
Popular Tags