KickJava   Java API By Example, From Geeks To Geeks.

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


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  * XYPlotTests.java
28  * ----------------
29  * (C) Copyright 2003-2005, by Object Refinery Limited and Contributors.
30  *
31  * Original Author: David Gilbert (for Object Refinery Limited);
32  * Contributor(s): -;
33  *
34  * $Id: XYPlotTests.java,v 1.10 2005/06/01 09:41:14 mungady Exp $
35  *
36  * Changes
37  * -------
38  * 26-Mar-2003 : Version 1 (DG);
39  * 22-Mar-2004 : Added new cloning test (DG);
40  * 05-Oct-2004 : Strengthened test for clone independence (DG);
41  */

42
43 package org.jfree.chart.plot.junit;
44
45 import java.awt.BasicStroke JavaDoc;
46 import java.awt.Color JavaDoc;
47 import java.awt.Stroke JavaDoc;
48 import java.io.ByteArrayInputStream JavaDoc;
49 import java.io.ByteArrayOutputStream JavaDoc;
50 import java.io.ObjectInput JavaDoc;
51 import java.io.ObjectInputStream JavaDoc;
52 import java.io.ObjectOutput JavaDoc;
53 import java.io.ObjectOutputStream JavaDoc;
54
55 import junit.framework.Test;
56 import junit.framework.TestCase;
57 import junit.framework.TestSuite;
58
59 import org.jfree.chart.ChartFactory;
60 import org.jfree.chart.JFreeChart;
61 import org.jfree.chart.LegendItemCollection;
62 import org.jfree.chart.axis.AxisLocation;
63 import org.jfree.chart.axis.DateAxis;
64 import org.jfree.chart.axis.NumberAxis;
65 import org.jfree.chart.labels.StandardXYToolTipGenerator;
66 import org.jfree.chart.plot.IntervalMarker;
67 import org.jfree.chart.plot.PlotOrientation;
68 import org.jfree.chart.plot.ValueMarker;
69 import org.jfree.chart.plot.XYPlot;
70 import org.jfree.chart.renderer.xy.DefaultXYItemRenderer;
71 import org.jfree.chart.renderer.xy.StandardXYItemRenderer;
72 import org.jfree.chart.renderer.xy.XYBarRenderer;
73 import org.jfree.chart.renderer.xy.XYItemRenderer;
74 import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
75 import org.jfree.data.time.Day;
76 import org.jfree.data.time.TimeSeries;
77 import org.jfree.data.time.TimeSeriesCollection;
78 import org.jfree.data.xy.IntervalXYDataset;
79 import org.jfree.data.xy.XYDataset;
80 import org.jfree.data.xy.XYSeries;
81 import org.jfree.data.xy.XYSeriesCollection;
82 import org.jfree.date.MonthConstants;
83 import org.jfree.ui.Layer;
84 import org.jfree.ui.RectangleInsets;
85
86 /**
87  * Tests for the {@link XYPlot} class.
88  */

