KickJava   Java API By Example, From Geeks To Geeks.

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


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  * LevelRendererTests.java
29  * -----------------------
30  * (C) Copyright 2005, 2006, by Object Refinery Limited and Contributors.
31  *
32  * Original Author: David Gilbert (for Object Refinery Limited);
33  * Contributor(s): -;
34  *
35  * $Id: LevelRendererTests.java,v 1.1.2.1 2006/10/03 15:41:34 mungady Exp $
36  *
37  * Changes
38  * -------
39  * 29-Mar-2005 : 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.JFreeChart;
57 import org.jfree.chart.axis.CategoryAxis;
58 import org.jfree.chart.axis.NumberAxis;
59 import org.jfree.chart.plot.CategoryPlot;
60 import org.jfree.chart.renderer.category.LevelRenderer;
61 import org.jfree.data.category.DefaultCategoryDataset;
62
63 /**
64  * Tests for the {@link LevelRenderer} class.
65  */

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

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

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

89     public void testEquals() {
90         LevelRenderer r1 = new LevelRenderer();
91         LevelRenderer r2 = new LevelRenderer();
92         assertTrue(r1.equals(r2));
93         assertTrue(r2.equals(r1));
94         
95         r1.setItemMargin(0.123);
96         assertFalse(r1.equals(r2));
97         r2.setItemMargin(0.123);
98         assertTrue(r1.equals(r2));
99
100         r1.setMaximumItemWidth(0.234);
101         assertFalse(r1.equals(r2));
102         r2.setMaximumItemWidth(0.234);
103         assertTrue(r1.equals(r2));
104     
105     }
106
107     /**
108      * Two objects that are equal are required to return the same hashCode.
109      */

110     public void testHashcode() {
111         LevelRenderer r1 = new LevelRenderer();
112         LevelRenderer r2 = new LevelRenderer();
113         assertTrue(r1.equals(r2));
114         int h1 = r1.hashCode();
115         int h2 = r2.hashCode();
116         assertEquals(h1, h2);
117     }
118     
119     /**
120      * Confirm that cloning works.
121      */

122     public void testCloning() {
123         LevelRenderer r1 = new LevelRenderer();
124         r1.setItemMargin(0.123);
125         r1.setMaximumItemWidth(0.234);
126         LevelRenderer r2 = null;
127         try {
128             r2 = (LevelRenderer) r1.clone();
129         }
130         catch (CloneNotSupportedException JavaDoc e) {
131             System.err.println("Failed to clone.");
132         }
133         assertTrue(r1 != r2);
134         assertTrue(r1.getClass() == r2.getClass());
135         assertTrue(r1.equals(r2));
136         
137         assertTrue(checkIndependence(r1, r2));
138         
139     }
140
141     /**
142      * Checks that the two renderers are equal but independent of one another.
143      *
144      * @param r1 renderer 1.
145      * @param r2 renderer 2.
146      *
147      * @return A boolean.
148      */

149     private boolean checkIndependence(LevelRenderer r1, LevelRenderer r2) {
150
151         // should be equal...
152
boolean b0 = r1.equals(r2);
153         
154         // and independent...
155
r1.setItemMargin(0.0);
156         boolean b1 = !r1.equals(r2);
157         r2.setItemMargin(0.0);
158         boolean b2 = r1.equals(r2);
159         
160         return b0 && b1 && b2;
161     
162     }
163     
164     /**
165      * Serialize an instance, restore it, and check for equality.
166      */

167     public void testSerialization() {
168
169         LevelRenderer r1 = new LevelRenderer();
170         LevelRenderer r2 = null;
171
172         try {
173             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
174             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
175             out.writeObject(r1);
176             out.close();
177
178             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
179                 new ByteArrayInputStream JavaDoc(buffer.toByteArray())
180             );
181             r2 = (LevelRenderer) in.readObject();
182             in.close();
183         }
184         catch (Exception JavaDoc e) {
185             e.printStackTrace();
186         }
187         assertEquals(r1, r2);
188
189     }
190     
191     /**
192      * Draws the chart with a <code>null</code> info object to make sure that
193      * no exceptions are thrown (particularly by code in the renderer).
194      */

195     public void testDrawWithNullInfo() {
196         boolean success = false;
197         try {
198             DefaultCategoryDataset dataset = new DefaultCategoryDataset();
199             dataset.addValue(1.0, "S1", "C1");
200             CategoryPlot plot = new CategoryPlot(dataset,
201                     new CategoryAxis("Category"), new NumberAxis("Value"),
202                     new LevelRenderer());
203             JFreeChart chart = new JFreeChart(plot);
204             /* BufferedImage image = */ chart.createBufferedImage(300, 200,
205                     null);
206             success = true;
207         }
208         catch (NullPointerException JavaDoc e) {
209             e.printStackTrace();
210             success = false;
211         }
212         assertTrue(success);
213     }
214
215 }
216
Popular Tags