KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > chart > renderer > xy > junit > XYDifferenceRendererTests


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
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  * XYDifferenceRendererTests.java
29  * ------------------------------
30  * (C) Copyright 2003-2005, by Object Refinery Limited and Contributors.
31  *
32  * Original Author: David Gilbert (for Object Refinery Limited);
33  * Contributor(s): -;
34  *
35  * $Id: XYDifferenceRendererTests.java,v 1.1.2.1 2006/10/03 15:41:45 mungady Exp $
36  *
37  * Changes
38  * -------
39  * 22-Oct-2003 : Version 1 (DG);
40  * 04-May-2005 : Improved equals() test (DG);
41  *
42  */

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

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

81     public XYDifferenceRendererTests(String JavaDoc name) {
82         super(name);
83     }
84
85     /**
86      * Check that the equals() method distinguishes all fields.
87      */

88     public void testEquals() {
89         XYDifferenceRenderer r1 = new XYDifferenceRenderer(
90                 Color.red, Color.blue, false);
91         XYDifferenceRenderer r2 = new XYDifferenceRenderer(
92                 Color.red, Color.blue, false);
93         assertEquals(r1, r2);
94         
95         // positive paint
96
r1.setPositivePaint(new GradientPaint JavaDoc(1.0f, 2.0f, Color.red,
97                 3.0f, 4.0f, Color.blue));
98         assertFalse(r1.equals(r2));
99         r2.setPositivePaint(new GradientPaint JavaDoc(1.0f, 2.0f, Color.red,
100                 3.0f, 4.0f, Color.blue));
101         assertTrue(r1.equals(r2));
102         
103         // negative paint
104
r1.setNegativePaint(new GradientPaint JavaDoc(1.0f, 2.0f, Color.yellow,
105                 3.0f, 4.0f, Color.blue));
106         assertFalse(r1.equals(r2));
107         r2.setNegativePaint(new GradientPaint JavaDoc(1.0f, 2.0f, Color.yellow,
108                 3.0f, 4.0f, Color.blue));
109         assertTrue(r1.equals(r2));
110         
111         // shapesVisible
112
r1 = new XYDifferenceRenderer(Color.green, Color.yellow, true);
113         assertFalse(r1.equals(r2));
114         r2 = new XYDifferenceRenderer(Color.green, Color.yellow, true);
115         assertTrue(r1.equals(r2));
116         
117         // legendLine
118
r1.setLegendLine(new Line2D.Double JavaDoc(1.0, 2.0, 3.0, 4.0));
119         assertFalse(r1.equals(r2));
120         r2.setLegendLine(new Line2D.Double JavaDoc(1.0, 2.0, 3.0, 4.0));
121         assertTrue(r1.equals(r2));
122         
123         assertFalse(r1.equals(null));
124     }
125
126     /**
127      * Two objects that are equal are required to return the same hashCode.
128      */

129     public void testHashcode() {
130         XYDifferenceRenderer r1
131             = new XYDifferenceRenderer(Color.red, Color.blue, false);
132         XYDifferenceRenderer r2
133             = new XYDifferenceRenderer(Color.red, Color.blue, false);
134         assertTrue(r1.equals(r2));
135         int h1 = r1.hashCode();
136         int h2 = r2.hashCode();
137         assertEquals(h1, h2);
138     }
139     
140     /**
141      * Confirm that cloning works.
142      */

143     public void testCloning() {
144         XYDifferenceRenderer r1
145             = new XYDifferenceRenderer(Color.red, Color.blue, false);
146         XYDifferenceRenderer r2 = null;
147         try {
148             r2 = (XYDifferenceRenderer) r1.clone();
149         }
150         catch (CloneNotSupportedException JavaDoc e) {
151             System.err.println("Failed to clone.");
152         }
153         assertTrue(r1 != r2);
154         assertTrue(r1.getClass() == r2.getClass());
155         assertTrue(r1.equals(r2));
156     }
157
158     /**
159      * Serialize an instance, restore it, and check for equality.
160      */

161     public void testSerialization() {
162
163         XYDifferenceRenderer r1
164             = new XYDifferenceRenderer(Color.red, Color.blue, false);
165         XYDifferenceRenderer r2 = null;
166
167         try {
168             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
169             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
170             out.writeObject(r1);
171             out.close();
172
173             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
174                 new ByteArrayInputStream JavaDoc(buffer.toByteArray())
175             );
176             r2 = (XYDifferenceRenderer) in.readObject();
177             in.close();
178         }
179         catch (Exception JavaDoc e) {
180             System.out.println(e.toString());
181         }
182         assertEquals(r1, r2);
183
184     }
185
186 }
187
Popular Tags