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: * BarChartTests.java
029: * ------------------
030: * (C) Copyright 2002-2007, by Object Refinery Limited.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: BarChartTests.java,v 1.1.2.2 2007/03/02 11:16:20 mungady Exp $
036: *
037: * Changes:
038: * --------
039: * 11-Jun-2002 : Version 1 (DG);
040: * 25-Jun-2002 : Removed redundant code (DG);
041: * 17-Oct-2002 : Fixed errors reported by Checkstyle (DG);
042: * 14-Jul-2003 : Renamed BarChartTests.java (DG);
043: *
044: */
045:
046: package org.jfree.chart.junit;
047:
048: import java.awt.Graphics2D;
049: import java.awt.geom.Rectangle2D;
050: import java.awt.image.BufferedImage;
051:
052: import junit.framework.Test;
053: import junit.framework.TestCase;
054: import junit.framework.TestSuite;
055:
056: import org.jfree.chart.ChartFactory;
057: import org.jfree.chart.JFreeChart;
058: import org.jfree.chart.axis.ValueAxis;
059: import org.jfree.chart.event.ChartChangeEvent;
060: import org.jfree.chart.event.ChartChangeListener;
061: import org.jfree.chart.labels.CategoryToolTipGenerator;
062: import org.jfree.chart.labels.StandardCategoryToolTipGenerator;
063: import org.jfree.chart.plot.CategoryPlot;
064: import org.jfree.chart.plot.PlotOrientation;
065: import org.jfree.chart.renderer.category.CategoryItemRenderer;
066: import org.jfree.chart.urls.CategoryURLGenerator;
067: import org.jfree.chart.urls.StandardCategoryURLGenerator;
068: import org.jfree.data.Range;
069: import org.jfree.data.category.CategoryDataset;
070: import org.jfree.data.general.DatasetUtilities;
071:
072: /**
073: * Tests for a bar chart.
074: */
075: public class BarChartTests extends TestCase {
076:
077: /** A chart. */
078: private JFreeChart chart;
079:
080: /**
081: * Returns the tests as a test suite.
082: *
083: * @return The test suite.
084: */
085: public static Test suite() {
086: return new TestSuite(BarChartTests.class);
087: }
088:
089: /**
090: * Constructs a new set of tests.
091: *
092: * @param name the name of the tests.
093: */
094: public BarChartTests(String name) {
095: super (name);
096: }
097:
098: /**
099: * Common test setup.
100: */
101: protected void setUp() {
102: this .chart = createBarChart();
103: }
104:
105: /**
106: * Draws the chart with a null info object to make sure that no exceptions
107: * are thrown (a problem that was occurring at one point).
108: */
109: public void testDrawWithNullInfo() {
110:
111: boolean success = false;
112:
113: try {
114: BufferedImage image = new BufferedImage(200, 100,
115: BufferedImage.TYPE_INT_RGB);
116: Graphics2D g2 = image.createGraphics();
117: this .chart.draw(g2, new Rectangle2D.Double(0, 0, 200, 100),
118: null, null);
119: g2.dispose();
120: success = true;
121: } catch (Exception e) {
122: success = false;
123: }
124:
125: assertTrue(success);
126:
127: }
128:
129: /**
130: * Replaces the chart's dataset and then checks that the new dataset is OK.
131: */
132: public void testReplaceDataset() {
133:
134: // create a dataset...
135: Number[][] data = new Integer[][] {
136: { new Integer(-30), new Integer(-20) },
137: { new Integer(-10), new Integer(10) },
138: { new Integer(20), new Integer(30) } };
139:
140: CategoryDataset newData = DatasetUtilities
141: .createCategoryDataset("S", "C", data);
142:
143: LocalListener l = new LocalListener();
144: this .chart.addChangeListener(l);
145: CategoryPlot plot = (CategoryPlot) this .chart.getPlot();
146: plot.setDataset(newData);
147: assertEquals(true, l.flag);
148: ValueAxis axis = plot.getRangeAxis();
149: Range range = axis.getRange();
150: assertTrue(
151: "Expecting the lower bound of the range to be around -30: "
152: + range.getLowerBound(),
153: range.getLowerBound() <= -30);
154: assertTrue(
155: "Expecting the upper bound of the range to be around 30: "
156: + range.getUpperBound(),
157: range.getUpperBound() >= 30);
158:
159: }
160:
161: /**
162: * Check that setting a tool tip generator for a series does override the
163: * default generator.
164: */
165: public void testSetSeriesToolTipGenerator() {
166: CategoryPlot plot = (CategoryPlot) this .chart.getPlot();
167: CategoryItemRenderer renderer = plot.getRenderer();
168: StandardCategoryToolTipGenerator tt = new StandardCategoryToolTipGenerator();
169: renderer.setSeriesToolTipGenerator(0, tt);
170: CategoryToolTipGenerator tt2 = renderer.getToolTipGenerator(0,
171: 0);
172: assertTrue(tt2 == tt);
173: }
174:
175: /**
176: * Check that setting a URL generator for a series does override the
177: * default generator.
178: */
179: public void testSetSeriesURLGenerator() {
180: CategoryPlot plot = (CategoryPlot) this .chart.getPlot();
181: CategoryItemRenderer renderer = plot.getRenderer();
182: StandardCategoryURLGenerator url1 = new StandardCategoryURLGenerator();
183: renderer.setSeriesItemURLGenerator(0, url1);
184: CategoryURLGenerator url2 = renderer.getItemURLGenerator(0, 0);
185: assertTrue(url2 == url1);
186: }
187:
188: /**
189: * Create a bar chart with sample data in the range -3 to +3.
190: *
191: * @return The chart.
192: */
193: private static JFreeChart createBarChart() {
194:
195: // create a dataset...
196: Number[][] data = new Integer[][] {
197: { new Integer(-3), new Integer(-2) },
198: { new Integer(-1), new Integer(1) },
199: { new Integer(2), new Integer(3) } };
200:
201: CategoryDataset dataset = DatasetUtilities
202: .createCategoryDataset("S", "C", data);
203:
204: // create the chart...
205: return ChartFactory.createBarChart("Bar Chart", "Domain",
206: "Range", dataset, PlotOrientation.HORIZONTAL, true, // include legend
207: true, true);
208:
209: }
210:
211: /**
212: * A chart change listener.
213: *
214: */
215: static class LocalListener implements ChartChangeListener {
216:
217: /** A flag. */
218: private boolean flag = false;
219:
220: /**
221: * Event handler.
222: *
223: * @param event the event.
224: */
225: public void chartChanged(ChartChangeEvent event) {
226: this .flag = true;
227: }
228:
229: }
230:
231: }
|