001: /* ===========================================================
002: * JFreeChart : a free chart library for the Java(tm) platform
003: * ===========================================================
004: *
005: * (C) Copyright 2000-2007, by Object Refinery Limited and Contributors.
006: *
007: * Project Info: http://www.jfree.org/jfreechart/index.html
008: *
009: * This library is free software; you can redistribute it and/or modify it
010: * under the terms of the GNU Lesser General Public License as published by
011: * the Free Software Foundation; either version 2.1 of the License, or
012: * (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful, but
015: * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
016: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
017: * License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library; if not, write to the Free Software
021: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
022: * USA.
023: *
024: * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
025: * in the United States and other countries.]
026: *
027: * -----------------------
028: * SpiderWebPlotTests.java
029: * -----------------------
030: * (C) Copyright 2005-2007, by Object Refinery Limited and Contributors.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: SpiderWebPlotTests.java,v 1.1.2.2 2007/02/05 14:50:45 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 10-Jun-2005 : Version 1 (DG);
040: * 01-Jun-2006 : Added testDrawWithNullInfo() method (DG);
041: * 05-Feb-2007 : Added more checks to testCloning (DG);
042: *
043: */
044:
045: package org.jfree.chart.plot.junit;
046:
047: import java.awt.BasicStroke;
048: import java.awt.Color;
049: import java.awt.Font;
050: import java.awt.GradientPaint;
051: import java.awt.Graphics2D;
052: import java.awt.Rectangle;
053: import java.awt.geom.Rectangle2D;
054: import java.awt.image.BufferedImage;
055: import java.io.ByteArrayInputStream;
056: import java.io.ByteArrayOutputStream;
057: import java.io.ObjectInput;
058: import java.io.ObjectInputStream;
059: import java.io.ObjectOutput;
060: import java.io.ObjectOutputStream;
061: import java.text.DecimalFormat;
062:
063: import junit.framework.Test;
064: import junit.framework.TestCase;
065: import junit.framework.TestSuite;
066:
067: import org.jfree.chart.JFreeChart;
068: import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
069: import org.jfree.chart.labels.StandardCategoryToolTipGenerator;
070: import org.jfree.chart.plot.SpiderWebPlot;
071: import org.jfree.chart.urls.StandardCategoryURLGenerator;
072: import org.jfree.data.category.DefaultCategoryDataset;
073: import org.jfree.util.Rotation;
074: import org.jfree.util.TableOrder;
075:
076: /**
077: * Tests for the {@link SpiderWebPlot} class.
078: */
079: public class SpiderWebPlotTests extends TestCase {
080:
081: /**
082: * Returns the tests as a test suite.
083: *
084: * @return The test suite.
085: */
086: public static Test suite() {
087: return new TestSuite(SpiderWebPlotTests.class);
088: }
089:
090: /**
091: * Constructs a new set of tests.
092: *
093: * @param name the name of the tests.
094: */
095: public SpiderWebPlotTests(String name) {
096: super (name);
097: }
098:
099: /**
100: * Some checks for the equals() method.
101: */
102: public void testEquals() {
103: SpiderWebPlot p1 = new SpiderWebPlot(
104: new DefaultCategoryDataset());
105: SpiderWebPlot p2 = new SpiderWebPlot(
106: new DefaultCategoryDataset());
107: assertTrue(p1.equals(p2));
108: assertTrue(p2.equals(p1));
109:
110: // dataExtractOrder
111: p1.setDataExtractOrder(TableOrder.BY_COLUMN);
112: assertFalse(p1.equals(p2));
113: p2.setDataExtractOrder(TableOrder.BY_COLUMN);
114: assertTrue(p1.equals(p2));
115:
116: // headPercent
117: p1.setHeadPercent(0.321);
118: assertFalse(p1.equals(p2));
119: p2.setHeadPercent(0.321);
120: assertTrue(p1.equals(p2));
121:
122: // interiorGap
123: p1.setInteriorGap(0.123);
124: assertFalse(p1.equals(p2));
125: p2.setInteriorGap(0.123);
126: assertTrue(p1.equals(p2));
127:
128: // startAngle
129: p1.setStartAngle(0.456);
130: assertFalse(p1.equals(p2));
131: p2.setStartAngle(0.456);
132: assertTrue(p1.equals(p2));
133:
134: // direction
135: p1.setDirection(Rotation.ANTICLOCKWISE);
136: assertFalse(p1.equals(p2));
137: p2.setDirection(Rotation.ANTICLOCKWISE);
138: assertTrue(p1.equals(p2));
139:
140: // maxValue
141: p1.setMaxValue(123.4);
142: assertFalse(p1.equals(p2));
143: p2.setMaxValue(123.4);
144: assertTrue(p1.equals(p2));
145:
146: // legendItemShape
147: p1.setLegendItemShape(new Rectangle(1, 2, 3, 4));
148: assertFalse(p1.equals(p2));
149: p2.setLegendItemShape(new Rectangle(1, 2, 3, 4));
150: assertTrue(p1.equals(p2));
151:
152: // seriesPaint
153: p1.setSeriesPaint(new GradientPaint(1.0f, 2.0f, Color.red,
154: 3.0f, 4.0f, Color.white));
155: assertFalse(p1.equals(p2));
156: p2.setSeriesPaint(new GradientPaint(1.0f, 2.0f, Color.red,
157: 3.0f, 4.0f, Color.white));
158: assertTrue(p1.equals(p2));
159:
160: // seriesPaintList
161: p1.setSeriesPaint(1, new GradientPaint(1.0f, 2.0f,
162: Color.yellow, 3.0f, 4.0f, Color.white));
163: assertFalse(p1.equals(p2));
164: p2.setSeriesPaint(1, new GradientPaint(1.0f, 2.0f,
165: Color.yellow, 3.0f, 4.0f, Color.white));
166: assertTrue(p1.equals(p2));
167:
168: // baseSeriesPaint
169: p1.setBaseSeriesPaint(new GradientPaint(1.0f, 2.0f, Color.red,
170: 3.0f, 4.0f, Color.black));
171: assertFalse(p1.equals(p2));
172: p2.setBaseSeriesPaint(new GradientPaint(1.0f, 2.0f, Color.red,
173: 3.0f, 4.0f, Color.black));
174: assertTrue(p1.equals(p2));
175:
176: // seriesOutlinePaint
177: p1.setSeriesOutlinePaint(new GradientPaint(1.0f, 2.0f,
178: Color.blue, 3.0f, 4.0f, Color.black));
179: assertFalse(p1.equals(p2));
180: p2.setSeriesOutlinePaint(new GradientPaint(1.0f, 2.0f,
181: Color.blue, 3.0f, 4.0f, Color.black));
182: assertTrue(p1.equals(p2));
183:
184: // seriesOutlinePaintList
185: p1.setSeriesOutlinePaint(1, new GradientPaint(1.0f, 2.0f,
186: Color.blue, 3.0f, 4.0f, Color.green));
187: assertFalse(p1.equals(p2));
188: p2.setSeriesOutlinePaint(1, new GradientPaint(1.0f, 2.0f,
189: Color.blue, 3.0f, 4.0f, Color.green));
190: assertTrue(p1.equals(p2));
191:
192: // baseSeriesOutlinePaint
193: p1.setBaseSeriesOutlinePaint(new GradientPaint(1.0f, 2.0f,
194: Color.cyan, 3.0f, 4.0f, Color.green));
195: assertFalse(p1.equals(p2));
196: p2.setBaseSeriesOutlinePaint(new GradientPaint(1.0f, 2.0f,
197: Color.cyan, 3.0f, 4.0f, Color.green));
198: assertTrue(p1.equals(p2));
199:
200: // seriesOutlineStroke
201: BasicStroke s = new BasicStroke(1.23f);
202: p1.setSeriesOutlineStroke(s);
203: assertFalse(p1.equals(p2));
204: p2.setSeriesOutlineStroke(s);
205: assertTrue(p1.equals(p2));
206:
207: // seriesOutlineStrokeList
208: p1.setSeriesOutlineStroke(1, s);
209: assertFalse(p1.equals(p2));
210: p2.setSeriesOutlineStroke(1, s);
211: assertTrue(p1.equals(p2));
212:
213: // baseSeriesOutlineStroke
214: p1.setBaseSeriesOutlineStroke(s);
215: assertFalse(p1.equals(p2));
216: p2.setBaseSeriesOutlineStroke(s);
217: assertTrue(p1.equals(p2));
218:
219: // webFilled
220: p1.setWebFilled(false);
221: assertFalse(p1.equals(p2));
222: p2.setWebFilled(false);
223: assertTrue(p1.equals(p2));
224:
225: // axisLabelGap
226: p1.setAxisLabelGap(0.11);
227: assertFalse(p1.equals(p2));
228: p2.setAxisLabelGap(0.11);
229: assertTrue(p1.equals(p2));
230:
231: // labelFont
232: p1.setLabelFont(new Font("Serif", Font.PLAIN, 9));
233: assertFalse(p1.equals(p2));
234: p2.setLabelFont(new Font("Serif", Font.PLAIN, 9));
235: assertTrue(p1.equals(p2));
236:
237: // labelPaint
238: p1.setLabelPaint(new GradientPaint(1.0f, 2.0f, Color.red, 3.0f,
239: 4.0f, Color.blue));
240: assertFalse(p1.equals(p2));
241: p2.setLabelPaint(new GradientPaint(1.0f, 2.0f, Color.red, 3.0f,
242: 4.0f, Color.blue));
243: assertTrue(p1.equals(p2));
244:
245: // labelGenerator
246: p1.setLabelGenerator(new StandardCategoryItemLabelGenerator(
247: "XYZ: {0}", new DecimalFormat("0.000")));
248: assertFalse(p1.equals(p2));
249: p2.setLabelGenerator(new StandardCategoryItemLabelGenerator(
250: "XYZ: {0}", new DecimalFormat("0.000")));
251: assertTrue(p1.equals(p2));
252:
253: // toolTipGenerator
254: p1.setToolTipGenerator(new StandardCategoryToolTipGenerator());
255: assertFalse(p1.equals(p2));
256: p2.setToolTipGenerator(new StandardCategoryToolTipGenerator());
257: assertTrue(p1.equals(p2));
258:
259: // urlGenerator
260: p1.setURLGenerator(new StandardCategoryURLGenerator());
261: assertFalse(p1.equals(p2));
262: p2.setURLGenerator(new StandardCategoryURLGenerator());
263: assertTrue(p1.equals(p2));
264:
265: // axisLinePaint
266: p1.setAxisLinePaint(Color.red);
267: assertFalse(p1.equals(p2));
268: p2.setAxisLinePaint(Color.red);
269: assertTrue(p1.equals(p2));
270:
271: // axisLineStroke
272: p1.setAxisLineStroke(new BasicStroke(1.1f));
273: assertFalse(p1.equals(p2));
274: p2.setAxisLineStroke(new BasicStroke(1.1f));
275: assertTrue(p1.equals(p2));
276: }
277:
278: /**
279: * Confirm that cloning works.
280: */
281: public void testCloning() {
282: SpiderWebPlot p1 = new SpiderWebPlot(
283: new DefaultCategoryDataset());
284: Rectangle2D legendShape = new Rectangle2D.Double(1.0, 2.0, 3.0,
285: 4.0);
286: p1.setLegendItemShape(legendShape);
287: SpiderWebPlot p2 = null;
288: try {
289: p2 = (SpiderWebPlot) p1.clone();
290: } catch (CloneNotSupportedException e) {
291: e.printStackTrace();
292: }
293: assertTrue(p1 != p2);
294: assertTrue(p1.getClass() == p2.getClass());
295: assertTrue(p1.equals(p2));
296:
297: // change the legendItemShape
298: legendShape.setRect(4.0, 3.0, 2.0, 1.0);
299: assertFalse(p1.equals(p2));
300: p2.setLegendItemShape(legendShape);
301: assertTrue(p1.equals(p2));
302:
303: // change a series paint
304: p1.setSeriesPaint(1, Color.black);
305: assertFalse(p1.equals(p2));
306: p2.setSeriesPaint(1, Color.black);
307: assertTrue(p1.equals(p2));
308:
309: // change a series outline paint
310: p1.setSeriesOutlinePaint(0, Color.red);
311: assertFalse(p1.equals(p2));
312: p2.setSeriesOutlinePaint(0, Color.red);
313: assertTrue(p1.equals(p2));
314:
315: // change a series outline stroke
316: p1.setSeriesOutlineStroke(0, new BasicStroke(1.1f));
317: assertFalse(p1.equals(p2));
318: p2.setSeriesOutlineStroke(0, new BasicStroke(1.1f));
319: assertTrue(p1.equals(p2));
320:
321: }
322:
323: /**
324: * Serialize an instance, restore it, and check for equality.
325: */
326: public void testSerialization() {
327:
328: SpiderWebPlot p1 = new SpiderWebPlot(
329: new DefaultCategoryDataset());
330: SpiderWebPlot p2 = null;
331:
332: try {
333: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
334: ObjectOutput out = new ObjectOutputStream(buffer);
335: out.writeObject(p1);
336: out.close();
337:
338: ObjectInput in = new ObjectInputStream(
339: new ByteArrayInputStream(buffer.toByteArray()));
340: p2 = (SpiderWebPlot) in.readObject();
341: in.close();
342: } catch (Exception e) {
343: e.printStackTrace();
344: }
345: assertEquals(p1, p2);
346:
347: }
348:
349: /**
350: * Draws the chart with a null info object to make sure that no exceptions
351: * are thrown.
352: */
353: public void testDrawWithNullInfo() {
354: DefaultCategoryDataset dataset = new DefaultCategoryDataset();
355: dataset.addValue(35.0, "S1", "C1");
356: dataset.addValue(45.0, "S1", "C2");
357: dataset.addValue(55.0, "S1", "C3");
358: dataset.addValue(15.0, "S1", "C4");
359: dataset.addValue(25.0, "S1", "C5");
360: SpiderWebPlot plot = new SpiderWebPlot(dataset);
361: JFreeChart chart = new JFreeChart(plot);
362: boolean success = false;
363: try {
364: BufferedImage image = new BufferedImage(200, 100,
365: BufferedImage.TYPE_INT_RGB);
366: Graphics2D g2 = image.createGraphics();
367: chart.draw(g2, new Rectangle2D.Double(0, 0, 200, 100),
368: null, null);
369: g2.dispose();
370: success = true;
371: } catch (Exception e) {
372: success = false;
373: }
374: assertTrue(success);
375: }
376:
377: }
|