KickJava   Java API By Example, From Geeks To Geeks.

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


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
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22  * USA.
23  *
24  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
25  * in the United States and other countries.]
26  *
27  * ----------------------
28  * KeyedObjectsTests.java
29  * ----------------------
30  * (C) Copyright 2004, 2005, by Object Refinery Limited.
31  *
32  * Original Author: David Gilbert (for Object Refinery Limited);
33  * Contributor(s): -;
34  *
35  * $Id: KeyedObjectsTests.java,v 1.1.2.1 2006/10/03 15:41:42 mungady Exp $
36  *
37  * Changes
38  * -------
39  * 27-Jan-2004 : Version 1 (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.KeyedObjects;
57
58 /**
59  * Tests for the {@link KeyedObjects} class.
60  */

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

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

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

84     protected void setUp() {
85         // no setup required
86
}
87
88     /**
89      * Confirm that cloning works.
90      */

91     public void testCloning() {
92         KeyedObjects ko1 = new KeyedObjects();
93         ko1.addObject("V1", new Integer JavaDoc(1));
94         ko1.addObject("V2", null);
95         ko1.addObject("V3", new Integer JavaDoc(3));
96         KeyedObjects ko2 = null;
97         try {
98             ko2 = (KeyedObjects) ko1.clone();
99         }
100         catch (CloneNotSupportedException JavaDoc e) {
101             System.err.println("Failed to clone.");
102         }
103         assertTrue(ko1 != ko2);
104         assertTrue(ko1.getClass() == ko2.getClass());
105         assertTrue(ko1.equals(ko2));
106     }
107     
108     /**
109      * Check that inserting and retrieving values works as expected.
110      */

111     public void testInsertAndRetrieve() {
112
113         KeyedObjects data = new KeyedObjects();
114         data.addObject("A", new Double JavaDoc(1.0));
115         data.addObject("B", new Double JavaDoc(2.0));
116         data.addObject("C", new Double JavaDoc(3.0));
117         data.addObject("D", null);
118
119         // check key order
120
assertEquals(data.getKey(0), "A");
121         assertEquals(data.getKey(1), "B");
122         assertEquals(data.getKey(2), "C");
123         assertEquals(data.getKey(3), "D");
124
125         // check retrieve value by key
126
assertEquals(data.getObject("A"), new Double JavaDoc(1.0));
127         assertEquals(data.getObject("B"), new Double JavaDoc(2.0));
128         assertEquals(data.getObject("C"), new Double JavaDoc(3.0));
129         assertEquals(data.getObject("D"), null);
130         assertEquals(data.getObject("Not a key"), null);
131
132         // check retrieve value by index
133
assertEquals(data.getObject(0), new Double JavaDoc(1.0));
134         assertEquals(data.getObject(1), new Double JavaDoc(2.0));
135         assertEquals(data.getObject(2), new Double JavaDoc(3.0));
136         assertEquals(data.getObject(3), null);
137
138     }
139
140     /**
141      * Serialize an instance, restore it, and check for equality.
142      */

143     public void testSerialization() {
144
145         KeyedObjects ko1 = new KeyedObjects();
146         ko1.addObject("Key 1", "Object 1");
147         ko1.addObject("Key 2", null);
148         ko1.addObject("Key 3", "Object 2");
149
150         KeyedObjects ko2 = null;
151
152         try {
153             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
154             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
155             out.writeObject(ko1);
156             out.close();
157
158             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
159                 new ByteArrayInputStream JavaDoc(buffer.toByteArray())
160             );
161             ko2 = (KeyedObjects) in.readObject();
162             in.close();
163         }
164         catch (Exception JavaDoc e) {
165             System.out.println(e.toString());
166         }
167         assertEquals(ko1, ko2);
168
169     }
170
171 }
172
Popular Tags