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: * AreaChartTests.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: AreaChartTests.java,v 1.1.2.2 2007/03/02 11:16:21 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.CategoryToolTipGenerator;
059: import org.jfree.chart.labels.StandardCategoryToolTipGenerator;
060: import org.jfree.chart.plot.CategoryPlot;
061: import org.jfree.chart.plot.PlotOrientation;
062: import org.jfree.chart.renderer.category.CategoryItemRenderer;
063: import org.jfree.chart.urls.CategoryURLGenerator;
064: import org.jfree.chart.urls.StandardCategoryURLGenerator;
065: import org.jfree.data.Range;
066: import org.jfree.data.category.CategoryDataset;
067: import org.jfree.data.general.DatasetUtilities;
068:
069: /**
070: * Tests for an area chart.
071: */
072: public class AreaChartTests extends TestCase {
073:
074: /** A chart. */
075: private JFreeChart chart;
076:
077: /**
078: * Returns the tests as a test suite.
079: *
080: * @return The test suite.
081: */
082: public static Test suite() {
083: return new TestSuite(AreaChartTests.class);
084: }
085:
086: /**
087: * Constructs a new set of tests.
088: *
089: * @param name the name of the tests.
090: */
091: public AreaChartTests(String name) {
092: super (name);
093: }
094:
095: /**
096: * Common test setup.
097: */
098: protected void setUp() {
099: this .chart = createAreaChart();
100: }
101:
102: /**
103: * Check that setting a tool tip generator for a series does override the
104: * default generator.
105: */
106: public void testSetSeriesToolTipGenerator() {
107: CategoryPlot plot = (CategoryPlot) this .chart.getPlot();
108: CategoryItemRenderer renderer = plot.getRenderer();
109: StandardCategoryToolTipGenerator tt = new StandardCategoryToolTipGenerator();
110: renderer.setSeriesToolTipGenerator(0, tt);
111: CategoryToolTipGenerator tt2 = renderer.getToolTipGenerator(0,
112: 0);
113: assertTrue(tt2 == tt);
114: }
115:
116: /**
117: * Check that setting a URL generator for a series does override the
118: * default generator.
119: */
120: public void testSetSeriesURLGenerator() {
121: CategoryPlot plot = (CategoryPlot) this .chart.getPlot();
122: CategoryItemRenderer renderer = plot.getRenderer();
123: StandardCategoryURLGenerator url1 = new StandardCategoryURLGenerator();
124: renderer.setSeriesItemURLGenerator(0, url1);
125: CategoryURLGenerator url2 = renderer.getItemURLGenerator(0, 0);
126: assertTrue(url2 == url1);
127: }
128:
129: /**
130: * Draws the chart with a null info object to make sure that no exceptions
131: * are thrown (a problem that was occurring at one point).
132: */
133: public void testDrawWithNullInfo() {
134: boolean success = false;
135: try {
136: BufferedImage image = new BufferedImage(200, 100,
137: BufferedImage.TYPE_INT_RGB);
138: Graphics2D g2 = image.createGraphics();
139: this .chart.draw(g2, new Rectangle2D.Double(0, 0, 200, 100),
140: null, null);
141: g2.dispose();
142: success = true;
143: } catch (Exception e) {
144: success = false;
145: }
146: assertTrue(success);
147: }
148:
149: /**
150: * Replaces the chart's dataset and then checks that the new dataset is OK.
151: */
152: public void testReplaceDataset() {
153: Number[][] data = new Integer[][] {
154: { new Integer(-30), new Integer(-20) },
155: { new Integer(-10), new Integer(10) },
156: { new Integer(20), new Integer(30) } };
157:
158: CategoryDataset newData = DatasetUtilities
159: .createCategoryDataset("S", "C", data);
160: LocalListener l = new LocalListener();
161: this .chart.addChangeListener(l);
162: CategoryPlot plot = (CategoryPlot) this .chart.getPlot();
163: plot.setDataset(newData);
164: assertEquals(true, l.flag);
165: ValueAxis axis = plot.getRangeAxis();
166: Range range = axis.getRange();
167: assertTrue(
168: "Expecting the lower bound of the range to be around -30: "
169: + range.getLowerBound(),
170: range.getLowerBound() <= -30);
171: assertTrue(
172: "Expecting the upper bound of the range to be around 30: "
173: + range.getUpperBound(),
174: range.getUpperBound() >= 30);
175:
176: }
177:
178: /**
179: * Create an area chart with sample data in the range -3 to +3.
180: *
181: * @return The chart.
182: */
183: private static JFreeChart createAreaChart() {
184: Number[][] data = new Integer[][] {
185: { new Integer(-3), new Integer(-2) },
186: { new Integer(-1), new Integer(1) },
187: { new Integer(2), new Integer(3) } };
188: CategoryDataset dataset = DatasetUtilities
189: .createCategoryDataset("S", "C", data);
190: return ChartFactory.createAreaChart("Area Chart", "Domain",
191: "Range", dataset, PlotOrientation.HORIZONTAL, true,
192: true, true);
193:
194: }
195:
196: /**
197: * A chart change listener.
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: }
|