KickJava   Java API By Example, From Geeks To Geeks.

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


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  * KeyToGroupMapTests.java
29  * -----------------------
30  * (C) Copyright 2004, 2005, by Object Refinery Limited and Contributors.
31  *
32  * Original Author: David Gilbert (for Object Refinery Limited);
33  * Contributor(s): -;
34  *
35  * $Id: KeyToGroupMapTests.java,v 1.1.2.1 2006/10/03 15:41:43 mungady Exp $
36  *
37  * Changes
38  * -------
39  * 29-Apr-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.KeyToGroupMap;
57
58 /**
59  * Tests for the {@link KeyToGroupMap} class.
60  */

61 public class KeyToGroupMapTests 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(KeyToGroupMapTests.class);
70     }
71
72     /**
73      * Constructs a new set of tests.
74      *
75      * @param name the name of the tests.
76      */

77     public KeyToGroupMapTests(String JavaDoc name) {
78         super(name);
79     }
80
81     /**
82      * Tests the mapKeyToGroup() method.
83      */

84     public void testMapKeyToGroup() {
85         KeyToGroupMap m1 = new KeyToGroupMap("G1");
86         
87         // map a key to the default group
88
m1.mapKeyToGroup("K1", "G1");
89         assertEquals("G1", m1.getGroup("K1"));
90         
91         // map a key to a new group
92
m1.mapKeyToGroup("K2", "G2");
93         assertEquals("G2", m1.getGroup("K2"));
94         
95         // clear a mapping
96
m1.mapKeyToGroup("K2", null);
97         assertEquals("G1", m1.getGroup("K2")); // after clearing, reverts to
98
// default group
99

100         // check handling of null key
101
boolean pass = false;
102         try {
103             m1.mapKeyToGroup(null, "G1");
104         }
105         catch (IllegalArgumentException JavaDoc e) {
106             pass = true;
107         }
108         assertTrue(pass);
109     }
110     
111     /**
112      * Tests that the getGroupCount() method returns the correct values under
113      * various circumstances.
114      */

115     public void testGroupCount() {
116         KeyToGroupMap m1 = new KeyToGroupMap("Default Group");
117         
118         // a new map always has 1 group (the default group)
119
assertEquals(1, m1.getGroupCount());
120         
121         // if the default group is not mapped to, it should still count towards
122
// the group count...
123
m1.mapKeyToGroup("C1", "G1");
124         assertEquals(2, m1.getGroupCount());
125         
126         // now when the default group is mapped to, it shouldn't increase the
127
// group count...
128
m1.mapKeyToGroup("C2", "Default Group");
129         assertEquals(2, m1.getGroupCount());
130     
131         // complicate things a little...
132
m1.mapKeyToGroup("C3", "Default Group");
133         m1.mapKeyToGroup("C4", "G2");
134         m1.mapKeyToGroup("C5", "G2");
135         m1.mapKeyToGroup("C6", "Default Group");
136         assertEquals(3, m1.getGroupCount());
137         
138         // now overwrite group "G2"...
139
m1.mapKeyToGroup("C4", "G1");
140         m1.mapKeyToGroup("C5", "G1");
141         assertEquals(2, m1.getGroupCount());
142     }
143     
144     /**
145      * Tests that the getKeyCount() method returns the correct values under
146      * various circumstances.
147      */

148     public void testKeyCount() {
149         KeyToGroupMap m1 = new KeyToGroupMap("Default Group");
150         
151         // a new map always has 1 group (the default group)
152
assertEquals(0, m1.getKeyCount("Default Group"));
153         
154         // simple case
155
m1.mapKeyToGroup("K1", "G1");
156         assertEquals(1, m1.getKeyCount("G1"));
157         m1.mapKeyToGroup("K1", null);
158         assertEquals(0, m1.getKeyCount("G1"));
159         
160         // if there is an explicit mapping to the default group, it is counted
161
m1.mapKeyToGroup("K2", "Default Group");
162         assertEquals(1, m1.getKeyCount("Default Group"));
163     
164         // complicate things a little...
165
m1.mapKeyToGroup("K3", "Default Group");
166         m1.mapKeyToGroup("K4", "G2");
167         m1.mapKeyToGroup("K5", "G2");
168         m1.mapKeyToGroup("K6", "Default Group");
169         assertEquals(3, m1.getKeyCount("Default Group"));
170         assertEquals(2, m1.getKeyCount("G2"));
171         
172         // now overwrite group "G2"...
173
m1.mapKeyToGroup("K4", "G1");
174         m1.mapKeyToGroup("K5", "G1");
175         assertEquals(2, m1.getKeyCount("G1"));
176         assertEquals(0, m1.getKeyCount("G2"));
177     }
178     
179     /**
180      * Tests the getGroupIndex() method.
181      */

