0001: /* ===========================================================
0002: * JFreeChart : a free chart library for the Java(tm) platform
0003: * ===========================================================
0004: *
0005: * (C) Copyright 2000-2007, by Object Refinery Limited and Contributors.
0006: *
0007: * Project Info: http://www.jfree.org/jfreechart/index.html
0008: *
0009: * This library is free software; you can redistribute it and/or modify it
0010: * under the terms of the GNU Lesser General Public License as published by
0011: * the Free Software Foundation; either version 2.1 of the License, or
0012: * (at your option) any later version.
0013: *
0014: * This library is distributed in the hope that it will be useful, but
0015: * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
0016: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
0017: * License for more details.
0018: *
0019: * You should have received a copy of the GNU Lesser General Public
0020: * License along with this library; if not, write to the Free Software
0021: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
0022: * USA.
0023: *
0024: * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
0025: * in the United States and other countries.]
0026: *
0027: * ----------------
0028: * XYPlotTests.java
0029: * ----------------
0030: * (C) Copyright 2003-2007, by Object Refinery Limited and Contributors.
0031: *
0032: * Original Author: David Gilbert (for Object Refinery Limited);
0033: * Contributor(s): -;
0034: *
0035: * $Id: XYPlotTests.java,v 1.1.2.7 2007/05/24 13:49:12 mungady Exp $
0036: *
0037: * Changes
0038: * -------
0039: * 26-Mar-2003 : Version 1 (DG);
0040: * 22-Mar-2004 : Added new cloning test (DG);
0041: * 05-Oct-2004 : Strengthened test for clone independence (DG);
0042: * 22-Nov-2006 : Added quadrant fields to equals() and clone() tests (DG);
0043: * 09-Jan-2007 : Mark and comment out testGetDatasetCount() (DG);
0044: * 05-Feb-2007 : Added testAddDomainMarker() and testAddRangeMarker() (DG);
0045: * 07-Feb-2007 : Added test1654215() (DG);
0046: * 24-May-2007 : Added testDrawSeriesWithZeroItems() (DG);
0047: *
0048: */
0049:
0050: package org.jfree.chart.plot.junit;
0051:
0052: import java.awt.BasicStroke;
0053: import java.awt.Color;
0054: import java.awt.GradientPaint;
0055: import java.awt.Graphics2D;
0056: import java.awt.Stroke;
0057: import java.awt.geom.Point2D;
0058: import java.awt.geom.Rectangle2D;
0059: import java.awt.image.BufferedImage;
0060: import java.io.ByteArrayInputStream;
0061: import java.io.ByteArrayOutputStream;
0062: import java.io.ObjectInput;
0063: import java.io.ObjectInputStream;
0064: import java.io.ObjectOutput;
0065: import java.io.ObjectOutputStream;
0066: import java.util.Arrays;
0067: import java.util.List;
0068:
0069: import junit.framework.Test;
0070: import junit.framework.TestCase;
0071: import junit.framework.TestSuite;
0072:
0073: import org.jfree.chart.ChartFactory;
0074: import org.jfree.chart.JFreeChart;
0075: import org.jfree.chart.LegendItemCollection;
0076: import org.jfree.chart.annotations.XYTextAnnotation;
0077: import org.jfree.chart.axis.AxisLocation;
0078: import org.jfree.chart.axis.DateAxis;
0079: import org.jfree.chart.axis.NumberAxis;
0080: import org.jfree.chart.event.MarkerChangeListener;
0081: import org.jfree.chart.labels.StandardXYToolTipGenerator;
0082: import org.jfree.chart.plot.IntervalMarker;
0083: import org.jfree.chart.plot.Marker;
0084: import org.jfree.chart.plot.PlotOrientation;
0085: import org.jfree.chart.plot.ValueMarker;
0086: import org.jfree.chart.plot.XYPlot;
0087: import org.jfree.chart.renderer.xy.DefaultXYItemRenderer;
0088: import org.jfree.chart.renderer.xy.StandardXYItemRenderer;
0089: import org.jfree.chart.renderer.xy.XYBarRenderer;
0090: import org.jfree.chart.renderer.xy.XYItemRenderer;
0091: import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
0092: import org.jfree.data.time.Day;
0093: import org.jfree.data.time.TimeSeries;
0094: import org.jfree.data.time.TimeSeriesCollection;
0095: import org.jfree.data.xy.DefaultXYDataset;
0096: import org.jfree.data.xy.IntervalXYDataset;
0097: import org.jfree.data.xy.XYDataset;
0098: import org.jfree.data.xy.XYSeries;
0099: import org.jfree.data.xy.XYSeriesCollection;
0100: import org.jfree.date.MonthConstants;
0101: import org.jfree.ui.Layer;
0102: import org.jfree.ui.RectangleInsets;
0103:
0104: /**
0105: * Tests for the {@link XYPlot} class.
0106: */
0107: public class XYPlotTests extends TestCase {
0108:
0109: /**
0110: * Returns the tests as a test suite.
0111: *
0112: * @return The test suite.
0113: */
0114: public static Test suite() {
0115: return new TestSuite(XYPlotTests.class);
0116: }
0117:
0118: /**
0119: * Constructs a new set of tests.
0120: *
0121: * @param name the name of the tests.
0122: */
0123: public XYPlotTests(String name) {
0124: super (name);
0125: }
0126:
0127: // FIXME: the getDatasetCount() method is returning a count of the slots
0128: // available for datasets, rather than the number of datasets actually
0129: // specified...see if there is some way to clean this up.
0130: // /**
0131: // * Added this test in response to a bug report.
0132: // */
0133: // public void testGetDatasetCount() {
0134: // XYPlot plot = new XYPlot();
0135: // assertEquals(0, plot.getDatasetCount());
0136: // }
0137:
0138: /**
0139: * Some checks for the equals() method.
0140: */
0141: public void testEquals() {
0142:
0143: XYPlot plot1 = new XYPlot();
0144: XYPlot plot2 = new XYPlot();
0145: assertTrue(plot1.equals(plot2));
0146:
0147: // orientation...
0148: plot1.setOrientation(PlotOrientation.HORIZONTAL);
0149: assertFalse(plot1.equals(plot2));
0150: plot2.setOrientation(PlotOrientation.HORIZONTAL);
0151: assertTrue(plot1.equals(plot2));
0152:
0153: // axisOffset...
0154: plot1
0155: .setAxisOffset(new RectangleInsets(0.05, 0.05, 0.05,
0156: 0.05));
0157: assertFalse(plot1.equals(plot2));
0158: plot2
0159: .setAxisOffset(new RectangleInsets(0.05, 0.05, 0.05,
0160: 0.05));
0161: assertTrue(plot1.equals(plot2));
0162:
0163: // domainAxis...
0164: plot1.setDomainAxis(new NumberAxis("Domain Axis"));
0165: assertFalse(plot1.equals(plot2));
0166: plot2.setDomainAxis(new NumberAxis("Domain Axis"));
0167: assertTrue(plot1.equals(plot2));
0168:
0169: // domainAxisLocation...
0170: plot1.setDomainAxisLocation(AxisLocation.TOP_OR_RIGHT);
0171: assertFalse(plot1.equals(plot2));
0172: plot2.setDomainAxisLocation(AxisLocation.TOP_OR_RIGHT);
0173: assertTrue(plot1.equals(plot2));
0174:
0175: // secondary DomainAxes...
0176: plot1
0177: .setDomainAxis(11, new NumberAxis(
0178: "Secondary Domain Axis"));
0179: assertFalse(plot1.equals(plot2));
0180: plot2
0181: .setDomainAxis(11, new NumberAxis(
0182: "Secondary Domain Axis"));
0183: assertTrue(plot1.equals(plot2));
0184:
0185: // secondary DomainAxisLocations...
0186: plot1.setDomainAxisLocation(11, AxisLocation.TOP_OR_RIGHT);
0187: assertFalse(plot1.equals(plot2));
0188: plot2.setDomainAxisLocation(11, AxisLocation.TOP_OR_RIGHT);
0189: assertTrue(plot1.equals(plot2));
0190:
0191: // rangeAxis...
0192: plot1.setRangeAxis(new NumberAxis("Range Axis"));
0193: assertFalse(plot1.equals(plot2));
0194: plot2.setRangeAxis(new NumberAxis("Range Axis"));
0195: assertTrue(plot1.equals(plot2));
0196:
0197: // rangeAxisLocation...
0198: plot1.setRangeAxisLocation(AxisLocation.TOP_OR_RIGHT);
0199: assertFalse(plot1.equals(plot2));
0200: plot2.setRangeAxisLocation(AxisLocation.TOP_OR_RIGHT);
0201: assertTrue(plot1.equals(plot2));
0202:
0203: // secondary RangeAxes...
0204: plot1.setRangeAxis(11, new NumberAxis("Secondary Range Axis"));
0205: assertFalse(plot1.equals(plot2));
0206: plot2.setRangeAxis(11, new NumberAxis("Secondary Range Axis"));
0207: assertTrue(plot1.equals(plot2));
0208:
0209: // secondary RangeAxisLocations...
0210: plot1.setRangeAxisLocation(11, AxisLocation.TOP_OR_RIGHT);
0211: assertFalse(plot1.equals(plot2));
0212: plot2.setRangeAxisLocation(11, AxisLocation.TOP_OR_RIGHT);
0213: assertTrue(plot1.equals(plot2));
0214:
0215: // secondary DatasetDomainAxisMap...
0216: plot1.mapDatasetToDomainAxis(11, 11);
0217: assertFalse(plot1.equals(plot2));
0218: plot2.mapDatasetToDomainAxis(11, 11);
0219: assertTrue(plot1.equals(plot2));
0220:
0221: // secondaryDatasetRangeAxisMap...
0222: plot1.mapDatasetToRangeAxis(11, 11);
0223: assertFalse(plot1.equals(plot2));
0224: plot2.mapDatasetToRangeAxis(11, 11);
0225: assertTrue(plot1.equals(plot2));
0226:
0227: // renderer
0228: plot1.setRenderer(new DefaultXYItemRenderer());
0229: assertFalse(plot1.equals(plot2));
0230: plot2.setRenderer(new DefaultXYItemRenderer());
0231: assertTrue(plot1.equals(plot2));
0232:
0233: // secondary renderers
0234: plot1.setRenderer(11, new DefaultXYItemRenderer());
0235: assertFalse(plot1.equals(plot2));
0236: plot2.setRenderer(11, new DefaultXYItemRenderer());
0237: assertTrue(plot1.equals(plot2));
0238:
0239: // domainGridlinesVisible
0240: plot1.setDomainGridlinesVisible(false);
0241: assertFalse(plot1.equals(plot2));
0242: plot2.setDomainGridlinesVisible(false);
0243: assertTrue(plot1.equals(plot2));
0244:
0245: // domainGridlineStroke
0246: Stroke stroke = new BasicStroke(2.0f);
0247: plot1.setDomainGridlineStroke(stroke);
0248: assertFalse(plot1.equals(plot2));
0249: plot2.setDomainGridlineStroke(stroke);
0250: assertTrue(plot1.equals(plot2));
0251:
0252: // domainGridlinePaint
0253: plot1.setDomainGridlinePaint(new GradientPaint(1.0f, 2.0f,
0254: Color.blue, 3.0f, 4.0f, Color.red));
0255: assertFalse(plot1.equals(plot2));
0256: plot2.setDomainGridlinePaint(new GradientPaint(1.0f, 2.0f,
0257: Color.blue, 3.0f, 4.0f, Color.red));
0258: assertTrue(plot1.equals(plot2));
0259:
0260: // rangeGridlinesVisible
0261: plot1.setRangeGridlinesVisible(false);
0262: assertFalse(plot1.equals(plot2));
0263: plot2.setRangeGridlinesVisible(false);
0264: assertTrue(plot1.equals(plot2));
0265:
0266: // rangeGridlineStroke
0267: plot1.setRangeGridlineStroke(stroke);
0268: assertFalse(plot1.equals(plot2));
0269: plot2.setRangeGridlineStroke(stroke);
0270: assertTrue(plot1.equals(plot2));
0271:
0272: // rangeGridlinePaint
0273: plot1.setRangeGridlinePaint(new GradientPaint(1.0f, 2.0f,
0274: Color.green, 3.0f, 4.0f, Color.red));
0275: assertFalse(plot1.equals(plot2));
0276: plot2.setRangeGridlinePaint(new GradientPaint(1.0f, 2.0f,
0277: Color.green, 3.0f, 4.0f, Color.red));
0278: assertTrue(plot1.equals(plot2));
0279:
0280: // rangeZeroBaselineVisible
0281: plot1.setRangeZeroBaselineVisible(true);
0282: assertFalse(plot1.equals(plot2));
0283: plot2.setRangeZeroBaselineVisible(true);
0284: assertTrue(plot1.equals(plot2));
0285:
0286: // rangeZeroBaselineStroke
0287: plot1.setRangeZeroBaselineStroke(stroke);
0288: assertFalse(plot1.equals(plot2));
0289: plot2.setRangeZeroBaselineStroke(stroke);
0290: assertTrue(plot1.equals(plot2));
0291:
0292: // rangeZeroBaselinePaint
0293: plot1.setRangeZeroBaselinePaint(new GradientPaint(1.0f, 2.0f,
0294: Color.white, 3.0f, 4.0f, Color.red));
0295: assertFalse(plot1.equals(plot2));
0296: plot2.setRangeZeroBaselinePaint(new GradientPaint(1.0f, 2.0f,
0297: Color.white, 3.0f, 4.0f, Color.red));
0298: assertTrue(plot1.equals(plot2));
0299:
0300: // rangeCrosshairVisible
0301: plot1.setRangeCrosshairVisible(true);
0302: assertFalse(plot1.equals(plot2));
0303: plot2.setRangeCrosshairVisible(true);
0304: assertTrue(plot1.equals(plot2));
0305:
0306: // rangeCrosshairValue
0307: plot1.setRangeCrosshairValue(100.0);
0308: assertFalse(plot1.equals(plot2));
0309: plot2.setRangeCrosshairValue(100.0);
0310: assertTrue(plot1.equals(plot2));
0311:
0312: // rangeCrosshairStroke
0313: plot1.setRangeCrosshairStroke(stroke);
0314: assertFalse(plot1.equals(plot2));
0315: plot2.setRangeCrosshairStroke(stroke);
0316: assertTrue(plot1.equals(plot2));
0317:
0318: // rangeCrosshairPaint
0319: plot1.setRangeCrosshairPaint(new GradientPaint(1.0f, 2.0f,
0320: Color.pink, 3.0f, 4.0f, Color.red));
0321: assertFalse(plot1.equals(plot2));
0322: plot2.setRangeCrosshairPaint(new GradientPaint(1.0f, 2.0f,
0323: Color.pink, 3.0f, 4.0f, Color.red));
0324: assertTrue(plot1.equals(plot2));
0325:
0326: // rangeCrosshairLockedOnData
0327: plot1.setRangeCrosshairLockedOnData(false);
0328: assertFalse(plot1.equals(plot2));
0329: plot2.setRangeCrosshairLockedOnData(false);
0330: assertTrue(plot1.equals(plot2));
0331:
0332: // range markers
0333: plot1.addRangeMarker(new ValueMarker(4.0));
0334: assertFalse(plot1.equals(plot2));
0335: plot2.addRangeMarker(new ValueMarker(4.0));
0336: assertTrue(plot1.equals(plot2));
0337:
0338: // secondary range markers
0339: plot1.addRangeMarker(1, new ValueMarker(4.0), Layer.FOREGROUND);
0340: assertFalse(plot1.equals(plot2));
0341: plot2.addRangeMarker(1, new ValueMarker(4.0), Layer.FOREGROUND);
0342: assertTrue(plot1.equals(plot2));
0343:
0344: plot1
0345: .addRangeMarker(1, new ValueMarker(99.0),
0346: Layer.BACKGROUND);
0347: assertFalse(plot1.equals(plot2));
0348: plot2
0349: .addRangeMarker(1, new ValueMarker(99.0),
0350: Layer.BACKGROUND);
0351: assertTrue(plot1.equals(plot2));
0352:
0353: // weight
0354: plot1.setWeight(3);
0355: assertFalse(plot1.equals(plot2));
0356: plot2.setWeight(3);
0357: assertTrue(plot1.equals(plot2));
0358:
0359: // quadrant origin
0360: plot1.setQuadrantOrigin(new Point2D.Double(12.3, 45.6));
0361: assertFalse(plot1.equals(plot2));
0362: plot2.setQuadrantOrigin(new Point2D.Double(12.3, 45.6));
0363: assertTrue(plot1.equals(plot2));
0364:
0365: // quadrant paint
0366: plot1.setQuadrantPaint(0, new GradientPaint(1.0f, 2.0f,
0367: Color.red, 3.0f, 4.0f, Color.blue));
0368: assertFalse(plot1.equals(plot2));
0369: plot2.setQuadrantPaint(0, new GradientPaint(1.0f, 2.0f,
0370: Color.red, 3.0f, 4.0f, Color.blue));
0371: assertTrue(plot1.equals(plot2));
0372: plot1.setQuadrantPaint(1, new GradientPaint(2.0f, 3.0f,
0373: Color.red, 4.0f, 5.0f, Color.blue));
0374: assertFalse(plot1.equals(plot2));
0375: plot2.setQuadrantPaint(1, new GradientPaint(2.0f, 3.0f,
0376: Color.red, 4.0f, 5.0f, Color.blue));
0377: assertTrue(plot1.equals(plot2));
0378: plot1.setQuadrantPaint(2, new GradientPaint(3.0f, 4.0f,
0379: Color.red, 5.0f, 6.0f, Color.blue));
0380: assertFalse(plot1.equals(plot2));
0381: plot2.setQuadrantPaint(2, new GradientPaint(3.0f, 4.0f,
0382: Color.red, 5.0f, 6.0f, Color.blue));
0383: assertTrue(plot1.equals(plot2));
0384: plot1.setQuadrantPaint(3, new GradientPaint(4.0f, 5.0f,
0385: Color.red, 6.0f, 7.0f, Color.blue));
0386: assertFalse(plot1.equals(plot2));
0387: plot2.setQuadrantPaint(3, new GradientPaint(4.0f, 5.0f,
0388: Color.red, 6.0f, 7.0f, Color.blue));
0389: assertTrue(plot1.equals(plot2));
0390: }
0391:
0392: /**
0393: * Confirm that basic cloning works.
0394: */
0395: public void testCloning() {
0396: XYPlot p1 = new XYPlot();
0397: XYPlot p2 = null;
0398: try {
0399: p2 = (XYPlot) p1.clone();
0400: } catch (CloneNotSupportedException e) {
0401: e.printStackTrace();
0402: }
0403: assertTrue(p1 != p2);
0404: assertTrue(p1.getClass() == p2.getClass());
0405: assertTrue(p1.equals(p2));
0406: }
0407:
0408: /**
0409: * Tests cloning for a more complex plot.
0410: */
0411: public void testCloning2() {
0412: XYPlot p1 = new XYPlot(null, new NumberAxis("Domain Axis"),
0413: new NumberAxis("Range Axis"),
0414: new StandardXYItemRenderer());
0415: p1.setRangeAxis(1, new NumberAxis("Range Axis 2"));
0416: p1.setRenderer(1, new XYBarRenderer());
0417: XYPlot p2 = null;
0418: try {
0419: p2 = (XYPlot) p1.clone();
0420: } catch (CloneNotSupportedException e) {
0421: e.printStackTrace();
0422: }
0423: assertTrue(p1 != p2);
0424: assertTrue(p1.getClass() == p2.getClass());
0425: assertTrue(p1.equals(p2));
0426: }
0427:
0428: /**
0429: * Confirm that cloning captures the quadrantOrigin field.
0430: */
0431: public void testCloning_QuadrantOrigin() {
0432: XYPlot p1 = new XYPlot();
0433: Point2D p = new Point2D.Double(1.2, 3.4);
0434: p1.setQuadrantOrigin(p);
0435: XYPlot p2 = null;
0436: try {
0437: p2 = (XYPlot) p1.clone();
0438: } catch (CloneNotSupportedException e) {
0439: e.printStackTrace();
0440: }
0441: assertTrue(p1 != p2);
0442: assertTrue(p1.getClass() == p2.getClass());
0443: assertTrue(p1.equals(p2));
0444: assertTrue(p2.getQuadrantOrigin() != p);
0445: }
0446:
0447: /**
0448: * Confirm that cloning captures the quadrantOrigin field.
0449: */
0450: public void testCloning_QuadrantPaint() {
0451: XYPlot p1 = new XYPlot();
0452: p1.setQuadrantPaint(3, new GradientPaint(1.0f, 2.0f, Color.red,
0453: 3.0f, 4.0f, Color.blue));
0454: XYPlot p2 = null;
0455: try {
0456: p2 = (XYPlot) p1.clone();
0457: } catch (CloneNotSupportedException e) {
0458: e.printStackTrace();
0459: }
0460: assertTrue(p1 != p2);
0461: assertTrue(p1.getClass() == p2.getClass());
0462: assertTrue(p1.equals(p2));
0463:
0464: // check for independence
0465: p1.setQuadrantPaint(1, Color.red);
0466: assertFalse(p1.equals(p2));
0467: p2.setQuadrantPaint(1, Color.red);
0468: assertTrue(p1.equals(p2));
0469: }
0470:
0471: /**
0472: * Tests the independence of the clones.
0473: */
0474: public void testCloneIndependence() {
0475: XYPlot p1 = new XYPlot(null, new NumberAxis("Domain Axis"),
0476: new NumberAxis("Range Axis"),
0477: new StandardXYItemRenderer());
0478: p1.setDomainAxis(1, new NumberAxis("Domain Axis 2"));
0479: p1.setDomainAxisLocation(1, AxisLocation.BOTTOM_OR_LEFT);
0480: p1.setRangeAxis(1, new NumberAxis("Range Axis 2"));
0481: p1.setRangeAxisLocation(1, AxisLocation.TOP_OR_RIGHT);
0482: p1.setRenderer(1, new XYBarRenderer());
0483: XYPlot p2 = null;
0484: try {
0485: p2 = (XYPlot) p1.clone();
0486: } catch (CloneNotSupportedException e) {
0487: e.printStackTrace();
0488: System.err.println("Failed to clone.");
0489: }
0490: assertTrue(p1.equals(p2));
0491:
0492: p1.getDomainAxis().setLabel("Label");
0493: assertFalse(p1.equals(p2));
0494: p2.getDomainAxis().setLabel("Label");
0495: assertTrue(p1.equals(p2));
0496:
0497: p1.getDomainAxis(1).setLabel("S1");
0498: assertFalse(p1.equals(p2));
0499: p2.getDomainAxis(1).setLabel("S1");
0500: assertTrue(p1.equals(p2));
0501:
0502: p1.setDomainAxisLocation(1, AxisLocation.TOP_OR_RIGHT);
0503: assertFalse(p1.equals(p2));
0504: p2.setDomainAxisLocation(1, AxisLocation.TOP_OR_RIGHT);
0505: assertTrue(p1.equals(p2));
0506:
0507: p1.mapDatasetToDomainAxis(2, 1);
0508: assertFalse(p1.equals(p2));
0509: p2.mapDatasetToDomainAxis(2, 1);
0510: assertTrue(p1.equals(p2));
0511:
0512: p1.getRangeAxis().setLabel("Label");
0513: assertFalse(p1.equals(p2));
0514: p2.getRangeAxis().setLabel("Label");
0515: assertTrue(p1.equals(p2));
0516:
0517: p1.getRangeAxis(1).setLabel("S1");
0518: assertFalse(p1.equals(p2));
0519: p2.getRangeAxis(1).setLabel("S1");
0520: assertTrue(p1.equals(p2));
0521:
0522: p1.setRangeAxisLocation(1, AxisLocation.TOP_OR_LEFT);
0523: assertFalse(p1.equals(p2));
0524: p2.setRangeAxisLocation(1, AxisLocation.TOP_OR_LEFT);
0525: assertTrue(p1.equals(p2));
0526:
0527: p1.mapDatasetToRangeAxis(2, 1);
0528: assertFalse(p1.equals(p2));
0529: p2.mapDatasetToRangeAxis(2, 1);
0530: assertTrue(p1.equals(p2));
0531:
0532: p1.getRenderer().setOutlinePaint(Color.cyan);
0533: assertFalse(p1.equals(p2));
0534: p2.getRenderer().setOutlinePaint(Color.cyan);
0535: assertTrue(p1.equals(p2));
0536:
0537: p1.getRenderer(1).setOutlinePaint(Color.red);
0538: assertFalse(p1.equals(p2));
0539: p2.getRenderer(1).setOutlinePaint(Color.red);
0540: assertTrue(p1.equals(p2));
0541:
0542: }
0543:
0544: /**
0545: * Setting a null renderer should be allowed, but is generating a null
0546: * pointer exception in 0.9.7.
0547: */
0548: public void testSetNullRenderer() {
0549: boolean failed = false;
0550: try {
0551: XYPlot plot = new XYPlot(null, new NumberAxis("X"),
0552: new NumberAxis("Y"), null);
0553: plot.setRenderer(null);
0554: } catch (Exception e) {
0555: failed = true;
0556: }
0557: assertTrue(!failed);
0558: }
0559:
0560: /**
0561: * Serialize an instance, restore it, and check for equality.
0562: */
0563: public void testSerialization1() {
0564:
0565: XYDataset data = new XYSeriesCollection();
0566: NumberAxis domainAxis = new NumberAxis("Domain");
0567: NumberAxis rangeAxis = new NumberAxis("Range");
0568: StandardXYItemRenderer renderer = new StandardXYItemRenderer();
0569: XYPlot p1 = new XYPlot(data, domainAxis, rangeAxis, renderer);
0570: XYPlot p2 = null;
0571:
0572: try {
0573: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
0574: ObjectOutput out = new ObjectOutputStream(buffer);
0575: out.writeObject(p1);
0576: out.close();
0577:
0578: ObjectInput in = new ObjectInputStream(
0579: new ByteArrayInputStream(buffer.toByteArray()));
0580: p2 = (XYPlot) in.readObject();
0581: in.close();
0582: } catch (Exception e) {
0583: fail(e.toString());
0584: }
0585: assertEquals(p1, p2);
0586:
0587: }
0588:
0589: /**
0590: * Serialize an instance, restore it, and check for equality. This test
0591: * uses a {@link DateAxis} and a {@link StandardXYToolTipGenerator}.
0592: */
0593: public void testSerialization2() {
0594:
0595: IntervalXYDataset data1 = createDataset1();
0596: XYItemRenderer renderer1 = new XYBarRenderer(0.20);
0597: renderer1.setToolTipGenerator(StandardXYToolTipGenerator
0598: .getTimeSeriesInstance());
0599: XYPlot p1 = new XYPlot(data1, new DateAxis("Date"), null,
0600: renderer1);
0601: XYPlot p2 = null;
0602:
0603: try {
0604: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
0605: ObjectOutput out = new ObjectOutputStream(buffer);
0606: out.writeObject(p1);
0607: out.close();
0608:
0609: ObjectInput in = new ObjectInputStream(
0610: new ByteArrayInputStream(buffer.toByteArray()));
0611: p2 = (XYPlot) in.readObject();
0612: in.close();
0613: } catch (Exception e) {
0614: fail(e.toString());
0615: }
0616: assertEquals(p1, p2);
0617:
0618: }
0619:
0620: /**
0621: * Problem to reproduce a bug in serialization. The bug (first reported
0622: * against the {@link org.jfree.chart.plot.CategoryPlot} class) is a null
0623: * pointer exception that occurs when drawing a plot after deserialization.
0624: * It is caused by four temporary storage structures (axesAtTop,
0625: * axesAtBottom, axesAtLeft and axesAtRight - all initialized as empty
0626: * lists in the constructor) not being initialized by the readObject()
0627: * method following deserialization. This test has been written to
0628: * reproduce the bug (now fixed).
0629: */
0630: public void testSerialization3() {
0631:
0632: XYSeriesCollection dataset = new XYSeriesCollection();
0633: JFreeChart chart = ChartFactory.createXYLineChart("Test Chart",
0634: "Domain Axis", "Range Axis", dataset,
0635: PlotOrientation.VERTICAL, true, true, false);
0636: JFreeChart chart2 = null;
0637:
0638: // serialize and deserialize the chart....
0639: try {
0640: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
0641: ObjectOutput out = new ObjectOutputStream(buffer);
0642: out.writeObject(chart);
0643: out.close();
0644:
0645: ObjectInput in = new ObjectInputStream(
0646: new ByteArrayInputStream(buffer.toByteArray()));
0647: chart2 = (JFreeChart) in.readObject();
0648: in.close();
0649: } catch (Exception e) {
0650: fail(e.toString());
0651: }
0652:
0653: assertEquals(chart, chart2);
0654: boolean passed = true;
0655: try {
0656: chart2.createBufferedImage(300, 200);
0657: } catch (Exception e) {
0658: passed = false;
0659: e.printStackTrace();
0660: }
0661: assertTrue(passed);
0662: }
0663:
0664: /**
0665: * A test to reproduce a bug in serialization: the domain and/or range
0666: * markers for a plot are not being serialized.
0667: */
0668: public void testSerialization4() {
0669:
0670: XYSeriesCollection dataset = new XYSeriesCollection();
0671: JFreeChart chart = ChartFactory.createXYLineChart("Test Chart",
0672: "Domain Axis", "Range Axis", dataset,
0673: PlotOrientation.VERTICAL, true, true, false);
0674: XYPlot plot = (XYPlot) chart.getPlot();
0675: plot.addDomainMarker(new ValueMarker(1.0), Layer.FOREGROUND);
0676: plot.addDomainMarker(new IntervalMarker(2.0, 3.0),
0677: Layer.BACKGROUND);
0678: plot.addRangeMarker(new ValueMarker(4.0), Layer.FOREGROUND);
0679: plot.addRangeMarker(new IntervalMarker(5.0, 6.0),
0680: Layer.BACKGROUND);
0681: JFreeChart chart2 = null;
0682:
0683: // serialize and deserialize the chart....
0684: try {
0685: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
0686: ObjectOutput out = new ObjectOutputStream(buffer);
0687: out.writeObject(chart);
0688: out.close();
0689:
0690: ObjectInput in = new ObjectInputStream(
0691: new ByteArrayInputStream(buffer.toByteArray()));
0692: chart2 = (JFreeChart) in.readObject();
0693: in.close();
0694: } catch (Exception e) {
0695: fail(e.toString());
0696: }
0697:
0698: assertEquals(chart, chart2);
0699: boolean passed = true;
0700: try {
0701: chart2.createBufferedImage(300, 200);
0702: } catch (Exception e) {
0703: passed = false;
0704: e.printStackTrace();
0705: }
0706: assertTrue(passed);
0707: }
0708:
0709: /**
0710: * Tests a bug where the plot is no longer registered as a listener
0711: * with the dataset(s) and axes after deserialization. See patch 1209475
0712: * at SourceForge.
0713: */
0714: public void testSerialization5() {
0715: XYSeriesCollection dataset1 = new XYSeriesCollection();
0716: NumberAxis domainAxis1 = new NumberAxis("Domain 1");
0717: NumberAxis rangeAxis1 = new NumberAxis("Range 1");
0718: StandardXYItemRenderer renderer1 = new StandardXYItemRenderer();
0719: XYPlot p1 = new XYPlot(dataset1, domainAxis1, rangeAxis1,
0720: renderer1);
0721: NumberAxis domainAxis2 = new NumberAxis("Domain 2");
0722: NumberAxis rangeAxis2 = new NumberAxis("Range 2");
0723: StandardXYItemRenderer renderer2 = new StandardXYItemRenderer();
0724: XYSeriesCollection dataset2 = new XYSeriesCollection();
0725: p1.setDataset(1, dataset2);
0726: p1.setDomainAxis(1, domainAxis2);
0727: p1.setRangeAxis(1, rangeAxis2);
0728: p1.setRenderer(1, renderer2);
0729: XYPlot p2 = null;
0730: try {
0731: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
0732: ObjectOutput out = new ObjectOutputStream(buffer);
0733: out.writeObject(p1);
0734: out.close();
0735: ObjectInput in = new ObjectInputStream(
0736: new ByteArrayInputStream(buffer.toByteArray()));
0737: p2 = (XYPlot) in.readObject();
0738: in.close();
0739: } catch (Exception e) {
0740: fail(e.toString());
0741: }
0742: assertEquals(p1, p2);
0743:
0744: // now check that all datasets, renderers and axes are being listened
0745: // too...
0746: NumberAxis domainAxisA = (NumberAxis) p2.getDomainAxis(0);
0747: NumberAxis rangeAxisA = (NumberAxis) p2.getRangeAxis(0);
0748: XYSeriesCollection datasetA = (XYSeriesCollection) p2
0749: .getDataset(0);
0750: StandardXYItemRenderer rendererA = (StandardXYItemRenderer) p2
0751: .getRenderer(0);
0752: NumberAxis domainAxisB = (NumberAxis) p2.getDomainAxis(1);
0753: NumberAxis rangeAxisB = (NumberAxis) p2.getRangeAxis(1);
0754: XYSeriesCollection datasetB = (XYSeriesCollection) p2
0755: .getDataset(1);
0756: StandardXYItemRenderer rendererB = (StandardXYItemRenderer) p2
0757: .getRenderer(1);
0758: assertTrue(datasetA.hasListener(p2));
0759: assertTrue(domainAxisA.hasListener(p2));
0760: assertTrue(rangeAxisA.hasListener(p2));
0761: assertTrue(rendererA.hasListener(p2));
0762: assertTrue(datasetB.hasListener(p2));
0763: assertTrue(domainAxisB.hasListener(p2));
0764: assertTrue(rangeAxisB.hasListener(p2));
0765: assertTrue(rendererB.hasListener(p2));
0766: }
0767:
0768: /**
0769: * Some checks for the getRendererForDataset() method.
0770: */
0771: public void testGetRendererForDataset() {
0772: XYDataset d0 = new XYSeriesCollection();
0773: XYDataset d1 = new XYSeriesCollection();
0774: XYDataset d2 = new XYSeriesCollection();
0775: XYDataset d3 = new XYSeriesCollection(); // not used by plot
0776: XYItemRenderer r0 = new XYLineAndShapeRenderer();
0777: XYItemRenderer r2 = new XYLineAndShapeRenderer();
0778: XYPlot plot = new XYPlot();
0779: plot.setDataset(0, d0);
0780: plot.setDataset(1, d1);
0781: plot.setDataset(2, d2);
0782: plot.setRenderer(0, r0);
0783: // no renderer 1
0784: plot.setRenderer(2, r2);
0785: assertEquals(r0, plot.getRendererForDataset(d0));
0786: assertEquals(r0, plot.getRendererForDataset(d1));
0787: assertEquals(r2, plot.getRendererForDataset(d2));
0788: assertEquals(null, plot.getRendererForDataset(d3));
0789: assertEquals(null, plot.getRendererForDataset(null));
0790: }
0791:
0792: /**
0793: * Some checks for the getLegendItems() method.
0794: */
0795: public void testGetLegendItems() {
0796: // check the case where there is a secondary dataset that doesn't
0797: // have a renderer (i.e. falls back to renderer 0)
0798: XYDataset d0 = createDataset1();
0799: XYDataset d1 = createDataset2();
0800: XYItemRenderer r0 = new XYLineAndShapeRenderer();
0801: XYPlot plot = new XYPlot();
0802: plot.setDataset(0, d0);
0803: plot.setDataset(1, d1);
0804: plot.setRenderer(0, r0);
0805: LegendItemCollection items = plot.getLegendItems();
0806: assertEquals(2, items.getItemCount());
0807: }
0808:
0809: /**
0810: * Creates a sample dataset.
0811: *
0812: * @return Series 1.
0813: */
0814: private IntervalXYDataset createDataset1() {
0815:
0816: // create dataset 1...
0817: TimeSeries series1 = new TimeSeries("Series 1", Day.class);
0818: series1.add(new Day(1, MonthConstants.MARCH, 2002), 12353.3);
0819: series1.add(new Day(2, MonthConstants.MARCH, 2002), 13734.4);
0820: series1.add(new Day(3, MonthConstants.MARCH, 2002), 14525.3);
0821: series1.add(new Day(4, MonthConstants.MARCH, 2002), 13984.3);
0822: series1.add(new Day(5, MonthConstants.MARCH, 2002), 12999.4);
0823: series1.add(new Day(6, MonthConstants.MARCH, 2002), 14274.3);
0824: series1.add(new Day(7, MonthConstants.MARCH, 2002), 15943.5);
0825: series1.add(new Day(8, MonthConstants.MARCH, 2002), 14845.3);
0826: series1.add(new Day(9, MonthConstants.MARCH, 2002), 14645.4);
0827: series1.add(new Day(10, MonthConstants.MARCH, 2002), 16234.6);
0828: series1.add(new Day(11, MonthConstants.MARCH, 2002), 17232.3);
0829: series1.add(new Day(12, MonthConstants.MARCH, 2002), 14232.2);
0830: series1.add(new Day(13, MonthConstants.MARCH, 2002), 13102.2);
0831: series1.add(new Day(14, MonthConstants.MARCH, 2002), 14230.2);
0832: series1.add(new Day(15, MonthConstants.MARCH, 2002), 11235.2);
0833:
0834: TimeSeriesCollection collection = new TimeSeriesCollection(
0835: series1);
0836: return collection;
0837:
0838: }
0839:
0840: /**
0841: * Creates a sample dataset.
0842: *
0843: * @return A sample dataset.
0844: */
0845: private XYDataset createDataset2() {
0846: // create dataset 1...
0847: XYSeries series = new XYSeries("Series 2");
0848: XYSeriesCollection collection = new XYSeriesCollection(series);
0849: return collection;
0850:
0851: }
0852:
0853: /**
0854: * A test for a bug where setting the renderer doesn't register the plot
0855: * as a RendererChangeListener.
0856: */
0857: public void testSetRenderer() {
0858: XYPlot plot = new XYPlot();
0859: XYItemRenderer renderer = new XYLineAndShapeRenderer();
0860: plot.setRenderer(renderer);
0861: // now make a change to the renderer and see if it triggers a plot
0862: // change event...
0863: MyPlotChangeListener listener = new MyPlotChangeListener();
0864: plot.addChangeListener(listener);
0865: renderer.setSeriesPaint(0, Color.black);
0866: assertTrue(listener.getEvent() != null);
0867: }
0868:
0869: /**
0870: * Some checks for the removeAnnotation() method.
0871: */
0872: public void testRemoveAnnotation() {
0873: XYPlot plot = new XYPlot();
0874: XYTextAnnotation a1 = new XYTextAnnotation("X", 1.0, 2.0);
0875: XYTextAnnotation a2 = new XYTextAnnotation("X", 3.0, 4.0);
0876: XYTextAnnotation a3 = new XYTextAnnotation("X", 1.0, 2.0);
0877: plot.addAnnotation(a1);
0878: plot.addAnnotation(a2);
0879: plot.addAnnotation(a3);
0880: plot.removeAnnotation(a2);
0881: XYTextAnnotation x = (XYTextAnnotation) plot.getAnnotations()
0882: .get(0);
0883: assertEquals(x, a1);
0884:
0885: // now remove a3, but since a3.equals(a1), this will in fact remove
0886: // a1...
0887: assertTrue(a1.equals(a3));
0888: plot.removeAnnotation(a3); // actually removes a1
0889: x = (XYTextAnnotation) plot.getAnnotations().get(0);
0890: assertEquals(x, a3);
0891: }
0892:
0893: /**
0894: * Some tests for the addDomainMarker() method(s).
0895: */
0896: public void testAddDomainMarker() {
0897: XYPlot plot = new XYPlot();
0898: Marker m = new ValueMarker(1.0);
0899: plot.addDomainMarker(m);
0900: List listeners = Arrays.asList(m
0901: .getListeners(MarkerChangeListener.class));
0902: assertTrue(listeners.contains(plot));
0903: plot.clearDomainMarkers();
0904: listeners = Arrays.asList(m
0905: .getListeners(MarkerChangeListener.class));
0906: assertFalse(listeners.contains(plot));
0907: }
0908:
0909: /**
0910: * Some tests for the addRangeMarker() method(s).
0911: */
0912: public void testAddRangeMarker() {
0913: XYPlot plot = new XYPlot();
0914: Marker m = new ValueMarker(1.0);
0915: plot.addRangeMarker(m);
0916: List listeners = Arrays.asList(m
0917: .getListeners(MarkerChangeListener.class));
0918: assertTrue(listeners.contains(plot));
0919: plot.clearRangeMarkers();
0920: listeners = Arrays.asList(m
0921: .getListeners(MarkerChangeListener.class));
0922: assertFalse(listeners.contains(plot));
0923: }
0924:
0925: /**
0926: * A test for bug 1654215 (where a renderer is added to the plot without
0927: * a corresponding dataset and it throws an exception at drawing time).
0928: */
0929: public void test1654215() {
0930: DefaultXYDataset dataset = new DefaultXYDataset();
0931: JFreeChart chart = ChartFactory.createXYLineChart("Title", "X",
0932: "Y", dataset, PlotOrientation.VERTICAL, true, false,
0933: false);
0934: XYPlot plot = (XYPlot) chart.getPlot();
0935: plot.setRenderer(1, new XYLineAndShapeRenderer());
0936: boolean success = false;
0937: try {
0938: BufferedImage image = new BufferedImage(200, 100,
0939: BufferedImage.TYPE_INT_RGB);
0940: Graphics2D g2 = image.createGraphics();
0941: chart.draw(g2, new Rectangle2D.Double(0, 0, 200, 100),
0942: null, null);
0943: g2.dispose();
0944: success = true;
0945: } catch (Exception e) {
0946: e.printStackTrace();
0947: success = false;
0948: }
0949: assertTrue(success);
0950: }
0951:
0952: /**
0953: * A test for drawing range grid lines when there is no primary renderer.
0954: * In 1.0.4, this is throwing a NullPointerException.
0955: */
0956: public void testDrawRangeGridlines() {
0957: DefaultXYDataset dataset = new DefaultXYDataset();
0958: JFreeChart chart = ChartFactory.createXYLineChart("Title", "X",
0959: "Y", dataset, PlotOrientation.VERTICAL, true, false,
0960: false);
0961: XYPlot plot = (XYPlot) chart.getPlot();
0962: plot.setRenderer(null);
0963: boolean success = false;
0964: try {
0965: BufferedImage image = new BufferedImage(200, 100,
0966: BufferedImage.TYPE_INT_RGB);
0967: Graphics2D g2 = image.createGraphics();
0968: chart.draw(g2, new Rectangle2D.Double(0, 0, 200, 100),
0969: null, null);
0970: g2.dispose();
0971: success = true;
0972: } catch (Exception e) {
0973: e.printStackTrace();
0974: success = false;
0975: }
0976: assertTrue(success);
0977: }
0978:
0979: /**
0980: * A test for drawing a plot where a series has zero items. With
0981: * JFreeChart 1.0.5+cvs this was throwing an exception at one point.
0982: */
0983: public void testDrawSeriesWithZeroItems() {
0984: DefaultXYDataset dataset = new DefaultXYDataset();
0985: dataset.addSeries("Series 1", new double[][] { { 1.0, 2.0 },
0986: { 3.0, 4.0 } });
0987: dataset.addSeries("Series 2", new double[][] { {}, {} });
0988: JFreeChart chart = ChartFactory.createXYLineChart("Title", "X",
0989: "Y", dataset, PlotOrientation.VERTICAL, true, false,
0990: false);
0991: boolean success = false;
0992: try {
0993: BufferedImage image = new BufferedImage(200, 100,
0994: BufferedImage.TYPE_INT_RGB);
0995: Graphics2D g2 = image.createGraphics();
0996: chart.draw(g2, new Rectangle2D.Double(0, 0, 200, 100),
0997: null, null);
0998: g2.dispose();
0999: success = true;
1000: } catch (Exception e) {
1001: e.printStackTrace();
1002: success = false;
1003: }
1004: assertTrue(success);
1005: }
1006:
1007: }
|