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: * LineChartTests.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: LineChartTests.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.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: * Some tests for a line chart.
071: */
072: public class LineChartTests 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(LineChartTests.class);
084: }
085:
086: /**
087: * Constructs a new set of tests.
088: *
089: * @param name the name of the tests.
090: */
091: public LineChartTests(String name) {
092: super (name);
093: }
094:
095: /**
096: * Common test setup.
097: */
098: protected void setUp() {
099: this .chart = createLineChart();
100: }
101:
102: /**
103: * Draws the chart with a null info object to make sure that no exceptions
104: * are thrown (a problem that was occurring at one point).
105: */
106: public void testDrawWithNullInfo() {
107:
108: boolean success = false;
109: try {
110: BufferedImage image = new BufferedImage(200, 100,
111: BufferedImage.TYPE_INT_RGB);
112: Graphics2D g2 = image.createGraphics();
113: this .chart.draw(g2, new Rectangle2D.Double(0, 0, 200, 100),
114: null, null);
115: g2.dispose();
116: success = true;
117: } catch (Exception e) {
118: success = false;
119: }
120: assertTrue(success);
121:
122: }
123:
124: /**
125: * Replaces the chart's dataset and then checks that the new dataset is OK.
126: */
127: public void testReplaceDataset() {
128:
129: // create a dataset...
130: Number[][] data = new Integer[][] {
131: { new Integer(-30), new Integer(-20) },
132: { new Integer(-10), new Integer(10) },
133: { new Integer(20), new Integer(30) } };
134:
135: CategoryDataset newData = DatasetUtilities
136: .createCategoryDataset("S", "C", data);
137:
138: LocalListener l = new LocalListener();
139: this .chart.addChangeListener(l);
140: CategoryPlot plot = (CategoryPlot) this .chart.getPlot();
141: plot.setDataset(newData);
142: assertEquals(true, l.flag);
143: ValueAxis axis = plot.getRangeAxis();
144: Range range = axis.getRange();
145: assertTrue(
146: "Expecting the lower bound of the range to be around -30: "
147: + range.getLowerBound(),
148: range.getLowerBound() <= -30);
149: assertTrue(
150: "Expecting the upper bound of the range to be around 30: "
151: + range.getUpperBound(),
152: range.getUpperBound() >= 30);
153:
154: }
155:
156: /**
157: * Check that setting a tool tip generator for a series does override the
158: * default generator.
159: */
160: public void testSetSeriesToolTipGenerator() {
161: CategoryPlot plot = (CategoryPlot) this .chart.getPlot();
162: CategoryItemRenderer renderer = plot.getRenderer();
163: StandardCategoryToolTipGenerator tt = new StandardCategoryToolTipGenerator();
164: renderer.setSeriesToolTipGenerator(0, tt);
165: CategoryToolTipGenerator tt2 = renderer.getToolTipGenerator(0,
166: 0);
167: assertTrue(tt2 == tt);
168: }
169:
170: /**
171: * Check that setting a URL generator for a series does override the
172: * default generator.
173: */
174: public void testSetSeriesURLGenerator() {
175: CategoryPlot plot = (CategoryPlot) this .chart.getPlot();
176: CategoryItemRenderer renderer = plot.getRenderer();
177: StandardCategoryURLGenerator url1 = new StandardCategoryURLGenerator();
178: renderer.setSeriesItemURLGenerator(0, url1);
179: CategoryURLGenerator url2 = renderer.getItemURLGenerator(0, 0);
180: assertTrue(url2 == url1);
181: }
182:
183: /**
184: * Create a line chart with sample data in the range -3 to +3.
185: *
186: * @return The chart.
187: */
188: private static JFreeChart createLineChart() {
189:
190: // create a dataset...
191: Number[][] data = new Integer[][] {
192: { new Integer(-3), new Integer(-2) },
193: { new Integer(-1), new Integer(1) },
194: { new Integer(2), new Integer(3) } };
195:
196: CategoryDataset dataset = DatasetUtilities
197: .createCategoryDataset("S", "C", data);
198:
199: // create the chart...
200: return ChartFactory.createLineChart("Line Chart", "Domain",
201: "Range", dataset, PlotOrientation.HORIZONTAL, true, // include legend
202: true, true);
203:
204: }
205:
206: /**
207: * A chart change listener.
208: *
209: */
210: static class LocalListener implements ChartChangeListener {
211:
212: /** A flag. */
213: private boolean flag = false;
214:
215: /**
216: * Event handler.
217: *
218: * @param event the event.
219: */
220: public void chartChanged(ChartChangeEvent event) {
221: this .flag = true;
222: }
223:
224: }
225:
226: }
|