KickJava   Java API By Example, From Geeks To Geeks.

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


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  * DialCapTests.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: DialCapTests.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.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.experimental.chart.plot.dial.DialCap;
60
61 /**
62  * Tests for the {@link DialCap} class.
63  */

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

80     public DialCapTests(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         DialCap c1 = new DialCap();
89         DialCap c2 = new DialCap();
90         assertTrue(c1.equals(c2));
91         
92         // radius
93
c1.setRadius(0.5);
94         assertFalse(c1.equals(c2));
95         c2.setRadius(0.5);
96         assertTrue(c1.equals(c2));
97         
98         // fill paint
99
c1.setFillPaint(new GradientPaint JavaDoc(1.0f, 2.0f, Color.blue,
100                 3.0f, 4.0f, Color.green));
101         assertFalse(c1.equals(c2));
102         c2.setFillPaint(new GradientPaint JavaDoc(1.0f, 2.0f, Color.blue,
103                 3.0f, 4.0f, Color.green));
104         
105         // outline paint
106
c1.setOutlinePaint(new GradientPaint JavaDoc(1.0f, 2.0f, Color.white,
107                 3.0f, 4.0f, Color.gray));
108         assertFalse(c1.equals(c2));
109         c2.setOutlinePaint(new GradientPaint JavaDoc(1.0f, 2.0f, Color.white,
110                 3.0f, 4.0f, Color.gray));
111         
112         assertTrue(c1.equals(c2));
113
114         // outline stroke
115
c1.setOutlineStroke(new BasicStroke JavaDoc(1.1f));
116         assertFalse(c1.equals(c2));
117         c2.setOutlineStroke(new BasicStroke JavaDoc(1.1f));
118         assertTrue(c1.equals(c2));
119     }
120
121     /**
122      * Two objects that are equal are required to return the same hashCode.
123      */

124     public void testHashCode() {
125         DialCap c1 = new DialCap();
126         DialCap c2 = new DialCap();
127         assertTrue(c1.equals(c2));
128         int h1 = c1.hashCode();
129         int h2 = c2.hashCode();
130         assertEquals(h1, h2);
131     }
132
133     /**
134      * Confirm that cloning works.
135      */

136     public void testCloning() {
137         // test a default instance
138
DialCap c1 = new DialCap();
139         DialCap c2 = null;
140         try {
141             c2 = (DialCap) c1.clone();
142         }
143         catch (CloneNotSupportedException JavaDoc e) {
144             e.printStackTrace();
145         }
146         assertTrue(c1 != c2);
147         assertTrue(c1.getClass() == c2.getClass());
148         assertTrue(c1.equals(c2));
149         
150         // test a customised instance
151
c1 = new DialCap();
152         c1.setFillPaint(new GradientPaint JavaDoc(1.0f, 2.0f, Color.blue,
153                 3.0f, 4.0f, Color.green));
154         c1.setOutlinePaint(new GradientPaint JavaDoc(1.0f, 2.0f, Color.white,
155                 3.0f, 4.0f, Color.gray));
156         c1.setOutlineStroke(new BasicStroke JavaDoc(2.0f));
157         c2 = null;
158         try {
159             c2 = (DialCap) c1.clone();
160         }
161         catch (CloneNotSupportedException JavaDoc e) {
162             e.printStackTrace();
163         }
164         assertTrue(c1 != c2);
165         assertTrue(c1.getClass() == c2.getClass());
166         assertTrue(c1.equals(c2));
167     }
168
169
170     /**
171      * Serialize an instance, restore it, and check for equality.
172      */

173     public void testSerialization() {
174         // test a default instance
175
DialCap c1 = new DialCap();
176         DialCap c2 = null;
177
178         try {
179             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
180             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
181             out.writeObject(c1);
182             out.close();
183
184             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
185                     new ByteArrayInputStream JavaDoc(buffer.toByteArray()));
186             c2 = (DialCap) in.readObject();
187             in.close();
188         }
189         catch (Exception JavaDoc e) {
190             e.printStackTrace();
191         }
192         assertEquals(c1, c2);
193         
194         // test a custom instance
195
c1 = new DialCap();
196         c1.setFillPaint(new GradientPaint JavaDoc(1.0f, 2.0f, Color.blue,
197                 3.0f, 4.0f, Color.green));
198         c1.setOutlinePaint(new GradientPaint JavaDoc(1.0f, 2.0f, Color.white,
199                 3.0f, 4.0f, Color.gray));
200         c1.setOutlineStroke(new BasicStroke JavaDoc(2.0f));
201         c2 = null;
202
203         try {
204             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
205             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
206             out.writeObject(c1);
207             out.close();
208
209             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
210                     new ByteArrayInputStream JavaDoc(buffer.toByteArray()));
211             c2 = (DialCap) in.readObject();
212             in.close();
213         }
214         catch (Exception JavaDoc e) {
215             e.printStackTrace();
216         }
217         assertEquals(c1, c2);
218     }
219
220 }
221
Popular Tags