KickJava   Java API By Example, From Geeks To Geeks.

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


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  * BoxAndWhiskerRendererTests.java
29  * -------------------------------
30  * (C) Copyright 2003-2006, by Object Refinery Limited and Contributors.
31  *
32  * Original Author: David Gilbert (for Object Refinery Limited);
33  * Contributor(s): -;
34  *
35  * $Id: BoxAndWhiskerRendererTests.java,v 1.1.2.2 2006/10/12 16:06:18 mungady Exp $
36  *
37  * Changes
38  * -------
39  * 22-Oct-2003 : Version 1 (DG);
40  * 23-Apr-2004 : Extended testEquals() method (DG);
41  * 12-Oct-2006 : Added new checks for bug 1572478 (DG);
42  *
43  */

44
45 package org.jfree.chart.renderer.category.junit;
46
47 import java.awt.Color JavaDoc;
48 import java.awt.GradientPaint JavaDoc;
49 import java.awt.Graphics2D JavaDoc;
50 import java.awt.geom.Rectangle2D JavaDoc;
51 import java.awt.image.BufferedImage JavaDoc;
52 import java.io.ByteArrayInputStream JavaDoc;
53 import java.io.ByteArrayOutputStream JavaDoc;
54 import java.io.ObjectInput JavaDoc;
55 import java.io.ObjectInputStream JavaDoc;
56 import java.io.ObjectOutput JavaDoc;
57 import java.io.ObjectOutputStream JavaDoc;
58 import java.util.ArrayList JavaDoc;
59 import java.util.List JavaDoc;
60
61 import junit.framework.Test;
62 import junit.framework.TestCase;
63 import junit.framework.TestSuite;
64 import org.jfree.chart.ChartRenderingInfo;
65
66 import org.jfree.chart.JFreeChart;
67 import org.jfree.chart.axis.CategoryAxis;
68 import org.jfree.chart.axis.NumberAxis;
69 import org.jfree.chart.plot.CategoryPlot;
70 import org.jfree.chart.plot.PlotOrientation;
71 import org.jfree.chart.renderer.category.BoxAndWhiskerRenderer;
72 import org.jfree.data.statistics.BoxAndWhiskerItem;
73 import org.jfree.data.statistics.DefaultBoxAndWhiskerCategoryDataset;
74
75 /**
76  * Tests for the {@link BoxAndWhiskerRenderer} class.
77  */

78 public class BoxAndWhiskerRendererTests extends TestCase {
79
80     /**
81      * Returns the tests as a test suite.
82      *
83      * @return The test suite.
84      */

85     public static Test suite() {
86         return new TestSuite(BoxAndWhiskerRendererTests.class);
87     }
88
89     /**
90      * Constructs a new set of tests.
91      *
92      * @param name the name of the tests.
93      */

94     public BoxAndWhiskerRendererTests(String JavaDoc name) {
95         super(name);
96     }
97
98     /**
99      * Test that the equals() method distinguishes all fields.
100      */

101     public void testEquals() {
102         BoxAndWhiskerRenderer r1 = new BoxAndWhiskerRenderer();
103         BoxAndWhiskerRenderer r2 = new BoxAndWhiskerRenderer();
104         assertEquals(r1, r2);
105         
106         r1.setArtifactPaint(new GradientPaint JavaDoc(1.0f, 2.0f, Color.yellow,
107                 3.0f, 4.0f, Color.blue));
108         assertFalse(r1.equals(r2));
109         r2.setArtifactPaint(new GradientPaint JavaDoc(1.0f, 2.0f, Color.yellow,
110                 3.0f, 4.0f, Color.blue));
111         assertEquals(r1, r2);
112         
113         r1.setFillBox(!r1.getFillBox());
114         assertFalse(r1.equals(r2));
115         r2.setFillBox(!r2.getFillBox());
116         assertEquals(r1, r2);
117         
118         r1.setItemMargin(0.11);
119         assertFalse(r1.equals(r2));
120         r2.setItemMargin(0.11);
121         assertEquals(r1, r2);
122         
123     }
124
125     /**
126      * Two objects that are equal are required to return the same hashCode.
127      */

128     public void testHashcode() {
129         BoxAndWhiskerRenderer r1 = new BoxAndWhiskerRenderer();
130         BoxAndWhiskerRenderer r2 = new BoxAndWhiskerRenderer();
131         assertTrue(r1.equals(r2));
132         int h1 = r1.hashCode();
133         int h2 = r2.hashCode();
134         assertEquals(h1, h2);
135     }
136     
137     /**
138      * Confirm that cloning works.
139      */

140     public void testCloning() {
141         BoxAndWhiskerRenderer r1 = new BoxAndWhiskerRenderer();
142         BoxAndWhiskerRenderer r2 = null;
143         try {
144             r2 = (BoxAndWhiskerRenderer) r1.clone();
145         }
146         catch (CloneNotSupportedException JavaDoc e) {
147             e.printStackTrace();
148         }
149         assertTrue(r1 != r2);
150         assertTrue(r1.getClass() == r2.getClass());
151         assertTrue(r1.equals(r2));
152     }
153
154     /**
155      * Serialize an instance, restore it, and check for equality.
156      */

157     public void testSerialization() {
158
159         BoxAndWhiskerRenderer r1 = new BoxAndWhiskerRenderer();
160         BoxAndWhiskerRenderer r2 = null;
161
162         try {
163             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
164             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
165             out.writeObject(r1);
166             out.close();
167
168             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
169                     new ByteArrayInputStream JavaDoc(buffer.toByteArray()));
170             r2 = (BoxAndWhiskerRenderer) in.readObject();
171             in.close();
172         }
173         catch (Exception JavaDoc e) {
174             e.printStackTrace();
175         }
176         assertEquals(r1, r2);
177
178     }
179     
180     /**
181      * Draws the chart with a <code>null</code> info object to make sure that
182      * no exceptions are thrown (particularly by code in the renderer).
183      */

