KickJava   Java API By Example, From Geeks To Geeks.

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


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  * DialBackgroundTests.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: DialBackgroundTests.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.DialBackground;
59 import org.jfree.ui.GradientPaintTransformType;
60 import org.jfree.ui.StandardGradientPaintTransformer;
61
62 /**
63  * Tests for the {@link DialBackground} class.
64  */

65 public class DialBackgroundTests extends TestCase {
66
67     /**
68      * Returns the tests as a test suite.
69      *
70      * @return The test suite.
71      */

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

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

88     public void testEquals() {
89         DialBackground b1 = new DialBackground();
90         DialBackground b2 = new DialBackground();
91         assertTrue(b1.equals(b2));
92         
93         // paint
94
b1.setPaint(new GradientPaint JavaDoc(1.0f, 2.0f, Color.red, 3.0f, 4.0f,
95                 Color.yellow));
96         assertFalse(b1.equals(b2));
97         b2.setPaint(new GradientPaint JavaDoc(1.0f, 2.0f, Color.red, 3.0f, 4.0f,
98                 Color.yellow));
99         assertTrue(b1.equals(b2));
100         
101         // gradient paint transformer
102
b1.setGradientPaintTransformer(new StandardGradientPaintTransformer(
103                 GradientPaintTransformType.CENTER_VERTICAL));
104         assertFalse(b1.equals(b2));
105         b2.setGradientPaintTransformer(new StandardGradientPaintTransformer(
106                 GradientPaintTransformType.CENTER_VERTICAL));
107         assertTrue(b1.equals(b2));
108     }
109
110     /**
111      * Two objects that are equal are required to return the same hashCode.
112      */

113     public void testHashCode() {
114         DialBackground b1 = new DialBackground(Color.red);
115         DialBackground b2 = new DialBackground(Color.red);
116         assertTrue(b1.equals(b2));
117         int h1 = b1.hashCode();
118         int h2 = b2.hashCode();
119         assertEquals(h1, h2);
120     }
121
122     /**
123      * Confirm that cloning works.
124      */

125     public void testCloning() {
126         // test default instance
127
DialBackground b1 = new DialBackground();
128         DialBackground b2 = null;
129         try {
130             b2 = (DialBackground) b1.clone();
131         }
132         catch (CloneNotSupportedException JavaDoc e) {
133             e.printStackTrace();
134         }
135         assertTrue(b1 != b2);
136         assertTrue(b1.getClass() == b2.getClass());
137         assertTrue(b1.equals(b2));
138         
139         // test a customised instance
140
b1 = new DialBackground();
141         b1.setPaint(new GradientPaint JavaDoc(1.0f, 2.0f, Color.red, 3.0f, 4.0f,
142                 Color.green));
143         b1.setGradientPaintTransformer(new StandardGradientPaintTransformer(
144                 GradientPaintTransformType.CENTER_VERTICAL));
145         b2 = null;
146         try {
147             b2 = (DialBackground) b1.clone();
148         }
149         catch (CloneNotSupportedException JavaDoc e) {
150             e.printStackTrace();
151         }
152         assertTrue(b1 != b2);
153         assertTrue(b1.getClass() == b2.getClass());
154         assertTrue(b1.equals(b2));
155     }
156
157
158     /**
159      * Serialize an instance, restore it, and check for equality.
160      */

161     public void testSerialization() {
162         // test a default instance
163
DialBackground b1 = new DialBackground();
164         DialBackground b2 = null;
165
166         try {
167             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
168             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
169             out.writeObject(b1);
170             out.close();
171
172             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
173                     new ByteArrayInputStream JavaDoc(buffer.toByteArray()));
174             b2 = (DialBackground) in.readObject();
175             in.close();
176         }
177         catch (Exception JavaDoc e) {
178             e.printStackTrace();
179         }
180         assertEquals(b1, b2);
181         
182         // test a customised instance
183
b1 = new DialBackground();
184         b1.setPaint(new GradientPaint JavaDoc(1.0f, 2.0f, Color.red, 3.0f, 4.0f,
185                 Color.green));
186         b1.setGradientPaintTransformer(new StandardGradientPaintTransformer(
187                 GradientPaintTransformType.CENTER_VERTICAL));
188         b2 = null;
189
190         try {
191             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
192             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
193             out.writeObject(b1);
194             out.close();
195
196             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
197                     new ByteArrayInputStream JavaDoc(buffer.toByteArray()));
198             b2 = (DialBackground) in.readObject();
199             in.close();
200         }
201         catch (Exception JavaDoc e) {
202             e.printStackTrace();
203         }
204         assertEquals(b1, b2);
205     }
206
207 }
208
Popular Tags