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: * XYAreaChartTests.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: XYAreaChartTests.java,v 1.1.2.2 2007/03/02 11:16:20 mungady Exp $
036: *
037: * Changes:
038: * --------
039: * 12-Apr-2005 : Version 1 (DG);
040: *
041: */
042:
043: package org.jfree.chart.junit;
044:
045: import java.awt.Graphics2D;
046: import java.awt.geom.Rectangle2D;
047: import java.awt.image.BufferedImage;
048:
049: import junit.framework.Test;
050: import junit.framework.TestCase;
051: import junit.framework.TestSuite;
052:
053: import org.jfree.chart.ChartFactory;
054: import org.jfree.chart.JFreeChart;
055: import org.jfree.chart.axis.ValueAxis;
056: import org.jfree.chart.event.ChartChangeEvent;
057: import org.jfree.chart.event.ChartChangeListener;
058: import org.jfree.chart.labels.StandardXYToolTipGenerator;
059: import org.jfree.chart.labels.XYToolTipGenerator;
060: import org.jfree.chart.plot.PlotOrientation;
061: import org.jfree.chart.plot.XYPlot;
062: import org.jfree.chart.renderer.xy.XYItemRenderer;
063: import org.jfree.data.Range;
064: import org.jfree.data.xy.XYDataset;
065: import org.jfree.data.xy.XYSeries;
066: import org.jfree.data.xy.XYSeriesCollection;
067:
068: /**
069: * Some tests for an XY area chart.
070: */
071: public class XYAreaChartTests extends TestCase {
072:
073: /** A chart. */
074: private JFreeChart chart;
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(XYAreaChartTests.class);
083: }
084:
085: /**
086: * Constructs a new set of tests.
087: *
088: * @param name the name of the tests.
089: */
090: public XYAreaChartTests(String name) {
091: super (name);
092: }
093:
094: /**
095: * Common test setup.
096: */
097: protected void setUp() {
098: this .chart = createChart();
099: }
100:
101: /**
102: * Draws the chart with a null info object to make sure that no exceptions
103: * are thrown (a problem that was occurring at one point).
104: */
105: public void testDrawWithNullInfo() {
106:
107: boolean success = false;
108: try {
109: BufferedImage image = new BufferedImage(200, 100,
110: BufferedImage.TYPE_INT_RGB);
111: Graphics2D g2 = image.createGraphics();
112: this .chart.draw(g2, new Rectangle2D.Double(0, 0, 200, 100),
113: null, null);
114: g2.dispose();
115: success = true;
116: } catch (Exception e) {
117: success = false;
118: e.printStackTrace();
119: }
120: assertTrue(success);
121:
122: }
123:
124: /**
125: * Replaces the dataset and checks that it has changed as expected.
126: */
127: public void testReplaceDataset() {
128:
129: // create a dataset...
130: XYSeries series1 = new XYSeries("Series 1");
131: series1.add(10.0, 10.0);
132: series1.add(20.0, 20.0);
133: series1.add(30.0, 30.0);
134: XYDataset dataset = new XYSeriesCollection(series1);
135:
136: LocalListener l = new LocalListener();
137: this .chart.addChangeListener(l);
138: XYPlot plot = (XYPlot) this .chart.getPlot();
139: plot.setDataset(dataset);
140: assertEquals(true, l.flag);
141: ValueAxis axis = plot.getRangeAxis();
142: Range range = axis.getRange();
143: assertTrue(
144: "Expecting the lower bound of the range to be around 10: "
145: + range.getLowerBound(),
146: range.getLowerBound() <= 10);
147: assertTrue(
148: "Expecting the upper bound of the range to be around 30: "
149: + range.getUpperBound(),
150: range.getUpperBound() >= 30);
151:
152: }
153:
154: /**
155: * Check that setting a tool tip generator for a series does override the
156: * default generator.
157: */
158: public void testSetSeriesToolTipGenerator() {
159: XYPlot plot = (XYPlot) this .chart.getPlot();
160: XYItemRenderer renderer = plot.getRenderer();
161: StandardXYToolTipGenerator tt = new StandardXYToolTipGenerator();
162: renderer.setSeriesToolTipGenerator(0, tt);
163: XYToolTipGenerator tt2 = renderer.getToolTipGenerator(0, 0);
164: assertTrue(tt2 == tt);
165: }
166:
167: /**
168: * Create a horizontal bar chart with sample data in the range -3 to +3.
169: *
170: * @return The chart.
171: */
172: private static JFreeChart createChart() {
173:
174: // create a dataset...
175: XYSeries series1 = new XYSeries("Series 1");
176: series1.add(1.0, 1.0);
177: series1.add(2.0, 2.0);
178: series1.add(3.0, 3.0);
179: XYDataset dataset = new XYSeriesCollection(series1);
180:
181: // create the chart...
182: return ChartFactory.createXYAreaChart("Area Chart", // chart title
183: "Domain", "Range", dataset, // data
184: PlotOrientation.VERTICAL, true, // include legend
185: true, // tooltips
186: true // urls
187: );
188:
189: }
190:
191: /**
192: * A chart change listener.
193: *
194: */
195: static class LocalListener implements ChartChangeListener {
196:
197: /** A flag. */
198: private boolean flag = false;
199:
200: /**
201: * Event handler.
202: *
203: * @param event the event.
204: */
205: public void chartChanged(ChartChangeEvent event) {
206: this .flag = true;
207: }
208:
209: }
210:
211: }
|