KickJava   Java API By Example, From Geeks To Geeks.

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


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  * StackedXYBarRendererTests.java
29  * ------------------------------
30  * (C) Copyright 2004, 2005, by Object Refinery Limited and Contributors.
31  *
32  * Original Author: David Gilbert (for Object Refinery Limited);
33  * Contributor(s): -;
34  *
35  * $Id: StackedXYBarRendererTests.java,v 1.1.2.1 2006/10/03 15:41:46 mungady Exp $
36  *
37  * Changes
38  * -------
39  * 10-Sep-2004 : Version 1 (DG);
40  * 06-Jan-2005 : Added test for auto range calculation (DG);
41  *
42  */

43
44 package org.jfree.chart.renderer.xy.junit;
45
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.ChartFactory;
58 import org.jfree.chart.JFreeChart;
59 import org.jfree.chart.axis.NumberAxis;
60 import org.jfree.chart.plot.PlotOrientation;
61 import org.jfree.chart.plot.XYPlot;
62 import org.jfree.chart.renderer.xy.StackedXYBarRenderer;
63 import org.jfree.data.Range;
64 import org.jfree.data.xy.TableXYDataset;
65
66 /**
67  * Tests for the {@link StackedXYBarRenderer} class.
68  */

69 public class StackedXYBarRendererTests extends TestCase {
70
71     /**
72      * Returns the tests as a test suite.
73      *
74      * @return The test suite.
75      */

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

85     public StackedXYBarRendererTests(String JavaDoc name) {
86         super(name);
87     }
88
89     /**
90      * Test that the equals() method distinguishes all fields.
91      */

92     public void testEquals() {
93         StackedXYBarRenderer r1 = new StackedXYBarRenderer();
94         StackedXYBarRenderer r2 = new StackedXYBarRenderer();
95         assertTrue(r1.equals(r2));
96         assertTrue(r2.equals(r1));
97     }
98
99     /**
100      * Two objects that are equal are required to return the same hashCode.
101      */

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

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

131     public void testSerialization() {
132         StackedXYBarRenderer r1 = new StackedXYBarRenderer();
133         StackedXYBarRenderer r2 = null;
134         try {
135             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
136             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
137             out.writeObject(r1);
138             out.close();
139
140             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
141                 new ByteArrayInputStream JavaDoc(buffer.toByteArray())
142             );
143             r2 = (StackedXYBarRenderer) in.readObject();
144             in.close();
145         }
146         catch (Exception JavaDoc e) {
147             System.out.println(e.toString());
148         }
149         assertEquals(r1, r2);
150     }
151
152     /**
153      * Check that the renderer is calculating the domain bounds correctly.
154      */

155     public void testFindDomainBounds() {
156         TableXYDataset dataset
157             = RendererXYPackageTests.createTestTableXYDataset();
158         JFreeChart chart = ChartFactory.createStackedXYAreaChart(
159             "Test Chart", "X", "Y", dataset,
160             PlotOrientation.VERTICAL, false, false, false
161         );
162         XYPlot plot = (XYPlot) chart.getPlot();
163         plot.setRenderer(new StackedXYBarRenderer());
164         NumberAxis domainAxis = (NumberAxis) plot.getDomainAxis();
165         domainAxis.setAutoRangeIncludesZero(false);
166         Range bounds = domainAxis.getRange();
167         assertFalse(bounds.contains(0.3));
168         assertTrue(bounds.contains(0.5));
169         assertTrue(bounds.contains(2.5));
170         assertFalse(bounds.contains(2.8));
171     }
172
173     /**
174      * Check that the renderer is calculating the range bounds correctly.
175      */

176     public void testFindRangeBounds() {
177         TableXYDataset dataset
178             = RendererXYPackageTests.createTestTableXYDataset();
179         JFreeChart chart = ChartFactory.createStackedXYAreaChart(
180             "Test Chart", "X", "Y", dataset,
181             PlotOrientation.VERTICAL, false, false, false
182         );
183         XYPlot plot = (XYPlot) chart.getPlot();
184         plot.setRenderer(new StackedXYBarRenderer());
185         NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
186         Range bounds = rangeAxis.getRange();
187         assertTrue(bounds.contains(6.0));
188         assertTrue(bounds.contains(8.0));
189     }
190
191 }
192
Popular Tags