KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* ===========================================================
2  * JFreeChart : a free chart library for the Java(tm) platform
3  * ===========================================================
4  *
5  * (C) Copyright 2000-2006, 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  * SpiderWebPlotTests.java
29  * -----------------------
30  * (C) Copyright 2005, 2006, by Object Refinery Limited and Contributors.
31  *
32  * Original Author: David Gilbert (for Object Refinery Limited);
33  * Contributor(s): -;
34  *
35  * $Id: SpiderWebPlotTests.java,v 1.1.2.1 2006/10/03 15:41:25 mungady Exp $
36  *
37  * Changes
38  * -------
39  * 10-Jun-2005 : Version 1 (DG);
40  * 01-Jun-2006 : Added testDrawWithNullInfo() method (DG);
41  *
42  */

43
44 package org.jfree.chart.plot.junit;
45
46 import java.awt.BasicStroke JavaDoc;
47 import java.awt.Color JavaDoc;
48 import java.awt.Font JavaDoc;
49 import java.awt.GradientPaint JavaDoc;
50 import java.awt.Graphics2D JavaDoc;
51 import java.awt.Rectangle JavaDoc;
52 import java.awt.geom.Rectangle2D JavaDoc;
53 import java.awt.image.BufferedImage JavaDoc;
54 import java.io.ByteArrayInputStream JavaDoc;
55 import java.io.ByteArrayOutputStream JavaDoc;
56 import java.io.ObjectInput JavaDoc;
57 import java.io.ObjectInputStream JavaDoc;
58 import java.io.ObjectOutput JavaDoc;
59 import java.io.ObjectOutputStream JavaDoc;
60 import java.text.DecimalFormat JavaDoc;
61
62 import junit.framework.Test;
63 import junit.framework.TestCase;
64 import junit.framework.TestSuite;
65
66 import org.jfree.chart.JFreeChart;
67 import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
68 import org.jfree.chart.labels.StandardCategoryToolTipGenerator;
69 import org.jfree.chart.plot.SpiderWebPlot;
70 import org.jfree.chart.urls.StandardCategoryURLGenerator;
71 import org.jfree.data.category.DefaultCategoryDataset;
72 import org.jfree.util.Rotation;
73 import org.jfree.util.TableOrder;
74
75 /**
76  * Tests for the {@link SpiderWebPlot} class.
77  */

