001: /* ===========================================================
002: * JFreeChart : a free chart library for the Java(tm) platform
003: * ===========================================================
004: *
005: * (C) Copyright 2000-2006, 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: * StackedXYAreaRenderer2Tests.java
029: * -------------------------------
030: * (C) Copyright 2005, 2006, by Object Refinery Limited and Contributors.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: StackedXYAreaRenderer2Tests.java,v 1.1.2.2 2006/11/30 15:31:38 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 06-Jan-2005 : Version 1 (DG);
040: * 22-Aug-2006 : Added testDrawWithEmptyDataset() method (DG);
041: * 30-Nov-2006 : Extended testEquals() (DG);
042: *
043: */
044:
045: package org.jfree.chart.renderer.xy.junit;
046:
047: import java.awt.Graphics2D;
048: import java.awt.geom.Rectangle2D;
049: import java.awt.image.BufferedImage;
050: import java.io.ByteArrayInputStream;
051: import java.io.ByteArrayOutputStream;
052: import java.io.ObjectInput;
053: import java.io.ObjectInputStream;
054: import java.io.ObjectOutput;
055: import java.io.ObjectOutputStream;
056:
057: import junit.framework.Test;
058: import junit.framework.TestCase;
059: import junit.framework.TestSuite;
060:
061: import org.jfree.chart.ChartFactory;
062: import org.jfree.chart.JFreeChart;
063: import org.jfree.chart.axis.NumberAxis;
064: import org.jfree.chart.plot.PlotOrientation;
065: import org.jfree.chart.plot.XYPlot;
066: import org.jfree.chart.renderer.xy.StackedXYAreaRenderer2;
067: import org.jfree.data.Range;
068: import org.jfree.data.xy.DefaultTableXYDataset;
069: import org.jfree.data.xy.TableXYDataset;
070:
071: /**
072: * Tests for the {@link StackedXYAreaRenderer2} class.
073: */
074: public class StackedXYAreaRenderer2Tests extends TestCase {
075:
076: /**
077: * Returns the tests as a test suite.
078: *
079: * @return The test suite.
080: */
081: public static Test suite() {
082: return new TestSuite(StackedXYAreaRenderer2Tests.class);
083: }
084:
085: /**
086: * Constructs a new set of tests.
087: *
088: * @param name the name of the tests.
089: */
090: public StackedXYAreaRenderer2Tests(String name) {
091: super (name);
092: }
093:
094: /**
095: * Test chart drawing with an empty dataset to ensure that this special
096: * case doesn't cause any exceptions.
097: */
098: public void testDrawWithEmptyDataset() {
099: boolean success = false;
100: JFreeChart chart = ChartFactory.createStackedXYAreaChart(
101: "title", "x", "y", new DefaultTableXYDataset(),
102: PlotOrientation.VERTICAL, true, false, false);
103: XYPlot plot = (XYPlot) chart.getPlot();
104: plot.setRenderer(new StackedXYAreaRenderer2());
105: try {
106: BufferedImage image = new BufferedImage(200, 100,
107: BufferedImage.TYPE_INT_RGB);
108: Graphics2D g2 = image.createGraphics();
109: chart.draw(g2, new Rectangle2D.Double(0, 0, 200, 100),
110: null, null);
111: g2.dispose();
112: success = true;
113: } catch (Exception e) {
114: success = false;
115: }
116: assertTrue(success);
117: }
118:
119: /**
120: * Test that the equals() method distinguishes all fields.
121: */
122: public void testEquals() {
123: StackedXYAreaRenderer2 r1 = new StackedXYAreaRenderer2();
124: StackedXYAreaRenderer2 r2 = new StackedXYAreaRenderer2();
125: assertEquals(r1, r2);
126: assertEquals(r2, r1);
127:
128: r1.setRoundXCoordinates(!r1.getRoundXCoordinates());
129: assertFalse(r1.equals(r2));
130: r2.setRoundXCoordinates(r1.getRoundXCoordinates());
131: assertTrue(r1.equals(r2));
132: }
133:
134: /**
135: * Two objects that are equal are required to return the same hashCode.
136: */
137: public void testHashcode() {
138: StackedXYAreaRenderer2 r1 = new StackedXYAreaRenderer2();
139: StackedXYAreaRenderer2 r2 = new StackedXYAreaRenderer2();
140: assertTrue(r1.equals(r2));
141: int h1 = r1.hashCode();
142: int h2 = r2.hashCode();
143: assertEquals(h1, h2);
144: }
145:
146: /**
147: * Confirm that cloning works.
148: */
149: public void testCloning() {
150: StackedXYAreaRenderer2 r1 = new StackedXYAreaRenderer2();
151: StackedXYAreaRenderer2 r2 = null;
152: try {
153: r2 = (StackedXYAreaRenderer2) r1.clone();
154: } catch (CloneNotSupportedException e) {
155: e.printStackTrace();
156: }
157: assertTrue(r1 != r2);
158: assertTrue(r1.getClass() == r2.getClass());
159: assertTrue(r1.equals(r2));
160: }
161:
162: /**
163: * Serialize an instance, restore it, and check for equality.
164: */
165: public void testSerialization() {
166: StackedXYAreaRenderer2 r1 = new StackedXYAreaRenderer2();
167: StackedXYAreaRenderer2 r2 = null;
168: try {
169: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
170: ObjectOutput out = new ObjectOutputStream(buffer);
171: out.writeObject(r1);
172: out.close();
173:
174: ObjectInput in = new ObjectInputStream(
175: new ByteArrayInputStream(buffer.toByteArray()));
176: r2 = (StackedXYAreaRenderer2) in.readObject();
177: in.close();
178: } catch (Exception e) {
179: e.printStackTrace();
180: }
181: assertEquals(r1, r2);
182: }
183:
184: /**
185: * Check that the renderer is calculating the range bounds correctly.
186: */
187: public void testFindRangeBounds() {
188: TableXYDataset dataset = RendererXYPackageTests
189: .createTestTableXYDataset();
190: JFreeChart chart = ChartFactory.createStackedXYAreaChart(
191: "Test Chart", "X", "Y", dataset,
192: PlotOrientation.VERTICAL, false, false, false);
193: XYPlot plot = (XYPlot) chart.getPlot();
194: StackedXYAreaRenderer2 renderer = new StackedXYAreaRenderer2();
195: plot.setRenderer(renderer);
196: NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
197: Range bounds = rangeAxis.getRange();
198: assertTrue(bounds.contains(6.0));
199: assertTrue(bounds.contains(8.0));
200:
201: // try null argument
202: assertNull(renderer.findRangeBounds(null));
203:
204: // try empty dataset
205: assertNull(renderer
206: .findRangeBounds(new DefaultTableXYDataset()));
207: }
208:
209: }
|