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: * JFreeChartTests.java
029: * --------------------
030: * (C) Copyright 2002-2007, by Object Refinery Limited.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: JFreeChartTests.java,v 1.1.2.5 2007/06/05 15:41:39 mungady Exp $
036: *
037: * Changes:
038: * --------
039: * 11-Jun-2002 : Version 1 (DG);
040: * 17-Oct-2002 : Fixed errors reported by Checkstyle (DG);
041: * 23-Sep-2003 : Removed null title test, since TM has added code to ensure
042: * null titles cannot be created (DG);
043: * 24-Nov-2005 : Removed OldLegend (DG);
044: * 16-May-2007 : Added some new tests (DG);
045: *
046: */
047:
048: package org.jfree.chart.junit;
049:
050: import java.awt.BasicStroke;
051: import java.awt.Color;
052: import java.awt.Font;
053: import java.awt.GradientPaint;
054: import java.awt.RenderingHints;
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.util.List;
062:
063: import junit.framework.Test;
064: import junit.framework.TestCase;
065: import junit.framework.TestSuite;
066:
067: import org.jfree.chart.ChartFactory;
068: import org.jfree.chart.JFreeChart;
069: import org.jfree.chart.event.ChartChangeEvent;
070: import org.jfree.chart.event.ChartChangeListener;
071: import org.jfree.chart.plot.PiePlot;
072: import org.jfree.chart.plot.PlotOrientation;
073: import org.jfree.chart.plot.RingPlot;
074: import org.jfree.chart.title.LegendTitle;
075: import org.jfree.chart.title.TextTitle;
076: import org.jfree.chart.title.Title;
077: import org.jfree.data.category.DefaultCategoryDataset;
078: import org.jfree.data.general.DefaultPieDataset;
079: import org.jfree.data.time.Day;
080: import org.jfree.data.time.RegularTimePeriod;
081: import org.jfree.data.time.TimeSeries;
082: import org.jfree.data.time.TimeSeriesCollection;
083: import org.jfree.ui.Align;
084: import org.jfree.ui.RectangleEdge;
085: import org.jfree.ui.RectangleInsets;
086:
087: /**
088: * Tests for the {@link JFreeChart} class.
089: */
090: public class JFreeChartTests extends TestCase implements
091: ChartChangeListener {
092:
093: /** A pie chart. */
094: private JFreeChart pieChart;
095:
096: /**
097: * Returns the tests as a test suite.
098: *
099: * @return The test suite.
100: */
101: public static Test suite() {
102: return new TestSuite(JFreeChartTests.class);
103: }
104:
105: /**
106: * Constructs a new set of tests.
107: *
108: * @param name the name of the tests.
109: */
110: public JFreeChartTests(String name) {
111: super (name);
112: }
113:
114: /**
115: * Common test setup.
116: */
117: protected void setUp() {
118:
119: // create a dataset...
120: DefaultPieDataset data = new DefaultPieDataset();
121: data.setValue("Java", new Double(43.2));
122: data.setValue("Visual Basic", new Double(0.0));
123: data.setValue("C/C++", new Double(17.5));
124:
125: // create the chart...
126: this .pieChart = ChartFactory.createPieChart("Pie Chart", // chart title
127: data, // data
128: true, // include legend
129: true, false);
130:
131: }
132:
133: /**
134: * Check that the equals() method can distinguish all fields.
135: */
136: public void testEquals() {
137: JFreeChart chart1 = new JFreeChart("Title", new Font(
138: "SansSerif", Font.PLAIN, 12), new PiePlot(), true);
139: JFreeChart chart2 = new JFreeChart("Title", new Font(
140: "SansSerif", Font.PLAIN, 12), new PiePlot(), true);
141: assertTrue(chart1.equals(chart2));
142: assertTrue(chart2.equals(chart1));
143:
144: // renderingHints
145: chart1.setRenderingHints(new RenderingHints(
146: RenderingHints.KEY_TEXT_ANTIALIASING,
147: RenderingHints.VALUE_TEXT_ANTIALIAS_ON));
148: assertFalse(chart1.equals(chart2));
149: chart2.setRenderingHints(new RenderingHints(
150: RenderingHints.KEY_TEXT_ANTIALIASING,
151: RenderingHints.VALUE_TEXT_ANTIALIAS_ON));
152: assertTrue(chart1.equals(chart2));
153:
154: // borderVisible
155: chart1.setBorderVisible(true);
156: assertFalse(chart1.equals(chart2));
157: chart2.setBorderVisible(true);
158: assertTrue(chart1.equals(chart2));
159:
160: // borderStroke
161: BasicStroke s = new BasicStroke(2.0f);
162: chart1.setBorderStroke(s);
163: assertFalse(chart1.equals(chart2));
164: chart2.setBorderStroke(s);
165: assertTrue(chart1.equals(chart2));
166:
167: // borderPaint
168: chart1.setBorderPaint(Color.red);
169: assertFalse(chart1.equals(chart2));
170: chart2.setBorderPaint(Color.red);
171: assertTrue(chart1.equals(chart2));
172:
173: // padding
174: chart1.setPadding(new RectangleInsets(1, 2, 3, 4));
175: assertFalse(chart1.equals(chart2));
176: chart2.setPadding(new RectangleInsets(1, 2, 3, 4));
177: assertTrue(chart1.equals(chart2));
178:
179: // title
180: chart1.setTitle("XYZ");
181: assertFalse(chart1.equals(chart2));
182: chart2.setTitle("XYZ");
183: assertTrue(chart1.equals(chart2));
184:
185: // subtitles
186: chart1.addSubtitle(new TextTitle("Subtitle"));
187: assertFalse(chart1.equals(chart2));
188: chart2.addSubtitle(new TextTitle("Subtitle"));
189: assertTrue(chart1.equals(chart2));
190:
191: // plot
192: chart1 = new JFreeChart("Title", new Font("SansSerif",
193: Font.PLAIN, 12), new RingPlot(), false);
194: chart2 = new JFreeChart("Title", new Font("SansSerif",
195: Font.PLAIN, 12), new PiePlot(), false);
196: assertFalse(chart1.equals(chart2));
197: chart2 = new JFreeChart("Title", new Font("SansSerif",
198: Font.PLAIN, 12), new RingPlot(), false);
199: assertTrue(chart1.equals(chart2));
200:
201: // backgroundPaint
202: chart1.setBackgroundPaint(new GradientPaint(1.0f, 2.0f,
203: Color.red, 3.0f, 4.0f, Color.blue));
204: assertFalse(chart1.equals(chart2));
205: chart2.setBackgroundPaint(new GradientPaint(1.0f, 2.0f,
206: Color.red, 3.0f, 4.0f, Color.blue));
207: assertTrue(chart1.equals(chart2));
208:
209: // backgroundImage
210: chart1.setBackgroundImage(JFreeChart.INFO.getLogo());
211: assertFalse(chart1.equals(chart2));
212: chart2.setBackgroundImage(JFreeChart.INFO.getLogo());
213: assertTrue(chart1.equals(chart2));
214:
215: // backgroundImageAlignment
216: chart1.setBackgroundImageAlignment(Align.BOTTOM_LEFT);
217: assertFalse(chart1.equals(chart2));
218: chart2.setBackgroundImageAlignment(Align.BOTTOM_LEFT);
219: assertTrue(chart1.equals(chart2));
220:
221: // backgroundImageAlpha
222: chart1.setBackgroundImageAlpha(0.1f);
223: assertFalse(chart1.equals(chart2));
224: chart2.setBackgroundImageAlpha(0.1f);
225: assertTrue(chart1.equals(chart2));
226: }
227:
228: /**
229: * A test to make sure that the legend is being picked up in the
230: * equals() testing.
231: */
232: public void testEquals2() {
233: JFreeChart chart1 = new JFreeChart("Title", new Font(
234: "SansSerif", Font.PLAIN, 12), new PiePlot(), true);
235: JFreeChart chart2 = new JFreeChart("Title", new Font(
236: "SansSerif", Font.PLAIN, 12), new PiePlot(), false);
237: assertFalse(chart1.equals(chart2));
238: assertFalse(chart2.equals(chart1));
239: }
240:
241: /**
242: * Checks the subtitle count - should be 1 (the legend).
243: */
244: public void testSubtitleCount() {
245: int count = this .pieChart.getSubtitleCount();
246: assertEquals(1, count);
247: }
248:
249: /**
250: * Some checks for the getSubtitle() method.
251: */
252: public void testGetSubtitle() {
253: DefaultPieDataset dataset = new DefaultPieDataset();
254: JFreeChart chart = ChartFactory.createPieChart("title",
255: dataset, true, false, false);
256: Title t = chart.getSubtitle(0);
257: assertTrue(t instanceof LegendTitle);
258:
259: boolean pass = false;
260: try {
261: t = chart.getSubtitle(-1);
262: } catch (IllegalArgumentException e) {
263: pass = true;
264: }
265: assertTrue(pass);
266:
267: pass = false;
268: try {
269: t = chart.getSubtitle(1);
270: } catch (IllegalArgumentException e) {
271: pass = true;
272: }
273: assertTrue(pass);
274:
275: pass = false;
276: try {
277: t = chart.getSubtitle(2);
278: } catch (IllegalArgumentException e) {
279: pass = true;
280: }
281: assertTrue(pass);
282: }
283:
284: /**
285: * Serialize a pie chart, restore it, and check for equality.
286: */
287: public void testSerialization1() {
288:
289: DefaultPieDataset data = new DefaultPieDataset();
290: data.setValue("Type 1", 54.5);
291: data.setValue("Type 2", 23.9);
292: data.setValue("Type 3", 45.8);
293:
294: JFreeChart c1 = ChartFactory.createPieChart("Test", data, true,
295: true, true);
296: JFreeChart c2 = null;
297:
298: try {
299: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
300: ObjectOutput out = new ObjectOutputStream(buffer);
301: out.writeObject(c1);
302: out.close();
303:
304: ObjectInput in = new ObjectInputStream(
305: new ByteArrayInputStream(buffer.toByteArray()));
306: c2 = (JFreeChart) in.readObject();
307: in.close();
308: } catch (Exception e) {
309: e.printStackTrace();
310: }
311: assertEquals(c1, c2);
312: LegendTitle lt2 = c2.getLegend();
313: assertTrue(lt2.getSources()[0] == c2.getPlot());
314: }
315:
316: /**
317: * Serialize a 3D pie chart, restore it, and check for equality.
318: */
319: public void testSerialization2() {
320:
321: DefaultPieDataset data = new DefaultPieDataset();
322: data.setValue("Type 1", 54.5);
323: data.setValue("Type 2", 23.9);
324: data.setValue("Type 3", 45.8);
325:
326: JFreeChart c1 = ChartFactory.createPieChart3D("Test", data,
327: true, true, true);
328: JFreeChart c2 = null;
329:
330: try {
331: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
332: ObjectOutput out = new ObjectOutputStream(buffer);
333: out.writeObject(c1);
334: out.close();
335:
336: ObjectInput in = new ObjectInputStream(
337: new ByteArrayInputStream(buffer.toByteArray()));
338: c2 = (JFreeChart) in.readObject();
339: in.close();
340: } catch (Exception e) {
341: e.printStackTrace();
342: }
343: assertEquals(c1, c2);
344:
345: }
346:
347: /**
348: * Serialize a bar chart, restore it, and check for equality.
349: */
350: public void testSerialization3() {
351:
352: // row keys...
353: String series1 = "First";
354: String series2 = "Second";
355: String series3 = "Third";
356:
357: // column keys...
358: String category1 = "Category 1";
359: String category2 = "Category 2";
360: String category3 = "Category 3";
361: String category4 = "Category 4";
362: String category5 = "Category 5";
363: String category6 = "Category 6";
364: String category7 = "Category 7";
365: String category8 = "Category 8";
366:
367: // create the dataset...
368: DefaultCategoryDataset dataset = new DefaultCategoryDataset();
369:
370: dataset.addValue(1.0, series1, category1);
371: dataset.addValue(4.0, series1, category2);
372: dataset.addValue(3.0, series1, category3);
373: dataset.addValue(5.0, series1, category4);
374: dataset.addValue(5.0, series1, category5);
375: dataset.addValue(7.0, series1, category6);
376: dataset.addValue(7.0, series1, category7);
377: dataset.addValue(8.0, series1, category8);
378:
379: dataset.addValue(5.0, series2, category1);
380: dataset.addValue(7.0, series2, category2);
381: dataset.addValue(6.0, series2, category3);
382: dataset.addValue(8.0, series2, category4);
383: dataset.addValue(4.0, series2, category5);
384: dataset.addValue(4.0, series2, category6);
385: dataset.addValue(2.0, series2, category7);
386: dataset.addValue(1.0, series2, category8);
387:
388: dataset.addValue(4.0, series3, category1);
389: dataset.addValue(3.0, series3, category2);
390: dataset.addValue(2.0, series3, category3);
391: dataset.addValue(3.0, series3, category4);
392: dataset.addValue(6.0, series3, category5);
393: dataset.addValue(3.0, series3, category6);
394: dataset.addValue(4.0, series3, category7);
395: dataset.addValue(3.0, series3, category8);
396:
397: // create the chart...
398: JFreeChart c1 = ChartFactory.createBarChart(
399: "Vertical Bar Chart", // chart title
400: "Category", // domain axis label
401: "Value", // range axis label
402: dataset, // data
403: PlotOrientation.VERTICAL, // orientation
404: true, // include legend
405: true, false);
406:
407: JFreeChart c2 = null;
408:
409: try {
410: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
411: ObjectOutput out = new ObjectOutputStream(buffer);
412: out.writeObject(c1);
413: out.close();
414:
415: ObjectInput in = new ObjectInputStream(
416: new ByteArrayInputStream(buffer.toByteArray()));
417: c2 = (JFreeChart) in.readObject();
418: in.close();
419: } catch (Exception e) {
420: e.printStackTrace();
421: }
422: assertEquals(c1, c2);
423:
424: }
425:
426: /**
427: * Serialize a time seroes chart, restore it, and check for equality.
428: */
429: public void testSerialization4() {
430:
431: RegularTimePeriod t = new Day();
432: TimeSeries series = new TimeSeries("Series 1");
433: series.add(t, 36.4);
434: t = t.next();
435: series.add(t, 63.5);
436: TimeSeriesCollection dataset = new TimeSeriesCollection();
437: dataset.addSeries(series);
438:
439: JFreeChart c1 = ChartFactory.createTimeSeriesChart("Test",
440: "Date", "Value", dataset, true, true, true);
441: JFreeChart c2 = null;
442:
443: try {
444: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
445: ObjectOutput out = new ObjectOutputStream(buffer);
446: out.writeObject(c1);
447: out.close();
448:
449: ObjectInput in = new ObjectInputStream(
450: new ByteArrayInputStream(buffer.toByteArray()));
451: c2 = (JFreeChart) in.readObject();
452: in.close();
453: } catch (Exception e) {
454: e.printStackTrace();
455: }
456: assertEquals(c1, c2);
457:
458: }
459:
460: /**
461: * Some checks for the addSubtitle() methods.
462: */
463: public void testAddSubtitle() {
464: DefaultPieDataset dataset = new DefaultPieDataset();
465: JFreeChart chart = ChartFactory.createPieChart("title",
466: dataset, true, false, false);
467:
468: TextTitle t0 = new TextTitle("T0");
469: chart.addSubtitle(0, t0);
470: assertEquals(t0, chart.getSubtitle(0));
471:
472: TextTitle t1 = new TextTitle("T1");
473: chart.addSubtitle(t1);
474: assertEquals(t1, chart.getSubtitle(2)); // subtitle 1 is the legend
475:
476: boolean pass = false;
477: try {
478: chart.addSubtitle(null);
479: } catch (IllegalArgumentException e) {
480: pass = true;
481: }
482: assertTrue(pass);
483:
484: pass = false;
485: try {
486: chart.addSubtitle(-1, t0);
487: } catch (IllegalArgumentException e) {
488: pass = true;
489: }
490: assertTrue(pass);
491:
492: pass = false;
493: try {
494: chart.addSubtitle(4, t0);
495: } catch (IllegalArgumentException e) {
496: pass = true;
497: }
498: assertTrue(pass);
499: }
500:
501: /**
502: * Some checks for the getSubtitles() method.
503: */
504: public void testGetSubtitles() {
505: DefaultPieDataset dataset = new DefaultPieDataset();
506: JFreeChart chart = ChartFactory.createPieChart("title",
507: dataset, true, false, false);
508: List subtitles = chart.getSubtitles();
509:
510: assertEquals(1, chart.getSubtitleCount());
511:
512: // adding something to the returned list should NOT change the chart
513: subtitles.add(new TextTitle("T"));
514: assertEquals(1, chart.getSubtitleCount());
515: }
516:
517: /**
518: * Some checks for the default legend firing change events.
519: */
520: public void testLegendEvents() {
521: DefaultPieDataset dataset = new DefaultPieDataset();
522: JFreeChart chart = ChartFactory.createPieChart("title",
523: dataset, true, false, false);
524: chart.addChangeListener(this );
525: this .lastChartChangeEvent = null;
526: LegendTitle legend = chart.getLegend();
527: legend.setPosition(RectangleEdge.TOP);
528: assertNotNull(this .lastChartChangeEvent);
529: }
530:
531: /** The last ChartChangeEvent received. */
532: private ChartChangeEvent lastChartChangeEvent;
533:
534: /* (non-Javadoc)
535: * @see org.jfree.chart.event.ChartChangeListener#chartChanged(org.jfree.chart.event.ChartChangeEvent)
536: */
537: public void chartChanged(ChartChangeEvent event) {
538: this.lastChartChangeEvent = event;
539: }
540:
541: }
|