KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > chart > annotations > junit > XYShapeAnnotationTests


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  * XYShapeAnnotationTests.java
29  * ---------------------------
30  * (C) Copyright 2004-2006, by Object Refinery Limited and Contributors.
31  *
32  * Original Author: David Gilbert (for Object Refinery Limited);
33  * Contributor(s): -;
34  *
35  * $Id: XYShapeAnnotationTests.java,v 1.1.2.2 2006/10/24 13:57:52 mungady Exp $
36  *
37  * Changes
38  * -------
39  * 29-Sep-2004 : Version 1 (DG);
40  * 07-Jan-2005 : Added hashCode() test (DG);
41  *
42  */

43
44 package org.jfree.chart.annotations.junit;
45
46 import java.awt.BasicStroke JavaDoc;
47 import java.awt.Color JavaDoc;
48 import java.awt.GradientPaint 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.annotations.XYShapeAnnotation;
62
63 /**
64  * Some tests for the {@link XYShapeAnnotation} class.
65  */

66 public class XYShapeAnnotationTests 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(XYShapeAnnotationTests.class);
75     }
76
77     /**
78      * Constructs a new set of tests.
79      *
80      * @param name the name of the tests.
81      */

82     public XYShapeAnnotationTests(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         XYShapeAnnotation a1 = new XYShapeAnnotation(
92                 new Rectangle2D.Double JavaDoc(1.0, 2.0, 3.0, 4.0),
93                 new BasicStroke JavaDoc(1.2f), Color.red, Color.blue);
94         XYShapeAnnotation a2 = new XYShapeAnnotation(
95                 new Rectangle2D.Double JavaDoc(1.0, 2.0, 3.0, 4.0),
96                 new BasicStroke JavaDoc(1.2f), Color.red, Color.blue);
97         assertTrue(a1.equals(a2));
98         assertTrue(a2.equals(a1));
99       
100         // shape
101
a1 = new XYShapeAnnotation(
102                 new Rectangle2D.Double JavaDoc(4.0, 3.0, 2.0, 1.0),
103                 new BasicStroke JavaDoc(1.2f), Color.red, Color.blue);
104         assertFalse(a1.equals(a2));
105         a2 = new XYShapeAnnotation(
106                 new Rectangle2D.Double JavaDoc(4.0, 3.0, 2.0, 1.0),
107                 new BasicStroke JavaDoc(1.2f), Color.red, Color.blue);
108         assertTrue(a1.equals(a2));
109         
110         // stroke
111
a1 = new XYShapeAnnotation(
112                 new Rectangle2D.Double JavaDoc(4.0, 3.0, 2.0, 1.0),
113                 new BasicStroke JavaDoc(2.3f), Color.red, Color.blue);
114         assertFalse(a1.equals(a2));
115         a2 = new XYShapeAnnotation(
116                 new Rectangle2D.Double JavaDoc(4.0, 3.0, 2.0, 1.0),
117                 new BasicStroke JavaDoc(2.3f), Color.red, Color.blue);
118         assertTrue(a1.equals(a2));
119         
120         GradientPaint JavaDoc gp1a = new GradientPaint JavaDoc(1.0f, 2.0f, Color.blue,
121                 3.0f, 4.0f, Color.red);
122         GradientPaint JavaDoc gp1b = new GradientPaint JavaDoc(1.0f, 2.0f, Color.blue,
123                 3.0f, 4.0f, Color.red);
124         GradientPaint JavaDoc gp2a = new GradientPaint JavaDoc(5.0f, 6.0f, Color.pink,
125                 7.0f, 8.0f, Color.white);
126         GradientPaint JavaDoc gp2b = new GradientPaint JavaDoc(5.0f, 6.0f, Color.pink,
127                 7.0f, 8.0f, Color.white);
128         
129         // outlinePaint
130
a1 = new XYShapeAnnotation(
131                 new Rectangle2D.Double JavaDoc(4.0, 3.0, 2.0, 1.0),
132                 new BasicStroke JavaDoc(2.3f), gp1a, Color.blue);
133         assertFalse(a1.equals(a2));
134         a2 = new XYShapeAnnotation(
135                 new Rectangle2D.Double JavaDoc(4.0, 3.0, 2.0, 1.0),
136                 new BasicStroke JavaDoc(2.3f), gp1b, Color.blue);
137         assertTrue(a1.equals(a2));
138         
139         // fillPaint
140
a1 = new XYShapeAnnotation(
141                 new Rectangle2D.Double JavaDoc(4.0, 3.0, 2.0, 1.0),
142                 new BasicStroke JavaDoc(2.3f), gp1a, gp2a);
143         assertFalse(a1.equals(a2));
144         a2 = new XYShapeAnnotation(
145                 new Rectangle2D.Double JavaDoc(4.0, 3.0, 2.0, 1.0),
146                 new BasicStroke JavaDoc(2.3f), gp1b, gp2b);
147         assertTrue(a1.equals(a2));
148     }
149
150     /**
151      * Two objects that are equal are required to return the same hashCode.
152      */

153     public void testHashCode() {
154         XYShapeAnnotation a1 = new XYShapeAnnotation(
155                 new Rectangle2D.Double JavaDoc(1.0, 2.0, 3.0, 4.0),
156                 new BasicStroke JavaDoc(1.2f), Color.red, Color.blue);
157         XYShapeAnnotation a2 = new XYShapeAnnotation(
158                 new Rectangle2D.Double JavaDoc(1.0, 2.0, 3.0, 4.0),
159                 new BasicStroke JavaDoc(1.2f), Color.red, Color.blue);
160         assertTrue(a1.equals(a2));
161         int h1 = a1.hashCode();
162         int h2 = a2.hashCode();
163         assertEquals(h1, h2);
164     }
165
166     /**
167      * Confirm that cloning works.
168      */

169     public void testCloning() {
170
171         XYShapeAnnotation a1 = new XYShapeAnnotation(
172                 new Rectangle2D.Double JavaDoc(1.0, 2.0, 3.0, 4.0),
173                 new BasicStroke JavaDoc(1.2f), Color.red, Color.blue);
174         XYShapeAnnotation a2 = null;
175         try {
176             a2 = (XYShapeAnnotation) a1.clone();
177         }
178         catch (CloneNotSupportedException JavaDoc e) {
179             e.printStackTrace();
180         }
181         assertTrue(a1 != a2);
182         assertTrue(a1.getClass() == a2.getClass());
183         assertTrue(a1.equals(a2));
184     }
185
186     /**
187      * Serialize an instance, restore it, and check for equality.
188      */

189     public void testSerialization() {
190         XYShapeAnnotation a1 = new XYShapeAnnotation(
191                 new Rectangle2D.Double JavaDoc(1.0, 2.0, 3.0, 4.0),
192                 new BasicStroke JavaDoc(1.2f), Color.red, Color.blue);
193         XYShapeAnnotation a2 = null;
194
195         try {
196             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
197             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
198             out.writeObject(a1);
199             out.close();
200
201             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
202                     new ByteArrayInputStream JavaDoc(buffer.toByteArray()));
203             a2 = (XYShapeAnnotation) in.readObject();
204             in.close();
205         }
206         catch (Exception JavaDoc e) {
207             e.printStackTrace();
208         }
209         assertEquals(a1, a2);
210     }
211
212 }
213
Popular Tags