89 public class XYPlotTests extends TestCase {
90
91     /**
92      * Returns the tests as a test suite.
93      *
94      * @return The test suite.
95      */

96     public static Test suite() {
97         return new TestSuite(XYPlotTests.class);
98     }
99
100     /**
101      * Constructs a new set of tests.
102      *
103      * @param name the name of the tests.
104      */

105     public XYPlotTests(String JavaDoc name) {
106         super(name);
107     }
108     
109     /**
110      * Added this test in response to a bug report.
111      */

112     public void testGetDatasetCount() {
113         XYPlot plot = new XYPlot();
114         assertEquals(0, plot.getDatasetCount());
115     }
116
117     /**
118      * Some checks for the equals() method.
119      */

120     public void testEquals() {
121         
122         XYPlot plot1 = new XYPlot();
123         XYPlot plot2 = new XYPlot();
124         assertTrue(plot1.equals(plot2));
125         
126         // orientation...
127
plot1.setOrientation(PlotOrientation.HORIZONTAL);
128         assertFalse(plot1.equals(plot2));
129         plot2.setOrientation(PlotOrientation.HORIZONTAL);
130         assertTrue(plot1.equals(plot2));
131         
132         // axisOffset...
133
plot1.setAxisOffset(new RectangleInsets(0.05, 0.05, 0.05, 0.05));
134         assertFalse(plot1.equals(plot2));
135         plot2.setAxisOffset(new RectangleInsets(0.05, 0.05, 0.05, 0.05));
136         assertTrue(plot1.equals(plot2));
137
138         // domainAxis...
139
plot1.setDomainAxis(new NumberAxis("Domain Axis"));
140         assertFalse(plot1.equals(plot2));
141         plot2.setDomainAxis(new NumberAxis("Domain Axis"));
142         assertTrue(plot1.equals(plot2));
143
144         // domainAxisLocation...
145
plot1.setDomainAxisLocation(AxisLocation.TOP_OR_RIGHT);
146         assertFalse(plot1.equals(plot2));
147         plot2.setDomainAxisLocation(AxisLocation.TOP_OR_RIGHT);
148         assertTrue(plot1.equals(plot2));
149
150         // secondary DomainAxes...
151
plot1.setDomainAxis(11, new NumberAxis("Secondary Domain Axis"));
152         assertFalse(plot1.equals(plot2));
153         plot2.setDomainAxis(11, new NumberAxis("Secondary Domain Axis"));
154         assertTrue(plot1.equals(plot2));
155
156         // secondary DomainAxisLocations...
157
plot1.setDomainAxisLocation(11, AxisLocation.TOP_OR_RIGHT);
158         assertFalse(plot1.equals(plot2));
159         plot2.setDomainAxisLocation(11, AxisLocation.TOP_OR_RIGHT);
160         assertTrue(plot1.equals(plot2));
161
162         // rangeAxis...
163
plot1.setRangeAxis(new NumberAxis("Range Axis"));
164         assertFalse(plot1.equals(plot2));
165         plot2.setRangeAxis(new NumberAxis("Range Axis"));
166         assertTrue(plot1.equals(plot2));
167
168         // rangeAxisLocation...
169
plot1.setRangeAxisLocation(AxisLocation.TOP_OR_RIGHT);
170         assertFalse(plot1.equals(plot2));
171         plot2.setRangeAxisLocation(AxisLocation.TOP_OR_RIGHT);
172         assertTrue(plot1.equals(plot2));
173
174         // secondary RangeAxes...
175
plot1.setRangeAxis(11, new NumberAxis("Secondary Range Axis"));
176         assertFalse(plot1.equals(plot2));
177         plot2.setRangeAxis(11, new NumberAxis("Secondary Range Axis"));
178         assertTrue(plot1.equals(plot2));
179
180         // secondary RangeAxisLocations...
181
plot1.setRangeAxisLocation(11, AxisLocation.TOP_OR_RIGHT);
182         assertFalse(plot1.equals(plot2));
183         plot2.setRangeAxisLocation(11, AxisLocation.TOP_OR_RIGHT);
184         assertTrue(plot1.equals(plot2));
185         
186         // secondary DatasetDomainAxisMap...
187
plot1.mapDatasetToDomainAxis(11, 11);
188         assertFalse(plot1.equals(plot2));
189         plot2.mapDatasetToDomainAxis(11, 11);
190         assertTrue(plot1.equals(plot2));
191
192         // secondaryDatasetRangeAxisMap...
193
plot1.mapDatasetToRangeAxis(11, 11);
194         assertFalse(plot1.equals(plot2));
195         plot2.mapDatasetToRangeAxis(11, 11);
196         assertTrue(plot1.equals(plot2));
197         
198         // renderer
199
plot1.setRenderer(new DefaultXYItemRenderer());
200         assertFalse(plot1.equals(plot2));
201         plot2.setRenderer(new DefaultXYItemRenderer());
202         assertTrue(plot1.equals(plot2));
203         
204         // secondary renderers
205
plot1.setRenderer(11, new DefaultXYItemRenderer());
206         assertFalse(plot1.equals(plot2));
207         plot2.setRenderer(11, new DefaultXYItemRenderer());
208         assertTrue(plot1.equals(plot2));
209         
210         // domainGridlinesVisible
211
plot1.setDomainGridlinesVisible(false);
212         assertFalse(plot1.equals(plot2));
213         plot2.setDomainGridlinesVisible(false);
214         assertTrue(plot1.equals(plot2));
215
216         // domainGridlineStroke
217
Stroke JavaDoc stroke = new BasicStroke JavaDoc(2.0f);
218         plot1.setDomainGridlineStroke(stroke);
219         assertFalse(plot1.equals(plot2));
220         plot2.setDomainGridlineStroke(stroke);
221         assertTrue(plot1.equals(plot2));
222         
223         // domainGridlinePaint
224
plot1.setDomainGridlinePaint(Color.blue);
225         assertFalse(plot1.equals(plot2));
226         plot2.setDomainGridlinePaint(Color.blue);
227         assertTrue(plot1.equals(plot2));
228         
229         // rangeGridlinesVisible
230
plot1.setRangeGridlinesVisible(false);
231         assertFalse(plot1.equals(plot2));
232         plot2.setRangeGridlinesVisible(false);
233         assertTrue(plot1.equals(plot2));
234
235         // rangeGridlineStroke
236
plot1.setRangeGridlineStroke(stroke);
237         assertFalse(plot1.equals(plot2));
238         plot2.setRangeGridlineStroke(stroke);
239         assertTrue(plot1.equals(plot2));
240         
241         // rangeGridlinePaint
242
plot1.setRangeGridlinePaint(Color.blue);
243         assertFalse(plot1.equals(plot2));
244         plot2.setRangeGridlinePaint(Color.blue);
245         assertTrue(plot1.equals(plot2));
246                 
247         // rangeZeroBaselineVisible
248
plot1.setRangeZeroBaselineVisible(true);
249         assertFalse(plot1.equals(plot2));
250         plot2.setRangeZeroBaselineVisible(true);
251         assertTrue(plot1.equals(plot2));
252
253         // rangeZeroBaselineStroke
254
plot1.setRangeZeroBaselineStroke(stroke);
255         assertFalse(plot1.equals(plot2));
256         plot2.setRangeZeroBaselineStroke(stroke);
257         assertTrue(plot1.equals(plot2));
258         
259         // rangeZeroBaselinePaint
260
plot1.setRangeZeroBaselinePaint(Color.blue);
261         assertFalse(plot1.equals(plot2));
262         plot2.setRangeZeroBaselinePaint(Color.blue);
263         assertTrue(plot1.equals(plot2));
264
265         // rangeCrosshairVisible
266
plot1.setRangeCrosshairVisible(true);
267         assertFalse(plot1.equals(plot2));
268         plot2.setRangeCrosshairVisible(true);
269         assertTrue(plot1.equals(plot2));
270         
271         // rangeCrosshairValue
272
plot1.setRangeCrosshairValue(100.0);
273         assertFalse(plot1.equals(plot2));
274         plot2.setRangeCrosshairValue(100.0);
275         assertTrue(plot1.equals(plot2));
276         
277         // rangeCrosshairStroke
278
plot1.setRangeCrosshairStroke(stroke);
279         assertFalse(plot1.equals(plot2));
280         plot2.setRangeCrosshairStroke(stroke);
281         assertTrue(plot1.equals(plot2));
282         
283         // rangeCrosshairPaint
284
plot1.setRangeCrosshairPaint(Color.red);
285         assertFalse(plot1.equals(plot2));
286         plot2.setRangeCrosshairPaint(Color.red);
287         assertTrue(plot1.equals(plot2));
288         
289         // rangeCrosshairLockedOnData
290
plot1.setRangeCrosshairLockedOnData(false);
291         assertFalse(plot1.equals(plot2));
292         plot2.setRangeCrosshairLockedOnData(false);
293         assertTrue(plot1.equals(plot2));
294         
295         // range markers
296
plot1.addRangeMarker(new ValueMarker(4.0));
297         assertFalse(plot1.equals(plot2));
298         plot2.addRangeMarker(new ValueMarker(4.0));
299         assertTrue(plot1.equals(plot2));
300         
301         // secondary range markers
302
plot1.addRangeMarker(1, new ValueMarker(4.0), Layer.FOREGROUND);
303         assertFalse(plot1.equals(plot2));
304         plot2.addRangeMarker(1, new ValueMarker(4.0), Layer.FOREGROUND);
305         assertTrue(plot1.equals(plot2));
306         
307         plot1.addRangeMarker(1, new ValueMarker(99.0), Layer.BACKGROUND);
308         assertFalse(plot1.equals(plot2));
309         plot2.addRangeMarker(1, new ValueMarker(99.0), Layer.BACKGROUND);
310         assertTrue(plot1.equals(plot2));
311                 
312         // weight
313
plot1.setWeight(3);
314         assertFalse(plot1.equals(plot2));
315         plot2.setWeight(3);
316         assertTrue(plot1.equals(plot2));
317         
318     }
319
320     /**
321      * Confirm that basic cloning works.
322      */

323     public void testCloning() {
324         XYPlot p1 = new XYPlot();
325         XYPlot p2 = null;
326         try {
327             p2 = (XYPlot) p1.clone();
328         }
329         catch (CloneNotSupportedException JavaDoc e) {
330             e.printStackTrace();
331             System.err.println("XYPlotTests.testCloning: failed to clone.");
332         }
333         assertTrue(p1 != p2);
334         assertTrue(p1.getClass() == p2.getClass());
335         assertTrue(p1.equals(p2));
336     }
337     
338     /**
339      * Tests cloning for a more complex plot.
340      */

341     public void testCloning2() {
342         XYPlot p1 = new XYPlot(
343             null, new NumberAxis("Domain Axis"), new NumberAxis("Range Axis"),
344             new StandardXYItemRenderer()
345         );
346         p1.setRangeAxis(1, new NumberAxis("Range Axis 2"));
347         p1.setRenderer(1, new XYBarRenderer());
348         XYPlot p2 = null;
349         try {
350             p2 = (XYPlot) p1.clone();
351         }
352         catch (CloneNotSupportedException JavaDoc e) {
353             e.printStackTrace();
354             System.err.println("Failed to clone.");
355         }
356         assertTrue(p1 != p2);
357         assertTrue(p1.getClass() == p2.getClass());
358         assertTrue(p1.equals(p2));
359     }
360     
361     /**
362      * Tests the independence of the clones.
363      */

364     public void testCloneIndependence() {
365         XYPlot p1 = new XYPlot(
366             null, new NumberAxis("Domain Axis"), new NumberAxis("Range Axis"),
367             new StandardXYItemRenderer()
368         );
369         p1.setDomainAxis(1, new NumberAxis("Domain Axis 2"));
370         p1.setDomainAxisLocation(1, AxisLocation.BOTTOM_OR_LEFT);
371         p1.setRangeAxis(1, new NumberAxis("Range Axis 2"));
372         p1.setRangeAxisLocation(1, AxisLocation.TOP_OR_RIGHT);
373         p1.setRenderer(1, new XYBarRenderer());
374         XYPlot p2 = null;
375         try {
376             p2 = (XYPlot) p1.clone();
377         }
378         catch (CloneNotSupportedException JavaDoc e) {
379             e.printStackTrace();
380             System.err.println("Failed to clone.");
381         }
382         assertTrue(p1.equals(p2));
383         
384         p1.getDomainAxis().setLabel("Label");
385         assertFalse(p1.equals(p2));
386         p2.getDomainAxis().setLabel("Label");
387         assertTrue(p1.equals(p2));
388         
389         p1.getDomainAxis(1).setLabel("S1");
390         assertFalse(p1.equals(p2));
391         p2.getDomainAxis(1).setLabel("S1");
392         assertTrue(p1.equals(p2));
393         
394         p1.setDomainAxisLocation(1, AxisLocation.TOP_OR_RIGHT);
395         assertFalse(p1.equals(p2));
396         p2.setDomainAxisLocation(1, AxisLocation.TOP_OR_RIGHT);
397         assertTrue(p1.equals(p2));
398         
399         p1.mapDatasetToDomainAxis(2, 1);
400         assertFalse(p1.equals(p2));
401         p2.mapDatasetToDomainAxis(2, 1);
402         assertTrue(p1.equals(p2));
403
404         p1.getRangeAxis().setLabel("Label");
405         assertFalse(p1.equals(p2));
406         p2.getRangeAxis().setLabel("Label");
407         assertTrue(p1.equals(p2));
408         
409         p1.getRangeAxis(1).setLabel("S1");
410         assertFalse(p1.equals(p2));
411         p2.getRangeAxis(1).setLabel("S1");
412         assertTrue(p1.equals(p2));
413         
414         p1.setRangeAxisLocation(1, AxisLocation.TOP_OR_LEFT);
415         assertFalse(p1.equals(p2));
416         p2.setRangeAxisLocation(1, AxisLocation.TOP_OR_LEFT);
417         assertTrue(p1.equals(p2));
418         
419         p1.mapDatasetToRangeAxis(2, 1);
420         assertFalse(p1.equals(p2));
421         p2.mapDatasetToRangeAxis(2, 1);
422         assertTrue(p1.equals(p2));
423
424         p1.getRenderer().setOutlinePaint(Color.cyan);
425         assertFalse(p1.equals(p2));
426         p2.getRenderer().setOutlinePaint(Color.cyan);
427         assertTrue(p1.equals(p2));
428         
429         p1.getRenderer(1).setOutlinePaint(Color.red);
430         assertFalse(p1.equals(p2));
431         p2.getRenderer(1).setOutlinePaint(Color.red);
432         assertTrue(p1.equals(p2));
433         
434     }
435     
436     /**
437      * Setting a null renderer should be allowed, but is generating a null
438      * pointer exception in 0.9.7.
439      */

440     public void testSetNullRenderer() {
441         boolean failed = false;
442         try {
443             XYPlot plot = new XYPlot(
444                 null, new NumberAxis("X"), new NumberAxis("Y"), null
445             );
446             plot.setRenderer(null);
447         }
448         catch (Exception JavaDoc e) {
449             failed = true;
450         }
451         assertTrue(!failed);
452     }
453
454     /**
455      * Serialize an instance, restore it, and check for equality.
456      */

457     public void testSerialization1() {
458
459         XYDataset data = new XYSeriesCollection();
460         NumberAxis domainAxis = new NumberAxis("Domain");
461         NumberAxis rangeAxis = new NumberAxis("Range");
462         StandardXYItemRenderer renderer = new StandardXYItemRenderer();
463         XYPlot p1 = new XYPlot(data, domainAxis, rangeAxis, renderer);
464         XYPlot p2 = null;
465
466         try {
467             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
468             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
469             out.writeObject(p1);
470             out.close();
471
472             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
473                 new ByteArrayInputStream JavaDoc(buffer.toByteArray())
474             );
475             p2 = (XYPlot) in.readObject();
476             in.close();
477         }
478         catch (Exception JavaDoc e) {
479             fail(e.toString());
480         }
481         assertEquals(p1, p2);
482
483     }
484
485     /**
486      * Serialize an instance, restore it, and check for equality. This test
487      * uses a {@link DateAxis} and a {@link StandardXYToolTipGenerator}.
488      */