182     public void testGetGroupIndex() {
183         KeyToGroupMap m1 = new KeyToGroupMap("Default Group");
184        
185         // the default group is always at index 0
186
assertEquals(0, m1.getGroupIndex("Default Group"));
187         
188         // a non-existent group should return -1
189
assertEquals(-1, m1.getGroupIndex("G3"));
190         
191         // indices are assigned in the order that groups are originally mapped
192
m1.mapKeyToGroup("K3", "G3");
193         m1.mapKeyToGroup("K1", "G1");
194         m1.mapKeyToGroup("K2", "G2");
195         assertEquals(1, m1.getGroupIndex("G3"));
196         assertEquals(2, m1.getGroupIndex("G1"));
197         assertEquals(3, m1.getGroupIndex("G2"));
198     }
199     
200     /**
201      * Tests the getGroup() method.
202      */

203     public void testGetGroup() {
204         KeyToGroupMap m1 = new KeyToGroupMap("Default Group");
205         
206         // a key that hasn't been mapped should return the default group
207
assertEquals("Default Group", m1.getGroup("K1"));
208         
209         m1.mapKeyToGroup("K1", "G1");
210         assertEquals("G1", m1.getGroup("K1"));
211         m1.mapKeyToGroup("K1", "G2");
212         assertEquals("G2", m1.getGroup("K1"));
213         m1.mapKeyToGroup("K1", null);
214         assertEquals("Default Group", m1.getGroup("K1"));
215         
216         // a null argument should throw an exception
217
boolean pass = false;
218         try {
219             Comparable JavaDoc g = m1.getGroup(null);
220             System.out.println(g);
221         }
222         catch (IllegalArgumentException JavaDoc e) {
223             pass = true;
224         }
225         assertTrue(pass);
226     }
227     
228     /**
229      * Confirm that the equals method can distinguish all the required fields.
230      */

231     public void testEquals() {
232         KeyToGroupMap m1 = new KeyToGroupMap("Default Group");
233         KeyToGroupMap m2 = new KeyToGroupMap("Default Group");
234         assertTrue(m1.equals(m2));
235         assertTrue(m2.equals(m1));
236         
237         m1.mapKeyToGroup("K1", "G1");
238         assertFalse(m1.equals(m2));
239         m2.mapKeyToGroup("K1", "G1");
240         assertTrue(m1.equals(m2));
241     }
242
243     /**
244      * Confirm that cloning works.
245      */

246     public void testCloning() {
247         KeyToGroupMap m1 = new KeyToGroupMap("Test");
248         m1.mapKeyToGroup("K1", "G1");
249         KeyToGroupMap m2 = null;
250         try {
251             m2 = (KeyToGroupMap) m1.clone();
252         }
253         catch (CloneNotSupportedException JavaDoc e) {
254             System.err.println("Failed to clone.");
255         }
256         assertTrue(m1 != m2);
257         assertTrue(m1.getClass() == m2.getClass());
258         assertTrue(m1.equals(m2));
259         
260         // a small check for independence
261
m1.mapKeyToGroup("K1", "G2");
262         assertFalse(m1.equals(m2));
263         m2.mapKeyToGroup("K1", "G2");
264         assertTrue(m1.equals(m2));
265     }
266
267     /**
268      * Serialize an instance, restore it, and check for equality.
269      */

270     public void testSerialization() {
271
272         KeyToGroupMap m1 = new KeyToGroupMap("Test");
273         KeyToGroupMap m2 = null;
274
275         try {
276             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
277             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
278             out.writeObject(m1);
279             out.close();
280
281             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
282                 new ByteArrayInputStream JavaDoc(buffer.toByteArray())
283             );
284             m2 = (KeyToGroupMap) in.readObject();
285             in.close();
286         }
287         catch (Exception JavaDoc e) {
288             System.out.println(e.toString());
289         }
290         assertEquals(m1, m2);
291
292     }
293
294 }
295
Popular Tags