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