KickJava   Java API By Example, From Geeks To Geeks.

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


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  * CombinedDomainCategoryPlotTests.java
28  * ------------------------------------
29  * (C) Copyright 2003-2005, by Object Refinery Limited and Contributors.
30  *
31  * Original Author: David Gilbert (for Object Refinery Limited);
32  * Contributor(s): -;
33  *
34  * $Id: CombinedDomainCategoryPlotTests.java,v 1.2 2005/02/21 12:43:30 mungady Exp $
35  *
36  * Changes
37  * -------
38  * 19-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.CategoryAxis;
57 import org.jfree.chart.axis.NumberAxis;
58 import org.jfree.chart.labels.StandardCategoryToolTipGenerator;
59 import org.jfree.chart.plot.CategoryPlot;
60 import org.jfree.chart.plot.CombinedDomainCategoryPlot;
61 import org.jfree.chart.renderer.category.BarRenderer;
62 import org.jfree.chart.renderer.category.LineAndShapeRenderer;
63 import org.jfree.data.category.CategoryDataset;
64 import org.jfree.data.category.DefaultCategoryDataset;
65
66 /**
67  * Tests for the {@link CombinedDomainCategoryPlot} class.
68  */

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

85     public CombinedDomainCategoryPlotTests(String JavaDoc name) {
86         super(name);
87     }
88
89     /**
90      * This is a test to replicate the bug report 987080.
91      */

92     public void testRemoveSubplot() {
93         CombinedDomainCategoryPlot plot = new CombinedDomainCategoryPlot();
94         CategoryPlot plot1 = new CategoryPlot();
95         CategoryPlot plot2 = new CategoryPlot();
96         plot.add(plot1);
97         plot.add(plot2);
98         // remove plot2, but plot1 is removed instead
99
plot.remove(plot2);
100         List JavaDoc plots = plot.getSubplots();
101         assertTrue(plots.get(0) == plot1);
102         assertEquals(1, plots.size());
103     }
104     
105     /**
106      * Some checks for the equals() method.
107      */

108     public void testEquals() {
109         CombinedDomainCategoryPlot plot1 = createPlot();
110         CombinedDomainCategoryPlot plot2 = createPlot();
111         assertTrue(plot1.equals(plot2));
112     }
113
114     /**
115      * Some checks for cloning.
116      */

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

134     public void testSerialization() {
135         CombinedDomainCategoryPlot plot1 = createPlot();
136         CombinedDomainCategoryPlot plot2 = null;
137         try {
138             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
139             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
140             out.writeObject(plot1);
141             out.close();
142             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
143                 new ByteArrayInputStream JavaDoc(buffer.toByteArray())
144             );
145             plot2 = (CombinedDomainCategoryPlot) in.readObject();
146             in.close();
147         }
148         catch (Exception JavaDoc e) {
149             e.printStackTrace();
150         }
151         assertEquals(plot1, plot2);
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 sample plot.
248      */

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