KickJava   Java API By Example, From Geeks To Geeks.

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


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  * CombinedRangeXYPlotTests.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: CombinedRangeXYPlotTests.java,v 1.2 2005/03/16 09:42:35 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.awt.Font JavaDoc;
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 import java.util.List JavaDoc;
52
53 import junit.framework.Test;
54 import junit.framework.TestCase;
55 import junit.framework.TestSuite;
56
57 import org.jfree.chart.annotations.XYTextAnnotation;
58 import org.jfree.chart.axis.AxisLocation;
59 import org.jfree.chart.axis.NumberAxis;
60 import org.jfree.chart.plot.CombinedRangeXYPlot;
61 import org.jfree.chart.plot.PlotOrientation;
62 import org.jfree.chart.plot.XYPlot;
63 import org.jfree.chart.renderer.xy.StandardXYItemRenderer;
64 import org.jfree.chart.renderer.xy.XYItemRenderer;
65 import org.jfree.data.xy.XYDataset;
66 import org.jfree.data.xy.XYSeries;
67 import org.jfree.data.xy.XYSeriesCollection;
68
69 /**
70  * Tests for the {@link CombinedRangeXYPlot} class.
71  */

72 public class CombinedRangeXYPlotTests extends TestCase {
73
74     /**
75      * Returns the tests as a test suite.
76      *
77      * @return The test suite.
78      */

79     public static Test suite() {
80         return new TestSuite(CombinedRangeXYPlotTests.class);
81     }
82
83     /**
84      * Constructs a new set of tests.
85      *
86      * @param name the name of the tests.
87      */

88     public CombinedRangeXYPlotTests(String JavaDoc name) {
89         super(name);
90     }
91
92     /**
93      * Test the equals method.
94      */

95     public void testEquals() {
96         CombinedRangeXYPlot plot1 = createPlot();
97         CombinedRangeXYPlot plot2 = createPlot();
98         assertTrue(plot1.equals(plot2));
99         assertTrue(plot2.equals(plot1));
100     }
101
102     /**
103      * This is a test to replicate the bug report 987080.
104      */

105     public void testRemoveSubplot() {
106         CombinedRangeXYPlot plot = new CombinedRangeXYPlot();
107         XYPlot plot1 = new XYPlot();
108         XYPlot plot2 = new XYPlot();
109         plot.add(plot1);
110         plot.add(plot2);
111         // remove plot2, but plot1 is removed instead
112
plot.remove(plot2);
113         List JavaDoc plots = plot.getSubplots();
114         assertTrue(plots.get(0) == plot1);
115     }
116     
117     /**
118      * Confirm that cloning works.
119      */

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

137     public void testSerialization() {
138
139         CombinedRangeXYPlot plot1 = createPlot();
140         CombinedRangeXYPlot plot2 = null;
141
142         try {
143             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
144             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
145             out.writeObject(plot1);
146             out.close();
147
148             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
149                 new ByteArrayInputStream JavaDoc(buffer.toByteArray())
150             );
151             plot2 = (CombinedRangeXYPlot) in.readObject();
152             in.close();
153         }
154         catch (Exception JavaDoc e) {
155             e.printStackTrace();
156         }
157         assertEquals(plot1, plot2);
158
159     }
160     
161     /**
162      * Creates a sample dataset.
163      *
164      * @return Series 1.
165      */

