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: * StackedXYAreaRendererTests.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: StackedXYAreaRendererTests.java,v 1.1.2.3 2007/03/22 16:58:37 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 25-Mar-2003 : Version 1 (DG);
040: * 06-Jan-2005 : Renamed StackedAreaXYRendererTests -->
041: * StackedXYAreaRendererTests, improved testEquals() method,
042: * added check for auto range calculation (DG);
043: * 10-Nov-2006 : Added testBug1593156() (DG);
044: *
045: */
046:
047: package org.jfree.chart.renderer.xy.junit;
048:
049: import java.awt.BasicStroke;
050: import java.awt.Color;
051: import java.awt.GradientPaint;
052: import java.awt.Stroke;
053: import java.io.ByteArrayInputStream;
054: import java.io.ByteArrayOutputStream;
055: import java.io.ObjectInput;
056: import java.io.ObjectInputStream;
057: import java.io.ObjectOutput;
058: import java.io.ObjectOutputStream;
059:
060: import junit.framework.Test;
061: import junit.framework.TestCase;
062: import junit.framework.TestSuite;
063:
064: import org.jfree.chart.ChartFactory;
065: import org.jfree.chart.JFreeChart;
066: import org.jfree.chart.axis.NumberAxis;
067: import org.jfree.chart.plot.PlotOrientation;
068: import org.jfree.chart.plot.XYPlot;
069: import org.jfree.chart.renderer.xy.StackedXYAreaRenderer;
070: import org.jfree.chart.renderer.xy.XYAreaRenderer;
071: import org.jfree.data.Range;
072: import org.jfree.data.xy.DefaultTableXYDataset;
073: import org.jfree.data.xy.TableXYDataset;
074: import org.jfree.data.xy.XYSeries;
075:
076: /**
077: * Tests for the {@link StackedXYAreaRenderer} class.
078: */
079: public class StackedXYAreaRendererTests extends TestCase {
080:
081: /**
082: * Returns the tests as a test suite.
083: *
084: * @return The test suite.
085: */
086: public static Test suite() {
087: return new TestSuite(StackedXYAreaRendererTests.class);
088: }
089:
090: /**
091: * Constructs a new set of tests.
092: *
093: * @param name the name of the tests.
094: */
095: public StackedXYAreaRendererTests(String name) {
096: super (name);
097: }
098:
099: /**
100: * Test that the equals() method distinguishes all fields.
101: */
102: public void testEquals() {
103: StackedXYAreaRenderer r1 = new StackedXYAreaRenderer();
104: StackedXYAreaRenderer r2 = new StackedXYAreaRenderer();
105: assertEquals(r1, r2);
106: assertEquals(r2, r1);
107:
108: r1.setShapePaint(new GradientPaint(1.0f, 2.0f, Color.yellow,
109: 3.0f, 4.0f, Color.green));
110: assertFalse(r1.equals(r2));
111: r2.setShapePaint(new GradientPaint(1.0f, 2.0f, Color.yellow,
112: 3.0f, 4.0f, Color.green));
113: assertTrue(r1.equals(r2));
114:
115: Stroke s = new BasicStroke(1.23f);
116: r1.setShapeStroke(s);
117: assertFalse(r1.equals(r2));
118: r2.setShapeStroke(s);
119: assertTrue(r1.equals(r2));
120: }
121:
122: /**
123: * Two objects that are equal are required to return the same hashCode.
124: */
125: public void testHashcode() {
126: StackedXYAreaRenderer r1 = new StackedXYAreaRenderer();
127: StackedXYAreaRenderer r2 = new StackedXYAreaRenderer();
128: assertTrue(r1.equals(r2));
129: int h1 = r1.hashCode();
130: int h2 = r2.hashCode();
131: assertEquals(h1, h2);
132: }
133:
134: /**
135: * Confirm that cloning works.
136: */
137: public void testCloning() {
138: StackedXYAreaRenderer r1 = new StackedXYAreaRenderer();
139: StackedXYAreaRenderer r2 = null;
140: try {
141: r2 = (StackedXYAreaRenderer) r1.clone();
142: } catch (CloneNotSupportedException e) {
143: e.printStackTrace();
144: }
145: assertTrue(r1 != r2);
146: assertTrue(r1.getClass() == r2.getClass());
147: assertTrue(r1.equals(r2));
148: }
149:
150: /**
151: * Serialize an instance, restore it, and check for equality.
152: */
153: public void testSerialization() {
154: StackedXYAreaRenderer r1 = new StackedXYAreaRenderer();
155: r1.setShapePaint(Color.red);
156: r1.setShapeStroke(new BasicStroke(1.23f));
157: StackedXYAreaRenderer r2 = null;
158: try {
159: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
160: ObjectOutput out = new ObjectOutputStream(buffer);
161: out.writeObject(r1);
162: out.close();
163:
164: ObjectInput in = new ObjectInputStream(
165: new ByteArrayInputStream(buffer.toByteArray()));
166: r2 = (StackedXYAreaRenderer) in.readObject();
167: in.close();
168: } catch (Exception e) {
169: e.printStackTrace();
170: }
171: assertEquals(r1, r2);
172: }
173:
174: /**
175: * Check that the renderer is calculating the range bounds correctly.
176: */
177: public void testFindRangeBounds() {
178: TableXYDataset dataset = RendererXYPackageTests
179: .createTestTableXYDataset();
180: JFreeChart chart = ChartFactory.createStackedXYAreaChart(
181: "Test Chart", "X", "Y", dataset,
182: PlotOrientation.VERTICAL, false, false, false);
183: XYPlot plot = (XYPlot) chart.getPlot();
184: NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
185: Range bounds = rangeAxis.getRange();
186: assertTrue(bounds.contains(6.0));
187: assertTrue(bounds.contains(8.0));
188: }
189:
190: /**
191: * Draws the chart with a <code>null</code> info object to make sure that
192: * no exceptions are thrown (particularly by code in the renderer).
193: */
194: public void testDrawWithNullInfo() {
195: boolean success = false;
196: try {
197: DefaultTableXYDataset dataset = new DefaultTableXYDataset();
198:
199: XYSeries s1 = new XYSeries("Series 1", true, false);
200: s1.add(5.0, 5.0);
201: s1.add(10.0, 15.5);
202: s1.add(15.0, 9.5);
203: s1.add(20.0, 7.5);
204: dataset.addSeries(s1);
205:
206: XYSeries s2 = new XYSeries("Series 2", true, false);
207: s2.add(5.0, 5.0);
208: s2.add(10.0, 15.5);
209: s2.add(15.0, 9.5);
210: s2.add(20.0, 3.5);
211: dataset.addSeries(s2);
212: XYPlot plot = new XYPlot(dataset, new NumberAxis("X"),
213: new NumberAxis("Y"), new StackedXYAreaRenderer());
214: JFreeChart chart = new JFreeChart(plot);
215: /* BufferedImage image = */chart.createBufferedImage(300,
216: 200, null);
217: success = true;
218: } catch (NullPointerException e) {
219: e.printStackTrace();
220: success = false;
221: }
222: assertTrue(success);
223: }
224:
225: /**
226: * A test for bug 1593156.
227: */
228: public void testBug1593156() {
229: boolean success = false;
230: try {
231: DefaultTableXYDataset dataset = new DefaultTableXYDataset();
232:
233: XYSeries s1 = new XYSeries("Series 1", true, false);
234: s1.add(5.0, 5.0);
235: s1.add(10.0, 15.5);
236: s1.add(15.0, 9.5);
237: s1.add(20.0, 7.5);
238: dataset.addSeries(s1);
239:
240: XYSeries s2 = new XYSeries("Series 2", true, false);
241: s2.add(5.0, 5.0);
242: s2.add(10.0, 15.5);
243: s2.add(15.0, 9.5);
244: s2.add(20.0, 3.5);
245: dataset.addSeries(s2);
246: StackedXYAreaRenderer renderer = new StackedXYAreaRenderer(
247: XYAreaRenderer.LINES);
248: XYPlot plot = new XYPlot(dataset, new NumberAxis("X"),
249: new NumberAxis("Y"), renderer);
250: JFreeChart chart = new JFreeChart(plot);
251: /* BufferedImage image = */chart.createBufferedImage(300,
252: 200, null);
253: success = true;
254: } catch (NullPointerException e) {
255: e.printStackTrace();
256: success = false;
257: }
258: assertTrue(success);
259: }
260:
261: }
|