184     public void testDrawWithNullInfo() {
185         boolean success = false;
186         try {
187             DefaultBoxAndWhiskerCategoryDataset dataset
188                 = new DefaultBoxAndWhiskerCategoryDataset();
189             dataset.add(new BoxAndWhiskerItem(new Double JavaDoc(1.0), new Double JavaDoc(2.0),
190                     new Double JavaDoc(0.0), new Double JavaDoc(4.0), new Double JavaDoc(0.5),
191                     new Double JavaDoc(4.5), new Double JavaDoc(-0.5), new Double JavaDoc(5.5),
192                     null), "S1", "C1");
193             CategoryPlot plot = new CategoryPlot(dataset,
194                     new CategoryAxis("Category"), new NumberAxis("Value"),
195                     new BoxAndWhiskerRenderer());
196             JFreeChart chart = new JFreeChart(plot);
197             /* BufferedImage image = */ chart.createBufferedImage(300, 200,
198                     null);
199             success = true;
200         }
201         catch (NullPointerException JavaDoc e) {
202             success = false;
203         }
204         assertTrue(success);
205     }
206     
207     /**
208      * A check for bug 1572478 (for the vertical orientation).
209      */

210     public void testBug1572478Vertical() {
211         DefaultBoxAndWhiskerCategoryDataset dataset
212                 = new DefaultBoxAndWhiskerCategoryDataset() {
213                 
214             public Number JavaDoc getQ1Value(int row, int column) {
215                 return null;
216             }
217
218             public Number JavaDoc getQ1Value(Comparable JavaDoc rowKey, Comparable JavaDoc columnKey) {
219                 return null;
220             }
221         };
222         List JavaDoc values = new ArrayList JavaDoc();
223         values.add(new Double JavaDoc(1.0));
224         values.add(new Double JavaDoc(10.0));
225         values.add(new Double JavaDoc(100.0));
226         dataset.add(values, "row", "column");
227         CategoryPlot plot = new CategoryPlot(dataset, new CategoryAxis("x"),
228                 new NumberAxis("y"), new BoxAndWhiskerRenderer());
229         JFreeChart chart = new JFreeChart(plot);
230         boolean success = false;
231
232         try {
233             BufferedImage JavaDoc image = new BufferedImage JavaDoc(200 , 100,
234                     BufferedImage.TYPE_INT_RGB);
235             Graphics2D JavaDoc g2 = image.createGraphics();
236             chart.draw(g2, new Rectangle2D.Double JavaDoc(0, 0, 200, 100), null,
237                     new ChartRenderingInfo());
238             g2.dispose();
239             success = true;
240         }
241         catch (Exception JavaDoc e) {
242             success = false;
243         }
244
245         assertTrue(success);
246
247     }
248
249     /**
250      * A check for bug 1572478 (for the horizontal orientation).
251      */

252     public void testBug1572478Horizontal() {
253         DefaultBoxAndWhiskerCategoryDataset dataset
254                 = new DefaultBoxAndWhiskerCategoryDataset() {
255                 
256             public Number JavaDoc getQ1Value(int row, int column) {
257                 return null;
258             }
259
260             public Number JavaDoc getQ1Value(Comparable JavaDoc rowKey, Comparable JavaDoc columnKey) {
261                 return null;
262             }
263         };
264         List JavaDoc values = new ArrayList JavaDoc();
265         values.add(new Double JavaDoc(1.0));
266         values.add(new Double JavaDoc(10.0));
267         values.add(new Double JavaDoc(100.0));
268         dataset.add(values, "row", "column");
269         CategoryPlot plot = new CategoryPlot(dataset, new CategoryAxis("x"),
270                 new NumberAxis("y"), new BoxAndWhiskerRenderer());
271         plot.setOrientation(PlotOrientation.HORIZONTAL);
272         JFreeChart chart = new JFreeChart(plot);
273         boolean success = false;
274
275         try {
276             BufferedImage JavaDoc image = new BufferedImage JavaDoc(200 , 100,
277                     BufferedImage.TYPE_INT_RGB);
278             Graphics2D JavaDoc g2 = image.createGraphics();
279             chart.draw(g2, new Rectangle2D.Double JavaDoc(0, 0, 200, 100), null,
280                     new ChartRenderingInfo());
281             g2.dispose();
282             success = true;
283         }
284         catch (Exception JavaDoc e) {
285             success = false;
286         }
287
288         assertTrue(success);
289
290     }
291 }
292
Popular Tags