StackedXYAreaRenderer2Test.java 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /* ===========================================================
  2. * JFreeChart : a free chart library for the Java(tm) platform
  3. * ===========================================================
  4. *
  5. * (C) Copyright 2000-2013, 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
  10. * under the terms of the GNU Lesser General Public License as published by
  11. * the Free Software Foundation; either version 2.1 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This library is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  16. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  17. * License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
  22. * USA.
  23. *
  24. * [Oracle and Java are registered trademarks of Oracle and/or its affiliates.
  25. * Other names may be trademarks of their respective owners.]
  26. *
  27. * -------------------------------
  28. * StackedXYAreaRenderer2Test.java
  29. * -------------------------------
  30. * (C) Copyright 2005-2013, by Object Refinery Limited and Contributors.
  31. *
  32. * Original Author: David Gilbert (for Object Refinery Limited);
  33. * Contributor(s): -;
  34. *
  35. * Changes
  36. * -------
  37. * 06-Jan-2005 : Version 1 (DG);
  38. * 22-Aug-2006 : Added testDrawWithEmptyDataset() method (DG);
  39. * 30-Nov-2006 : Extended testEquals() (DG);
  40. * 22-Apr-2008 : Added testPublicCloneable (DG);
  41. *
  42. */
  43. package org.jfree.chart.renderer.xy;
  44. import static org.junit.Assert.assertEquals;
  45. import static org.junit.Assert.assertTrue;
  46. import static org.junit.Assert.assertFalse;
  47. import static org.junit.Assert.assertNull;
  48. import java.awt.Graphics2D;
  49. import java.awt.geom.Rectangle2D;
  50. import java.awt.image.BufferedImage;
  51. import org.jfree.chart.ChartFactory;
  52. import org.jfree.chart.JFreeChart;
  53. import org.jfree.chart.TestUtilities;
  54. import org.jfree.chart.axis.NumberAxis;
  55. import org.jfree.chart.plot.PlotOrientation;
  56. import org.jfree.chart.plot.XYPlot;
  57. import org.jfree.data.Range;
  58. import org.jfree.data.xy.DefaultTableXYDataset;
  59. import org.jfree.data.xy.TableXYDataset;
  60. import org.jfree.util.PublicCloneable;
  61. import org.junit.Test;
  62. /**
  63. * Tests for the {@link StackedXYAreaRenderer2} class.
  64. */
  65. public class StackedXYAreaRenderer2Test {
  66. /**
  67. * Test chart drawing with an empty dataset to ensure that this special
  68. * case doesn't cause any exceptions.
  69. */
  70. @Test
  71. public void testDrawWithEmptyDataset() {
  72. boolean success = false;
  73. JFreeChart chart = ChartFactory.createStackedXYAreaChart("title", "x",
  74. "y", new DefaultTableXYDataset(), PlotOrientation.VERTICAL,
  75. true, false, false);
  76. XYPlot plot = (XYPlot) chart.getPlot();
  77. plot.setRenderer(new StackedXYAreaRenderer2());
  78. try {
  79. BufferedImage image = new BufferedImage(200 , 100,
  80. BufferedImage.TYPE_INT_RGB);
  81. Graphics2D g2 = image.createGraphics();
  82. chart.draw(g2, new Rectangle2D.Double(0, 0, 200, 100), null, null);
  83. g2.dispose();
  84. success = true;
  85. }
  86. catch (Exception e) {
  87. success = false;
  88. }
  89. assertTrue(success);
  90. }
  91. /**
  92. * Test that the equals() method distinguishes all fields.
  93. */
  94. @Test
  95. public void testEquals() {
  96. StackedXYAreaRenderer2 r1 = new StackedXYAreaRenderer2();
  97. StackedXYAreaRenderer2 r2 = new StackedXYAreaRenderer2();
  98. assertEquals(r1, r2);
  99. assertEquals(r2, r1);
  100. r1.setRoundXCoordinates(!r1.getRoundXCoordinates());
  101. assertFalse(r1.equals(r2));
  102. r2.setRoundXCoordinates(r1.getRoundXCoordinates());
  103. assertTrue(r1.equals(r2));
  104. }
  105. /**
  106. * Two objects that are equal are required to return the same hashCode.
  107. */
  108. @Test
  109. public void testHashcode() {
  110. StackedXYAreaRenderer2 r1 = new StackedXYAreaRenderer2();
  111. StackedXYAreaRenderer2 r2 = new StackedXYAreaRenderer2();
  112. assertTrue(r1.equals(r2));
  113. int h1 = r1.hashCode();
  114. int h2 = r2.hashCode();
  115. assertEquals(h1, h2);
  116. }
  117. /**
  118. * Confirm that cloning works.
  119. */
  120. @Test
  121. public void testCloning() throws CloneNotSupportedException {
  122. StackedXYAreaRenderer2 r1 = new StackedXYAreaRenderer2();
  123. StackedXYAreaRenderer2 r2 = (StackedXYAreaRenderer2) r1.clone();
  124. assertTrue(r1 != r2);
  125. assertTrue(r1.getClass() == r2.getClass());
  126. assertTrue(r1.equals(r2));
  127. }
  128. /**
  129. * Verify that this class implements {@link PublicCloneable}.
  130. */
  131. @Test
  132. public void testPublicCloneable() {
  133. StackedXYAreaRenderer2 r1 = new StackedXYAreaRenderer2();
  134. assertTrue(r1 instanceof PublicCloneable);
  135. }
  136. /**
  137. * Serialize an instance, restore it, and check for equality.
  138. */
  139. @Test
  140. public void testSerialization() {
  141. StackedXYAreaRenderer2 r1 = new StackedXYAreaRenderer2();
  142. StackedXYAreaRenderer2 r2 = (StackedXYAreaRenderer2)
  143. TestUtilities.serialised(r1);
  144. assertEquals(r1, r2);
  145. }
  146. /**
  147. * Check that the renderer is calculating the range bounds correctly.
  148. */
  149. @Test
  150. public void testFindRangeBounds() {
  151. TableXYDataset dataset
  152. = RendererXYPackageUtils.createTestTableXYDataset();
  153. JFreeChart chart = ChartFactory.createStackedXYAreaChart(
  154. "Test Chart", "X", "Y", dataset, PlotOrientation.VERTICAL,
  155. false, false, false);
  156. XYPlot plot = (XYPlot) chart.getPlot();
  157. StackedXYAreaRenderer2 renderer = new StackedXYAreaRenderer2();
  158. plot.setRenderer(renderer);
  159. NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
  160. Range bounds = rangeAxis.getRange();
  161. assertTrue(bounds.contains(6.0));
  162. assertTrue(bounds.contains(8.0));
  163. // try null argument
  164. assertNull(renderer.findRangeBounds(null));
  165. // try empty dataset
  166. assertNull(renderer.findRangeBounds(new DefaultTableXYDataset()));
  167. }
  168. }