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: * XYBubbleRendererTests.java
029: * --------------------------
030: * (C) Copyright 2003-2007, by Object Refinery Limited and Contributors.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: XYBubbleRendererTests.java,v 1.1.2.3 2007/05/17 16:23:31 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 25-Mar-2003 : Version 1 (DG);
040: * 24-Jan-2007 : Added more checks to testEquals() (DG);
041: * 17-May-2007 : Added testGetLegendItemSeriesIndex() (DG);
042: *
043: */
044:
045: package org.jfree.chart.renderer.xy.junit;
046:
047: import java.io.ByteArrayInputStream;
048: import java.io.ByteArrayOutputStream;
049: import java.io.ObjectInput;
050: import java.io.ObjectInputStream;
051: import java.io.ObjectOutput;
052: import java.io.ObjectOutputStream;
053:
054: import junit.framework.Test;
055: import junit.framework.TestCase;
056: import junit.framework.TestSuite;
057:
058: import org.jfree.chart.JFreeChart;
059: import org.jfree.chart.LegendItem;
060: import org.jfree.chart.axis.NumberAxis;
061: import org.jfree.chart.plot.XYPlot;
062: import org.jfree.chart.renderer.xy.XYBubbleRenderer;
063: import org.jfree.data.xy.DefaultXYZDataset;
064:
065: /**
066: * Tests for the {@link XYBubbleRenderer} class.
067: */
068: public class XYBubbleRendererTests extends TestCase {
069:
070: /**
071: * Returns the tests as a test suite.
072: *
073: * @return The test suite.
074: */
075: public static Test suite() {
076: return new TestSuite(XYBubbleRendererTests.class);
077: }
078:
079: /**
080: * Constructs a new set of tests.
081: *
082: * @param name the name of the tests.
083: */
084: public XYBubbleRendererTests(String name) {
085: super (name);
086: }
087:
088: /**
089: * Check that the equals() method distinguishes all fields.
090: */
091: public void testEquals() {
092: XYBubbleRenderer r1 = new XYBubbleRenderer();
093: XYBubbleRenderer r2 = new XYBubbleRenderer();
094: assertEquals(r1, r2);
095:
096: r1 = new XYBubbleRenderer(XYBubbleRenderer.SCALE_ON_RANGE_AXIS);
097: assertFalse(r1.equals(r2));
098: r2 = new XYBubbleRenderer(XYBubbleRenderer.SCALE_ON_RANGE_AXIS);
099: assertTrue(r1.equals(r2));
100: }
101:
102: /**
103: * Two objects that are equal are required to return the same hashCode.
104: */
105: public void testHashcode() {
106: XYBubbleRenderer r1 = new XYBubbleRenderer();
107: XYBubbleRenderer r2 = new XYBubbleRenderer();
108: assertTrue(r1.equals(r2));
109: int h1 = r1.hashCode();
110: int h2 = r2.hashCode();
111: assertEquals(h1, h2);
112: }
113:
114: /**
115: * Confirm that cloning works.
116: */
117: public void testCloning() {
118: XYBubbleRenderer r1 = new XYBubbleRenderer();
119: XYBubbleRenderer r2 = null;
120: try {
121: r2 = (XYBubbleRenderer) r1.clone();
122: } catch (CloneNotSupportedException e) {
123: e.printStackTrace();
124: }
125: assertTrue(r1 != r2);
126: assertTrue(r1.getClass() == r2.getClass());
127: assertTrue(r1.equals(r2));
128: }
129:
130: /**
131: * Serialize an instance, restore it, and check for equality.
132: */
133: public void testSerialization() {
134: XYBubbleRenderer r1 = new XYBubbleRenderer();
135: XYBubbleRenderer r2 = null;
136:
137: try {
138: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
139: ObjectOutput out = new ObjectOutputStream(buffer);
140: out.writeObject(r1);
141: out.close();
142:
143: ObjectInput in = new ObjectInputStream(
144: new ByteArrayInputStream(buffer.toByteArray()));
145: r2 = (XYBubbleRenderer) in.readObject();
146: in.close();
147: } catch (Exception e) {
148: e.printStackTrace();
149: }
150: assertEquals(r1, r2);
151: }
152:
153: /**
154: * A check for the datasetIndex and seriesIndex fields in the LegendItem
155: * returned by the getLegendItem() method.
156: */
157: public void testGetLegendItemSeriesIndex() {
158: DefaultXYZDataset d1 = new DefaultXYZDataset();
159: double[] x = { 2.1, 2.3, 2.3, 2.2, 2.2, 1.8, 1.8, 1.9, 2.3, 3.8 };
160: double[] y = { 14.1, 11.1, 10.0, 8.8, 8.7, 8.4, 5.4, 4.1, 4.1,
161: 25 };
162: double[] z = { 2.4, 2.7, 2.7, 2.2, 2.2, 2.2, 2.1, 2.2, 1.6, 4 };
163: double[][] s1 = new double[][] { x, y, z };
164: d1.addSeries("S1", s1);
165: x = new double[] { 2.1 };
166: y = new double[] { 14.1 };
167: z = new double[] { 2.4 };
168: double[][] s2 = new double[][] { x, y, z };
169: d1.addSeries("S2", s2);
170:
171: DefaultXYZDataset d2 = new DefaultXYZDataset();
172: x = new double[] { 2.1 };
173: y = new double[] { 14.1 };
174: z = new double[] { 2.4 };
175: double[][] s3 = new double[][] { x, y, z };
176: d2.addSeries("S3", s3);
177: x = new double[] { 2.1 };
178: y = new double[] { 14.1 };
179: z = new double[] { 2.4 };
180: double[][] s4 = new double[][] { x, y, z };
181: d2.addSeries("S4", s4);
182: x = new double[] { 2.1 };
183: y = new double[] { 14.1 };
184: z = new double[] { 2.4 };
185: double[][] s5 = new double[][] { x, y, z };
186: d2.addSeries("S5", s5);
187:
188: XYBubbleRenderer r = new XYBubbleRenderer();
189: XYPlot plot = new XYPlot(d1, new NumberAxis("x"),
190: new NumberAxis("y"), r);
191: plot.setDataset(1, d2);
192: /*JFreeChart chart =*/new JFreeChart(plot);
193: LegendItem li = r.getLegendItem(1, 2);
194: assertEquals("S5", li.getLabel());
195: assertEquals(1, li.getDatasetIndex());
196: assertEquals(2, li.getSeriesIndex());
197: }
198:
199: }
|