489     public void testSerialization2() {
490
491         IntervalXYDataset data1 = createDataset1();
492         XYItemRenderer renderer1 = new XYBarRenderer(0.20);
493         renderer1.setToolTipGenerator(
494             StandardXYToolTipGenerator.getTimeSeriesInstance()
495         );
496         XYPlot p1 = new XYPlot(data1, new DateAxis("Date"), null, renderer1);
497         XYPlot p2 = null;
498
499         try {
500             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
501             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
502             out.writeObject(p1);
503             out.close();
504
505             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
506                 new ByteArrayInputStream JavaDoc(buffer.toByteArray())
507             );
508             p2 = (XYPlot) in.readObject();
509             in.close();
510         }
511         catch (Exception JavaDoc e) {
512             fail(e.toString());
513         }
514         assertEquals(p1, p2);
515
516     }
517
518     /**
519      * Problem to reproduce a bug in serialization. The bug (first reported
520      * against the {@link org.jfree.chart.plot.CategoryPlot} class) is a null
521      * pointer exception that occurs when drawing a plot after deserialization.
522      * It is caused by four temporary storage structures (axesAtTop,
523      * axesAtBottom, axesAtLeft and axesAtRight - all initialized as empty
524      * lists in the constructor) not being initialized by the readObject()
525      * method following deserialization. This test has been written to
526      * reproduce the bug (now fixed).
527      */

