KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > chart > plot > junit > CombinedRangeCategoryPlotTests


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 License
20  * along with this library; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22  *
23  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
24  * in the United States and other countries.]
25  *
26  * ------------------------------------
27  * CombinedRangeCategoryPlotTests.java
28  * ------------------------------------
29  * (C) Copyright 2003, 2004, by Object Refinery Limited and Contributors.
30  *
31  * Original Author: David Gilbert (for Object Refinery Limited);
32  * Contributor(s): -;
33  *
34  * $Id: CombinedRangeCategoryPlotTests.java,v 1.2 2005/02/21 12:43:28 mungady Exp $
35  *
36  * Changes
37  * -------
38  * 21-Aug-2003 : Version 1 (DG);
39  *
40  */

41
42 package org.jfree.chart.plot.junit;
43
44 import java.io.ByteArrayInputStream JavaDoc;
45 import java.io.ByteArrayOutputStream JavaDoc;
46 import java.io.ObjectInput JavaDoc;
47 import java.io.ObjectInputStream JavaDoc;
48 import java.io.ObjectOutput JavaDoc;
49 import java.io.ObjectOutputStream JavaDoc;
50 import java.util.List JavaDoc;
51
52 import junit.framework.Test;
53 import junit.framework.TestCase;
54 import junit.framework.TestSuite;
55
56 import org.jfree.chart.axis.NumberAxis;
57 import org.jfree.chart.labels.StandardCategoryToolTipGenerator;
58 import org.jfree.chart.plot.CategoryPlot;
59 import org.jfree.chart.plot.CombinedRangeCategoryPlot;
60 import org.jfree.chart.renderer.category.BarRenderer;
61 import org.jfree.chart.renderer.category.LineAndShapeRenderer;
62 import org.jfree.data.category.CategoryDataset;
63 import org.jfree.data.category.DefaultCategoryDataset;
64
65 /**
66  * Tests for the {@link CombinedRangeCategoryPlot} class.
67  */

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

84     public CombinedRangeCategoryPlotTests(String JavaDoc name) {
85         super(name);
86     }
87
88     /**
89      * Test the equals() method.
90      */

91     public void testEquals() {
92         CombinedRangeCategoryPlot plot1 = createPlot();
93         CombinedRangeCategoryPlot plot2 = createPlot();
94         assertTrue(plot1.equals(plot2));
95     }
96
97     /**
98      * Confirm that cloning works.
99      */

100     public void testCloning() {
101         CombinedRangeCategoryPlot plot1 = createPlot();
102         CombinedRangeCategoryPlot plot2 = null;
103         try {
104             plot2 = (CombinedRangeCategoryPlot) plot1.clone();
105         }
106         catch (CloneNotSupportedException JavaDoc e) {
107             System.err.println("Failed to clone.");
108         }
109         assertTrue(plot1 != plot2);
110         assertTrue(plot1.getClass() == plot2.getClass());
111         assertTrue(plot1.equals(plot2));
112     }
113
114     /**
115      * Serialize an instance, restore it, and check for equality.
116      */

117     public void testSerialization() {
118         CombinedRangeCategoryPlot plot1 = createPlot();
119         CombinedRangeCategoryPlot plot2 = null;
120         try {
121             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
122             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
123             out.writeObject(plot1);
124             out.close();
125             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
126                 new ByteArrayInputStream JavaDoc(buffer.toByteArray())
127             );
128             plot2 = (CombinedRangeCategoryPlot) in.readObject();
129             in.close();
130         }
131         catch (Exception JavaDoc e) {
132             e.printStackTrace();
133         }
134         assertEquals(plot1, plot2);
135
136     }
137     
138     /**
139      * This is a test to replicate the bug report 1121172.
140      */

141     public void testRemoveSubplot() {
142         CombinedRangeCategoryPlot plot = new CombinedRangeCategoryPlot();
143         CategoryPlot plot1 = new CategoryPlot();
144         CategoryPlot plot2 = new CategoryPlot();
145         CategoryPlot plot3 = new CategoryPlot();
146         plot.add(plot1);
147         plot.add(plot2);
148         plot.add(plot3);
149         plot.remove(plot2);
150         List JavaDoc plots = plot.getSubplots();
151         assertEquals(2, plots.size());
152     }
153     
154     /**
155      * Creates a dataset.
156      *
157      * @return A dataset.
158      */

