001: /* ===========================================================
002: * JFreeChart : a free chart library for the Java(tm) platform
003: * ===========================================================
004: *
005: * (C) Copyright 2000-2005, 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: * KeyToGroupMapTests.java
029: * -----------------------
030: * (C) Copyright 2004, 2005, by Object Refinery Limited and Contributors.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: KeyToGroupMapTests.java,v 1.1.2.1 2006/10/03 15:41:43 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 29-Apr-2004 : Version 1 (DG);
040: *
041: */
042:
043: package org.jfree.data.junit;
044:
045: import java.io.ByteArrayInputStream;
046: import java.io.ByteArrayOutputStream;
047: import java.io.ObjectInput;
048: import java.io.ObjectInputStream;
049: import java.io.ObjectOutput;
050: import java.io.ObjectOutputStream;
051:
052: import junit.framework.Test;
053: import junit.framework.TestCase;
054: import junit.framework.TestSuite;
055:
056: import org.jfree.data.KeyToGroupMap;
057:
058: /**
059: * Tests for the {@link KeyToGroupMap} class.
060: */
061: public class KeyToGroupMapTests extends TestCase {
062:
063: /**
064: * Returns the tests as a test suite.
065: *
066: * @return The test suite.
067: */
068: public static Test suite() {
069: return new TestSuite(KeyToGroupMapTests.class);
070: }
071:
072: /**
073: * Constructs a new set of tests.
074: *
075: * @param name the name of the tests.
076: */
077: public KeyToGroupMapTests(String name) {
078: super (name);
079: }
080:
081: /**
082: * Tests the mapKeyToGroup() method.
083: */
084: public void testMapKeyToGroup() {
085: KeyToGroupMap m1 = new KeyToGroupMap("G1");
086:
087: // map a key to the default group
088: m1.mapKeyToGroup("K1", "G1");
089: assertEquals("G1", m1.getGroup("K1"));
090:
091: // map a key to a new group
092: m1.mapKeyToGroup("K2", "G2");
093: assertEquals("G2", m1.getGroup("K2"));
094:
095: // clear a mapping
096: m1.mapKeyToGroup("K2", null);
097: assertEquals("G1", m1.getGroup("K2")); // after clearing, reverts to
098: // default group
099:
100: // check handling of null key
101: boolean pass = false;
102: try {
103: m1.mapKeyToGroup(null, "G1");
104: } catch (IllegalArgumentException e) {
105: pass = true;
106: }
107: assertTrue(pass);
108: }
109:
110: /**
111: * Tests that the getGroupCount() method returns the correct values under
112: * various circumstances.
113: */
114: public void testGroupCount() {
115: KeyToGroupMap m1 = new KeyToGroupMap("Default Group");
116:
117: // a new map always has 1 group (the default group)
118: assertEquals(1, m1.getGroupCount());
119:
120: // if the default group is not mapped to, it should still count towards
121: // the group count...
122: m1.mapKeyToGroup("C1", "G1");
123: assertEquals(2, m1.getGroupCount());
124:
125: // now when the default group is mapped to, it shouldn't increase the
126: // group count...
127: m1.mapKeyToGroup("C2", "Default Group");
128: assertEquals(2, m1.getGroupCount());
129:
130: // complicate things a little...
131: m1.mapKeyToGroup("C3", "Default Group");
132: m1.mapKeyToGroup("C4", "G2");
133: m1.mapKeyToGroup("C5", "G2");
134: m1.mapKeyToGroup("C6", "Default Group");
135: assertEquals(3, m1.getGroupCount());
136:
137: // now overwrite group "G2"...
138: m1.mapKeyToGroup("C4", "G1");
139: m1.mapKeyToGroup("C5", "G1");
140: assertEquals(2, m1.getGroupCount());
141: }
142:
143: /**
144: * Tests that the getKeyCount() method returns the correct values under
145: * various circumstances.
146: */
147: public void testKeyCount() {
148: KeyToGroupMap m1 = new KeyToGroupMap("Default Group");
149:
150: // a new map always has 1 group (the default group)
151: assertEquals(0, m1.getKeyCount("Default Group"));
152:
153: // simple case
154: m1.mapKeyToGroup("K1", "G1");
155: assertEquals(1, m1.getKeyCount("G1"));
156: m1.mapKeyToGroup("K1", null);
157: assertEquals(0, m1.getKeyCount("G1"));
158:
159: // if there is an explicit mapping to the default group, it is counted
160: m1.mapKeyToGroup("K2", "Default Group");
161: assertEquals(1, m1.getKeyCount("Default Group"));
162:
163: // complicate things a little...
164: m1.mapKeyToGroup("K3", "Default Group");
165: m1.mapKeyToGroup("K4", "G2");
166: m1.mapKeyToGroup("K5", "G2");
167: m1.mapKeyToGroup("K6", "Default Group");
168: assertEquals(3, m1.getKeyCount("Default Group"));
169: assertEquals(2, m1.getKeyCount("G2"));
170:
171: // now overwrite group "G2"...
172: m1.mapKeyToGroup("K4", "G1");
173: m1.mapKeyToGroup("K5", "G1");
174: assertEquals(2, m1.getKeyCount("G1"));
175: assertEquals(0, m1.getKeyCount("G2"));
176: }
177:
178: /**
179: * Tests the getGroupIndex() method.
180: */
181: public void testGetGroupIndex() {
182: KeyToGroupMap m1 = new KeyToGroupMap("Default Group");
183:
184: // the default group is always at index 0
185: assertEquals(0, m1.getGroupIndex("Default Group"));
186:
187: // a non-existent group should return -1
188: assertEquals(-1, m1.getGroupIndex("G3"));
189:
190: // indices are assigned in the order that groups are originally mapped
191: m1.mapKeyToGroup("K3", "G3");
192: m1.mapKeyToGroup("K1", "G1");
193: m1.mapKeyToGroup("K2", "G2");
194: assertEquals(1, m1.getGroupIndex("G3"));
195: assertEquals(2, m1.getGroupIndex("G1"));
196: assertEquals(3, m1.getGroupIndex("G2"));
197: }
198:
199: /**
200: * Tests the getGroup() method.
201: */
202: public void testGetGroup() {
203: KeyToGroupMap m1 = new KeyToGroupMap("Default Group");
204:
205: // a key that hasn't been mapped should return the default group
206: assertEquals("Default Group", m1.getGroup("K1"));
207:
208: m1.mapKeyToGroup("K1", "G1");
209: assertEquals("G1", m1.getGroup("K1"));
210: m1.mapKeyToGroup("K1", "G2");
211: assertEquals("G2", m1.getGroup("K1"));
212: m1.mapKeyToGroup("K1", null);
213: assertEquals("Default Group", m1.getGroup("K1"));
214:
215: // a null argument should throw an exception
216: boolean pass = false;
217: try {
218: Comparable g = m1.getGroup(null);
219: System.out.println(g);
220: } catch (IllegalArgumentException e) {
221: pass = true;
222: }
223: assertTrue(pass);
224: }
225:
226: /**
227: * Confirm that the equals method can distinguish all the required fields.
228: */
229: public void testEquals() {
230: KeyToGroupMap m1 = new KeyToGroupMap("Default Group");
231: KeyToGroupMap m2 = new KeyToGroupMap("Default Group");
232: assertTrue(m1.equals(m2));
233: assertTrue(m2.equals(m1));
234:
235: m1.mapKeyToGroup("K1", "G1");
236: assertFalse(m1.equals(m2));
237: m2.mapKeyToGroup("K1", "G1");
238: assertTrue(m1.equals(m2));
239: }
240:
241: /**
242: * Confirm that cloning works.
243: */
244: public void testCloning() {
245: KeyToGroupMap m1 = new KeyToGroupMap("Test");
246: m1.mapKeyToGroup("K1", "G1");
247: KeyToGroupMap m2 = null;
248: try {
249: m2 = (KeyToGroupMap) m1.clone();
250: } catch (CloneNotSupportedException e) {
251: System.err.println("Failed to clone.");
252: }
253: assertTrue(m1 != m2);
254: assertTrue(m1.getClass() == m2.getClass());
255: assertTrue(m1.equals(m2));
256:
257: // a small check for independence
258: m1.mapKeyToGroup("K1", "G2");
259: assertFalse(m1.equals(m2));
260: m2.mapKeyToGroup("K1", "G2");
261: assertTrue(m1.equals(m2));
262: }
263:
264: /**
265: * Serialize an instance, restore it, and check for equality.
266: */
267: public void testSerialization() {
268:
269: KeyToGroupMap m1 = new KeyToGroupMap("Test");
270: KeyToGroupMap m2 = null;
271:
272: try {
273: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
274: ObjectOutput out = new ObjectOutputStream(buffer);
275: out.writeObject(m1);
276: out.close();
277:
278: ObjectInput in = new ObjectInputStream(
279: new ByteArrayInputStream(buffer.toByteArray()));
280: m2 = (KeyToGroupMap) in.readObject();
281: in.close();
282: } catch (Exception e) {
283: System.out.println(e.toString());
284: }
285: assertEquals(m1, m2);
286:
287: }
288:
289: }
|