KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > experimental > chart > plot > dial > junit > DialTextAnnotationTests


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  * DialTextAnnotationTests.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: DialTextAnnotationTests.java,v 1.1.2.2 2006/11/06 16:31:01 mungady Exp $
36  *
37  * Changes
38  * -------
39  * 03-Nov-2006 : Version 1 (DG);
40  *
41  */

42
43 package org.jfree.experimental.chart.plot.dial.junit;
44
45 import java.awt.Color JavaDoc;
46 import java.awt.GradientPaint JavaDoc;
47 import java.io.ByteArrayInputStream JavaDoc;
48 import java.io.ByteArrayOutputStream JavaDoc;
49 import java.io.ObjectInput JavaDoc;
50 import java.io.ObjectInputStream JavaDoc;
51 import java.io.ObjectOutput JavaDoc;
52 import java.io.ObjectOutputStream JavaDoc;
53
54 import junit.framework.Test;
55 import junit.framework.TestCase;
56 import junit.framework.TestSuite;
57
58 import org.jfree.experimental.chart.plot.dial.DialTextAnnotation;
59
60 /**
61  * Tests for the {@link DialTextAnnotation} class.
62  */

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

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

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

86     public void testEquals() {
87         DialTextAnnotation a1 = new DialTextAnnotation("A1");
88         DialTextAnnotation a2 = new DialTextAnnotation("A1");
89         assertTrue(a1.equals(a2));
90         
91         
92     }
93
94     /**
95      * Two objects that are equal are required to return the same hashCode.
96      */

97     public void testHashCode() {
98         DialTextAnnotation a1 = new DialTextAnnotation("A1");
99         DialTextAnnotation a2 = new DialTextAnnotation("A1");
100         assertTrue(a1.equals(a2));
101         int h1 = a1.hashCode();
102         int h2 = a2.hashCode();
103         assertEquals(h1, h2);
104     }
105
106     /**
107      * Confirm that cloning works.
108      */

109     public void testCloning() {
110         // test a default instance
111
DialTextAnnotation a1 = new DialTextAnnotation("A1");
112         DialTextAnnotation a2 = null;
113         try {
114             a2 = (DialTextAnnotation) a1.clone();
115         }
116         catch (CloneNotSupportedException JavaDoc e) {
117             e.printStackTrace();
118         }
119         assertTrue(a1 != a2);
120         assertTrue(a1.getClass() == a2.getClass());
121         assertTrue(a1.equals(a2));
122         
123         // test a customised instance
124

125     }
126
127
128     /**
129      * Serialize an instance, restore it, and check for equality.
130      */

131     public void testSerialization() {
132         // test a default instance
133
DialTextAnnotation a1 = new DialTextAnnotation("A1");
134         DialTextAnnotation a2 = null;
135
136         try {
137             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
138             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
139             out.writeObject(a1);
140             out.close();
141
142             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
143                     new ByteArrayInputStream JavaDoc(buffer.toByteArray()));
144             a2 = (DialTextAnnotation) in.readObject();
145             in.close();
146         }
147         catch (Exception JavaDoc e) {
148             e.printStackTrace();
149         }
150         assertEquals(a1, a2);
151         
152         // test a custom instance
153
a1 = new DialTextAnnotation("A1");
154         a1.setPaint(new GradientPaint JavaDoc(1.0f, 2.0f, Color.red, 3.0f, 4.0f,
155                 Color.blue));
156         a2 = null;
157
158         try {
159             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
160             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
161             out.writeObject(a1);
162             out.close();
163
164             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
165                     new ByteArrayInputStream JavaDoc(buffer.toByteArray()));
166             a2 = (DialTextAnnotation) in.readObject();
167             in.close();
168         }
169         catch (Exception JavaDoc e) {
170             e.printStackTrace();
171         }
172         assertEquals(a1, a2);
173         
174         
175     }
176
177 }
178
Popular Tags