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: * BarChart3DTests.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: BarChart3DTests.java,v 1.1.2.2 2007/03/02 11:16:21 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 BarChart3DTests.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 3D bar chart.
074: */
075: public class BarChart3DTests extends TestCase {
076:
077: /** The 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(BarChart3DTests.class);
087: }
088:
089: /**
090: * Constructs a new set of tests.
091: *
092: * @param name the name of the tests.
093: */
094: public BarChart3DTests(String name) {
095: super (name);
096: }
097:
098: /**
099: * Common test setup.
100: */
101: protected void setUp() {
102: this .chart = createBarChart3D();
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: boolean success = false;
111: try {
112: BufferedImage image = new BufferedImage(200, 100,
113: BufferedImage.TYPE_INT_RGB);
114: Graphics2D g2 = image.createGraphics();
115: this .chart.draw(g2, new Rectangle2D.Double(0, 0, 200, 100),
116: null, null);
117: g2.dispose();
118: success = true;
119: } catch (Exception e) {
120: success = false;
121: }
122: assertTrue(success);
123: }
124:
125: /**
126: * Replaces the dataset and checks that the data range is as expected.
127: */
128: public void testReplaceDataset() {
129:
130: // create a dataset...
131: Number[][] data = new Integer[][] {
132: { new Integer(-30), new Integer(-20) },
133: { new Integer(-10), new Integer(10) },
134: { new Integer(20), new Integer(30) } };
135:
136: CategoryDataset newData = DatasetUtilities
137: .createCategoryDataset("S", "C", data);
138:
139: LocalListener l = new LocalListener();
140: this .chart.addChangeListener(l);
141: CategoryPlot plot = (CategoryPlot) this .chart.getPlot();
142: plot.setDataset(newData);
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 -30: "
148: + range.getLowerBound(),
149: range.getLowerBound() <= -30);
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: CategoryPlot plot = (CategoryPlot) this .chart.getPlot();
163: CategoryItemRenderer renderer = plot.getRenderer();
164: StandardCategoryToolTipGenerator tt = new StandardCategoryToolTipGenerator();
165: renderer.setSeriesToolTipGenerator(0, tt);
166: CategoryToolTipGenerator tt2 = renderer.getToolTipGenerator(0,
167: 0);
168: assertTrue(tt2 == tt);
169: }
170:
171: /**
172: * Check that setting a URL generator for a series does override the
173: * default generator.
174: */
175: public void testSetSeriesURLGenerator() {
176: CategoryPlot plot = (CategoryPlot) this .chart.getPlot();
177: CategoryItemRenderer renderer = plot.getRenderer();
178: StandardCategoryURLGenerator url1 = new StandardCategoryURLGenerator();
179: renderer.setSeriesItemURLGenerator(0, url1);
180: CategoryURLGenerator url2 = renderer.getItemURLGenerator(0, 0);
181: assertTrue(url2 == url1);
182: }
183:
184: /**
185: * Create a horizontal bar chart with sample data in the range -3 to +3.
186: *
187: * @return The chart.
188: */
189: private static JFreeChart createBarChart3D() {
190:
191: // create a dataset...
192: Number[][] data = new Integer[][] {
193: { new Integer(-3), new Integer(-2) },
194: { new Integer(-1), new Integer(1) },
195: { new Integer(2), new Integer(3) } };
196:
197: CategoryDataset dataset = DatasetUtilities
198: .createCategoryDataset("S", "C", data);
199:
200: // create the chart...
201: return ChartFactory.createBarChart3D("Bar Chart 3D", "Domain",
202: "Range", dataset, PlotOrientation.HORIZONTAL, true,
203: true, true);
204:
205: }
206:
207: /**
208: * A chart change listener.
209: *
210: */
211: static class LocalListener implements ChartChangeListener {
212:
213: /** A flag. */
214: private boolean flag = false;
215:
216: /**
217: * Event handler.
218: *
219: * @param event the event.
220: */
221: public void chartChanged(ChartChangeEvent event) {
222: this .flag = true;
223: }
224:
225: }
226:
227: }
|