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: * AreaXYRenderer2Tests.java
029: * -------------------------
030: * (C) Copyright 2005-2007, by Object Refinery Limited.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: XYAreaRenderer2Tests.java,v 1.1.2.3 2007/05/17 16:23:31 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 24-May-2005 : Version 1 (DG);
040: * 30-Nov-2006 : Extended testEquals() and testCloning() (DG);
041: * 17-May-2007 : Added testGetLegendItemSeriesIndex() (DG);
042: *
043: */
044:
045: package org.jfree.chart.renderer.xy.junit;
046:
047: import java.awt.Rectangle;
048: import java.io.ByteArrayInputStream;
049: import java.io.ByteArrayOutputStream;
050: import java.io.ObjectInput;
051: import java.io.ObjectInputStream;
052: import java.io.ObjectOutput;
053: import java.io.ObjectOutputStream;
054:
055: import junit.framework.Test;
056: import junit.framework.TestCase;
057: import junit.framework.TestSuite;
058:
059: import org.jfree.chart.JFreeChart;
060: import org.jfree.chart.LegendItem;
061: import org.jfree.chart.axis.NumberAxis;
062: import org.jfree.chart.plot.XYPlot;
063: import org.jfree.chart.renderer.xy.XYAreaRenderer2;
064: import org.jfree.data.xy.DefaultTableXYDataset;
065: import org.jfree.data.xy.XYSeries;
066: import org.jfree.data.xy.XYSeriesCollection;
067:
068: /**
069: * Tests for the {@link XYAreaRenderer2} class.
070: */
071: public class XYAreaRenderer2Tests extends TestCase {
072:
073: /**
074: * Returns the tests as a test suite.
075: *
076: * @return The test suite.
077: */
078: public static Test suite() {
079: return new TestSuite(XYAreaRenderer2Tests.class);
080: }
081:
082: /**
083: * Constructs a new set of tests.
084: *
085: * @param name the name of the tests.
086: */
087: public XYAreaRenderer2Tests(String name) {
088: super (name);
089: }
090:
091: /**
092: * Check that the equals() method distinguishes all fields.
093: */
094: public void testEquals() {
095: XYAreaRenderer2 r1 = new XYAreaRenderer2();
096: XYAreaRenderer2 r2 = new XYAreaRenderer2();
097: assertEquals(r1, r2);
098:
099: r1.setOutline(!r1.isOutline());
100: assertFalse(r1.equals(r2));
101: r2.setOutline(r1.isOutline());
102: assertTrue(r1.equals(r2));
103:
104: r1.setLegendArea(new Rectangle(1, 2, 3, 4));
105: assertFalse(r1.equals(r2));
106: r2.setLegendArea(new Rectangle(1, 2, 3, 4));
107: assertTrue(r1.equals(r2));
108: }
109:
110: /**
111: * Two objects that are equal are required to return the same hashCode.
112: */
113: public void testHashcode() {
114: XYAreaRenderer2 r1 = new XYAreaRenderer2();
115: XYAreaRenderer2 r2 = new XYAreaRenderer2();
116: assertTrue(r1.equals(r2));
117: int h1 = r1.hashCode();
118: int h2 = r2.hashCode();
119: assertEquals(h1, h2);
120: }
121:
122: /**
123: * Confirm that cloning works.
124: */
125: public void testCloning() {
126: XYAreaRenderer2 r1 = new XYAreaRenderer2();
127: Rectangle rect = new Rectangle(1, 2, 3, 4);
128: r1.setLegendArea(rect);
129: XYAreaRenderer2 r2 = null;
130: try {
131: r2 = (XYAreaRenderer2) r1.clone();
132: } catch (CloneNotSupportedException e) {
133: e.printStackTrace();
134: }
135: assertTrue(r1 != r2);
136: assertTrue(r1.getClass() == r2.getClass());
137: assertTrue(r1.equals(r2));
138:
139: // check independence
140: rect.setBounds(99, 99, 99, 99);
141: assertFalse(r1.equals(r2));
142: }
143:
144: /**
145: * Serialize an instance, restore it, and check for equality.
146: */
147: public void testSerialization() {
148:
149: XYAreaRenderer2 r1 = new XYAreaRenderer2();
150: XYAreaRenderer2 r2 = null;
151:
152: try {
153: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
154: ObjectOutput out = new ObjectOutputStream(buffer);
155: out.writeObject(r1);
156: out.close();
157:
158: ObjectInput in = new ObjectInputStream(
159: new ByteArrayInputStream(buffer.toByteArray()));
160: r2 = (XYAreaRenderer2) in.readObject();
161: in.close();
162: } catch (Exception e) {
163: fail(e.toString());
164: }
165: assertEquals(r1, r2);
166:
167: }
168:
169: /**
170: * Draws the chart with a <code>null</code> info object to make sure that
171: * no exceptions are thrown (particularly by code in the renderer).
172: */
173: public void testDrawWithNullInfo() {
174: boolean success = false;
175: try {
176: DefaultTableXYDataset dataset = new DefaultTableXYDataset();
177:
178: XYSeries s1 = new XYSeries("Series 1", true, false);
179: s1.add(5.0, 5.0);
180: s1.add(10.0, 15.5);
181: s1.add(15.0, 9.5);
182: s1.add(20.0, 7.5);
183: dataset.addSeries(s1);
184:
185: XYSeries s2 = new XYSeries("Series 2", true, false);
186: s2.add(5.0, 5.0);
187: s2.add(10.0, 15.5);
188: s2.add(15.0, 9.5);
189: s2.add(20.0, 3.5);
190: dataset.addSeries(s2);
191: XYPlot plot = new XYPlot(dataset, new NumberAxis("X"),
192: new NumberAxis("Y"), new XYAreaRenderer2());
193: JFreeChart chart = new JFreeChart(plot);
194: /* BufferedImage image = */chart.createBufferedImage(300,
195: 200, null);
196: success = true;
197: } catch (NullPointerException e) {
198: e.printStackTrace();
199: success = false;
200: }
201: assertTrue(success);
202: }
203:
204: /**
205: * A check for the datasetIndex and seriesIndex fields in the LegendItem
206: * returned by the getLegendItem() method.
207: */
208: public void testGetLegendItemSeriesIndex() {
209: XYSeriesCollection d1 = new XYSeriesCollection();
210: XYSeries s1 = new XYSeries("S1");
211: s1.add(1.0, 1.1);
212: XYSeries s2 = new XYSeries("S2");
213: s2.add(1.0, 1.1);
214: d1.addSeries(s1);
215: d1.addSeries(s2);
216:
217: XYSeriesCollection d2 = new XYSeriesCollection();
218: XYSeries s3 = new XYSeries("S3");
219: s3.add(1.0, 1.1);
220: XYSeries s4 = new XYSeries("S4");
221: s4.add(1.0, 1.1);
222: XYSeries s5 = new XYSeries("S5");
223: s5.add(1.0, 1.1);
224: d2.addSeries(s3);
225: d2.addSeries(s4);
226: d2.addSeries(s5);
227:
228: XYAreaRenderer2 r = new XYAreaRenderer2();
229: XYPlot plot = new XYPlot(d1, new NumberAxis("x"),
230: new NumberAxis("y"), r);
231: plot.setDataset(1, d2);
232: /*JFreeChart chart =*/new JFreeChart(plot);
233: LegendItem li = r.getLegendItem(1, 2);
234: assertEquals("S5", li.getLabel());
235: assertEquals(1, li.getDatasetIndex());
236: assertEquals(2, li.getSeriesIndex());
237: }
238:
239: }
|