KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

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

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

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

88     public void testEquals() {
89         Stroke JavaDoc stroke1 = new BasicStroke JavaDoc(2.0f);
90         Stroke JavaDoc stroke2 = new BasicStroke JavaDoc(2.5f);
91         XYPolygonAnnotation a1 = new XYPolygonAnnotation(new double[] {1.0,
92                 2.0, 3.0, 4.0, 5.0, 6.0}, stroke1, Color.red, Color.blue);
93         XYPolygonAnnotation a2 = new XYPolygonAnnotation(new double[] {1.0,
94                 2.0, 3.0, 4.0, 5.0, 6.0}, stroke1, Color.red, Color.blue);
95         assertTrue(a1.equals(a2));
96         assertTrue(a2.equals(a1));
97         
98         a1 = new XYPolygonAnnotation(new double[] {99.0, 2.0, 3.0, 4.0, 5.0,
99                 6.0}, stroke1, Color.red, Color.blue);
100         assertFalse(a1.equals(a2));
101         a2 = new XYPolygonAnnotation(new double[] {99.0, 2.0, 3.0, 4.0, 5.0,
102                 6.0}, stroke1, Color.red, Color.blue);
103         assertTrue(a1.equals(a2));
104
105         a1 = new XYPolygonAnnotation(new double[] {99.0, 2.0, 3.0, 4.0, 5.0,
106                 6.0}, stroke2, Color.red, Color.blue);
107         assertFalse(a1.equals(a2));
108         a2 = new XYPolygonAnnotation(new double[] {99.0, 2.0, 3.0, 4.0, 5.0,
109                 6.0}, stroke2, Color.red, Color.blue);
110         assertTrue(a1.equals(a2));
111
112         GradientPaint JavaDoc gp1 = new GradientPaint JavaDoc(1.0f, 2.0f, Color.yellow, 3.0f,
113                 4.0f, Color.white);
114         GradientPaint JavaDoc gp2 = new GradientPaint JavaDoc(1.0f, 2.0f, Color.yellow, 3.0f,
115                 4.0f, Color.white);
116         a1 = new XYPolygonAnnotation(new double[] {99.0, 2.0, 3.0, 4.0, 5.0,
117                 6.0}, stroke2, gp1, Color.blue);
118         assertFalse(a1.equals(a2));
119         a2 = new XYPolygonAnnotation(new double[] {99.0, 2.0, 3.0, 4.0, 5.0,
120                 6.0}, stroke2, gp2, Color.blue);
121         assertTrue(a1.equals(a2));
122
123         GradientPaint JavaDoc gp3 = new GradientPaint JavaDoc(1.0f, 2.0f, Color.green, 3.0f,
124                 4.0f, Color.white);
125         GradientPaint JavaDoc gp4 = new GradientPaint JavaDoc(1.0f, 2.0f, Color.green, 3.0f,
126                 4.0f, Color.white);
127         a1 = new XYPolygonAnnotation(new double[] {99.0, 2.0, 3.0, 4.0, 5.0,
128                 6.0}, stroke2, gp1, gp3);
129         assertFalse(a1.equals(a2));
130         a2 = new XYPolygonAnnotation(new double[] {99.0, 2.0, 3.0, 4.0, 5.0,
131                 6.0}, stroke2, gp2, gp4);
132         assertTrue(a1.equals(a2));
133     }
134     
135     /**
136      * Two objects that are equal are required to return the same hashCode.
137      */

138     public void testHashCode() {
139         Stroke JavaDoc stroke = new BasicStroke JavaDoc(2.0f);
140         XYPolygonAnnotation a1 = new XYPolygonAnnotation(new double[] {1.0,
141                 2.0, 3.0, 4.0, 5.0, 6.0}, stroke, Color.red, Color.blue);
142         XYPolygonAnnotation a2 = new XYPolygonAnnotation(new double[] {1.0,
143                 2.0, 3.0, 4.0, 5.0, 6.0}, stroke, Color.red, Color.blue);
144         assertTrue(a1.equals(a2));
145         int h1 = a1.hashCode();
146         int h2 = a2.hashCode();
147         assertEquals(h1, h2);
148     }
149     
150     /**
151      * Confirm that cloning works.
152      */

153     public void testCloning() {
154         Stroke JavaDoc stroke1 = new BasicStroke JavaDoc(2.0f);
155         XYPolygonAnnotation a1 = new XYPolygonAnnotation(new double[] {1.0,
156                 2.0, 3.0, 4.0, 5.0, 6.0}, stroke1, Color.red, Color.blue);
157         XYPolygonAnnotation a2 = null;
158         try {
159             a2 = (XYPolygonAnnotation) a1.clone();
160         }
161         catch (CloneNotSupportedException JavaDoc e) {
162             e.printStackTrace();
163         }
164         assertTrue(a1 != a2);
165         assertTrue(a1.getClass() == a2.getClass());
166         assertTrue(a1.equals(a2));
167     }
168
169     /**
170      * Serialize an instance, restore it, and check for equality.
171      */

172     public void testSerialization() {
173
174         Stroke JavaDoc stroke1 = new BasicStroke JavaDoc(2.0f);
175         XYPolygonAnnotation a1 = new XYPolygonAnnotation(new double[] {1.0,
176                 2.0, 3.0, 4.0, 5.0, 6.0}, stroke1, Color.red, Color.blue);
177         XYPolygonAnnotation a2 = null;
178
179         try {
180             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
181             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
182             out.writeObject(a1);
183             out.close();
184
185             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
186                 new ByteArrayInputStream JavaDoc(buffer.toByteArray())
187             );
188             a2 = (XYPolygonAnnotation) in.readObject();
189             in.close();
190         }
191         catch (Exception JavaDoc e) {
192             e.printStackTrace();
193         }
194         assertEquals(a1, a2);
195
196     }
197
198 }
199
Popular Tags