528     public void testSerialization3() {
529         
530         XYSeriesCollection dataset = new XYSeriesCollection();
531         JFreeChart chart = ChartFactory.createXYLineChart(
532             "Test Chart",
533             "Domain Axis",
534             "Range Axis",
535             dataset,
536             PlotOrientation.VERTICAL,
537             true,
538             true,
539             false
540         );
541         JFreeChart chart2 = null;
542         
543         // serialize and deserialize the chart....
544
try {
545             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
546             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
547             out.writeObject(chart);
548             out.close();
549
550             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
551                 new ByteArrayInputStream JavaDoc(buffer.toByteArray())
552             );
553             chart2 = (JFreeChart) in.readObject();
554             in.close();
555         }
556         catch (Exception JavaDoc e) {
557             fail(e.toString());
558         }
559
560         boolean b = chart.equals(chart2);
561         System.out.println(b);
562         assertEquals(chart, chart2);
563         boolean passed = true;
564         try {
565             chart2.createBufferedImage(300, 200);
566         }
567         catch (Exception JavaDoc e) {
568             passed = false;
569             e.printStackTrace();
570         }
571         assertTrue(passed);
572     }
573     
574     /**
575      * A test to reproduce a bug in serialization: the domain and/or range
576      * markers for a plot are not being serialized.
577      */

