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