KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > chart > junit > PaintMapTests


1 /* ===========================================================
2  * JFreeChart : a free chart library for the Java(tm) platform
3  * ===========================================================
4  *
5  * (C) Copyright 2000-2006, 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  * PaintMapTests.java
29  * ------------------
30  * (C) Copyright 2006, by Object Refinery Limited.
31  *
32  * Original Author: David Gilbert (for Object Refinery Limited);
33  * Contributor(s): -;
34  *
35  * $Id: PaintMapTests.java,v 1.1.2.1 2006/10/03 15:41:30 mungady Exp $
36  *
37  * Changes:
38  * --------
39  * 27-Sep-2006 : Version 1 (DG);
40  *
41  */

42
43 package org.jfree.chart.junit;
44
45 import java.awt.Color JavaDoc;
46 import java.awt.GradientPaint JavaDoc;
47 import java.io.ByteArrayInputStream JavaDoc;
48 import java.io.ByteArrayOutputStream JavaDoc;
49 import java.io.ObjectInput JavaDoc;
50 import java.io.ObjectInputStream JavaDoc;
51 import java.io.ObjectOutput JavaDoc;
52 import java.io.ObjectOutputStream JavaDoc;
53
54 import junit.framework.Test;
55 import junit.framework.TestCase;
56 import junit.framework.TestSuite;
57
58 import org.jfree.chart.PaintMap;
59
60 /**
61  * Some tests for the {@link PaintMap} class.
62  */

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

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

79     public PaintMapTests(String JavaDoc name) {
80         super(name);
81     }
82
83     /**
84      * Some checks for the getPaint() method.
85      */

86     public void testGetPaint() {
87         PaintMap m1 = new PaintMap();
88         assertEquals(null, m1.getPaint("A"));
89         m1.put("A", Color.red);
90         assertEquals(Color.red, m1.getPaint("A"));
91         m1.put("A", null);
92         assertEquals(null, m1.getPaint("A"));
93         
94         // a null key should throw an IllegalArgumentException
95
boolean pass = false;
96         try {
97             m1.getPaint(null);
98         }
99         catch (IllegalArgumentException JavaDoc 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         }
118         catch (IllegalArgumentException JavaDoc e) {
119             pass = true;
120         }
121         assertTrue(pass);
122     }
123     
124     /**
125      * Some checks for the equals() method.
126      */

127     public void testEquals() {
128         PaintMap m1 = new PaintMap();
129         PaintMap m2 = new PaintMap();
130         assertTrue(m1.equals(m1));
131         assertTrue(m1.equals(m2));
132         assertFalse(m1.equals(null));
133         assertFalse(m1.equals("ABC"));
134         
135         m1.put("K1", Color.red);
136         assertFalse(m1.equals(m2));
137         m2.put("K1", Color.red);
138         assertTrue(m1.equals(m2));
139         
140         m1.put("K2", new GradientPaint JavaDoc(1.0f, 2.0f, Color.green, 3.0f, 4.0f,
141                 Color.yellow));
142         assertFalse(m1.equals(m2));
143         m2.put("K2", new GradientPaint JavaDoc(1.0f, 2.0f, Color.green, 3.0f, 4.0f,
144                 Color.yellow));
145         assertTrue(m1.equals(m2));
146         
147         m1.put("K2", null);
148         assertFalse(m1.equals(m2));
149         m2.put("K2", null);
150         assertTrue(m1.equals(m2));
151     }
152     
153     /**
154      * Some checks for cloning.
155      */

156     public void testCloning() {
157         PaintMap m1 = new PaintMap();
158         PaintMap m2 = null;
159         try {
160             m2 = (PaintMap) m1.clone();
161         }
162         catch (CloneNotSupportedException JavaDoc e) {
163             e.printStackTrace();
164         }
165         assertTrue(m1.equals(m2));
166         
167         m1.put("K1", Color.red);
168         m1.put("K2", new GradientPaint JavaDoc(1.0f, 2.0f, Color.green, 3.0f, 4.0f,
169                 Color.yellow));
170         try {
171             m2 = (PaintMap) m1.clone();
172         }
173         catch (CloneNotSupportedException JavaDoc e) {
174             e.printStackTrace();
175         }
176         assertTrue(m1.equals(m2));
177     }
178     
179     /**
180      * A check for serialization.
181      */

182     public void testSerialization1() {
183         PaintMap m1 = new PaintMap();
184         PaintMap m2 = null;
185         try {
186             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
187             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
188             out.writeObject(m1);
189             out.close();
190
191             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(new ByteArrayInputStream JavaDoc(
192                     buffer.toByteArray()));
193             m2 = (PaintMap) in.readObject();
194             in.close();
195         }
196         catch (Exception JavaDoc e) {
197             e.printStackTrace();
198         }
199         assertEquals(m1, m2);
200     }
201
202     /**
203      * A check for serialization.
204      */

205     public void testSerialization2() {
206         PaintMap m1 = new PaintMap();
207         m1.put("K1", Color.red);
208         m1.put("K2", new GradientPaint JavaDoc(1.0f, 2.0f, Color.green, 3.0f, 4.0f,
209                 Color.yellow));
210         PaintMap m2 = null;
211         try {
212             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
213             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
214             out.writeObject(m1);
215             out.close();
216
217             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(new ByteArrayInputStream JavaDoc(
218                     buffer.toByteArray()));
219             m2 = (PaintMap) in.readObject();
220             in.close();
221         }
222         catch (Exception JavaDoc e) {
223             e.printStackTrace();
224         }
225         assertEquals(m1, m2);
226     }
227
228 }
229
230
Popular Tags