578     public void testSerialization4() {
579         
580         XYSeriesCollection dataset = new XYSeriesCollection();
581         JFreeChart chart = ChartFactory.createXYLineChart(
582             "Test Chart",
583             "Domain Axis",
584             "Range Axis",
585             dataset,
586             PlotOrientation.VERTICAL,
587             true,
588             true,
589             false
590         );
591         XYPlot plot = (XYPlot) chart.getPlot();
592         plot.addDomainMarker(new ValueMarker(1.0), Layer.FOREGROUND);
593         plot.addDomainMarker(new IntervalMarker(2.0, 3.0), Layer.BACKGROUND);
594         plot.addRangeMarker(new ValueMarker(4.0), Layer.FOREGROUND);
595         plot.addRangeMarker(new IntervalMarker(5.0, 6.0), Layer.BACKGROUND);
596         JFreeChart chart2 = null;
597         
598         // serialize and deserialize the chart....
599
try {
600             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
601             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
602             out.writeObject(chart);
603             out.close();
604
605             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
606                 new ByteArrayInputStream JavaDoc(buffer.toByteArray())
607             );
608             chart2 = (JFreeChart) in.readObject();
609             in.close();
610         }
611         catch (Exception JavaDoc e) {
612             fail(e.toString());
613         }
614
615         assertEquals(chart, chart2);
616         boolean passed = true;
617         try {
618             chart2.createBufferedImage(300, 200);
619         }
620         catch (Exception JavaDoc e) {
621             passed = false;
622             e.printStackTrace();
623         }
624         assertTrue(passed);
625     }
626     
627     /**
628      * Tests a bug where the plot is no longer registered as a listener
629      * with the dataset(s) and axes after deserialization. See patch 1209475
630      * at SourceForge.
631      */

