KickJava   Java API By Example, From Geeks To Geeks.

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


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  * LegendItemTests.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: LegendItemTests.java,v 1.1.2.1 2006/10/03 15:41:30 mungady Exp $
36  *
37  * Changes
38  * -------
39  * 04-Jun-2004 : Version 1 (DG);
40  * 10-Dec-2005 : Addded new test to cover bug report 1374328 (DG);
41  *
42  */

43
44 package org.jfree.chart.junit;
45
46 import java.awt.BasicStroke JavaDoc;
47 import java.awt.Color JavaDoc;
48 import java.awt.Font JavaDoc;
49 import java.awt.font.TextAttribute JavaDoc;
50 import java.awt.geom.Line2D JavaDoc;
51 import java.awt.geom.Rectangle2D JavaDoc;
52 import java.io.ByteArrayInputStream JavaDoc;
53 import java.io.ByteArrayOutputStream JavaDoc;
54 import java.io.ObjectInput JavaDoc;
55 import java.io.ObjectInputStream JavaDoc;
56 import java.io.ObjectOutput JavaDoc;
57 import java.io.ObjectOutputStream JavaDoc;
58 import java.text.AttributedString JavaDoc;
59
60 import junit.framework.Test;
61 import junit.framework.TestCase;
62 import junit.framework.TestSuite;
63
64 import org.jfree.chart.LegendItem;
65
66 /**
67  * Tests for the {@link LegendItem} class.
68  */

