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: * TimeSeriesChartTests.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: TimeSeriesChartTests.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.StandardXYToolTipGenerator;
059: import org.jfree.chart.labels.XYToolTipGenerator;
060: import org.jfree.chart.plot.XYPlot;
061: import org.jfree.chart.renderer.xy.XYItemRenderer;
062: import org.jfree.data.Range;
063: import org.jfree.data.xy.XYDataset;
064: import org.jfree.data.xy.XYSeries;
065: import org.jfree.data.xy.XYSeriesCollection;
066:
067: /**
068: * Some tests for a time series chart.
069: */
070: public class TimeSeriesChartTests extends TestCase {
071:
072: /** A chart. */
073: private JFreeChart chart;
074:
075: /**
076: * Returns the tests as a test suite.
077: *
078: * @return The test suite.
079: */
080: public static Test suite() {
081: return new TestSuite(TimeSeriesChartTests.class);
082: }
083:
084: /**
085: * Constructs a new set of tests.
086: *
087: * @param name the name of the tests.
088: */
089: public TimeSeriesChartTests(String name) {
090: super (name);
091: }
092:
093: /**
094: * Common test setup.
095: */
096: protected void setUp() {
097: this .chart = createChart();
098: }
099:
100: /**
101: * Draws the chart with a null info object to make sure that no exceptions
102: * are thrown (a problem that was occurring at one point).
103: */
104: public void testDrawWithNullInfo() {
105:
106: boolean success = false;
107: try {
108: BufferedImage image = new BufferedImage(200, 100,
109: BufferedImage.TYPE_INT_RGB);
110: Graphics2D g2 = image.createGraphics();
111: this .chart.draw(g2, new Rectangle2D.Double(0, 0, 200, 100),
112: null, null);
113: g2.dispose();
114: success = true;
115: } catch (Exception e) {
116: success = false;
117: e.printStackTrace();
118: }
119: assertTrue(success);
120:
121: }
122:
123: /**
124: * Replaces the dataset and checks that it has changed as expected.
125: */
126: public void testReplaceDataset() {
127:
128: // create a dataset...
129: XYSeries series1 = new XYSeries("Series 1");
130: series1.add(10.0, 10.0);
131: series1.add(20.0, 20.0);
132: series1.add(30.0, 30.0);
133: XYDataset dataset = new XYSeriesCollection(series1);
134:
135: LocalListener l = new LocalListener();
136: this .chart.addChangeListener(l);
137: XYPlot plot = (XYPlot) this .chart.getPlot();
138: plot.setDataset(dataset);
139: assertEquals(true, l.flag);
140: ValueAxis axis = plot.getRangeAxis();
141: Range range = axis.getRange();
142: assertTrue(
143: "Expecting the lower bound of the range to be around 10: "
144: + range.getLowerBound(),
145: range.getLowerBound() <= 10);
146: assertTrue(
147: "Expecting the upper bound of the range to be around 30: "
148: + range.getUpperBound(),
149: range.getUpperBound() >= 30);
150:
151: }
152:
153: /**
154: * Check that setting a tool tip generator for a series does override the
155: * default generator.
156: */
157: public void testSetSeriesToolTipGenerator() {
158: XYPlot plot = (XYPlot) this .chart.getPlot();
159: XYItemRenderer renderer = plot.getRenderer();
160: StandardXYToolTipGenerator tt = new StandardXYToolTipGenerator();
161: renderer.setSeriesToolTipGenerator(0, tt);
162: XYToolTipGenerator tt2 = renderer.getToolTipGenerator(0, 0);
163: assertTrue(tt2 == tt);
164: }
165:
166: /**
167: * Create a horizontal bar chart with sample data in the range -3 to +3.
168: *
169: * @return The chart.
170: */
171: private static JFreeChart createChart() {
172:
173: // create a dataset...
174: XYSeries series1 = new XYSeries("Series 1");
175: series1.add(1.0, 1.0);
176: series1.add(2.0, 2.0);
177: series1.add(3.0, 3.0);
178: XYDataset dataset = new XYSeriesCollection(series1);
179:
180: // create the chart...
181: return ChartFactory.createTimeSeriesChart("XY Line Chart", // chart title
182: "Domain", "Range", dataset, // data
183: true, // include legend
184: true, // tooltips
185: true // urls
186: );
187:
188: }
189:
190: /**
191: * A chart change listener.
192: *
193: */
194: static class LocalListener implements ChartChangeListener {
195:
196: /** A flag. */
197: private boolean flag = false;
198:
199: /**
200: * Event handler.
201: *
202: * @param event the event.
203: */
204: public void chartChanged(ChartChangeEvent event) {
205: this .flag = true;
206: }
207:
208: }
209:
210: }
|