KickJava   Java API By Example, From Geeks To Geeks.

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


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  * StatisticalLineAndShapeRendererTests.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: StatisticalLineAndShapeRendererTests.java,v 1.1.2.1 2006/10/03 15:41:34 mungady Exp $
36  *
37  * Changes
38  * -------
39  * 15-Jun-2005 : Version 1 (DG);
40  * 25-Sep-2006 : Added test1562759() (DG);
41  *
42  */

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

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

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

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

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

108     public void testHashcode() {
109         StatisticalLineAndShapeRenderer r1
110             = new StatisticalLineAndShapeRenderer();
111         StatisticalLineAndShapeRenderer r2
112             = new StatisticalLineAndShapeRenderer();
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         StatisticalLineAndShapeRenderer r1
124             = new StatisticalLineAndShapeRenderer();
125         StatisticalLineAndShapeRenderer r2 = null;
126         try {
127             r2 = (StatisticalLineAndShapeRenderer) r1.clone();
128         }
129         catch (CloneNotSupportedException JavaDoc e) {
130             System.err.println("Failed to clone.");
131         }
132         assertTrue(r1 != r2);
133         assertTrue(r1.getClass() == r2.getClass());
134         assertTrue(r1.equals(r2));
135     }
136
137     /**
138      * Serialize an instance, restore it, and check for equality.
139      */

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

168     public void testDrawWithNullInfo() {
169         boolean success = false;
170         try {
171             DefaultStatisticalCategoryDataset dataset
172                 = new DefaultStatisticalCategoryDataset();
173             dataset.add(1.0, 2.0, "S1", "C1");
174             dataset.add(3.0, 4.0, "S1", "C2");
175             CategoryPlot plot = new CategoryPlot(dataset,
176                     new CategoryAxis("Category"), new NumberAxis("Value"),
177                     new StatisticalLineAndShapeRenderer());
178             JFreeChart chart = new JFreeChart(plot);
179             /* BufferedImage image = */ chart.createBufferedImage(300, 200,
180                     null);
181             success = true;
182         }
183         catch (NullPointerException JavaDoc e) {
184             e.printStackTrace();
185             success = false;
186         }
187         assertTrue(success);
188     }
189     
190     /**
191      * A simple test for bug report 1562759.
192      */

193     public void test1562759() {
194         StatisticalLineAndShapeRenderer r
195             = new StatisticalLineAndShapeRenderer(true, false);
196         assertTrue(r.getBaseLinesVisible());
197         assertFalse(r.getBaseShapesVisible());
198         
199         r = new StatisticalLineAndShapeRenderer(false, true);
200         assertFalse(r.getBaseLinesVisible());
201         assertTrue(r.getBaseShapesVisible());
202     }
203
204 }
205
Popular Tags