159     public CategoryDataset createDataset1() {
160
161         DefaultCategoryDataset result = new DefaultCategoryDataset();
162
163         // row keys...
164
String JavaDoc series1 = "First";
165         String JavaDoc series2 = "Second";
166
167         // column keys...
168
String JavaDoc type1 = "Type 1";
169         String JavaDoc type2 = "Type 2";
170         String JavaDoc type3 = "Type 3";
171         String JavaDoc type4 = "Type 4";
172         String JavaDoc type5 = "Type 5";
173         String JavaDoc type6 = "Type 6";
174         String JavaDoc type7 = "Type 7";
175         String JavaDoc type8 = "Type 8";
176
177         result.addValue(1.0, series1, type1);
178         result.addValue(4.0, series1, type2);
179         result.addValue(3.0, series1, type3);
180         result.addValue(5.0, series1, type4);
181         result.addValue(5.0, series1, type5);
182         result.addValue(7.0, series1, type6);
183         result.addValue(7.0, series1, type7);
184         result.addValue(8.0, series1, type8);
185
186         result.addValue(5.0, series2, type1);
187         result.addValue(7.0, series2, type2);
188         result.addValue(6.0, series2, type3);
189         result.addValue(8.0, series2, type4);
190         result.addValue(4.0, series2, type5);
191         result.addValue(4.0, series2, type6);
192         result.addValue(2.0, series2, type7);
193         result.addValue(1.0, series2, type8);
194
195         return result;
196
197     }
198
199     /**
200      * Creates a dataset.
201      *
202      * @return A dataset.
203      */

204     public CategoryDataset createDataset2() {
205
206         DefaultCategoryDataset result = new DefaultCategoryDataset();
207
208         // row keys...
209
String JavaDoc series1 = "Third";
210         String JavaDoc series2 = "Fourth";
211
212         // column keys...
213
String JavaDoc type1 = "Type 1";
214         String JavaDoc type2 = "Type 2";
215         String JavaDoc type3 = "Type 3";
216         String JavaDoc type4 = "Type 4";
217         String JavaDoc type5 = "Type 5";
218         String JavaDoc type6 = "Type 6";
219         String JavaDoc type7 = "Type 7";
220         String JavaDoc type8 = "Type 8";
221
222         result.addValue(11.0, series1, type1);
223         result.addValue(14.0, series1, type2);
224         result.addValue(13.0, series1, type3);
225         result.addValue(15.0, series1, type4);
226         result.addValue(15.0, series1, type5);
227         result.addValue(17.0, series1, type6);
228         result.addValue(17.0, series1, type7);
229         result.addValue(18.0, series1, type8);
230
231         result.addValue(15.0, series2, type1);
232         result.addValue(17.0, series2, type2);
233         result.addValue(16.0, series2, type3);
234         result.addValue(18.0, series2, type4);
235         result.addValue(14.0, series2, type5);
236         result.addValue(14.0, series2, type6);
237         result.addValue(12.0, series2, type7);
238         result.addValue(11.0, series2, type8);
239
240         return result;
241
242     }
243
244     /**
245      * Creates a sample plot.
246      *
247      * @return A plot.
248      */

249     private CombinedRangeCategoryPlot createPlot() {
250         CategoryDataset dataset1 = createDataset1();
251         NumberAxis rangeAxis1 = new NumberAxis("Value");
252         rangeAxis1.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
253         LineAndShapeRenderer renderer1 = new LineAndShapeRenderer();
254         renderer1.setBaseToolTipGenerator(
255             new StandardCategoryToolTipGenerator()
256         );
257         CategoryPlot subplot1 = new CategoryPlot(
258             dataset1, null, rangeAxis1, renderer1
259         );
260         subplot1.setDomainGridlinesVisible(true);
261         
262         CategoryDataset dataset2 = createDataset2();
263         NumberAxis rangeAxis2 = new NumberAxis("Value");
264         rangeAxis2.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
265         BarRenderer renderer2 = new BarRenderer();
266         renderer2.setBaseToolTipGenerator(
267             new StandardCategoryToolTipGenerator()
268         );
269         CategoryPlot subplot2 = new CategoryPlot(
270             dataset2, null, rangeAxis2, renderer2
271         );
272         subplot2.setDomainGridlinesVisible(true);
273
274         NumberAxis rangeAxis = new NumberAxis("Value");
275         CombinedRangeCategoryPlot plot
276             = new CombinedRangeCategoryPlot(rangeAxis);
277         plot.add(subplot1, 2);
278         plot.add(subplot2, 1);
279         return plot;
280     }
281 }
282
Popular Tags