KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > chart > title > junit > LegendTitleTests


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  * LegendTitleTests.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: LegendTitleTests.java,v 1.1.2.1 2006/10/03 15:41:29 mungady Exp $
36  *
37  * Changes
38  * -------
39  * 25-Feb-2005 : Version 1 (DG);
40  * 16-Mar-2005 : Extended testEquals() (DG);
41  *
42  */

43
44 package org.jfree.chart.title.junit;
45
46 import java.awt.Color JavaDoc;
47 import java.awt.Font JavaDoc;
48 import java.awt.GradientPaint 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.plot.XYPlot;
61 import org.jfree.chart.title.LegendTitle;
62 import org.jfree.ui.RectangleAnchor;
63 import org.jfree.ui.RectangleEdge;
64
65 /**
66  * Some tests for the {@link LegendTitle} class.
67  */

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

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

84     public LegendTitleTests(String JavaDoc name) {
85         super(name);
86     }
87
88     /**
89      * Check that the equals() method distinguishes all fields.
90      */

91     public void testEquals() {
92         XYPlot plot1 = new XYPlot();
93         LegendTitle t1 = new LegendTitle(plot1);
94         LegendTitle t2 = new LegendTitle(plot1);
95         assertEquals(t1, t2);
96         
97         t1.setBackgroundPaint(
98             new GradientPaint JavaDoc(1.0f, 2.0f, Color.red, 3.0f, 4.0f, Color.yellow)
99         );
100         assertFalse(t1.equals(t2));
101         t2.setBackgroundPaint(
102             new GradientPaint JavaDoc(1.0f, 2.0f, Color.red, 3.0f, 4.0f, Color.yellow)
103         );
104         assertTrue(t1.equals(t2));
105         
106         t1.setLegendItemGraphicEdge(RectangleEdge.BOTTOM);
107         assertFalse(t1.equals(t2));
108         t2.setLegendItemGraphicEdge(RectangleEdge.BOTTOM);
109         assertTrue(t1.equals(t2));
110         
111         t1.setLegendItemGraphicAnchor(RectangleAnchor.BOTTOM_LEFT);
112         assertFalse(t1.equals(t2));
113         t2.setLegendItemGraphicAnchor(RectangleAnchor.BOTTOM_LEFT);
114         assertTrue(t1.equals(t2));
115         
116         t1.setLegendItemGraphicLocation(RectangleAnchor.TOP_LEFT);
117         assertFalse(t1.equals(t2));
118         t2.setLegendItemGraphicLocation(RectangleAnchor.TOP_LEFT);
119         assertTrue(t1.equals(t2));
120         
121         t1.setItemFont(new Font JavaDoc("Dialog", Font.PLAIN, 19));
122         assertFalse(t1.equals(t2));
123         t2.setItemFont(new Font JavaDoc("Dialog", Font.PLAIN, 19));
124         assertTrue(t1.equals(t2));
125     }
126
127     /**
128      * Two objects that are equal are required to return the same hashCode.
129      */

130     public void testHashcode() {
131         XYPlot plot1 = new XYPlot();
132         LegendTitle t1 = new LegendTitle(plot1);
133         LegendTitle t2 = new LegendTitle(plot1);
134         assertTrue(t1.equals(t2));
135         int h1 = t1.hashCode();
136         int h2 = t2.hashCode();
137         assertEquals(h1, h2);
138     }
139     
140     /**
141      * Confirm that cloning works.
142      */

143     public void testCloning() {
144         XYPlot plot = new XYPlot();
145         LegendTitle t1 = new LegendTitle(plot);
146         t1.setBackgroundPaint(
147             new GradientPaint JavaDoc(1.0f, 2.0f, Color.red, 3.0f, 4.0f, Color.yellow)
148         );
149         LegendTitle t2 = null;
150         try {
151             t2 = (LegendTitle) t1.clone();
152         }
153         catch (CloneNotSupportedException JavaDoc e) {
154             System.err.println("Failed to clone.");
155         }
156         assertTrue(t1 != t2);
157         assertTrue(t1.getClass() == t2.getClass());
158         assertTrue(t1.equals(t2));
159     }
160
161     /**
162      * Serialize an instance, restore it, and check for equality.
163      */

164     public void testSerialization() {
165
166         XYPlot plot = new XYPlot();
167         LegendTitle t1 = new LegendTitle(plot);
168         LegendTitle t2 = null;
169
170         try {
171             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
172             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
173             out.writeObject(t1);
174             out.close();
175
176             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
177                 new ByteArrayInputStream JavaDoc(buffer.toByteArray())
178             );
179             t2 = (LegendTitle) in.readObject();
180             in.close();
181         }
182         catch (Exception JavaDoc e) {
183             fail(e.toString());
184         }
185         assertTrue(t1.equals(t2));
186         assertTrue(t2.getSources()[0].equals(plot));
187     }
188
189 }
190
Popular Tags