KickJava   Java API By Example, From Geeks To Geeks.

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


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  * CategoryPointerAnnotationTests.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: CategoryPointerAnnotationTests.java,v 1.1.2.1 2006/10/03 15:41:41 mungady Exp $
36  *
37  * Changes
38  * -------
39  * 02-Oct-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.Stroke JavaDoc;
48 import java.io.ByteArrayInputStream JavaDoc;
49 import java.io.ByteArrayOutputStream JavaDoc;
50 import java.io.ObjectInput JavaDoc;
51 import java.io.ObjectInputStream JavaDoc;
52 import java.io.ObjectOutput JavaDoc;
53 import java.io.ObjectOutputStream JavaDoc;
54
55 import junit.framework.Test;
56 import junit.framework.TestCase;
57 import junit.framework.TestSuite;
58
59 import org.jfree.chart.annotations.CategoryPointerAnnotation;
60
61 /**
62  * Tests for the {@link CategoryPointerAnnotation} class.
63  */

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

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

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

87     public void testEquals() {
88         
89         CategoryPointerAnnotation a1 = new CategoryPointerAnnotation("Label",
90                 "Key 1", 20.0, Math.PI);
91         CategoryPointerAnnotation a2 = new CategoryPointerAnnotation("Label",
92                 "Key 1", 20.0, Math.PI);
93         assertTrue(a1.equals(a2));
94         
95         a1 = new CategoryPointerAnnotation("Label2", "Key 1", 20.0, Math.PI);
96         assertFalse(a1.equals(a2));
97         a2 = new CategoryPointerAnnotation("Label2", "Key 1", 20.0, Math.PI);
98         assertTrue(a1.equals(a2));
99         
100         a1.setCategory("Key 2");
101         assertFalse(a1.equals(a2));
102         a2.setCategory("Key 2");
103         assertTrue(a1.equals(a2));
104         
105         a1.setValue(22.0);
106         assertFalse(a1.equals(a2));
107         a2.setValue(22.0);
108         assertTrue(a1.equals(a2));
109         
110         //private double angle;
111
a1.setAngle(Math.PI / 4.0);
112         assertFalse(a1.equals(a2));
113         a2.setAngle(Math.PI / 4.0);
114         assertTrue(a1.equals(a2));
115         
116         //private double tipRadius;
117
a1.setTipRadius(20.0);
118         assertFalse(a1.equals(a2));
119         a2.setTipRadius(20.0);
120         assertTrue(a1.equals(a2));
121
122         //private double baseRadius;
123
a1.setBaseRadius(5.0);
124         assertFalse(a1.equals(a2));
125         a2.setBaseRadius(5.0);
126         assertTrue(a1.equals(a2));
127         
128         //private double arrowLength;
129
a1.setArrowLength(33.0);
130         assertFalse(a1.equals(a2));
131         a2.setArrowLength(33.0);
132         assertTrue(a1.equals(a2));
133         
134         //private double arrowWidth;
135
a1.setArrowWidth(9.0);
136         assertFalse(a1.equals(a2));
137         a2.setArrowWidth(9.0);
138         assertTrue(a1.equals(a2));
139         
140         //private Stroke arrowStroke;
141
Stroke JavaDoc stroke = new BasicStroke JavaDoc(1.5f);
142         a1.setArrowStroke(stroke);
143         assertFalse(a1.equals(a2));
144         a2.setArrowStroke(stroke);
145         assertTrue(a1.equals(a2));
146         
147         //private Paint arrowPaint;
148
a1.setArrowPaint(Color.blue);
149         assertFalse(a1.equals(a2));
150         a2.setArrowPaint(Color.blue);
151         assertTrue(a1.equals(a2));
152
153         //private double labelOffset;
154
a1.setLabelOffset(10.0);
155         assertFalse(a1.equals(a2));
156         a2.setLabelOffset(10.0);
157         assertTrue(a1.equals(a2));
158       
159     }
160
161     /**
162      * Two objects that are equal are required to return the same hashCode.
163      */

164     public void testHashCode() {
165         CategoryPointerAnnotation a1 = new CategoryPointerAnnotation("Label",
166                 "A", 20.0, Math.PI);
167         CategoryPointerAnnotation a2 = new CategoryPointerAnnotation("Label",
168                 "A", 20.0, Math.PI);
169         assertTrue(a1.equals(a2));
170         int h1 = a1.hashCode();
171         int h2 = a2.hashCode();
172         assertEquals(h1, h2);
173     }
174
175     /**
176      * Confirm that cloning works.
177      */

178     public void testCloning() {
179         CategoryPointerAnnotation a1 = new CategoryPointerAnnotation("Label",
180                 "A", 20.0, Math.PI);
181         CategoryPointerAnnotation a2 = null;
182         try {
183             a2 = (CategoryPointerAnnotation) a1.clone();
184         }
185         catch (CloneNotSupportedException JavaDoc e) {
186             e.printStackTrace();
187         }
188         assertTrue(a1 != a2);
189         assertTrue(a1.getClass() == a2.getClass());
190         assertTrue(a1.equals(a2));
191     }
192
193     /**
194      * Serialize an instance, restore it, and check for equality.
195      */

196     public void testSerialization() {
197
198         CategoryPointerAnnotation a1 = new CategoryPointerAnnotation("Label",
199                 "A", 20.0, Math.PI);
200         CategoryPointerAnnotation a2 = null;
201
202         try {
203             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
204             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
205             out.writeObject(a1);
206             out.close();
207
208             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(new ByteArrayInputStream JavaDoc(
209                     buffer.toByteArray()));
210             a2 = (CategoryPointerAnnotation) in.readObject();
211             in.close();
212         }
213         catch (Exception JavaDoc e) {
214             e.printStackTrace();
215         }
216         assertEquals(a1, a2);
217
218     }
219
220 }
221
Popular Tags