XYBlockRendererTest.java 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. * XYBlockRendererTest.java
  29. * ------------------------
  30. * (C) Copyright 2006-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. * 05-Jul-2006 : Version 1 (DG);
  38. * 09-Mar-2007 : Added independence check to testCloning (DG);
  39. * 22-Apr-2008 : Added testPublicCloneable (DG);
  40. * 20-Oct-2011 : Added testFindDomainBounds() and testFindRangeBounds() (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.Color;
  49. import org.jfree.chart.TestUtilities;
  50. import org.jfree.chart.renderer.GrayPaintScale;
  51. import org.jfree.chart.renderer.LookupPaintScale;
  52. import org.jfree.data.Range;
  53. import org.jfree.data.xy.DefaultXYZDataset;
  54. import org.jfree.data.xy.XYSeriesCollection;
  55. import org.jfree.data.xy.XYSeries;
  56. import org.jfree.util.PublicCloneable;
  57. import org.junit.Test;
  58. /**
  59. * Tests for the {@link XYBlockRenderer} class.
  60. */
  61. public class XYBlockRendererTest {
  62. private static final double EPSILON = 0.0000000001;
  63. /**
  64. * Test that the equals() method distinguishes all fields.
  65. */
  66. @Test
  67. public void testEquals() {
  68. // default instances
  69. XYBlockRenderer r1 = new XYBlockRenderer();
  70. XYBlockRenderer r2 = new XYBlockRenderer();
  71. assertTrue(r1.equals(r2));
  72. assertTrue(r2.equals(r1));
  73. // blockHeight
  74. r1.setBlockHeight(2.0);
  75. assertFalse(r1.equals(r2));
  76. r2.setBlockHeight(2.0);
  77. assertTrue(r1.equals(r2));
  78. // blockWidth
  79. r1.setBlockWidth(2.0);
  80. assertFalse(r1.equals(r2));
  81. r2.setBlockWidth(2.0);
  82. assertTrue(r1.equals(r2));
  83. // paintScale
  84. r1.setPaintScale(new GrayPaintScale(0.0, 1.0));
  85. assertFalse(r1.equals(r2));
  86. r2.setPaintScale(new GrayPaintScale(0.0, 1.0));
  87. assertTrue(r1.equals(r2));
  88. }
  89. /**
  90. * Two objects that are equal are required to return the same hashCode.
  91. */
  92. @Test
  93. public void testHashcode() {
  94. XYBlockRenderer r1 = new XYBlockRenderer();
  95. XYBlockRenderer r2 = new XYBlockRenderer();
  96. assertTrue(r1.equals(r2));
  97. int h1 = r1.hashCode();
  98. int h2 = r2.hashCode();
  99. assertEquals(h1, h2);
  100. }
  101. /**
  102. * Confirm that cloning works.
  103. */
  104. @Test
  105. public void testCloning() throws CloneNotSupportedException {
  106. XYBlockRenderer r1 = new XYBlockRenderer();
  107. LookupPaintScale scale1 = new LookupPaintScale();
  108. r1.setPaintScale(scale1);
  109. XYBlockRenderer r2 = (XYBlockRenderer) r1.clone();
  110. assertTrue(r1 != r2);
  111. assertTrue(r1.getClass() == r2.getClass());
  112. assertTrue(r1.equals(r2));
  113. // check independence
  114. scale1.add(0.5, Color.red);
  115. assertFalse(r1.equals(r2));
  116. LookupPaintScale scale2 = (LookupPaintScale) r2.getPaintScale();
  117. scale2.add(0.5, Color.red);
  118. assertTrue(r1.equals(r2));
  119. }
  120. /**
  121. * Verify that this class implements {@link PublicCloneable}.
  122. */
  123. @Test
  124. public void testPublicCloneable() {
  125. XYBlockRenderer r1 = new XYBlockRenderer();
  126. assertTrue(r1 instanceof PublicCloneable);
  127. }
  128. /**
  129. * Serialize an instance, restore it, and check for equality.
  130. */
  131. @Test
  132. public void testSerialization() {
  133. XYBlockRenderer r1 = new XYBlockRenderer();
  134. XYBlockRenderer r2 = (XYBlockRenderer) TestUtilities.serialised(r1);
  135. assertEquals(r1, r2);
  136. }
  137. /**
  138. * A simple test for bug 1766646.
  139. */
  140. @Test
  141. public void testBug1766646A() {
  142. XYBlockRenderer r = new XYBlockRenderer();
  143. Range range = r.findDomainBounds(null);
  144. assertTrue(range == null);
  145. DefaultXYZDataset emptyDataset = new DefaultXYZDataset();
  146. range = r.findDomainBounds(emptyDataset);
  147. assertTrue(range == null);
  148. }
  149. /**
  150. * A simple test for bug 1766646.
  151. */
  152. @Test
  153. public void testBug1766646B() {
  154. XYBlockRenderer r = new XYBlockRenderer();
  155. Range range = r.findRangeBounds(null);
  156. assertTrue(range == null);
  157. DefaultXYZDataset emptyDataset = new DefaultXYZDataset();
  158. range = r.findRangeBounds(emptyDataset);
  159. assertTrue(range == null);
  160. }
  161. /**
  162. * Some tests for the findRangeBounds() method.
  163. */
  164. @Test
  165. public void testFindRangeBounds() {
  166. XYBlockRenderer renderer = new XYBlockRenderer();
  167. assertNull(renderer.findRangeBounds(null));
  168. XYSeriesCollection dataset = new XYSeriesCollection();
  169. XYSeries series = new XYSeries("S1");
  170. series.add(1.0, null);
  171. dataset.addSeries(series);
  172. Range r = renderer.findRangeBounds(dataset);
  173. assertNull(r);
  174. }
  175. /**
  176. * Some tests for the findDomainBounds() method.
  177. */
  178. @Test
  179. public void testFindDomainBounds() {
  180. XYBlockRenderer renderer = new XYBlockRenderer();
  181. assertNull(renderer.findRangeBounds(null));
  182. XYSeriesCollection dataset = new XYSeriesCollection();
  183. XYSeries series = new XYSeries("S1");
  184. series.add(1.0, null);
  185. dataset.addSeries(series);
  186. Range r = renderer.findDomainBounds(dataset);
  187. assertEquals(0.5, r.getLowerBound(), EPSILON);
  188. assertEquals(1.5, r.getUpperBound(), EPSILON);
  189. dataset.removeAllSeries();
  190. r = renderer.findDomainBounds(dataset);
  191. assertNull(r);
  192. }
  193. }