78 public class SpiderWebPlotTests extends TestCase {
79
80     /**
81      * Returns the tests as a test suite.
82      *
83      * @return The test suite.
84      */

85     public static Test suite() {
86         return new TestSuite(SpiderWebPlotTests.class);
87     }
88
89     /**
90      * Constructs a new set of tests.
91      *
92      * @param name the name of the tests.
93      */

94     public SpiderWebPlotTests(String JavaDoc name) {
95         super(name);
96     }
97
98     /**
99      * Some checks for the equals() method.
100      */

101     public void testEquals() {
102         SpiderWebPlot p1 = new SpiderWebPlot(new DefaultCategoryDataset());
103         SpiderWebPlot p2 = new SpiderWebPlot(new DefaultCategoryDataset());
104         assertTrue(p1.equals(p2));
105         assertTrue(p2.equals(p1));
106         
107         // dataExtractOrder
108
p1.setDataExtractOrder(TableOrder.BY_COLUMN);
109         assertFalse(p1.equals(p2));
110         p2.setDataExtractOrder(TableOrder.BY_COLUMN);
111         assertTrue(p1.equals(p2));
112         
113         // headPercent
114
p1.setHeadPercent(0.321);
115         assertFalse(p1.equals(p2));
116         p2.setHeadPercent(0.321);
117         assertTrue(p1.equals(p2));
118         
119         // interiorGap
120
p1.setInteriorGap(0.123);
121         assertFalse(p1.equals(p2));
122         p2.setInteriorGap(0.123);
123         assertTrue(p1.equals(p2));
124         
125         // startAngle
126
p1.setStartAngle(0.456);
127         assertFalse(p1.equals(p2));
128         p2.setStartAngle(0.456);
129         assertTrue(p1.equals(p2));
130         
131         // direction
132
p1.setDirection(Rotation.ANTICLOCKWISE);
133         assertFalse(p1.equals(p2));
134         p2.setDirection(Rotation.ANTICLOCKWISE);
135         assertTrue(p1.equals(p2));
136         
137         // maxValue
138
p1.setMaxValue(123.4);
139         assertFalse(p1.equals(p2));
140         p2.setMaxValue(123.4);
141         assertTrue(p1.equals(p2));
142         
143         // legendItemShape
144
p1.setLegendItemShape(new Rectangle JavaDoc(1, 2, 3, 4));
145         assertFalse(p1.equals(p2));
146         p2.setLegendItemShape(new Rectangle JavaDoc(1, 2, 3, 4));
147         assertTrue(p1.equals(p2));
148
149         // seriesPaint
150
p1.setSeriesPaint(new GradientPaint JavaDoc(1.0f, 2.0f, Color.red,
151                 3.0f, 4.0f, Color.white));
152         assertFalse(p1.equals(p2));
153         p2.setSeriesPaint(new GradientPaint JavaDoc(1.0f, 2.0f, Color.red,
154                 3.0f, 4.0f, Color.white));
155         assertTrue(p1.equals(p2));
156
157         // seriesPaintList
158
p1.setSeriesPaint(1, new GradientPaint JavaDoc(1.0f, 2.0f, Color.yellow,
159                 3.0f, 4.0f, Color.white));
160         assertFalse(p1.equals(p2));
161         p2.setSeriesPaint(1, new GradientPaint JavaDoc(1.0f, 2.0f, Color.yellow,
162                 3.0f, 4.0f, Color.white));
163         assertTrue(p1.equals(p2));
164         
165         // baseSeriesPaint
166
p1.setBaseSeriesPaint(new GradientPaint JavaDoc(1.0f, 2.0f, Color.red,
167                 3.0f, 4.0f, Color.black));
168         assertFalse(p1.equals(p2));
169         p2.setBaseSeriesPaint(new GradientPaint JavaDoc(1.0f, 2.0f, Color.red,
170                 3.0f, 4.0f, Color.black));
171         assertTrue(p1.equals(p2));
172         
173         // seriesOutlinePaint
174
p1.setSeriesOutlinePaint(new GradientPaint JavaDoc(1.0f, 2.0f, Color.blue,
175                 3.0f, 4.0f, Color.black));
176         assertFalse(p1.equals(p2));
177         p2.setSeriesOutlinePaint(new GradientPaint JavaDoc(1.0f, 2.0f, Color.blue,
178                 3.0f, 4.0f, Color.black));
179         assertTrue(p1.equals(p2));
180
181         // seriesOutlinePaintList
182
p1.setSeriesOutlinePaint(1, new GradientPaint JavaDoc(1.0f, 2.0f, Color.blue,
183                 3.0f, 4.0f, Color.green));
184         assertFalse(p1.equals(p2));
185         p2.setSeriesOutlinePaint(1, new GradientPaint JavaDoc(1.0f, 2.0f, Color.blue,
186                 3.0f, 4.0f, Color.green));
187         assertTrue(p1.equals(p2));
188
189         // baseSeriesOutlinePaint
190
p1.setBaseSeriesOutlinePaint(new GradientPaint JavaDoc(1.0f, 2.0f, Color.cyan,
191                 3.0f, 4.0f, Color.green));
192         assertFalse(p1.equals(p2));
193         p2.setBaseSeriesOutlinePaint(new GradientPaint JavaDoc(1.0f, 2.0f, Color.cyan,
194                 3.0f, 4.0f, Color.green));
195         assertTrue(p1.equals(p2));
196         
197         // seriesOutlineStroke
198
BasicStroke JavaDoc s = new BasicStroke JavaDoc(1.23f);
199         p1.setSeriesOutlineStroke(s);
200         assertFalse(p1.equals(p2));
201         p2.setSeriesOutlineStroke(s);
202         assertTrue(p1.equals(p2));
203         
204         // seriesOutlineStrokeList
205
p1.setSeriesOutlineStroke(1, s);
206         assertFalse(p1.equals(p2));
207         p2.setSeriesOutlineStroke(1, s);
208         assertTrue(p1.equals(p2));
209         
210         // baseSeriesOutlineStroke
211
p1.setBaseSeriesOutlineStroke(s);
212         assertFalse(p1.equals(p2));
213         p2.setBaseSeriesOutlineStroke(s);
214         assertTrue(p1.equals(p2));
215
216         // webFilled
217
p1.setWebFilled(false);
218         assertFalse(p1.equals(p2));
219         p2.setWebFilled(false);
220         assertTrue(p1.equals(p2));
221         
222         // axisLabelGap
223

224         // labelFont
225
p1.setLabelFont(new Font JavaDoc("Serif", Font.PLAIN, 9));
226         assertFalse(p1.equals(p2));
227         p2.setLabelFont(new Font JavaDoc("Serif", Font.PLAIN, 9));
228         assertTrue(p1.equals(p2));
229         
230         // labelPaint
231
p1.setLabelPaint(new GradientPaint JavaDoc(1.0f, 2.0f, Color.red,
232                 3.0f, 4.0f, Color.blue));
233         assertFalse(p1.equals(p2));
234         p2.setLabelPaint(new GradientPaint JavaDoc(1.0f, 2.0f, Color.red,
235                 3.0f, 4.0f, Color.blue));
236         assertTrue(p1.equals(p2));
237
238         // labelGenerator
239
p1.setLabelGenerator(new StandardCategoryItemLabelGenerator("XYZ: {0}",
240                 new DecimalFormat JavaDoc("0.000")));
241         assertFalse(p1.equals(p2));
242         p2.setLabelGenerator(new StandardCategoryItemLabelGenerator("XYZ: {0}",
243                 new DecimalFormat JavaDoc("0.000")));
244         assertTrue(p1.equals(p2));
245         
246         // toolTipGenerator
247
p1.setToolTipGenerator(new StandardCategoryToolTipGenerator());
248         assertFalse(p1.equals(p2));
249         p2.setToolTipGenerator(new StandardCategoryToolTipGenerator());
250         assertTrue(p1.equals(p2));
251
252         // urlGenerator
253
p1.setURLGenerator(new StandardCategoryURLGenerator());
254         assertFalse(p1.equals(p2));
255         p2.setURLGenerator(new StandardCategoryURLGenerator());
256         assertTrue(p1.equals(p2));
257     }
258
259     /**
260      * Confirm that cloning works.
261      */

262     public void testCloning() {
263         SpiderWebPlot p1 = new SpiderWebPlot(new DefaultCategoryDataset());
264         SpiderWebPlot p2 = null;
265         try {
266             p2 = (SpiderWebPlot) p1.clone();
267         }
268         catch (CloneNotSupportedException JavaDoc e) {
269             e.printStackTrace();
270         }
271         assertTrue(p1 != p2);
272         assertTrue(p1.getClass() == p2.getClass());
273         assertTrue(p1.equals(p2));
274     }
275
276     /**
277      * Serialize an instance, restore it, and check for equality.
278      */

279     public void testSerialization() {
280
281         SpiderWebPlot p1 = new SpiderWebPlot(new DefaultCategoryDataset());
282         SpiderWebPlot p2 = null;
283
284         try {
285             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
286             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
287             out.writeObject(p1);
288             out.close();
289
290             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
291                 new ByteArrayInputStream JavaDoc(buffer.toByteArray())
292             );
293             p2 = (SpiderWebPlot) in.readObject();
294             in.close();
295         }
296         catch (Exception JavaDoc e) {
297             e.printStackTrace();
298         }
299         assertEquals(p1, p2);
300
301     }
302  
303     /**
304      * Draws the chart with a null info object to make sure that no exceptions
305      * are thrown.
306      */

307     public void testDrawWithNullInfo() {
308         DefaultCategoryDataset dataset = new DefaultCategoryDataset();
309         dataset.addValue(35.0, "S1", "C1");
310         dataset.addValue(45.0, "S1", "C2");
311         dataset.addValue(55.0, "S1", "C3");
312         dataset.addValue(15.0, "S1", "C4");
313         dataset.addValue(25.0, "S1", "C5");
314         SpiderWebPlot plot = new SpiderWebPlot(dataset);
315         JFreeChart chart = new JFreeChart(plot);
316         boolean success = false;
317         try {
318             BufferedImage JavaDoc image = new BufferedImage JavaDoc(200 , 100,
319                     BufferedImage.TYPE_INT_RGB);
320             Graphics2D JavaDoc g2 = image.createGraphics();
321             chart.draw(g2, new Rectangle2D.Double JavaDoc(0, 0, 200, 100), null, null);
322             g2.dispose();
323             success = true;
324         }
325         catch (Exception JavaDoc e) {
326             success = false;
327         }
328         assertTrue(success);
329     }
330     
331 }
332
Popular Tags