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: * StackedXYBarRendererTests.java
029: * ------------------------------
030: * (C) Copyright 2004-2007, by Object Refinery Limited and Contributors.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: StackedXYBarRendererTests.java,v 1.1.2.3 2007/03/15 17:04:35 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 10-Sep-2004 : Version 1 (DG);
040: * 06-Jan-2005 : Added test for auto range calculation (DG);
041: * 06-Dec-2006 : Confirm serialization of GradientPaint (DG);
042: *
043: */
044:
045: package org.jfree.chart.renderer.xy.junit;
046:
047: import java.awt.Color;
048: import java.awt.GradientPaint;
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.ChartFactory;
061: import org.jfree.chart.JFreeChart;
062: import org.jfree.chart.axis.NumberAxis;
063: import org.jfree.chart.plot.PlotOrientation;
064: import org.jfree.chart.plot.XYPlot;
065: import org.jfree.chart.renderer.xy.StackedXYBarRenderer;
066: import org.jfree.data.Range;
067: import org.jfree.data.xy.TableXYDataset;
068:
069: /**
070: * Tests for the {@link StackedXYBarRenderer} class.
071: */
072: public class StackedXYBarRendererTests 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(StackedXYBarRendererTests.class);
081: }
082:
083: /**
084: * Constructs a new set of tests.
085: *
086: * @param name the name of the tests.
087: */
088: public StackedXYBarRendererTests(String name) {
089: super (name);
090: }
091:
092: /**
093: * Test that the equals() method distinguishes all fields.
094: */
095: public void testEquals() {
096: StackedXYBarRenderer r1 = new StackedXYBarRenderer();
097: StackedXYBarRenderer r2 = new StackedXYBarRenderer();
098: assertTrue(r1.equals(r2));
099: assertTrue(r2.equals(r1));
100:
101: r1.setRenderAsPercentages(true);
102: assertFalse(r1.equals(r2));
103: r2.setRenderAsPercentages(true);
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: StackedXYBarRenderer r1 = new StackedXYBarRenderer();
112: StackedXYBarRenderer r2 = new StackedXYBarRenderer();
113: assertTrue(r1.equals(r2));
114: int h1 = r1.hashCode();
115: int h2 = r2.hashCode();
116: assertEquals(h1, h2);
117:
118: r1.setRenderAsPercentages(true);
119: h1 = r1.hashCode();
120: h2 = r2.hashCode();
121: assertFalse(h1 == h2);
122: }
123:
124: /**
125: * Confirm that cloning works.
126: */
127: public void testCloning() {
128: StackedXYBarRenderer r1 = new StackedXYBarRenderer();
129: StackedXYBarRenderer r2 = null;
130: try {
131: r2 = (StackedXYBarRenderer) 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:
140: /**
141: * Serialize an instance, restore it, and check for equality.
142: */
143: public void testSerialization() {
144: StackedXYBarRenderer r1 = new StackedXYBarRenderer();
145: r1.setSeriesPaint(0, new GradientPaint(1.0f, 2.0f, Color.red,
146: 3.0f, 4.0f, Color.yellow));
147: StackedXYBarRenderer r2 = null;
148: try {
149: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
150: ObjectOutput out = new ObjectOutputStream(buffer);
151: out.writeObject(r1);
152: out.close();
153:
154: ObjectInput in = new ObjectInputStream(
155: new ByteArrayInputStream(buffer.toByteArray()));
156: r2 = (StackedXYBarRenderer) in.readObject();
157: in.close();
158: } catch (Exception e) {
159: e.printStackTrace();
160: }
161: assertEquals(r1, r2);
162: }
163:
164: /**
165: * Check that the renderer is calculating the domain bounds correctly.
166: */
167: public void testFindDomainBounds() {
168: TableXYDataset dataset = RendererXYPackageTests
169: .createTestTableXYDataset();
170: JFreeChart chart = ChartFactory.createStackedXYAreaChart(
171: "Test Chart", "X", "Y", dataset,
172: PlotOrientation.VERTICAL, false, false, false);
173: XYPlot plot = (XYPlot) chart.getPlot();
174: plot.setRenderer(new StackedXYBarRenderer());
175: NumberAxis domainAxis = (NumberAxis) plot.getDomainAxis();
176: domainAxis.setAutoRangeIncludesZero(false);
177: Range bounds = domainAxis.getRange();
178: assertFalse(bounds.contains(0.3));
179: assertTrue(bounds.contains(0.5));
180: assertTrue(bounds.contains(2.5));
181: assertFalse(bounds.contains(2.8));
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: plot.setRenderer(new StackedXYBarRenderer());
195: NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
196: Range bounds = rangeAxis.getRange();
197: assertTrue(bounds.contains(6.0));
198: assertTrue(bounds.contains(8.0));
199: }
200:
201: }
|