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: * ScatterPlotTests.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: ScatterPlotTests.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: * 17-Oct-2002 : Fixed errors reported by Checkstyle (DG);
041: *
042: */
043:
044: package org.jfree.chart.junit;
045:
046: import java.awt.Graphics2D;
047: import java.awt.geom.Rectangle2D;
048: import java.awt.image.BufferedImage;
049:
050: import junit.framework.Test;
051: import junit.framework.TestCase;
052: import junit.framework.TestSuite;
053:
054: import org.jfree.chart.ChartFactory;
055: import org.jfree.chart.JFreeChart;
056: import org.jfree.chart.axis.ValueAxis;
057: import org.jfree.chart.event.ChartChangeEvent;
058: import org.jfree.chart.event.ChartChangeListener;
059: import org.jfree.chart.labels.StandardXYToolTipGenerator;
060: import org.jfree.chart.labels.XYToolTipGenerator;
061: import org.jfree.chart.plot.PlotOrientation;
062: import org.jfree.chart.plot.XYPlot;
063: import org.jfree.chart.renderer.xy.XYItemRenderer;
064: import org.jfree.data.Range;
065: import org.jfree.data.xy.XYDataset;
066: import org.jfree.data.xy.XYSeries;
067: import org.jfree.data.xy.XYSeriesCollection;
068:
069: /**
070: * Tests for a scatter plot.
071: *
072: */
073: public class ScatterPlotTests extends TestCase {
074:
075: /** A chart. */
076: private JFreeChart chart;
077:
078: /**
079: * Returns the tests as a test suite.
080: *
081: * @return The test suite.
082: */
083: public static Test suite() {
084: return new TestSuite(ScatterPlotTests.class);
085: }
086:
087: /**
088: * Constructs a new set of tests.
089: *
090: * @param name the name of the tests.
091: */
092: public ScatterPlotTests(String name) {
093: super (name);
094: }
095:
096: /**
097: * Common test setup.
098: */
099: protected void setUp() {
100: this .chart = createChart();
101: }
102:
103: /**
104: * Draws the chart with a null info object to make sure that no exceptions
105: * are thrown (a problem that was occurring at one point).
106: */
107: public void testDrawWithNullInfo() {
108:
109: boolean success = false;
110:
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: e.printStackTrace();
122: }
123:
124: assertTrue(success);
125:
126: }
127:
128: /**
129: * Replaces the dataset and checks that it has changed as expected.
130: */
131: public void testReplaceDataset() {
132:
133: // create a dataset...
134: XYSeries series1 = new XYSeries("Series 1");
135: series1.add(10.0, 10.0);
136: series1.add(20.0, 20.0);
137: series1.add(30.0, 30.0);
138: XYDataset dataset = new XYSeriesCollection(series1);
139:
140: LocalListener l = new LocalListener();
141: this .chart.addChangeListener(l);
142: XYPlot plot = (XYPlot) this .chart.getPlot();
143: plot.setDataset(dataset);
144: assertEquals(true, l.flag);
145: ValueAxis axis = plot.getRangeAxis();
146: Range range = axis.getRange();
147: assertTrue(
148: "Expecting the lower bound of the range to be around 10: "
149: + range.getLowerBound(),
150: range.getLowerBound() <= 10);
151: assertTrue(
152: "Expecting the upper bound of the range to be around 30: "
153: + range.getUpperBound(),
154: range.getUpperBound() >= 30);
155:
156: }
157:
158: /**
159: * Check that setting a tool tip generator for a series does override the
160: * default generator.
161: */
162: public void testSetSeriesToolTipGenerator() {
163: XYPlot plot = (XYPlot) this .chart.getPlot();
164: XYItemRenderer renderer = plot.getRenderer();
165: StandardXYToolTipGenerator tt = new StandardXYToolTipGenerator();
166: renderer.setSeriesToolTipGenerator(0, tt);
167: XYToolTipGenerator tt2 = renderer.getToolTipGenerator(0, 0);
168: assertTrue(tt2 == tt);
169: }
170:
171: /**
172: * Create a horizontal bar chart with sample data in the range -3 to +3.
173: *
174: * @return The chart.
175: */
176: private static JFreeChart createChart() {
177:
178: // create a dataset...
179: XYSeries series1 = new XYSeries("Series 1");
180: series1.add(1.0, 1.0);
181: series1.add(2.0, 2.0);
182: series1.add(3.0, 3.0);
183: XYDataset dataset = new XYSeriesCollection(series1);
184:
185: // create the chart...
186: return ChartFactory.createScatterPlot("Scatter Plot", // chart title
187: "Domain", "Range", dataset, // data
188: PlotOrientation.VERTICAL, true, // include legend
189: true, // tooltips
190: false // urls
191: );
192:
193: }
194:
195: /**
196: * A chart change listener.
197: *
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: }
|