1. /* ===========================================================
  2. * JFreeChart : a free chart library for the Java(tm) platform
  3. * ===========================================================
  4. *
  5. * (C) Copyright 2000-2004, by Object Refinery Limited and Contributors.
  6. *
  7. * Project Info: http://www.jfree.org/jfreechart/index.html
  8. *
  9. * This library is free software; you can redistribute it and/or modify it under the terms
  10. * of the GNU Lesser General Public License as published by the Free Software Foundation;
  11. * either version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  14. * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. * See the GNU Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License along with this
  18. * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  19. * Boston, MA 02111-1307, USA.
  20. *
  21. * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
  22. * in the United States and other countries.]
  23. *
  24. * ---------------------
  25. * ScatterPlotTests.java
  26. * ---------------------
  27. * (C) Copyright 2002-2004, by Object Refinery Limited.
  28. *
  29. * Original Author: David Gilbert (for Object Refinery Limited);
  30. * Contributor(s): -;
  31. *
  32. * $Id: ScatterPlotTests.java,v 1.1 2004/08/31 14:35:34 mungady Exp $
  33. *
  34. * Changes:
  35. * --------
  36. * 11-Jun-2002 : Version 1 (DG);
  37. * 17-Oct-2002 : Fixed errors reported by Checkstyle (DG);
  38. *
  39. */
  40. package org.jfree.chart.junit;
  41. import java.awt.Graphics2D;
  42. import java.awt.geom.Rectangle2D;
  43. import java.awt.image.BufferedImage;
  44. import junit.framework.Test;
  45. import junit.framework.TestCase;
  46. import junit.framework.TestSuite;
  47. import org.jfree.chart.ChartFactory;
  48. import org.jfree.chart.JFreeChart;
  49. import org.jfree.chart.axis.ValueAxis;
  50. import org.jfree.chart.event.ChartChangeEvent;
  51. import org.jfree.chart.event.ChartChangeListener;
  52. import org.jfree.chart.plot.PlotOrientation;
  53. import org.jfree.data.Range;
  54. import org.jfree.data.xy.XYDataset;
  55. import org.jfree.data.xy.XYSeries;
  56. import org.jfree.data.xy.XYSeriesCollection;
  57. /**
  58. * Tests for a scatter plot.
  59. *
  60. */
  61. public class ScatterPlotTests extends TestCase {
  62. /** A chart. */
  63. private JFreeChart chart;
  64. /**
  65. * Returns the tests as a test suite.
  66. *
  67. * @return the test suite.
  68. */
  69. public static Test suite() {
  70. return new TestSuite(ScatterPlotTests.class);
  71. }
  72. /**
  73. * Constructs a new set of tests.
  74. *
  75. * @param name the name of the tests.
  76. */
  77. public ScatterPlotTests(String name) {
  78. super(name);
  79. }
  80. /**
  81. * Common test setup.
  82. */
  83. protected void setUp() {
  84. this.chart = createChart();
  85. }
  86. /**
  87. * Draws the chart with a null info object to make sure that no exceptions are thrown (a
  88. * problem that was occurring at one point).
  89. */
  90. public void testDrawWithNullInfo() {
  91. boolean success = false;
  92. try {
  93. BufferedImage image = new BufferedImage(200 , 100, BufferedImage.TYPE_INT_RGB);
  94. Graphics2D g2 = image.createGraphics();
  95. this.chart.draw(g2, new Rectangle2D.Double(0, 0, 200, 100), null, null);
  96. g2.dispose();
  97. success = true;
  98. }
  99. catch (Exception e) {
  100. success = false;
  101. e.printStackTrace();
  102. }
  103. assertTrue(success);
  104. }
  105. /**
  106. * Replaces the dataset and checks that it has changed as expected.
  107. */
  108. public void testReplaceDataset() {
  109. // create a dataset...
  110. XYSeries series1 = new XYSeries("Series 1");
  111. series1.add(10.0, 10.0);
  112. series1.add(20.0, 20.0);
  113. series1.add(30.0, 30.0);
  114. XYDataset dataset = new XYSeriesCollection(series1);
  115. LocalListener l = new LocalListener();
  116. this.chart.addChangeListener(l);
  117. this.chart.getXYPlot().setDataset(dataset);
  118. assertEquals(true, l.flag);
  119. ValueAxis axis = this.chart.getXYPlot().getRangeAxis();
  120. Range range = axis.getRange();
  121. assertTrue("Expecting the lower bound of the range to be around 10: "
  122. + range.getLowerBound(), range.getLowerBound() <= 10);
  123. assertTrue("Expecting the upper bound of the range to be around 30: "
  124. + range.getUpperBound(), range.getUpperBound() >= 30);
  125. }
  126. /**
  127. * Create a horizontal bar chart with sample data in the range -3 to +3.
  128. *
  129. * @return the chart.
  130. */
  131. private static JFreeChart createChart() {
  132. // create a dataset...
  133. XYSeries series1 = new XYSeries("Series 1");
  134. series1.add(1.0, 1.0);
  135. series1.add(2.0, 2.0);
  136. series1.add(3.0, 3.0);
  137. XYDataset dataset = new XYSeriesCollection(series1);
  138. // create the chart...
  139. return ChartFactory.createScatterPlot(
  140. "Scatter Plot", // chart title
  141. "Domain",
  142. "Range",
  143. dataset, // data
  144. PlotOrientation.VERTICAL,
  145. true, // include legend
  146. true, // tooltips
  147. false // urls
  148. );
  149. }
  150. /**
  151. * A chart change listener.
  152. *
  153. */
  154. static class LocalListener implements ChartChangeListener {
  155. /** A flag. */
  156. private boolean flag = false;
  157. /**
  158. * Event handler.
  159. *
  160. * @param event the event.
  161. */
  162. public void chartChanged(ChartChangeEvent event) {
  163. this.flag = true;
  164. }
  165. }
  166. }