StackedXYBarRendererTest.java 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. * StackedXYBarRendererTest.java
  29. * -----------------------------
  30. * (C) Copyright 2004-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. * 10-Sep-2004 : Version 1 (DG);
  38. * 06-Jan-2005 : Added test for auto range calculation (DG);
  39. * 06-Dec-2006 : Confirm serialization of GradientPaint (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 java.awt.Color;
  48. import java.awt.GradientPaint;
  49. import org.jfree.chart.ChartFactory;
  50. import org.jfree.chart.JFreeChart;
  51. import org.jfree.chart.TestUtilities;
  52. import org.jfree.chart.axis.NumberAxis;
  53. import org.jfree.chart.plot.PlotOrientation;
  54. import org.jfree.chart.plot.XYPlot;
  55. import org.jfree.data.Range;
  56. import org.jfree.data.xy.TableXYDataset;
  57. import org.jfree.util.PublicCloneable;
  58. import org.junit.Test;
  59. /**
  60. * Tests for the {@link StackedXYBarRenderer} class.
  61. */
  62. public class StackedXYBarRendererTest {
  63. /**
  64. * Test that the equals() method distinguishes all fields.
  65. */
  66. @Test
  67. public void testEquals() {
  68. StackedXYBarRenderer r1 = new StackedXYBarRenderer();
  69. StackedXYBarRenderer r2 = new StackedXYBarRenderer();
  70. assertTrue(r1.equals(r2));
  71. assertTrue(r2.equals(r1));
  72. r1.setRenderAsPercentages(true);
  73. assertFalse(r1.equals(r2));
  74. r2.setRenderAsPercentages(true);
  75. assertTrue(r1.equals(r2));
  76. }
  77. /**
  78. * Two objects that are equal are required to return the same hashCode.
  79. */
  80. @Test
  81. public void testHashcode() {
  82. StackedXYBarRenderer r1 = new StackedXYBarRenderer();
  83. StackedXYBarRenderer r2 = new StackedXYBarRenderer();
  84. assertTrue(r1.equals(r2));
  85. int h1 = r1.hashCode();
  86. int h2 = r2.hashCode();
  87. assertEquals(h1, h2);
  88. r1.setRenderAsPercentages(true);
  89. h1 = r1.hashCode();
  90. h2 = r2.hashCode();
  91. assertFalse(h1 == h2);
  92. }
  93. /**
  94. * Confirm that cloning works.
  95. */
  96. @Test
  97. public void testCloning() throws CloneNotSupportedException {
  98. StackedXYBarRenderer r1 = new StackedXYBarRenderer();
  99. StackedXYBarRenderer r2 = (StackedXYBarRenderer) r1.clone();
  100. assertTrue(r1 != r2);
  101. assertTrue(r1.getClass() == r2.getClass());
  102. assertTrue(r1.equals(r2));
  103. }
  104. /**
  105. * Verify that this class implements {@link PublicCloneable}.
  106. */
  107. @Test
  108. public void testPublicCloneable() {
  109. StackedXYBarRenderer r1 = new StackedXYBarRenderer();
  110. assertTrue(r1 instanceof PublicCloneable);
  111. }
  112. /**
  113. * Serialize an instance, restore it, and check for equality.
  114. */
  115. @Test
  116. public void testSerialization() {
  117. StackedXYBarRenderer r1 = new StackedXYBarRenderer();
  118. r1.setSeriesPaint(0, new GradientPaint(1.0f, 2.0f, Color.red, 3.0f,
  119. 4.0f, Color.yellow));
  120. StackedXYBarRenderer r2 = (StackedXYBarRenderer)
  121. TestUtilities.serialised(r1);
  122. assertEquals(r1, r2);
  123. }
  124. /**
  125. * Check that the renderer is calculating the domain bounds correctly.
  126. */
  127. @Test
  128. public void testFindDomainBounds() {
  129. TableXYDataset dataset
  130. = RendererXYPackageUtils.createTestTableXYDataset();
  131. JFreeChart chart = ChartFactory.createStackedXYAreaChart(
  132. "Test Chart", "X", "Y", dataset,
  133. PlotOrientation.VERTICAL, false, false, false);
  134. XYPlot plot = (XYPlot) chart.getPlot();
  135. plot.setRenderer(new StackedXYBarRenderer());
  136. NumberAxis domainAxis = (NumberAxis) plot.getDomainAxis();
  137. domainAxis.setAutoRangeIncludesZero(false);
  138. Range bounds = domainAxis.getRange();
  139. assertFalse(bounds.contains(0.3));
  140. assertTrue(bounds.contains(0.5));
  141. assertTrue(bounds.contains(2.5));
  142. assertFalse(bounds.contains(2.8));
  143. }
  144. /**
  145. * Check that the renderer is calculating the range bounds correctly.
  146. */
  147. @Test
  148. public void testFindRangeBounds() {
  149. TableXYDataset dataset
  150. = RendererXYPackageUtils.createTestTableXYDataset();
  151. JFreeChart chart = ChartFactory.createStackedXYAreaChart(
  152. "Test Chart", "X", "Y", dataset,
  153. PlotOrientation.VERTICAL, false, false, false);
  154. XYPlot plot = (XYPlot) chart.getPlot();
  155. plot.setRenderer(new StackedXYBarRenderer());
  156. NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
  157. Range bounds = rangeAxis.getRange();
  158. assertTrue(bounds.contains(6.0));
  159. assertTrue(bounds.contains(8.0));
  160. }
  161. }