KickJava   Java API By Example, From Geeks To Geeks.

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


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

42
43 package org.jfree.chart.renderer.category.junit;
44
45 import java.awt.Color JavaDoc;
46 import java.io.ByteArrayInputStream JavaDoc;
47 import java.io.ByteArrayOutputStream JavaDoc;
48 import java.io.ObjectInput JavaDoc;
49 import java.io.ObjectInputStream JavaDoc;
50 import java.io.ObjectOutput JavaDoc;
51 import java.io.ObjectOutputStream JavaDoc;
52
53 import junit.framework.Test;
54 import junit.framework.TestCase;
55 import junit.framework.TestSuite;
56
57 import org.jfree.chart.JFreeChart;
58 import org.jfree.chart.axis.CategoryAxis;
59 import org.jfree.chart.axis.NumberAxis;
60 import org.jfree.chart.plot.CategoryPlot;
61 import org.jfree.chart.renderer.category.StatisticalBarRenderer;
62 import org.jfree.data.statistics.DefaultStatisticalCategoryDataset;
63
64 /**
65  * Tests for the {@link StatisticalBarRenderer} class.
66  */

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

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

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

90     public void testEquals() {
91         StatisticalBarRenderer r1 = new StatisticalBarRenderer();
92         StatisticalBarRenderer r2 = new StatisticalBarRenderer();
93         assertEquals(r1, r2);
94         
95         r1.setErrorIndicatorPaint(Color.red);
96         assertFalse(r1.equals(r2));
97         r2.setErrorIndicatorPaint(Color.red);
98         assertTrue(r2.equals(r1));
99     }
100
101     /**
102      * Two objects that are equal are required to return the same hashCode.
103      */

104     public void testHashcode() {
105         StatisticalBarRenderer r1 = new StatisticalBarRenderer();
106         StatisticalBarRenderer r2 = new StatisticalBarRenderer();
107         assertTrue(r1.equals(r2));
108         int h1 = r1.hashCode();
109         int h2 = r2.hashCode();
110         assertEquals(h1, h2);
111     }
112     
113     /**
114      * Confirm that cloning works.
115      */

116     public void testCloning() {
117         StatisticalBarRenderer r1 = new StatisticalBarRenderer();
118         StatisticalBarRenderer r2 = null;
119         try {
120             r2 = (StatisticalBarRenderer) r1.clone();
121         }
122         catch (CloneNotSupportedException JavaDoc e) {
123             System.err.println("Failed to clone.");
124         }
125         assertTrue(r1 != r2);
126         assertTrue(r1.getClass() == r2.getClass());
127         assertTrue(r1.equals(r2));
128     }
129
130     /**
131      * Serialize an instance, restore it, and check for equality.
132      */

133     public void testSerialization() {
134
135         StatisticalBarRenderer r1 = new StatisticalBarRenderer();
136         StatisticalBarRenderer r2 = null;
137
138         try {
139             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
140             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
141             out.writeObject(r1);
142             out.close();
143
144             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
145                 new ByteArrayInputStream JavaDoc(buffer.toByteArray())
146             );
147             r2 = (StatisticalBarRenderer) in.readObject();
148             in.close();
149         }
150         catch (Exception JavaDoc e) {
151             System.out.println(e.toString());
152         }
153         assertEquals(r1, r2);
154
155     }
156
157     /**
158      * Draws the chart with a <code>null</code> info object to make sure that
159      * no exceptions are thrown (particularly by code in the renderer).
160      */

161     public void testDrawWithNullInfo() {
162         boolean success = false;
163         try {
164             DefaultStatisticalCategoryDataset dataset
165                 = new DefaultStatisticalCategoryDataset();
166             dataset.add(1.0, 2.0, "S1", "C1");
167             dataset.add(3.0, 4.0, "S1", "C2");
168             CategoryPlot plot = new CategoryPlot(dataset,
169                     new CategoryAxis("Category"), new NumberAxis("Value"),
170                     new StatisticalBarRenderer());
171             JFreeChart chart = new JFreeChart(plot);
172             /* BufferedImage image = */ chart.createBufferedImage(300, 200,
173                     null);
174             success = true;
175         }
176         catch (NullPointerException JavaDoc e) {
177             e.printStackTrace();
178             success = false;
179         }
180         assertTrue(success);
181     }
182
183 }
184
Popular Tags