KickJava   Java API By Example, From Geeks To Geeks.

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


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  * CombinedDomainXYPlotTests.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: CombinedDomainXYPlotTests.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.CombinedDomainXYPlot;
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 CombinedDomainXYPlot} class.
71  */

72 public class CombinedDomainXYPlotTests 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(CombinedDomainXYPlotTests.class);
81     }
82
83     /**
84      * Constructs a new set of tests.
85      *
86      * @param name the name of the tests.
87      */

88     public CombinedDomainXYPlotTests(String JavaDoc name) {
89         super(name);
90     }
91
92     /**
93      * Confirm that the constructor will accept a null axis.
94      */

95     public void testConstructor1() {
96         CombinedDomainXYPlot plot = new CombinedDomainXYPlot(null);
97         assertEquals(null, plot.getDomainAxis());
98     }
99     
100     /**
101      * This is a test to replicate the bug report 987080.
102      */

103     public void testRemoveSubplot() {
104         CombinedDomainXYPlot plot = new CombinedDomainXYPlot();
105         XYPlot plot1 = new XYPlot();
106         XYPlot plot2 = new XYPlot();
107         plot.add(plot1);
108         plot.add(plot2);
109         // remove plot2, but plot1 is removed instead
110
plot.remove(plot2);
111         List JavaDoc plots = plot.getSubplots();
112         assertTrue(plots.get(0) == plot1);
113     }
114     
115     /**
116      * Tests the equals() method.
117      */

118     public void testEquals() {
119         CombinedDomainXYPlot plot1 = createPlot();
120         CombinedDomainXYPlot plot2 = createPlot();
121         assertTrue(plot1.equals(plot2));
122         assertTrue(plot2.equals(plot1));
123     }
124
125     /**
126      * Confirm that cloning works.
127      */

128     public void testCloning() {
129         CombinedDomainXYPlot plot1 = createPlot();
130         CombinedDomainXYPlot plot2 = null;
131         try {
132             plot2 = (CombinedDomainXYPlot) plot1.clone();
133         }
134         catch (CloneNotSupportedException JavaDoc e) {
135             System.err.println("Failed to clone.");
136         }
137         assertTrue(plot1 != plot2);
138         assertTrue(plot1.getClass() == plot2.getClass());
139         assertTrue(plot1.equals(plot2));
140     }
141
142     /**
143      * Serialize an instance, restore it, and check for equality.
144      */

145     public void testSerialization() {
146
147         CombinedDomainXYPlot plot1 = createPlot();
148         CombinedDomainXYPlot plot2 = null;
149
150         try {
151             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
152             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
153             out.writeObject(plot1);
154             out.close();
155
156             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
157                 new ByteArrayInputStream JavaDoc(buffer.toByteArray())
158             );
159             plot2 = (CombinedDomainXYPlot) in.readObject();
160             in.close();
161         }
162         catch (Exception JavaDoc e) {
163             e.printStackTrace();
164         }
165         assertEquals(plot1, plot2);
166
167     }
168     
169     /**
170      * Creates a sample dataset.
171      *
172      * @return Series 1.
173      */

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

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

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