166     private XYDataset createDataset1() {
167
168         // create dataset 1...
169
XYSeries series1 = new XYSeries("Series 1");
170         series1.add(10.0, 12353.3);
171         series1.add(20.0, 13734.4);
172         series1.add(30.0, 14525.3);
173         series1.add(40.0, 13984.3);
174         series1.add(50.0, 12999.4);
175         series1.add(60.0, 14274.3);
176         series1.add(70.0, 15943.5);
177         series1.add(80.0, 14845.3);
178         series1.add(90.0, 14645.4);
179         series1.add(100.0, 16234.6);
180         series1.add(110.0, 17232.3);
181         series1.add(120.0, 14232.2);
182         series1.add(130.0, 13102.2);
183         series1.add(140.0, 14230.2);
184         series1.add(150.0, 11235.2);
185
186         XYSeries series2 = new XYSeries("Series 2");
187         series2.add(10.0, 15000.3);
188         series2.add(20.0, 11000.4);
189         series2.add(30.0, 17000.3);
190         series2.add(40.0, 15000.3);
191         series2.add(50.0, 14000.4);
192         series2.add(60.0, 12000.3);
193         series2.add(70.0, 11000.5);
194         series2.add(80.0, 12000.3);
195         series2.add(90.0, 13000.4);
196         series2.add(100.0, 12000.6);
197         series2.add(110.0, 13000.3);
198         series2.add(120.0, 17000.2);
199         series2.add(130.0, 18000.2);
200         series2.add(140.0, 16000.2);
201         series2.add(150.0, 17000.2);
202
203         XYSeriesCollection collection = new XYSeriesCollection();
204         collection.addSeries(series1);
205         collection.addSeries(series2);
206         return collection;
207
208     }
209
210     /**
211      * Creates a sample dataset.
212      *
213      * @return Series 2.
214      */

215     private XYDataset createDataset2() {
216
217         // create dataset 2...
218
XYSeries series2 = new XYSeries("Series 3");
219
220         series2.add(10.0, 16853.2);
221         series2.add(20.0, 19642.3);
222         series2.add(30.0, 18253.5);
223         series2.add(40.0, 15352.3);
224         series2.add(50.0, 13532.0);
225         series2.add(100.0, 12635.3);
226         series2.add(110.0, 13998.2);
227         series2.add(120.0, 11943.2);
228         series2.add(130.0, 16943.9);
229         series2.add(140.0, 17843.2);
230         series2.add(150.0, 16495.3);
231         series2.add(160.0, 17943.6);
232         series2.add(170.0, 18500.7);
233         series2.add(180.0, 19595.9);
234
235         return new XYSeriesCollection(series2);
236
237     }
238
239     /**
240      * Creates a sample plot.
241      *
242      * @return A sample plot.
243      */

244     private CombinedRangeXYPlot createPlot() {
245         // create subplot 1...
246
XYDataset data1 = createDataset1();
247         XYItemRenderer renderer1 = new StandardXYItemRenderer();
248         NumberAxis rangeAxis1 = new NumberAxis("Range 1");
249         XYPlot subplot1 = new XYPlot(data1, null, rangeAxis1, renderer1);
250         subplot1.setRangeAxisLocation(AxisLocation.BOTTOM_OR_LEFT);
251          
252         XYTextAnnotation annotation
253             = new XYTextAnnotation("Hello!", 50.0, 10000.0);
254         annotation.setFont(new Font JavaDoc("SansSerif", Font.PLAIN, 9));
255         annotation.setRotationAngle(Math.PI / 4.0);
256         subplot1.addAnnotation(annotation);
257          
258         // create subplot 2...
259
XYDataset data2 = createDataset2();
260         XYItemRenderer renderer2 = new StandardXYItemRenderer();
261         NumberAxis rangeAxis2 = new NumberAxis("Range 2");
262         rangeAxis2.setAutoRangeIncludesZero(false);
263         XYPlot subplot2 = new XYPlot(data2, null, rangeAxis2, renderer2);
264         subplot2.setRangeAxisLocation(AxisLocation.TOP_OR_LEFT);
265
266         // parent plot...
267
CombinedRangeXYPlot plot
268             = new CombinedRangeXYPlot(new NumberAxis("Range"));
269         plot.setGap(10.0);
270         
271         // add the subplots...
272
plot.add(subplot1, 1);
273         plot.add(subplot2, 1);
274         plot.setOrientation(PlotOrientation.VERTICAL);
275         return plot;
276     }
277     
278 }
279
Popular Tags