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: * PaintMapTests.java
029: * ------------------
030: * (C) Copyright 2006, 2007, by Object Refinery Limited.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: PaintMapTests.java,v 1.1.2.2 2007/01/17 10:08:19 mungady Exp $
036: *
037: * Changes:
038: * --------
039: * 27-Sep-2006 : Version 1 (DG);
040: * 17-Jan-2007 : Added testKeysOfDifferentClasses() (DG);
041: *
042: */
043:
044: package org.jfree.chart.junit;
045:
046: import java.awt.Color;
047: import java.awt.GradientPaint;
048: import java.io.ByteArrayInputStream;
049: import java.io.ByteArrayOutputStream;
050: import java.io.ObjectInput;
051: import java.io.ObjectInputStream;
052: import java.io.ObjectOutput;
053: import java.io.ObjectOutputStream;
054:
055: import junit.framework.Test;
056: import junit.framework.TestCase;
057: import junit.framework.TestSuite;
058:
059: import org.jfree.chart.PaintMap;
060:
061: /**
062: * Some tests for the {@link PaintMap} class.
063: */
064: public class PaintMapTests extends TestCase {
065:
066: /**
067: * Returns the tests as a test suite.
068: *
069: * @return The test suite.
070: */
071: public static Test suite() {
072: return new TestSuite(PaintMapTests.class);
073: }
074:
075: /**
076: * Constructs a new set of tests.
077: *
078: * @param name the name of the tests.
079: */
080: public PaintMapTests(String name) {
081: super (name);
082: }
083:
084: /**
085: * Some checks for the getPaint() method.
086: */
087: public void testGetPaint() {
088: PaintMap m1 = new PaintMap();
089: assertEquals(null, m1.getPaint("A"));
090: m1.put("A", Color.red);
091: assertEquals(Color.red, m1.getPaint("A"));
092: m1.put("A", null);
093: assertEquals(null, m1.getPaint("A"));
094:
095: // a null key should throw an IllegalArgumentException
096: boolean pass = false;
097: try {
098: m1.getPaint(null);
099: } catch (IllegalArgumentException e) {
100: pass = true;
101: }
102: assertTrue(pass);
103: }
104:
105: /**
106: * Some checks for the put() method.
107: */
108: public void testPut() {
109: PaintMap m1 = new PaintMap();
110: m1.put("A", Color.red);
111: assertEquals(Color.red, m1.getPaint("A"));
112:
113: // a null key should throw an IllegalArgumentException
114: boolean pass = false;
115: try {
116: m1.put(null, Color.blue);
117: } catch (IllegalArgumentException e) {
118: pass = true;
119: }
120: assertTrue(pass);
121: }
122:
123: /**
124: * Some checks for the equals() method.
125: */
126: public void testEquals() {
127: PaintMap m1 = new PaintMap();
128: PaintMap m2 = new PaintMap();
129: assertTrue(m1.equals(m1));
130: assertTrue(m1.equals(m2));
131: assertFalse(m1.equals(null));
132: assertFalse(m1.equals("ABC"));
133:
134: m1.put("K1", Color.red);
135: assertFalse(m1.equals(m2));
136: m2.put("K1", Color.red);
137: assertTrue(m1.equals(m2));
138:
139: m1.put("K2", new GradientPaint(1.0f, 2.0f, Color.green, 3.0f,
140: 4.0f, Color.yellow));
141: assertFalse(m1.equals(m2));
142: m2.put("K2", new GradientPaint(1.0f, 2.0f, Color.green, 3.0f,
143: 4.0f, Color.yellow));
144: assertTrue(m1.equals(m2));
145:
146: m1.put("K2", null);
147: assertFalse(m1.equals(m2));
148: m2.put("K2", null);
149: assertTrue(m1.equals(m2));
150: }
151:
152: /**
153: * Some checks for cloning.
154: */
155: public void testCloning() {
156: PaintMap m1 = new PaintMap();
157: PaintMap m2 = null;
158: try {
159: m2 = (PaintMap) m1.clone();
160: } catch (CloneNotSupportedException e) {
161: e.printStackTrace();
162: }
163: assertTrue(m1.equals(m2));
164:
165: m1.put("K1", Color.red);
166: m1.put("K2", new GradientPaint(1.0f, 2.0f, Color.green, 3.0f,
167: 4.0f, Color.yellow));
168: try {
169: m2 = (PaintMap) m1.clone();
170: } catch (CloneNotSupportedException e) {
171: e.printStackTrace();
172: }
173: assertTrue(m1.equals(m2));
174: }
175:
176: /**
177: * A check for serialization.
178: */
179: public void testSerialization1() {
180: PaintMap m1 = new PaintMap();
181: PaintMap m2 = null;
182: try {
183: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
184: ObjectOutput out = new ObjectOutputStream(buffer);
185: out.writeObject(m1);
186: out.close();
187:
188: ObjectInput in = new ObjectInputStream(
189: new ByteArrayInputStream(buffer.toByteArray()));
190: m2 = (PaintMap) in.readObject();
191: in.close();
192: } catch (Exception e) {
193: e.printStackTrace();
194: }
195: assertEquals(m1, m2);
196: }
197:
198: /**
199: * A check for serialization.
200: */
201: public void testSerialization2() {
202: PaintMap m1 = new PaintMap();
203: m1.put("K1", Color.red);
204: m1.put("K2", new GradientPaint(1.0f, 2.0f, Color.green, 3.0f,
205: 4.0f, Color.yellow));
206: PaintMap m2 = null;
207: try {
208: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
209: ObjectOutput out = new ObjectOutputStream(buffer);
210: out.writeObject(m1);
211: out.close();
212:
213: ObjectInput in = new ObjectInputStream(
214: new ByteArrayInputStream(buffer.toByteArray()));
215: m2 = (PaintMap) in.readObject();
216: in.close();
217: } catch (Exception e) {
218: e.printStackTrace();
219: }
220: assertEquals(m1, m2);
221: }
222:
223: /**
224: * This test covers a bug reported in the forum:
225: *
226: * http://www.jfree.org/phpBB2/viewtopic.php?t=19980
227: */
228: public void testKeysOfDifferentClasses() {
229: PaintMap m = new PaintMap();
230: m.put("ABC", Color.red);
231: m.put(new Integer(99), Color.blue);
232: assertEquals(Color.blue, m.getPaint(new Integer(99)));
233: }
234:
235: }
|