KickJava   Java API By Example, From Geeks To Geeks.

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


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  * XYDrawableAnnotationTests.java
28  * ------------------------------
29  * (C) Copyright 2003-2005, by Object Refinery Limited.
30  *
31  * Original Author: David Gilbert (for Object Refinery Limited);
32  * Contributor(s): -;
33  *
34  * $Id: XYDrawableAnnotationTests.java,v 1.4 2005/02/28 16:10:46 mungady Exp $
35  *
36  * Changes
37  * -------
38  * 19-Aug-2003 : Version 1 (DG);
39  * 01-Oct-2004 : Fixed bugs in tests (DG);
40  * 07-Jan-2005 : Added hashCode() test (DG);
41  *
42  */

43
44 package org.jfree.chart.annotations.junit;
45
46 import java.awt.Graphics2D JavaDoc;
47 import java.awt.geom.Rectangle2D 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 import java.io.Serializable JavaDoc;
55
56 import junit.framework.Test;
57 import junit.framework.TestCase;
58 import junit.framework.TestSuite;
59
60 import org.jfree.chart.annotations.XYDrawableAnnotation;
61 import org.jfree.ui.Drawable;
62
63 /**
64  * Tests for the {@link XYDrawableAnnotation} class.
65  */

66 public class XYDrawableAnnotationTests extends TestCase {
67
68     static class TestDrawable implements Drawable, Cloneable JavaDoc, Serializable JavaDoc {
69         /**
70          * Default constructor.
71          */

72         public TestDrawable() {
73         }
74         /**
75          * Draws something.
76          * @param g2 the graphics device.
77          * @param area the area in which to draw.
78          */

79         public void draw(Graphics2D JavaDoc g2, Rectangle2D JavaDoc area) {
80             // do nothing
81
}
82         /**
83          * Tests this object for equality with an arbitrary object.
84          * @param obj the object to test against (<code>null</code> permitted).
85          * @return A boolean.
86          */

87         public boolean equals(Object JavaDoc obj) {
88             if (obj == this) {
89                 return true;
90             }
91             if (!(obj instanceof TestDrawable)) {
92                 return false;
93             }
94             return true;
95         }
96     }
97     
98     /**
99      * Returns the tests as a test suite.
100      *
101      * @return The test suite.
102      */

103     public static Test suite() {
104         return new TestSuite(XYDrawableAnnotationTests.class);
105     }
106
107     /**
108      * Constructs a new set of tests.
109      *
110      * @param name the name of the tests.
111      */

112     public XYDrawableAnnotationTests(String JavaDoc name) {
113         super(name);
114     }
115
116     /**
117      * Confirm that the equals method can distinguish all the required fields.
118      */

119     public void testEquals() {
120         XYDrawableAnnotation a1 = new XYDrawableAnnotation(
121             10.0, 20.0, 100.0, 200.0, new TestDrawable()
122         );
123         XYDrawableAnnotation a2 = new XYDrawableAnnotation(
124             10.0, 20.0, 100.0, 200.0, new TestDrawable()
125         );
126         assertTrue(a1.equals(a2));
127     }
128     
129     /**
130      * Two objects that are equal are required to return the same hashCode.
131      */

132     public void testHashCode() {
133         XYDrawableAnnotation a1 = new XYDrawableAnnotation(
134             10.0, 20.0, 100.0, 200.0, new TestDrawable()
135         );
136         XYDrawableAnnotation a2 = new XYDrawableAnnotation(
137             10.0, 20.0, 100.0, 200.0, new TestDrawable()
138         );
139         assertTrue(a1.equals(a2));
140         int h1 = a1.hashCode();
141         int h2 = a2.hashCode();
142         assertEquals(h1, h2);
143     }
144
145     /**
146      * Confirm that cloning works.
147      */

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

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