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. * BarChart3DTests.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: BarChart3DTests.java,v 1.1 2004/08/31 14:35:34 mungady Exp $
  33. *
  34. * Changes:
  35. * --------
  36. * 11-Jun-2002 : Version 1 (DG);
  37. * 25-Jun-2002 : Removed redundant code (DG);
  38. * 17-Oct-2002 : Fixed errors reported by Checkstyle (DG);
  39. * 14-Jul-2003 : Renamed BarChart3DTests.java (DG);
  40. *
  41. */
  42. package org.jfree.chart.junit;
  43. import java.awt.Graphics2D;
  44. import java.awt.geom.Rectangle2D;
  45. import java.awt.image.BufferedImage;
  46. import junit.framework.Test;
  47. import junit.framework.TestCase;
  48. import junit.framework.TestSuite;
  49. import org.jfree.chart.ChartFactory;
  50. import org.jfree.chart.JFreeChart;
  51. import org.jfree.chart.axis.ValueAxis;
  52. import org.jfree.chart.event.ChartChangeEvent;
  53. import org.jfree.chart.event.ChartChangeListener;
  54. import org.jfree.chart.plot.PlotOrientation;
  55. import org.jfree.data.Range;
  56. import org.jfree.data.category.CategoryDataset;
  57. import org.jfree.data.general.DatasetUtilities;
  58. /**
  59. * Tests for a 3D bar chart.
  60. *
  61. */
  62. public class BarChart3DTests extends TestCase {
  63. /** The chart. */
  64. private JFreeChart chart;
  65. /**
  66. * Returns the tests as a test suite.
  67. *
  68. * @return the test suite.
  69. */
  70. public static Test suite() {
  71. return new TestSuite(BarChart3DTests.class);
  72. }
  73. /**
  74. * Constructs a new set of tests.
  75. *
  76. * @param name the name of the tests.
  77. */
  78. public BarChart3DTests(String name) {
  79. super(name);
  80. }
  81. /**
  82. * Common test setup.
  83. */
  84. protected void setUp() {
  85. this.chart = createBarChart3D();
  86. }
  87. /**
  88. * Draws the chart with a null info object to make sure that no exceptions are thrown (a
  89. * problem that was occurring at one point).
  90. */
  91. public void testDrawWithNullInfo() {
  92. boolean success = false;
  93. try {
  94. BufferedImage image = new BufferedImage(200 , 100, BufferedImage.TYPE_INT_RGB);
  95. Graphics2D g2 = image.createGraphics();
  96. this.chart.draw(g2, new Rectangle2D.Double(0, 0, 200, 100), null, null);
  97. g2.dispose();
  98. success = true;
  99. }
  100. catch (Exception e) {
  101. success = false;
  102. }
  103. assertTrue(success);
  104. }
  105. /**
  106. * Replaces the dataset and checks that the data range is as expected.
  107. */
  108. public void testReplaceDataset() {
  109. // create a dataset...
  110. Number[][] data = new Integer[][]
  111. {{new Integer(-30), new Integer(-20)},
  112. {new Integer(-10), new Integer(10)},
  113. {new Integer(20), new Integer(30)}};
  114. CategoryDataset newData = DatasetUtilities.createCategoryDataset("S", "C", data);
  115. LocalListener l = new LocalListener();
  116. this.chart.addChangeListener(l);
  117. this.chart.getCategoryPlot().setDataset(newData);
  118. assertEquals(true, l.flag);
  119. ValueAxis axis = this.chart.getCategoryPlot().getRangeAxis();
  120. Range range = axis.getRange();
  121. assertTrue("Expecting the lower bound of the range to be around -30: "
  122. + range.getLowerBound(), range.getLowerBound() <= -30);
  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 createBarChart3D() {
  132. // create a dataset...
  133. Number[][] data = new Integer[][]
  134. {{new Integer(-3), new Integer(-2)},
  135. {new Integer(-1), new Integer(1)},
  136. {new Integer(2), new Integer(3)}};
  137. CategoryDataset dataset = DatasetUtilities.createCategoryDataset("S", "C", data);
  138. // create the chart...
  139. return ChartFactory.createBarChart3D(
  140. "Bar Chart 3D",
  141. "Domain",
  142. "Range",
  143. dataset,
  144. PlotOrientation.HORIZONTAL,
  145. true, // include legend
  146. true,
  147. false
  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. }