KickJava   Java API By Example, From Geeks To Geeks.

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


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  * XYLineAnnotationTests.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: XYLineAnnotationTests.java,v 1.3 2005/02/28 16:10:46 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.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.XYLineAnnotation;
60
61 /**
62  * Tests for the {@link XYLineAnnotation} class.
63  */

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

80     public XYLineAnnotationTests(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         Stroke JavaDoc stroke = new BasicStroke JavaDoc(2.0f);
89         XYLineAnnotation a1 = new XYLineAnnotation(
90             10.0, 20.0, 100.0, 200.0, stroke, Color.blue
91         );
92         XYLineAnnotation a2 = new XYLineAnnotation(
93             10.0, 20.0, 100.0, 200.0, stroke, Color.blue
94         );
95         assertTrue(a1.equals(a2));
96     }
97     
98     /**
99      * Two objects that are equal are required to return the same hashCode.
100      */

101     public void testHashCode() {
102         Stroke JavaDoc stroke = new BasicStroke JavaDoc(2.0f);
103         XYLineAnnotation a1 = new XYLineAnnotation(
104             10.0, 20.0, 100.0, 200.0, stroke, Color.blue
105         );
106         XYLineAnnotation a2 = new XYLineAnnotation(
107             10.0, 20.0, 100.0, 200.0, stroke, Color.blue
108         );
109         assertTrue(a1.equals(a2));
110         int h1 = a1.hashCode();
111         int h2 = a2.hashCode();
112         assertEquals(h1, h2);
113     }
114     
115     /**
116      * Confirm that cloning works.
117      */

118     public void testCloning() {
119         Stroke JavaDoc stroke = new BasicStroke JavaDoc(2.0f);
120         XYLineAnnotation a1 = new XYLineAnnotation(
121             10.0, 20.0, 100.0, 200.0, stroke, Color.blue
122         );
123         XYLineAnnotation a2 = null;
124         try {
125             a2 = (XYLineAnnotation) a1.clone();
126         }
127         catch (CloneNotSupportedException JavaDoc e) {
128             System.err.println("Failed to clone.");
129         }
130         assertTrue(a1 != a2);
131         assertTrue(a1.getClass() == a2.getClass());
132         assertTrue(a1.equals(a2));
133     }
134
135     /**
136      * Serialize an instance, restore it, and check for equality.
137      */

138     public void testSerialization() {
139
140         Stroke JavaDoc stroke = new BasicStroke JavaDoc(2.0f);
141         XYLineAnnotation a1 = new XYLineAnnotation(
142             10.0, 20.0, 100.0, 200.0, stroke, Color.blue
143         );
144         XYLineAnnotation a2 = null;
145
146         try {
147             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
148             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
149             out.writeObject(a1);
150             out.close();
151
152             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
153                 new ByteArrayInputStream JavaDoc(buffer.toByteArray())
154             );
155             a2 = (XYLineAnnotation) in.readObject();
156             in.close();
157         }
158         catch (Exception JavaDoc e) {
159             System.out.println(e.toString());
160         }
161         assertEquals(a1, a2);
162
163     }
164
165 }
166
Popular Tags