KickJava   Java API By Example, From Geeks To Geeks.

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


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

44
45 package org.jfree.chart.renderer.xy.junit;
46
47 import java.awt.geom.Line2D 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.renderer.xy.StandardXYItemRenderer;
60 import org.jfree.util.UnitType;
61
62 /**
63  * Tests for the {@link StandardXYItemRenderer} class.
64  */

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

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

88     public void testEquals() {
89         StandardXYItemRenderer r1 = new StandardXYItemRenderer();
90         StandardXYItemRenderer r2 = new StandardXYItemRenderer();
91         assertEquals(r1, r2);
92         
93         r1.setBaseShapesVisible(true);
94         assertFalse(r1.equals(r2));
95         r2.setBaseShapesVisible(true);
96         assertTrue(r1.equals(r2));
97         
98         r1.setPlotLines(false);
99         assertFalse(r1.equals(r2));
100         r2.setPlotLines(false);
101         assertTrue(r1.equals(r2));
102
103         r1.setPlotImages(true);
104         assertFalse(r1.equals(r2));
105         r2.setPlotImages(true);
106         assertTrue(r1.equals(r2));
107
108         r1.setShapesFilled(false);
109         assertFalse(r1.equals(r2));
110         r2.setShapesFilled(false);
111         assertTrue(r1.equals(r2));
112         
113         r1.setGapThresholdType(UnitType.ABSOLUTE);
114         assertFalse(r1.equals(r2));
115         r2.setGapThresholdType(UnitType.ABSOLUTE);
116         assertTrue(r1.equals(r2));
117         
118         r1.setGapThreshold(1.23);
119         assertFalse(r1.equals(r2));
120         r2.setGapThreshold(1.23);
121         assertTrue(r1.equals(r2));
122         
123         r1.setLegendLine(new Line2D.Double JavaDoc(1.0, 2.0, 3.0, 4.0));
124         assertFalse(r1.equals(r2));
125         r2.setLegendLine(new Line2D.Double JavaDoc(1.0, 2.0, 3.0, 4.0));
126         assertTrue(r1.equals(r2));
127     }
128
129     /**
130      * Two objects that are equal are required to return the same hashCode.
131      */

132     public void testHashcode() {
133         StandardXYItemRenderer r1 = new StandardXYItemRenderer();
134         StandardXYItemRenderer r2 = new StandardXYItemRenderer();
135         assertTrue(r1.equals(r2));
136         int h1 = r1.hashCode();
137         int h2 = r2.hashCode();
138         assertEquals(h1, h2);
139     }
140     
141     /**
142      * Confirm that cloning works.
143      */

144     public void testCloning() {
145         StandardXYItemRenderer r1 = new StandardXYItemRenderer();
146         StandardXYItemRenderer r2 = null;
147         try {
148             r2 = (StandardXYItemRenderer) 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         StandardXYItemRenderer r1 = new StandardXYItemRenderer();
164         StandardXYItemRenderer r2 = null;
165
166         try {
167             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
168             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
169             out.writeObject(r1);
170             out.close();
171
172             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
173                 new ByteArrayInputStream JavaDoc(buffer.toByteArray())
174             );
175             r2 = (StandardXYItemRenderer) in.readObject();
176             in.close();
177         }
178         catch (Exception JavaDoc e) {
179             System.out.println(e.toString());
180         }
181         assertEquals(r1, r2);
182
183     }
184
185 }
186
Popular Tags