KickJava   Java API By Example, From Geeks To Geeks.

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


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
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  * XYBoxAnnotationTests.java
29  * -------------------------
30  * (C) Copyright 2005, by Object Refinery Limited and Contributors.
31  *
32  * Original Author: David Gilbert (for Object Refinery Limited);
33  * Contributor(s): -;
34  *
35  * $Id: XYBoxAnnotationTests.java,v 1.1.2.1 2006/10/03 15:41:40 mungady Exp $
36  *
37  * Changes
38  * -------
39  * 19-Jan-2005 : 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.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.XYBoxAnnotation;
60
61 /**
62  * Some tests for the {@link XYBoxAnnotation} class.
63  */

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

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

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

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

187     public void testSerialization() {
188
189         XYBoxAnnotation a1 = new XYBoxAnnotation(
190             1.0, 2.0, 3.0, 4.0,
191             new BasicStroke JavaDoc(1.2f), Color.red, Color.blue
192         );
193         XYBoxAnnotation 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 = (XYBoxAnnotation) 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