KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > chart > renderer > category > junit > LineAndShapeRendererTests


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  * LineAndShapeRendererTests.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: LineAndShapeRendererTests.java,v 1.1.2.1 2006/10/03 15:41:33 mungady Exp $
36  *
37  * Changes
38  * -------
39  * 22-Sep-2003 : Version 1 (DG);
40  *
41  */

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

61 public class LineAndShapeRendererTests extends TestCase {
62
63     /**
64      * Returns the tests as a test suite.
65      *
66      * @return The test suite.
67      */

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

77     public LineAndShapeRendererTests(String JavaDoc name) {
78         super(name);
79     }
80
81     /**
82      * Test that the equals() method distinguishes all fields.
83      */

84     public void testEquals() {
85         
86         LineAndShapeRenderer r1 = new LineAndShapeRenderer();
87         LineAndShapeRenderer r2 = new LineAndShapeRenderer();
88         assertEquals(r1, r2);
89         
90         r1.setBaseLinesVisible(!r1.getBaseLinesVisible());
91         assertFalse(r1.equals(r2));
92         r2.setBaseLinesVisible(r1.getBaseLinesVisible());
93         assertTrue(r1.equals(r2));
94         
95         r1.setSeriesLinesVisible(1, true);
96         assertFalse(r1.equals(r2));
97         r2.setSeriesLinesVisible(1, true);
98         assertTrue(r1.equals(r2));
99         
100         r1.setLinesVisible(false);
101         assertFalse(r1.equals(r2));
102         r2.setLinesVisible(false);
103         assertTrue(r1.equals(r2));
104         
105         r1.setBaseShapesVisible(!r1.getBaseShapesVisible());
106         assertFalse(r1.equals(r2));
107         r2.setBaseShapesVisible(r1.getBaseShapesVisible());
108         assertTrue(r1.equals(r2));
109         
110         r1.setSeriesShapesVisible(1, true);
111         assertFalse(r1.equals(r2));
112         r2.setSeriesShapesVisible(1, true);
113         assertTrue(r1.equals(r2));
114         
115         r1.setShapesVisible(false);
116         assertFalse(r1.equals(r2));
117         r2.setShapesVisible(false);
118         assertTrue(r1.equals(r2));
119
120         r1.setShapesFilled(false);
121         assertFalse(r1.equals(r2));
122         r2.setShapesFilled(false);
123         assertTrue(r1.equals(r2));
124         
125         r1.setSeriesShapesFilled(1, true);
126         assertFalse(r1.equals(r2));
127         r2.setSeriesShapesFilled(1, true);
128         assertTrue(r1.equals(r2));
129         
130         r1.setBaseShapesFilled(false);
131         assertFalse(r1.equals(r2));
132         r2.setBaseShapesFilled(false);
133         assertTrue(r1.equals(r2));
134         
135         r1.setUseOutlinePaint(true);
136         assertFalse(r1.equals(r2));
137         r2.setUseOutlinePaint(true);
138         assertTrue(r1.equals(r2));
139         
140     }
141
142     /**
143      * Two objects that are equal are required to return the same hashCode.
144      */

145     public void testHashcode() {
146         LineAndShapeRenderer r1 = new LineAndShapeRenderer();
147         LineAndShapeRenderer r2 = new LineAndShapeRenderer();
148         assertTrue(r1.equals(r2));
149         int h1 = r1.hashCode();
150         int h2 = r2.hashCode();
151         assertEquals(h1, h2);
152     }
153     
154     /**
155      * Confirm that cloning works.
156      */

157     public void testCloning() {
158         LineAndShapeRenderer r1 = new LineAndShapeRenderer();
159         LineAndShapeRenderer r2 = null;
160         try {
161             r2 = (LineAndShapeRenderer) r1.clone();
162         }
163         catch (CloneNotSupportedException JavaDoc e) {
164             e.printStackTrace();
165         }
166         assertTrue(r1 != r2);
167         assertTrue(r1.getClass() == r2.getClass());
168         assertTrue(r1.equals(r2));
169         
170         assertTrue(checkIndependence(r1, r2));
171         
172     }
173
174     /**
175      * Checks that the two renderers are equal but independent of one another.
176      *
177      * @param r1 renderer 1.
178      * @param r2 renderer 2.
179      *
180      * @return A boolean.
181      */

182     private boolean checkIndependence(LineAndShapeRenderer r1,
183                                       LineAndShapeRenderer r2) {
184
185         // should be equal...
186
if (!r1.equals(r2)) {
187             return false;
188         }
189         
190         // and independent...
191
r1.setBaseLinesVisible(!r1.getBaseLinesVisible());
192         if (r1.equals(r2)) {
193             return false;
194         }
195         r2.setBaseLinesVisible(r1.getBaseLinesVisible());
196         if (!r1.equals(r2)) {
197             return false;
198         }
199
200         r1.setSeriesLinesVisible(1, true);
201         if (r1.equals(r2)) {
202             return false;
203         }
204         r2.setSeriesLinesVisible(1, true);
205         if (!r1.equals(r2)) {
206             return false;
207         }
208             
209         r1.setLinesVisible(false);
210         if (r1.equals(r2)) {
211             return false;
212         }
213         r2.setLinesVisible(false);
214         if (!r1.equals(r2)) {
215             return false;
216         }
217         
218         r1.setBaseShapesVisible(!r1.getBaseShapesVisible());
219         if (r1.equals(r2)) {
220             return false;
221         }
222         r2.setBaseShapesVisible(r1.getBaseShapesVisible());
223         if (!r1.equals(r2)) {
224             return false;
225         }
226
227         r1.setSeriesShapesVisible(1, true);
228         if (r1.equals(r2)) {
229             return false;
230         }
231         r2.setSeriesShapesVisible(1, true);
232         if (!r1.equals(r2)) {
233             return false;
234         }
235             
236         r1.setShapesVisible(false);
237         if (r1.equals(r2)) {
238             return false;
239         }
240         r2.setShapesVisible(false);
241         if (!r1.equals(r2)) {
242             return false;
243         }
244         
245         boolean flag = true;
246         Boolean JavaDoc existing = r1.getShapesFilled();
247         if (existing != null) {
248             flag = !existing.booleanValue();
249         }
250         r1.setShapesFilled(flag);
251         if (r1.equals(r2)) {
252             return false;
253         }
254         r2.setShapesFilled(flag);
255         if (!r1.equals(r2)) {
256             return false;
257         }
258
259         r1.setShapesFilled(false);
260         r2.setShapesFilled(false);
261         r1.setSeriesShapesFilled(0, false);
262         r2.setSeriesShapesFilled(0, true);
263         if (r1.equals(r2)) {
264             return false;
265         }
266         r2.setSeriesShapesFilled(0, false);
267         if (!r1.equals(r2)) {
268             return false;
269         }
270         
271         r1.setBaseShapesFilled(false);
272         r2.setBaseShapesFilled(true);
273         if (r1.equals(r2)) {
274             return false;
275         }
276         r2.setBaseShapesFilled(false);
277         if (!r1.equals(r2)) {
278             return false;
279         }
280         return true;
281     
282     }
283
284     /**
285      * Serialize an instance, restore it, and check for equality.
286      */

287     public void testSerialization() {
288
289         LineAndShapeRenderer r1 = new LineAndShapeRenderer();
290         LineAndShapeRenderer r2 = null;
291
292         try {
293             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
294             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
295             out.writeObject(r1);
296             out.close();
297
298             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
299                 new ByteArrayInputStream JavaDoc(buffer.toByteArray())
300             );
301             r2 = (LineAndShapeRenderer) in.readObject();
302             in.close();
303         }
304         catch (Exception JavaDoc e) {
305             System.out.println(e.toString());
306         }
307         assertEquals(r1, r2);
308
309     }
310
311 }
312
Popular Tags