KickJava   Java API By Example, From Geeks To Geeks.

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


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  * GroupedStackedBarRendererTests.java
29  * -----------------------------------
30  * (C) Copyright 2004, by Object Refinery Limited and Contributors.
31  *
32  * Original Author: David Gilbert (for Object Refinery Limited);
33  * Contributor(s): -;
34  *
35  * $Id: GroupedStackedBarRendererTests.java,v 1.1.2.1 2006/10/03 15:41:35 mungady Exp $
36  *
37  * Changes
38  * -------
39  * 08-Jul-2004 : 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.GroupedStackedBarRenderer;
61 import org.jfree.data.KeyToGroupMap;
62 import org.jfree.data.category.DefaultCategoryDataset;
63
64 /**
65  * Tests for the {@link GroupedStackedBarRenderer} class.
66  */

67 public class GroupedStackedBarRendererTests 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(GroupedStackedBarRendererTests.class);
76     }
77
78     /**
79      * Constructs a new set of tests.
80      *
81      * @param name the name of the tests.
82      */

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

90     public void testEquals() {
91         GroupedStackedBarRenderer r1 = new GroupedStackedBarRenderer();
92         GroupedStackedBarRenderer r2 = new GroupedStackedBarRenderer();
93         assertTrue(r1.equals(r2));
94         assertTrue(r2.equals(r1));
95         
96         // map
97
KeyToGroupMap m1 = new KeyToGroupMap("G1");
98         m1.mapKeyToGroup("S1", "G2");
99         r1.setSeriesToGroupMap(m1);
100         assertFalse(r1.equals(r2));
101         KeyToGroupMap m2 = new KeyToGroupMap("G1");
102         m2.mapKeyToGroup("S1", "G2");
103         r2.setSeriesToGroupMap(m2);
104         assertTrue(r1.equals(r2));
105     }
106     
107     /**
108      * Confirm that cloning works.
109      */

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

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

154     public void testDrawWithNullInfo() {
155         boolean success = false;
156         try {
157             DefaultCategoryDataset dataset = new DefaultCategoryDataset();
158             dataset.addValue(1.0, "S1", "C1");
159             dataset.addValue(2.0, "S1", "C2");
160             dataset.addValue(3.0, "S2", "C1");
161             dataset.addValue(4.0, "S2", "C2");
162             GroupedStackedBarRenderer renderer
163                 = new GroupedStackedBarRenderer();
164             CategoryPlot plot = new CategoryPlot(dataset,
165                     new CategoryAxis("Category"), new NumberAxis("Value"),
166                     renderer);
167             JFreeChart chart = new JFreeChart(plot);
168             /* BufferedImage image = */ chart.createBufferedImage(300, 200,
169                     null);
170             success = true;
171         }
172         catch (NullPointerException JavaDoc e) {
173             e.printStackTrace();
174             success = false;
175         }
176         assertTrue(success);
177     }
178
179 }
180
Popular Tags