KickJava   Java API By Example, From Geeks To Geeks.

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


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 License
20  * along with this library; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22  *
23  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
24  * in the United States and other countries.]
25  *
26  * -----------------------------
27  * XYPointerAnnotationTests.java
28  * -----------------------------
29  * (C) Copyright 2003-2005, by Object Refinery Limited and Contributors.
30  *
31  * Original Author: David Gilbert (for Object Refinery Limited);
32  * Contributor(s): -;
33  *
34  * $Id: XYPointerAnnotationTests.java,v 1.4 2005/02/28 16:10:46 mungady Exp $
35  *
36  * Changes
37  * -------
38  * 19-Aug-2003 : Version 1 (DG);
39  * 13-Oct-2003 : Expanded test for equals() method (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.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.XYPointerAnnotation;
61
62 /**
63  * Tests for the {@link XYPointerAnnotation} class.
64  */

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

81     public XYPointerAnnotationTests(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         
90         XYPointerAnnotation a1 = new XYPointerAnnotation(
91             "Label", 10.0, 20.0, Math.PI
92         );
93         XYPointerAnnotation a2 = new XYPointerAnnotation(
94             "Label", 10.0, 20.0, Math.PI
95         );
96         assertTrue(a1.equals(a2));
97         
98         //private double angle;
99
a1.setAngle(Math.PI / 4.0);
100         assertFalse(a1.equals(a2));
101         a2.setAngle(Math.PI / 4.0);
102         assertTrue(a1.equals(a2));
103         
104         //private double tipRadius;
105
a1.setTipRadius(20.0);
106         assertFalse(a1.equals(a2));
107         a2.setTipRadius(20.0);
108         assertTrue(a1.equals(a2));
109
110         //private double baseRadius;
111
a1.setBaseRadius(5.0);
112         assertFalse(a1.equals(a2));
113         a2.setBaseRadius(5.0);
114         assertTrue(a1.equals(a2));
115         
116         //private double arrowLength;
117
a1.setArrowLength(33.0);
118         assertFalse(a1.equals(a2));
119         a2.setArrowLength(33.0);
120         assertTrue(a1.equals(a2));
121         
122         //private double arrowWidth;
123
a1.setArrowWidth(9.0);
124         assertFalse(a1.equals(a2));
125         a2.setArrowWidth(9.0);
126         assertTrue(a1.equals(a2));
127         
128         //private Stroke arrowStroke;
129
Stroke JavaDoc stroke = new BasicStroke JavaDoc(1.5f);
130         a1.setArrowStroke(stroke);
131         assertFalse(a1.equals(a2));
132         a2.setArrowStroke(stroke);
133         assertTrue(a1.equals(a2));
134         
135         //private Paint arrowPaint;
136
a1.setArrowPaint(Color.blue);
137         assertFalse(a1.equals(a2));
138         a2.setArrowPaint(Color.blue);
139         assertTrue(a1.equals(a2));
140
141         //private double labelOffset;
142
a1.setLabelOffset(10.0);
143         assertFalse(a1.equals(a2));
144         a2.setLabelOffset(10.0);
145         assertTrue(a1.equals(a2));
146       
147     }
148
149     /**
150      * Two objects that are equal are required to return the same hashCode.
151      */

152     public void testHashCode() {
153         XYPointerAnnotation a1 = new XYPointerAnnotation(
154             "Label", 10.0, 20.0, Math.PI
155         );
156         XYPointerAnnotation a2 = new XYPointerAnnotation(
157             "Label", 10.0, 20.0, Math.PI
158         );
159         assertTrue(a1.equals(a2));
160         int h1 = a1.hashCode();
161         int h2 = a2.hashCode();
162         assertEquals(h1, h2);
163     }
164
165     /**
166      * Confirm that cloning works.
167      */

168     public void testCloning() {
169
170         XYPointerAnnotation a1 = new XYPointerAnnotation(
171             "Label", 10.0, 20.0, Math.PI
172         );
173         XYPointerAnnotation a2 = null;
174         try {
175             a2 = (XYPointerAnnotation) a1.clone();
176         }
177         catch (CloneNotSupportedException JavaDoc e) {
178             System.err.println("Failed to clone.");
179         }
180         assertTrue(a1 != a2);
181         assertTrue(a1.getClass() == a2.getClass());
182         assertTrue(a1.equals(a2));
183     }
184
185     /**
186      * Serialize an instance, restore it, and check for equality.
187      */

188     public void testSerialization() {
189
190         XYPointerAnnotation a1 = new XYPointerAnnotation(
191             "Label", 10.0, 20.0, Math.PI
192         );
193         XYPointerAnnotation 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             );
204             a2 = (XYPointerAnnotation) in.readObject();
205             in.close();
206         }
207         catch (Exception JavaDoc e) {
208             System.out.println(e.toString());
209         }
210         assertEquals(a1, a2);
211
212     }
213
214 }
215
Popular Tags