KickJava   Java API By Example, From Geeks To Geeks.

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


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  * LegendGraphicTests.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: LegendGraphicTests.java,v 1.1.2.1 2006/10/03 15:41:29 mungady Exp $
36  *
37  * Changes
38  * -------
39  * 01-Sep-2005 : Version 1 (DG);
40  *
41  */

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

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

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

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

90     public void testEquals() {
91         LegendGraphic g1 = new LegendGraphic(new Rectangle2D.Double JavaDoc(1.0, 2.0,
92                 3.0, 4.0), Color.black);
93         LegendGraphic g2 = new LegendGraphic(new Rectangle2D.Double JavaDoc(1.0, 2.0,
94                 3.0, 4.0), Color.black);
95         assertEquals(g1, g2);
96         assertEquals(g2, g1);
97         
98         // TODO: write all the tests that go here
99
// shapeVisible
100
g1.setShapeVisible(!g1.isShapeVisible());
101         assertFalse(g1.equals(g2));
102         g2.setShapeVisible(!g2.isShapeVisible());
103         assertTrue(g1.equals(g2));
104         
105         // shape
106
g1.setShape(new Rectangle2D.Double JavaDoc(4.0, 3.0, 2.0, 1.0));
107         assertFalse(g1.equals(g2));
108         g2.setShape(new Rectangle2D.Double JavaDoc(4.0, 3.0, 2.0, 1.0));
109         assertTrue(g1.equals(g2));
110
111         // shapeFilled
112
g1.setShapeFilled(!g1.isShapeFilled());
113         assertFalse(g1.equals(g2));
114         g2.setShapeFilled(!g2.isShapeFilled());
115         assertTrue(g1.equals(g2));
116         
117         // fillPaint
118
g1.setFillPaint(Color.green);
119         assertFalse(g1.equals(g2));
120         g2.setFillPaint(Color.green);
121         assertTrue(g1.equals(g2));
122         
123         // shapeOutlineVisible
124
g1.setShapeOutlineVisible(!g1.isShapeOutlineVisible());
125         assertFalse(g1.equals(g2));
126         g2.setShapeOutlineVisible(!g2.isShapeOutlineVisible());
127         assertTrue(g1.equals(g2));
128
129         // outlinePaint
130
g1.setOutlinePaint(Color.green);
131         assertFalse(g1.equals(g2));
132         g2.setOutlinePaint(Color.green);
133         assertTrue(g1.equals(g2));
134
135         // outlineStroke
136
g1.setOutlineStroke(new BasicStroke JavaDoc(1.23f));
137         assertFalse(g1.equals(g2));
138         g2.setOutlineStroke(new BasicStroke JavaDoc(1.23f));
139         assertTrue(g1.equals(g2));
140         
141         // shapeAnchor
142
g1.setShapeAnchor(RectangleAnchor.BOTTOM_RIGHT);
143         assertFalse(g1.equals(g2));
144         g2.setShapeAnchor(RectangleAnchor.BOTTOM_RIGHT);
145         assertTrue(g1.equals(g2));
146         
147         // shapeLocation
148
g1.setShapeLocation(RectangleAnchor.BOTTOM_RIGHT);
149         assertFalse(g1.equals(g2));
150         g2.setShapeLocation(RectangleAnchor.BOTTOM_RIGHT);
151         assertTrue(g1.equals(g2));
152         
153         // lineVisible
154
g1.setLineVisible(!g1.isLineVisible());
155         assertFalse(g1.equals(g2));
156         g2.setLineVisible(!g2.isLineVisible());
157         assertTrue(g1.equals(g2));
158         
159         // line
160
g1.setLine(new Line2D.Double JavaDoc(1.0, 2.0, 3.0, 4.0));
161         assertFalse(g1.equals(g2));
162         g2.setLine(new Line2D.Double JavaDoc(1.0, 2.0, 3.0, 4.0));
163         assertTrue(g1.equals(g2));
164         
165         // linePaint
166
g1.setLinePaint(Color.green);
167         assertFalse(g1.equals(g2));
168         g2.setLinePaint(Color.green);
169         assertTrue(g1.equals(g2));
170         
171         // lineStroke
172
g1.setLineStroke(new BasicStroke JavaDoc(1.23f));
173         assertFalse(g1.equals(g2));
174         g2.setLineStroke(new BasicStroke JavaDoc(1.23f));
175         assertTrue(g1.equals(g2));
176     }
177
178     /**
179      * Two objects that are equal are required to return the same hashCode.
180      */

181     public void testHashcode() {
182         LegendGraphic g1 = new LegendGraphic(new Rectangle2D.Double JavaDoc(1.0, 2.0, 3.0, 4.0), Color.black);
183         LegendGraphic g2 = new LegendGraphic(new Rectangle2D.Double JavaDoc(1.0, 2.0, 3.0, 4.0), Color.black);
184         assertTrue(g1.equals(g2));
185         int h1 = g1.hashCode();
186         int h2 = g2.hashCode();
187         assertEquals(h1, h2);
188     }
189     
190     /**
191      * Confirm that cloning works.
192      */

193     public void testCloning() {
194         LegendGraphic g1 = new LegendGraphic(new Rectangle2D.Double JavaDoc(1.0, 2.0, 3.0, 4.0), Color.black);
195         LegendGraphic g2 = null;
196         try {
197             g2 = (LegendGraphic) g1.clone();
198         }
199         catch (CloneNotSupportedException JavaDoc e) {
200             System.err.println("Failed to clone.");
201         }
202         assertTrue(g1 != g2);
203         assertTrue(g1.getClass() == g2.getClass());
204         assertTrue(g1.equals(g2));
205     }
206
207     /**
208      * Serialize an instance, restore it, and check for equality.
209      */

210     public void testSerialization() {
211
212         Stroke JavaDoc s = new BasicStroke JavaDoc(1.23f);
213         LegendGraphic g1 = new LegendGraphic(new Rectangle2D.Double JavaDoc(1.0, 2.0, 3.0, 4.0), Color.black);
214         g1.setOutlineStroke(s);
215         LegendGraphic g2 = null;
216
217         try {
218             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
219             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
220             out.writeObject(g1);
221             out.close();
222
223             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
224                 new ByteArrayInputStream JavaDoc(buffer.toByteArray())
225             );
226             g2 = (LegendGraphic) in.readObject();
227             in.close();
228         }
229         catch (Exception JavaDoc e) {
230             System.out.println(e.toString());
231         }
232         assertTrue(g1.equals(g2));
233
234     }
235
236 }
237
Popular Tags