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