632     public void testSerialization5() {
633         XYSeriesCollection dataset1 = new XYSeriesCollection();
634         NumberAxis domainAxis1 = new NumberAxis("Domain 1");
635         NumberAxis rangeAxis1 = new NumberAxis("Range 1");
636         StandardXYItemRenderer renderer1 = new StandardXYItemRenderer();
637         XYPlot p1 = new XYPlot(dataset1, domainAxis1, rangeAxis1, renderer1);
638         NumberAxis domainAxis2 = new NumberAxis("Domain 2");
639         NumberAxis rangeAxis2 = new NumberAxis("Range 2");
640         StandardXYItemRenderer renderer2 = new StandardXYItemRenderer();
641         XYSeriesCollection dataset2 = new XYSeriesCollection();
642         p1.setDataset(1, dataset2);
643         p1.setDomainAxis(1, domainAxis2);
644         p1.setRangeAxis(1, rangeAxis2);
645         p1.setRenderer(1, renderer2);
646         XYPlot p2 = null;
647         try {
648             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
649             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
650             out.writeObject(p1);
651             out.close();
652             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
653                 new ByteArrayInputStream JavaDoc(buffer.toByteArray())
654             );
655             p2 = (XYPlot) in.readObject();
656             in.close();
657         }
658         catch (Exception JavaDoc e) {
659             fail(e.toString());
660         }
661         assertEquals(p1, p2);
662         
663         // now check that all datasets, renderers and axes are being listened
664
// too...
665
NumberAxis domainAxisA = (NumberAxis) p2.getDomainAxis(0);
666         NumberAxis rangeAxisA = (NumberAxis) p2.getRangeAxis(0);
667         XYSeriesCollection datasetA = (XYSeriesCollection) p2.getDataset(0);
668         StandardXYItemRenderer rendererA
669             = (StandardXYItemRenderer) p2.getRenderer(0);
670         NumberAxis domainAxisB = (NumberAxis) p2.getDomainAxis(1);
671         NumberAxis rangeAxisB = (NumberAxis) p2.getRangeAxis(1);
672         XYSeriesCollection datasetB = (XYSeriesCollection) p2.getDataset(1);
673         StandardXYItemRenderer rendererB
674             = (StandardXYItemRenderer) p2.getRenderer(1);
675         assertTrue(datasetA.hasListener(p2));
676         assertTrue(domainAxisA.hasListener(p2));
677         assertTrue(rangeAxisA.hasListener(p2));
678         assertTrue(rendererA.hasListener(p2));
679         assertTrue(datasetB.hasListener(p2));
680         assertTrue(domainAxisB.hasListener(p2));
681         assertTrue(rangeAxisB.hasListener(p2));
682         assertTrue(rendererB.hasListener(p2));
683     }
684
685     /**
686      * Some checks for the getRendererForDataset() method.
687      */

