XYStepRendererTest.java 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. * XYStepRendererTest.java
  29. * -----------------------
  30. * (C) Copyright 2003-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. * 25-Mar-2003 : Version 1 (DG);
  38. * 14-Feb-2008 : Added checks for new code (DG);
  39. * 22-Apr-2008 : Added testPublicCloneable() (DG);
  40. *
  41. */
  42. package org.jfree.chart.renderer.xy;
  43. import static org.junit.Assert.assertEquals;
  44. import static org.junit.Assert.assertTrue;
  45. import static org.junit.Assert.assertFalse;
  46. import static org.junit.Assert.fail;
  47. import org.jfree.chart.JFreeChart;
  48. import org.jfree.chart.TestUtilities;
  49. import org.jfree.chart.axis.NumberAxis;
  50. import org.jfree.chart.plot.XYPlot;
  51. import org.jfree.data.xy.DefaultTableXYDataset;
  52. import org.jfree.data.xy.XYSeries;
  53. import org.jfree.util.PublicCloneable;
  54. import org.junit.Test;
  55. /**
  56. * Tests for the {@link XYStepRenderer} class.
  57. */
  58. public class XYStepRendererTest {
  59. /**
  60. * Check that the equals() method distinguishes all fields.
  61. */
  62. @Test
  63. public void testEquals() {
  64. XYStepRenderer r1 = new XYStepRenderer();
  65. XYStepRenderer r2 = new XYStepRenderer();
  66. assertEquals(r1, r2);
  67. r1.setStepPoint(0.44);
  68. assertFalse(r1.equals(r2));
  69. r2.setStepPoint(0.44);
  70. assertTrue(r1.equals(r2));
  71. // try something from the base class
  72. r1.setBaseCreateEntities(false);
  73. assertFalse(r1.equals(r2));
  74. r2.setBaseCreateEntities(false);
  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. XYStepRenderer r1 = new XYStepRenderer();
  83. r1.setStepPoint(0.123);
  84. XYStepRenderer r2 = new XYStepRenderer();
  85. r2.setStepPoint(0.123);
  86. assertTrue(r1.equals(r2));
  87. int h1 = r1.hashCode();
  88. int h2 = r2.hashCode();
  89. assertEquals(h1, h2);
  90. }
  91. /**
  92. * Confirm that cloning works.
  93. */
  94. @Test
  95. public void testCloning() throws CloneNotSupportedException {
  96. XYStepRenderer r1 = new XYStepRenderer();
  97. XYStepRenderer r2 = (XYStepRenderer) r1.clone();
  98. assertTrue(r1 != r2);
  99. assertTrue(r1.getClass() == r2.getClass());
  100. assertTrue(r1.equals(r2));
  101. }
  102. /**
  103. * Verify that this class implements {@link PublicCloneable}.
  104. */
  105. @Test
  106. public void testPublicCloneable() {
  107. XYStepRenderer r1 = new XYStepRenderer();
  108. assertTrue(r1 instanceof PublicCloneable);
  109. }
  110. /**
  111. * Serialize an instance, restore it, and check for equality.
  112. */
  113. @Test
  114. public void testSerialization() {
  115. XYStepRenderer r1 = new XYStepRenderer();
  116. r1.setStepPoint(0.123);
  117. XYStepRenderer r2 = (XYStepRenderer) TestUtilities.serialised(r1);
  118. assertEquals(r1, r2);
  119. }
  120. /**
  121. * Draws the chart with a <code>null</code> info object to make sure that
  122. * no exceptions are thrown (particularly by code in the renderer).
  123. */
  124. @Test
  125. public void testDrawWithNullInfo() {
  126. try {
  127. DefaultTableXYDataset dataset = new DefaultTableXYDataset();
  128. XYSeries s1 = new XYSeries("Series 1", true, false);
  129. s1.add(5.0, 5.0);
  130. s1.add(10.0, 15.5);
  131. s1.add(15.0, 9.5);
  132. s1.add(20.0, 7.5);
  133. dataset.addSeries(s1);
  134. XYSeries s2 = new XYSeries("Series 2", true, false);
  135. s2.add(5.0, 5.0);
  136. s2.add(10.0, 15.5);
  137. s2.add(15.0, 9.5);
  138. s2.add(20.0, 3.5);
  139. dataset.addSeries(s2);
  140. XYPlot plot = new XYPlot(dataset,
  141. new NumberAxis("X"), new NumberAxis("Y"),
  142. new XYStepRenderer());
  143. JFreeChart chart = new JFreeChart(plot);
  144. /* BufferedImage image = */ chart.createBufferedImage(300, 200,
  145. null);
  146. }
  147. catch (NullPointerException e) {
  148. fail("No exception should be thrown.");
  149. }
  150. }
  151. /**
  152. * Draws the chart with a <code>null</code> value in the dataset to make
  153. * sure that no exceptions are thrown.
  154. */
  155. @Test
  156. public void testDrawWithNullValue() {
  157. try {
  158. DefaultTableXYDataset dataset = new DefaultTableXYDataset();
  159. XYSeries s1 = new XYSeries("Series 1", true, false);
  160. s1.add(5.0, 5.0);
  161. s1.add(10.0, null);
  162. s1.add(15.0, 9.5);
  163. s1.add(20.0, 7.5);
  164. dataset.addSeries(s1);
  165. XYSeries s2 = new XYSeries("Series 2", true, false);
  166. s2.add(5.0, 5.0);
  167. s2.add(10.0, 15.5);
  168. s2.add(15.0, null);
  169. s2.add(20.0, null);
  170. dataset.addSeries(s2);
  171. XYPlot plot = new XYPlot(dataset,
  172. new NumberAxis("X"), new NumberAxis("Y"),
  173. new XYStepRenderer());
  174. JFreeChart chart = new JFreeChart(plot);
  175. /* BufferedImage image = */ chart.createBufferedImage(300, 200,
  176. null);
  177. }
  178. catch (NullPointerException e) {
  179. fail("No exception should be thrown.");
  180. }
  181. }
  182. }