KickJava   Java API By Example, From Geeks To Geeks.

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


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  * LegendItemCollectionTests.java
29  * ------------------------------
30  * (C) Copyright 2005, by Object Refinery Limited and Contributors.
31  *
32  * Original Author: David Gilbert (for Object Refinery Limited);
33  * Contributor(s): -;
34  *
35  * $Id: LegendItemCollectionTests.java,v 1.1.2.1 2006/10/03 15:41:33 mungady Exp $
36  *
37  * Changes
38  * -------
39  * 18-Apr-2005 : Version 1 (DG);
40  *
41  */

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

66 public class LegendItemCollectionTests extends TestCase {
67
68     /**
69      * Returns the tests as a test suite.
70      *
71      * @return The test suite.
72      */

73     public static Test suite() {
74         return new TestSuite(LegendItemCollectionTests.class);
75     }
76
77     /**
78      * Constructs a new set of tests.
79      *
80      * @param name the name of the tests.
81      */

82     public LegendItemCollectionTests(String JavaDoc name) {
83         super(name);
84     }
85
86     /**
87      * Confirm that the equals method can distinguish all the required fields.
88      */

89     public void testEquals() {
90         
91         LegendItemCollection c1 = new LegendItemCollection();
92         LegendItemCollection c2 = new LegendItemCollection();
93         assertTrue(c1.equals(c2));
94         assertTrue(c2.equals(c1));
95
96         LegendItem item1 = new LegendItem("Label", "Description",
97                 "ToolTip", "URL", true,
98                 new Rectangle2D.Double JavaDoc(1.0, 2.0, 3.0, 4.0), true, Color.red,
99                 true, Color.blue, new BasicStroke JavaDoc(1.2f), true,
100                 new Line2D.Double JavaDoc(1.0, 2.0, 3.0, 4.0),
101                 new BasicStroke JavaDoc(2.1f), Color.green);
102         LegendItem item2 = new LegendItem("Label", "Description",
103                 "ToolTip", "URL", true,
104                 new Rectangle2D.Double JavaDoc(1.0, 2.0, 3.0, 4.0),
105                 true, Color.red, true, Color.blue, new BasicStroke JavaDoc(1.2f), true,
106                 new Line2D.Double JavaDoc(1.0, 2.0, 3.0, 4.0), new BasicStroke JavaDoc(2.1f),
107                 Color.green);
108         c1.add(item1);
109         c2.add(item2);
110         assertTrue(c1.equals(c2));
111         
112     }
113    
114
115     /**
116      * Serialize an instance, restore it, and check for equality.
117      */

118     public void testSerialization() {
119         LegendItemCollection c1 = new LegendItemCollection();
120         c1.add(new LegendItem("Item", "Description", "ToolTip", "URL",
121             new Rectangle2D.Double JavaDoc(1.0, 2.0, 3.0, 4.0), Color.red));
122         LegendItemCollection c2 = null;
123         try {
124             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
125             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
126             out.writeObject(c1);
127             out.close();
128
129             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
130                 new ByteArrayInputStream JavaDoc(buffer.toByteArray())
131             );
132             c2 = (LegendItemCollection) in.readObject();
133             in.close();
134         }
135         catch (Exception JavaDoc e) {
136             System.out.println(e.toString());
137         }
138         assertEquals(c1, c2);
139     }
140     
141     /**
142      * Confirm that cloning works.
143      */

144     public void testCloning() {
145
146         LegendItemCollection c1 = new LegendItemCollection();
147         c1.add(new LegendItem("Item", "Description", "ToolTip", "URL",
148             new Rectangle2D.Double JavaDoc(1.0, 2.0, 3.0, 4.0), Color.red));
149         LegendItemCollection c2 = null;
150         try {
151             c2 = (LegendItemCollection) c1.clone();
152         }
153         catch (CloneNotSupportedException JavaDoc e) {
154             System.err.println("Failed to clone.");
155         }
156         assertTrue(c1 != c2);
157         assertTrue(c1.getClass() == c2.getClass());
158         assertTrue(c1.equals(c2));
159         
160     }
161
162 }
163
Popular Tags