688     public void testGetRendererForDataset() {
689         XYDataset d0 = new XYSeriesCollection();
690         XYDataset d1 = new XYSeriesCollection();
691         XYDataset d2 = new XYSeriesCollection();
692         XYDataset d3 = new XYSeriesCollection(); // not used by plot
693
XYItemRenderer r0 = new XYLineAndShapeRenderer();
694         XYItemRenderer r2 = new XYLineAndShapeRenderer();
695         XYPlot plot = new XYPlot();
696         plot.setDataset(0, d0);
697         plot.setDataset(1, d1);
698         plot.setDataset(2, d2);
699         plot.setRenderer(0, r0);
700         // no renderer 1
701
plot.setRenderer(2, r2);
702         assertEquals(r0, plot.getRendererForDataset(d0));
703         assertEquals(r0, plot.getRendererForDataset(d1));
704         assertEquals(r2, plot.getRendererForDataset(d2));
705         assertEquals(null, plot.getRendererForDataset(d3));
706         assertEquals(null, plot.getRendererForDataset(null));
707     }
708
709     /**
710      * Some checks for the getLegendItems() method.
711      */

712     public void testGetLegendItems() {
713         // check the case where there is a secondary dataset that doesn't
714
// have a renderer (i.e. falls back to renderer 0)
715
XYDataset d0 = createDataset1();
716         XYDataset d1 = createDataset2();
717         XYItemRenderer r0 = new XYLineAndShapeRenderer();
718         XYPlot plot = new XYPlot();
719         plot.setDataset(0, d0);
720         plot.setDataset(1, d1);
721         plot.setRenderer(0, r0);
722         LegendItemCollection items = plot.getLegendItems();
723         assertEquals(2, items.getItemCount());
724     }
725     
726     /**
727      * Creates a sample dataset.
728      *
729      * @return Series 1.
730      */