69 public class LegendItemTests extends TestCase {
70
71     /**
72      * Returns the tests as a test suite.
73      *
74      * @return The test suite.
75      */

76     public static Test suite() {
77         return new TestSuite(LegendItemTests.class);
78     }
79
80     /**
81      * Constructs a new set of tests.
82      *
83      * @param name the name of the tests.
84      */

85     public LegendItemTests(String JavaDoc name) {
86         super(name);
87     }
88
89     /**
90      * Confirm that the equals method can distinguish all the required fields.
91      */

92     public void testEquals() {
93         
94         LegendItem item1 = new LegendItem("Label", "Description",
95                 "ToolTip", "URL", true,
96                 new Rectangle2D.Double JavaDoc(1.0, 2.0, 3.0, 4.0), true, Color.red,
97                 true, Color.blue, new BasicStroke JavaDoc(1.2f), true,
98                 new Line2D.Double JavaDoc(1.0, 2.0, 3.0, 4.0),
99                 new BasicStroke JavaDoc(2.1f), Color.green);
100         LegendItem item2 = new LegendItem("Label", "Description",
101                 "ToolTip", "URL", true,
102                 new Rectangle2D.Double JavaDoc(1.0, 2.0, 3.0, 4.0),
103                 true, Color.red, true, Color.blue, new BasicStroke JavaDoc(1.2f), true,
104                 new Line2D.Double JavaDoc(1.0, 2.0, 3.0, 4.0), new BasicStroke JavaDoc(2.1f),
105                 Color.green);
106         assertTrue(item1.equals(item2));
107         assertTrue(item2.equals(item1));
108         
109         item1 = new LegendItem("Label2", "Description", "ToolTip", "URL",
110                 true, new Rectangle2D.Double JavaDoc(1.0, 2.0, 3.0, 4.0), true,
111                 Color.red, true, Color.blue, new BasicStroke JavaDoc(1.2f), true,
112                 new Line2D.Double JavaDoc(1.0, 2.0, 3.0, 4.0), new BasicStroke JavaDoc(2.1f),
113                 Color.green);
114         assertFalse(item1.equals(item2));
115         item2 = new LegendItem("Label2", "Description", "ToolTip", "URL",
116                 true, new Rectangle2D.Double JavaDoc(1.0, 2.0, 3.0, 4.0),
117                 true, Color.red, true, Color.blue, new BasicStroke JavaDoc(1.2f), true,
118                 new Line2D.Double JavaDoc(1.0, 2.0, 3.0, 4.0), new BasicStroke JavaDoc(2.1f),
119                 Color.green);
120         assertTrue(item1.equals(item2));
121         
122         item1 = new LegendItem("Label2", "Description2", "ToolTip",
123                 "URL", true, new Rectangle2D.Double JavaDoc(1.0, 2.0, 3.0, 4.0),
124                 true, Color.red, true, Color.blue, new BasicStroke JavaDoc(1.2f), true,
125                 new Line2D.Double JavaDoc(1.0, 2.0, 3.0, 4.0), new BasicStroke JavaDoc(2.1f),
126                 Color.green);
127         assertFalse(item1.equals(item2));
128         item2 = new LegendItem("Label2", "Description2", "ToolTip",
129                 "URL", true, new Rectangle2D.Double JavaDoc(1.0, 2.0, 3.0, 4.0),
130                 true, Color.red, true, Color.blue, new BasicStroke JavaDoc(1.2f), true,
131                 new Line2D.Double JavaDoc(1.0, 2.0, 3.0, 4.0), new BasicStroke JavaDoc(2.1f),
132                 Color.green);
133         assertTrue(item1.equals(item2));
134         
135         item1 = new LegendItem("Label2", "Description2", "ToolTip",
136                 "URL", false, new Rectangle2D.Double JavaDoc(1.0, 2.0, 3.0, 4.0),
137                 true, Color.red, true, Color.blue, new BasicStroke JavaDoc(1.2f), true,
138                 new Line2D.Double JavaDoc(1.0, 2.0, 3.0, 4.0), new BasicStroke JavaDoc(2.1f),
139                 Color.green);
140         assertFalse(item1.equals(item2));
141         item2 = new LegendItem("Label2", "Description2", "ToolTip",
142                 "URL", false, new Rectangle2D.Double JavaDoc(1.0, 2.0, 3.0, 4.0),
143                 true, Color.red, true, Color.blue, new BasicStroke JavaDoc(1.2f), true,
144                 new Line2D.Double JavaDoc(1.0, 2.0, 3.0, 4.0), new BasicStroke JavaDoc(2.1f),
145                 Color.green);
146         assertTrue(item1.equals(item2));
147         
148         item1 = new LegendItem("Label2", "Description2", "ToolTip",
149                 "URL", false, new Rectangle2D.Double JavaDoc(4.0, 3.0, 2.0, 1.0),
150                 true, Color.red, true, Color.blue, new BasicStroke JavaDoc(1.2f), true,
151                 new Line2D.Double JavaDoc(1.0, 2.0, 3.0, 4.0), new BasicStroke JavaDoc(2.1f),
152                 Color.green);
153         assertFalse(item1.equals(item2));
154         item2 = new LegendItem("Label2", "Description2", "ToolTip",
155                 "URL", false, new Rectangle2D.Double JavaDoc(4.0, 3.0, 2.0, 1.0),
156                 true, Color.red, true, Color.blue, new BasicStroke JavaDoc(1.2f), true,
157                 new Line2D.Double JavaDoc(1.0, 2.0, 3.0, 4.0), new BasicStroke JavaDoc(2.1f),
158                 Color.green);
159         assertTrue(item1.equals(item2));
160         
161         item1 = new LegendItem("Label2", "Description2", "ToolTip",
162                 "URL", false, new Rectangle2D.Double JavaDoc(4.0, 3.0, 2.0, 1.0),
163                 false, Color.red, true, Color.blue, new BasicStroke JavaDoc(1.2f), true,
164                 new Line2D.Double JavaDoc(1.0, 2.0, 3.0, 4.0), new BasicStroke JavaDoc(2.1f),
165                 Color.green);
166         assertFalse(item1.equals(item2));
167         item2 = new LegendItem("Label2", "Description2", "ToolTip",
168                 "URL", false, new Rectangle2D.Double JavaDoc(4.0, 3.0, 2.0, 1.0),
169                 false, Color.red, true, Color.blue, new BasicStroke JavaDoc(1.2f), true,
170                 new Line2D.Double JavaDoc(1.0, 2.0, 3.0, 4.0), new BasicStroke JavaDoc(2.1f),
171                 Color.green);
172         assertTrue(item1.equals(item2));
173         
174         item1 = new LegendItem("Label2", "Description2", "ToolTip", "URL",
175                 false, new Rectangle2D.Double JavaDoc(4.0, 3.0, 2.0, 1.0), false,
176                 Color.black, true, Color.blue, new BasicStroke JavaDoc(1.2f), true,
177                 new Line2D.Double JavaDoc(1.0, 2.0, 3.0, 4.0), new BasicStroke JavaDoc(2.1f),
178                 Color.green);
179         assertFalse(item1.equals(item2));
180         item2 = new LegendItem("Label2", "Description2", "ToolTip", "URL",
181                 false, new Rectangle2D.Double JavaDoc(4.0, 3.0, 2.0, 1.0), false,
182                 Color.black, true, Color.blue, new BasicStroke JavaDoc(1.2f), true,
183                 new Line2D.Double JavaDoc(1.0, 2.0, 3.0, 4.0), new BasicStroke JavaDoc(2.1f),
184                 Color.green);
185         assertTrue(item1.equals(item2));
186         
187         item1 = new LegendItem("Label2", "Description2", "ToolTip",
188                 "URL", false, new Rectangle2D.Double JavaDoc(4.0, 3.0, 2.0, 1.0),
189                 false, Color.black, false, Color.blue, new BasicStroke JavaDoc(1.2f),
190                 true, new Line2D.Double JavaDoc(1.0, 2.0, 3.0, 4.0),
191                 new BasicStroke JavaDoc(2.1f), Color.green);
192         assertFalse(item1.equals(item2));
193         item2 = new LegendItem("Label2", "Description2", "ToolTip", "URL",
194                 false, new Rectangle2D.Double JavaDoc(4.0, 3.0, 2.0, 1.0), false,
195                 Color.black, false, Color.blue, new BasicStroke JavaDoc(1.2f), true,
196                 new Line2D.Double JavaDoc(1.0, 2.0, 3.0, 4.0), new BasicStroke JavaDoc(2.1f),
197                 Color.green);
198         assertTrue(item1.equals(item2));
199         
200         item1 = new LegendItem("Label2", "Description2", "ToolTip", "URL",
201                 false, new Rectangle2D.Double JavaDoc(4.0, 3.0, 2.0, 1.0), false,
202                 Color.black, false, Color.yellow, new BasicStroke JavaDoc(1.2f), true,
203                 new Line2D.Double JavaDoc(1.0, 2.0, 3.0, 4.0), new BasicStroke JavaDoc(2.1f),
204                 Color.green);
205         assertFalse(item1.equals(item2));
206         item2 = new LegendItem("Label2", "Description2", "ToolTip", "URL",
207                 false, new Rectangle2D.Double JavaDoc(4.0, 3.0, 2.0, 1.0), false,
208                 Color.black, false, Color.yellow, new BasicStroke JavaDoc(1.2f), true,
209                 new Line2D.Double JavaDoc(1.0, 2.0, 3.0, 4.0), new BasicStroke JavaDoc(2.1f),
210                 Color.green);
211         assertTrue(item1.equals(item2));
212         
213         item1 = new LegendItem("Label2", "Description2", "ToolTip", "URL",
214                 false, new Rectangle2D.Double JavaDoc(4.0, 3.0, 2.0, 1.0), false,
215                 Color.black, false, Color.yellow, new BasicStroke JavaDoc(2.1f), true,
216                 new Line2D.Double JavaDoc(1.0, 2.0, 3.0, 4.0), new BasicStroke JavaDoc(2.1f),
217                 Color.green);
218         assertFalse(item1.equals(item2));
219         item2 = new LegendItem("Label2", "Description2", "ToolTip", "URL",
220                 false, new Rectangle2D.Double JavaDoc(4.0, 3.0, 2.0, 1.0), false,
221                 Color.black, false, Color.yellow, new BasicStroke JavaDoc(2.1f), true,
222                 new Line2D.Double JavaDoc(1.0, 2.0, 3.0, 4.0), new BasicStroke JavaDoc(2.1f),
223                 Color.green);
224         assertTrue(item1.equals(item2));
225         
226         item1 = new LegendItem("Label2", "Description2", "ToolTip",
227                 "URL", false, new Rectangle2D.Double JavaDoc(4.0, 3.0, 2.0, 1.0),
228                 false, Color.black, false, Color.yellow, new BasicStroke JavaDoc(2.1f),
229                 false, new Line2D.Double JavaDoc(1.0, 2.0, 3.0, 4.0),
230                 new BasicStroke JavaDoc(2.1f), Color.green);
231         assertFalse(item1.equals(item2));
232         item2 = new LegendItem("Label2", "Description2", "ToolTip",
233                 "URL", false, new Rectangle2D.Double JavaDoc(4.0, 3.0, 2.0, 1.0),
234                 false, Color.black, false, Color.yellow, new BasicStroke JavaDoc(2.1f),
235                 false, new Line2D.Double JavaDoc(1.0, 2.0, 3.0, 4.0),
236                 new BasicStroke JavaDoc(2.1f), Color.green);
237         assertTrue(item1.equals(item2));
238         
239         item1 = new LegendItem("Label2", "Description2", "ToolTip",
240                 "URL", false, new Rectangle2D.Double JavaDoc(4.0, 3.0, 2.0, 1.0),
241                 false, Color.black, false, Color.yellow, new BasicStroke JavaDoc(2.1f),
242                 false, new Line2D.Double JavaDoc(4.0, 3.0, 2.0, 1.0),
243                 new BasicStroke JavaDoc(2.1f), Color.green);
244         assertFalse(item1.equals(item2));
245         item2 = new LegendItem("Label2", "Description2", "ToolTip",
246                 "URL", false, new Rectangle2D.Double JavaDoc(4.0, 3.0, 2.0, 1.0),
247                 false, Color.black, false, Color.yellow, new BasicStroke JavaDoc(2.1f),
248                 false, new Line2D.Double JavaDoc(4.0, 3.0, 2.0, 1.0),
249                 new BasicStroke JavaDoc(2.1f), Color.green);
250         assertTrue(item1.equals(item2));
251         
252         item1 = new LegendItem("Label2", "Description2", "ToolTip",
253                 "URL", false, new Rectangle2D.Double JavaDoc(4.0, 3.0, 2.0, 1.0),
254                 false, Color.black, false, Color.yellow, new BasicStroke JavaDoc(2.1f),
255                 false, new Line2D.Double JavaDoc(4.0, 3.0, 2.0, 1.0),
256                 new BasicStroke JavaDoc(3.3f), Color.green);
257         assertFalse(item1.equals(item2));
258         item2 = new LegendItem("Label2", "Description2", "ToolTip",
259                 "URL", false, new Rectangle2D.Double JavaDoc(4.0, 3.0, 2.0, 1.0),
260                 false, Color.black, false, Color.yellow, new BasicStroke JavaDoc(2.1f),
261                 false, new Line2D.Double JavaDoc(4.0, 3.0, 2.0, 1.0),
262                 new BasicStroke JavaDoc(3.3f), Color.green);
263         assertTrue(item1.equals(item2));
264         
265         item1 = new LegendItem("Label2", "Description2", "ToolTip", "URL",
266                 false, new Rectangle2D.Double JavaDoc(4.0, 3.0, 2.0, 1.0), false,
267                 Color.black, false, Color.yellow, new BasicStroke JavaDoc(2.1f), false,
268             new Line2D.Double JavaDoc(4.0, 3.0, 2.0, 1.0), new BasicStroke JavaDoc(3.3f),
269             Color.white
270         );
271         assertFalse(item1.equals(item2));
272         item2 = new LegendItem("Label2", "Description2", "ToolTip",
273                 "URL", false, new Rectangle2D.Double JavaDoc(4.0, 3.0, 2.0, 1.0),
274                 false, Color.black, false, Color.yellow, new BasicStroke JavaDoc(2.1f),
275                 false, new Line2D.Double JavaDoc(4.0, 3.0, 2.0, 1.0),
276                 new BasicStroke JavaDoc(3.3f),
277                 Color.white);
278         assertTrue(item1.equals(item2));
279         
280     }
281
282     /**
283      * Serialize an instance, restore it, and check for equality.
284      */

285     public void testSerialization() {
286         LegendItem item1 = new LegendItem("Item", "Description",
287                 "ToolTip", "URL",
288                 new Rectangle2D.Double JavaDoc(1.0, 2.0, 3.0, 4.0), Color.red);
289         LegendItem item2 = null;
290         try {
291             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
292             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
293             out.writeObject(item1);
294             out.close();
295
296             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
297                 new ByteArrayInputStream JavaDoc(buffer.toByteArray())
298             );
299             item2 = (LegendItem) in.readObject();
300             in.close();
301         }
302         catch (Exception JavaDoc e) {
303             System.out.println(e.toString());
304         }
305         assertEquals(item1, item2);
306     }
307
308     /**
309      * Serialize an instance, restore it, and check for equality.
310      */

