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: * XYAreaRendererTests.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: XYAreaRendererTests.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: * 22-Oct-2003 : Added hashCode test (DG);
041: * 14-Feb-2007 : Updated testCloning() (DG);
042: * 17-May-2007 : Added testGetLegendItemSeriesIndex() (DG);
043: *
044: */
045:
046: package org.jfree.chart.renderer.xy.junit;
047:
048: import java.awt.geom.Rectangle2D;
049: import java.io.ByteArrayInputStream;
050: import java.io.ByteArrayOutputStream;
051: import java.io.ObjectInput;
052: import java.io.ObjectInputStream;
053: import java.io.ObjectOutput;
054: import java.io.ObjectOutputStream;
055:
056: import junit.framework.Test;
057: import junit.framework.TestCase;
058: import junit.framework.TestSuite;
059:
060: import org.jfree.chart.JFreeChart;
061: import org.jfree.chart.LegendItem;
062: import org.jfree.chart.axis.NumberAxis;
063: import org.jfree.chart.plot.XYPlot;
064: import org.jfree.chart.renderer.xy.XYAreaRenderer;
065: import org.jfree.data.xy.DefaultTableXYDataset;
066: import org.jfree.data.xy.XYSeries;
067: import org.jfree.data.xy.XYSeriesCollection;
068:
069: /**
070: * Tests for the {@link XYAreaRenderer} class.
071: */
072: public class XYAreaRendererTests extends TestCase {
073:
074: /**
075: * Returns the tests as a test suite.
076: *
077: * @return The test suite.
078: */
079: public static Test suite() {
080: return new TestSuite(XYAreaRendererTests.class);
081: }
082:
083: /**
084: * Constructs a new set of tests.
085: *
086: * @param name the name of the tests.
087: */
088: public XYAreaRendererTests(String name) {
089: super (name);
090: }
091:
092: /**
093: * Check that the equals() method distinguishes all fields.
094: */
095: public void testEquals() {
096: XYAreaRenderer r1 = new XYAreaRenderer();
097: XYAreaRenderer r2 = new XYAreaRenderer();
098: assertEquals(r1, r2);
099:
100: r1 = new XYAreaRenderer(XYAreaRenderer.AREA_AND_SHAPES);
101: assertFalse(r1.equals(r2));
102: r2 = new XYAreaRenderer(XYAreaRenderer.AREA_AND_SHAPES);
103: assertTrue(r1.equals(r2));
104:
105: r1 = new XYAreaRenderer(XYAreaRenderer.AREA);
106: assertFalse(r1.equals(r2));
107: r2 = new XYAreaRenderer(XYAreaRenderer.AREA);
108: assertTrue(r1.equals(r2));
109:
110: r1 = new XYAreaRenderer(XYAreaRenderer.LINES);
111: assertFalse(r1.equals(r2));
112: r2 = new XYAreaRenderer(XYAreaRenderer.LINES);
113: assertTrue(r1.equals(r2));
114:
115: r1 = new XYAreaRenderer(XYAreaRenderer.SHAPES);
116: assertFalse(r1.equals(r2));
117: r2 = new XYAreaRenderer(XYAreaRenderer.SHAPES);
118: assertTrue(r1.equals(r2));
119:
120: r1 = new XYAreaRenderer(XYAreaRenderer.SHAPES_AND_LINES);
121: assertFalse(r1.equals(r2));
122: r2 = new XYAreaRenderer(XYAreaRenderer.SHAPES_AND_LINES);
123: assertTrue(r1.equals(r2));
124:
125: r1.setOutline(true);
126: assertFalse(r1.equals(r2));
127: r2.setOutline(true);
128: assertTrue(r1.equals(r2));
129:
130: r1.setLegendArea(new Rectangle2D.Double(1.0, 2.0, 3.0, 4.0));
131: assertFalse(r1.equals(r2));
132: r2.setLegendArea(new Rectangle2D.Double(1.0, 2.0, 3.0, 4.0));
133: assertTrue(r1.equals(r2));
134: }
135:
136: /**
137: * Two objects that are equal are required to return the same hashCode.
138: */
139: public void testHashcode() {
140: XYAreaRenderer r1 = new XYAreaRenderer();
141: XYAreaRenderer r2 = new XYAreaRenderer();
142: assertTrue(r1.equals(r2));
143: int h1 = r1.hashCode();
144: int h2 = r2.hashCode();
145: assertEquals(h1, h2);
146: }
147:
148: /**
149: * Confirm that cloning works.
150: */
151: public void testCloning() {
152: XYAreaRenderer r1 = new XYAreaRenderer();
153: Rectangle2D rect1 = new Rectangle2D.Double(1.0, 2.0, 3.0, 4.0);
154: r1.setLegendArea(rect1);
155: XYAreaRenderer r2 = null;
156: try {
157: r2 = (XYAreaRenderer) r1.clone();
158: } catch (CloneNotSupportedException e) {
159: e.printStackTrace();
160: }
161: assertTrue(r1 != r2);
162: assertTrue(r1.getClass() == r2.getClass());
163: assertTrue(r1.equals(r2));
164:
165: // check independence
166: rect1.setRect(4.0, 3.0, 2.0, 1.0);
167: assertFalse(r1.equals(r2));
168: r2.setLegendArea(new Rectangle2D.Double(4.0, 3.0, 2.0, 1.0));
169: assertTrue(r1.equals(r2));
170: }
171:
172: /**
173: * Serialize an instance, restore it, and check for equality.
174: */
175: public void testSerialization() {
176:
177: XYAreaRenderer r1 = new XYAreaRenderer();
178: XYAreaRenderer r2 = null;
179:
180: try {
181: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
182: ObjectOutput out = new ObjectOutputStream(buffer);
183: out.writeObject(r1);
184: out.close();
185:
186: ObjectInput in = new ObjectInputStream(
187: new ByteArrayInputStream(buffer.toByteArray()));
188: r2 = (XYAreaRenderer) in.readObject();
189: in.close();
190: } catch (Exception e) {
191: System.out.println(e.toString());
192: }
193: assertEquals(r1, r2);
194:
195: }
196:
197: /**
198: * Draws the chart with a <code>null</code> info object to make sure that
199: * no exceptions are thrown (particularly by code in the renderer).
200: */
201: public void testDrawWithNullInfo() {
202: boolean success = false;
203: try {
204: DefaultTableXYDataset dataset = new DefaultTableXYDataset();
205:
206: XYSeries s1 = new XYSeries("Series 1", true, false);
207: s1.add(5.0, 5.0);
208: s1.add(10.0, 15.5);
209: s1.add(15.0, 9.5);
210: s1.add(20.0, 7.5);
211: dataset.addSeries(s1);
212:
213: XYSeries s2 = new XYSeries("Series 2", true, false);
214: s2.add(5.0, 5.0);
215: s2.add(10.0, 15.5);
216: s2.add(15.0, 9.5);
217: s2.add(20.0, 3.5);
218: dataset.addSeries(s2);
219: XYPlot plot = new XYPlot(dataset, new NumberAxis("X"),
220: new NumberAxis("Y"), new XYAreaRenderer());
221: JFreeChart chart = new JFreeChart(plot);
222: /* BufferedImage image = */chart.createBufferedImage(300,
223: 200, null);
224: success = true;
225: } catch (NullPointerException e) {
226: e.printStackTrace();
227: success = false;
228: }
229: assertTrue(success);
230: }
231:
232: /**
233: * A check for the datasetIndex and seriesIndex fields in the LegendItem
234: * returned by the getLegendItem() method.
235: */
236: public void testGetLegendItemSeriesIndex() {
237: XYSeriesCollection d1 = new XYSeriesCollection();
238: XYSeries s1 = new XYSeries("S1");
239: s1.add(1.0, 1.1);
240: XYSeries s2 = new XYSeries("S2");
241: s2.add(1.0, 1.1);
242: d1.addSeries(s1);
243: d1.addSeries(s2);
244:
245: XYSeriesCollection d2 = new XYSeriesCollection();
246: XYSeries s3 = new XYSeries("S3");
247: s3.add(1.0, 1.1);
248: XYSeries s4 = new XYSeries("S4");
249: s4.add(1.0, 1.1);
250: XYSeries s5 = new XYSeries("S5");
251: s5.add(1.0, 1.1);
252: d2.addSeries(s3);
253: d2.addSeries(s4);
254: d2.addSeries(s5);
255:
256: XYAreaRenderer r = new XYAreaRenderer();
257: XYPlot plot = new XYPlot(d1, new NumberAxis("x"),
258: new NumberAxis("y"), r);
259: plot.setDataset(1, d2);
260: /*JFreeChart chart =*/new JFreeChart(plot);
261: LegendItem li = r.getLegendItem(1, 2);
262: assertEquals("S5", li.getLabel());
263: assertEquals(1, li.getDatasetIndex());
264: assertEquals(2, li.getSeriesIndex());
265: }
266:
267: }
|