731     private IntervalXYDataset createDataset1() {
732
733         // create dataset 1...
734
TimeSeries series1 = new TimeSeries("Series 1", Day.class);
735         series1.add(new Day(1, MonthConstants.MARCH, 2002), 12353.3);
736         series1.add(new Day(2, MonthConstants.MARCH, 2002), 13734.4);
737         series1.add(new Day(3, MonthConstants.MARCH, 2002), 14525.3);
738         series1.add(new Day(4, MonthConstants.MARCH, 2002), 13984.3);
739         series1.add(new Day(5, MonthConstants.MARCH, 2002), 12999.4);
740         series1.add(new Day(6, MonthConstants.MARCH, 2002), 14274.3);
741         series1.add(new Day(7, MonthConstants.MARCH, 2002), 15943.5);
742         series1.add(new Day(8, MonthConstants.MARCH, 2002), 14845.3);
743         series1.add(new Day(9, MonthConstants.MARCH, 2002), 14645.4);
744         series1.add(new Day(10, MonthConstants.MARCH, 2002), 16234.6);
745         series1.add(new Day(11, MonthConstants.MARCH, 2002), 17232.3);
746         series1.add(new Day(12, MonthConstants.MARCH, 2002), 14232.2);
747         series1.add(new Day(13, MonthConstants.MARCH, 2002), 13102.2);
748         series1.add(new Day(14, MonthConstants.MARCH, 2002), 14230.2);
749         series1.add(new Day(15, MonthConstants.MARCH, 2002), 11235.2);
750
751         TimeSeriesCollection collection = new TimeSeriesCollection(series1);
752         collection.setDomainIsPointsInTime(false);
753             // this tells the time series collection that
754
// we intend the data to represent time periods
755
// NOT points in time. This is required when
756
// determining the min/max values in the
757
// dataset's domain.
758
return collection;
759
760     }
761
762     /**
763      * Creates a sample dataset.
764      *
765      * @return A sample dataset.
766      */

767     private XYDataset createDataset2() {
768         // create dataset 1...
769
XYSeries series = new XYSeries("Series 2");
770         XYSeriesCollection collection = new XYSeriesCollection(series);
771         return collection;
772
773     }
774     
775     /**
776      * A test for a bug where setting the renderer doesn't register the plot
777      * as a RendererChangeListener.
778      */

779     public void testSetRenderer() {
780         XYPlot plot = new XYPlot();
781         XYItemRenderer renderer = new XYLineAndShapeRenderer();
782         plot.setRenderer(renderer);
783         // now make a change to the renderer and see if it triggers a plot
784
// change event...
785
MyPlotChangeListener listener = new MyPlotChangeListener();
786         plot.addChangeListener(listener);
787         renderer.setSeriesPaint(0, Color.black);
788         assertTrue(listener.getEvent() != null);
789     }
790     
791 }
792
Popular Tags