KickJava   Java API By Example, From Geeks To Geeks.

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


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  * XYTextAnnotationTests.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: XYTextAnnotationTests.java,v 1.3 2005/02/28 16:07:39 mungady Exp $
35  *
36  * Changes
37  * -------
38  * 19-Aug-2003 : Version 1 (DG);
39  * 07-Jan-2005 : Added hashCode() test (DG);
40  *
41  */

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

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

81     public XYTextAnnotationTests(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         XYTextAnnotation a1 = new XYTextAnnotation("Text", 10.0, 20.0);
90         XYTextAnnotation a2 = new XYTextAnnotation("Text", 10.0, 20.0);
91         assertTrue(a1.equals(a2));
92         
93         // text
94
a1 = new XYTextAnnotation("ABC", 10.0, 20.0);
95         assertFalse(a1.equals(a2));
96         a2 = new XYTextAnnotation("ABC", 10.0, 20.0);
97         assertTrue(a1.equals(a2));
98         
99         // font
100
a1.setFont(new Font JavaDoc("Serif", Font.PLAIN, 23));
101         assertFalse(a1.equals(a2));
102         a2.setFont(new Font JavaDoc("Serif", Font.PLAIN, 23));
103         assertTrue(a1.equals(a2));
104         
105         // paint
106
GradientPaint JavaDoc gp1 = new GradientPaint JavaDoc(
107             1.0f, 2.0f, Color.red, 3.0f, 4.0f, Color.yellow
108         );
109         GradientPaint JavaDoc gp2 = new GradientPaint JavaDoc(
110             1.0f, 2.0f, Color.red, 3.0f, 4.0f, Color.yellow
111         );
112         a1.setPaint(gp1);
113         assertFalse(a1.equals(a2));
114         a2.setPaint(gp2);
115         assertTrue(a1.equals(a2));
116         
117         // rotation anchor
118
a1.setRotationAnchor(TextAnchor.BASELINE_RIGHT);
119         assertFalse(a1.equals(a2));
120         a2.setRotationAnchor(TextAnchor.BASELINE_RIGHT);
121         assertTrue(a1.equals(a2));
122         
123         // rotation angle
124
a1.setRotationAngle(12.3);
125         assertFalse(a1.equals(a2));
126         a2.setRotationAngle(12.3);
127         assertTrue(a1.equals(a2));
128
129         // text anchor
130
a1.setTextAnchor(TextAnchor.BASELINE_RIGHT);
131         assertFalse(a1.equals(a2));
132         a2.setTextAnchor(TextAnchor.BASELINE_RIGHT);
133         assertTrue(a1.equals(a2));
134     }
135
136     /**
137      * Two objects that are equal are required to return the same hashCode.
138      */

139     public void testHashCode() {
140         XYTextAnnotation a1 = new XYTextAnnotation("Text", 10.0, 20.0);
141         XYTextAnnotation a2 = new XYTextAnnotation("Text", 10.0, 20.0);
142         assertTrue(a1.equals(a2));
143         int h1 = a1.hashCode();
144         int h2 = a2.hashCode();
145         assertEquals(h1, h2);
146     }
147
148     /**
149      * Confirm that cloning works.
150      */

151     public void testCloning() {
152         XYTextAnnotation a1 = new XYTextAnnotation("Text", 10.0, 20.0);
153         XYTextAnnotation a2 = null;
154         try {
155             a2 = (XYTextAnnotation) a1.clone();
156         }
157         catch (CloneNotSupportedException JavaDoc e) {
158             System.err.println("Failed to clone.");
159         }
160         assertTrue(a1 != a2);
161         assertTrue(a1.getClass() == a2.getClass());
162         assertTrue(a1.equals(a2));
163     }
164
165     /**
166      * Serialize an instance, restore it, and check for equality.
167      */

168     public void testSerialization() {
169
170         XYTextAnnotation a1 = new XYTextAnnotation("Text", 10.0, 20.0);
171         XYTextAnnotation a2 = null;
172
173         try {
174             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
175             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
176             out.writeObject(a1);
177             out.close();
178
179             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
180                 new ByteArrayInputStream JavaDoc(buffer.toByteArray())
181             );
182             a2 = (XYTextAnnotation) in.readObject();
183             in.close();
184         }
185         catch (Exception JavaDoc e) {
186             System.out.println(e.toString());
187         }
188         assertEquals(a1, a2);
189
190     }
191
192 }
193
Popular Tags