311     public void testSerialization2() {
312         AttributedString JavaDoc as = new AttributedString JavaDoc("Test String");
313         as.addAttribute(TextAttribute.FONT, new Font JavaDoc("Dialog", Font.PLAIN, 12));
314         LegendItem item1 = new LegendItem(as, "Description", "ToolTip", "URL",
315                 new Rectangle2D.Double JavaDoc(1.0, 2.0, 3.0, 4.0), Color.red);
316         LegendItem item2 = null;
317         try {
318             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
319             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
320             out.writeObject(item1);
321             out.close();
322
323             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
324                 new ByteArrayInputStream JavaDoc(buffer.toByteArray())
325             );
326             item2 = (LegendItem) in.readObject();
327             in.close();
328         }
329         catch (Exception JavaDoc e) {
330             System.out.println(e.toString());
331         }
332         assertEquals(item1, item2);
333     }
334
335     /**
336      * Cloning is not required for this class as it is designed to be
337      * immutable.
338      */

339     public void testCloning() {
340         LegendItem item = new LegendItem("Item", "Description",
341                 "ToolTip", "URL",
342                 new Rectangle2D.Double JavaDoc(1.0, 2.0, 3.0, 4.0), Color.red);
343         assertFalse(item instanceof Cloneable JavaDoc);
344     }
345
346 }
347
Popular Tags