XYBoxAndWhiskerRendererTest.java 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. * XYBoxAndWhiskerRendererTest.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. * 22-Oct-2003 : Version 1 (DG);
  38. * 23-Apr-2004 : Extended testEquals() method (DG);
  39. * 27-Mar-2008 : Extended testEquals() some more (DG);
  40. * 22-Apr-2008 : Added testPublicCloneable (DG);
  41. * 08-Dec-2008 : Added test2909215() (DG);
  42. *
  43. */
  44. package org.jfree.chart.renderer.xy;
  45. import static org.junit.Assert.assertEquals;
  46. import static org.junit.Assert.assertTrue;
  47. import static org.junit.Assert.assertFalse;
  48. import static org.junit.Assert.fail;
  49. import java.awt.Color;
  50. import java.awt.GradientPaint;
  51. import java.awt.Graphics2D;
  52. import java.awt.geom.Rectangle2D;
  53. import java.awt.image.BufferedImage;
  54. import java.util.Date;
  55. import org.jfree.chart.ChartFactory;
  56. import org.jfree.chart.JFreeChart;
  57. import org.jfree.chart.TestUtilities;
  58. import org.jfree.data.statistics.BoxAndWhiskerItem;
  59. import org.jfree.data.statistics.DefaultBoxAndWhiskerXYDataset;
  60. import org.jfree.util.PublicCloneable;
  61. import org.junit.Test;
  62. /**
  63. * Tests for the {@link XYBoxAndWhiskerRenderer} class.
  64. */
  65. public class XYBoxAndWhiskerRendererTest {
  66. /**
  67. * Check that the equals() method distinguishes all fields.
  68. */
  69. @Test
  70. public void testEquals() {
  71. XYBoxAndWhiskerRenderer r1 = new XYBoxAndWhiskerRenderer();
  72. XYBoxAndWhiskerRenderer r2 = new XYBoxAndWhiskerRenderer();
  73. assertEquals(r1, r2);
  74. r1.setPaint(new GradientPaint(1.0f, 2.0f, Color.yellow,
  75. 3.0f, 4.0f, Color.red));
  76. assertFalse(r1.equals(r2));
  77. r2.setPaint(new GradientPaint(1.0f, 2.0f, Color.yellow,
  78. 3.0f, 4.0f, Color.red));
  79. assertEquals(r1, r2);
  80. r1.setArtifactPaint(new GradientPaint(1.0f, 2.0f, Color.green,
  81. 3.0f, 4.0f, Color.red));
  82. assertFalse(r1.equals(r2));
  83. r2.setArtifactPaint(new GradientPaint(1.0f, 2.0f, Color.green,
  84. 3.0f, 4.0f, Color.red));
  85. assertEquals(r1, r2);
  86. r1.setBoxWidth(0.55);
  87. assertFalse(r1.equals(r2));
  88. r2.setBoxWidth(0.55);
  89. assertEquals(r1, r2);
  90. r1.setFillBox(!r1.getFillBox());
  91. assertFalse(r1.equals(r2));
  92. r2.setFillBox(!r2.getFillBox());
  93. assertEquals(r1, r2);
  94. r1.setBoxPaint(Color.yellow);
  95. assertFalse(r1.equals(r2));
  96. r2.setBoxPaint(Color.yellow);
  97. assertEquals(r1, r2);
  98. // check boxPaint null also
  99. r1.setBoxPaint(null);
  100. assertFalse(r1.equals(r2));
  101. r2.setBoxPaint(null);
  102. assertEquals(r1, r2);
  103. }
  104. /**
  105. * Two objects that are equal are required to return the same hashCode.
  106. */
  107. @Test
  108. public void testHashcode() {
  109. XYBoxAndWhiskerRenderer r1 = new XYBoxAndWhiskerRenderer();
  110. XYBoxAndWhiskerRenderer r2 = new XYBoxAndWhiskerRenderer();
  111. assertTrue(r1.equals(r2));
  112. int h1 = r1.hashCode();
  113. int h2 = r2.hashCode();
  114. assertEquals(h1, h2);
  115. }
  116. /**
  117. * Confirm that cloning works.
  118. */
  119. @Test
  120. public void testCloning() throws CloneNotSupportedException {
  121. XYBoxAndWhiskerRenderer r1 = new XYBoxAndWhiskerRenderer();
  122. XYBoxAndWhiskerRenderer r2 = (XYBoxAndWhiskerRenderer) r1.clone();
  123. assertTrue(r1 != r2);
  124. assertTrue(r1.getClass() == r2.getClass());
  125. assertTrue(r1.equals(r2));
  126. }
  127. /**
  128. * Verify that this class implements {@link PublicCloneable}.
  129. */
  130. @Test
  131. public void testPublicCloneable() {
  132. XYBoxAndWhiskerRenderer r1 = new XYBoxAndWhiskerRenderer();
  133. assertTrue(r1 instanceof PublicCloneable);
  134. }
  135. /**
  136. * Serialize an instance, restore it, and check for equality.
  137. */
  138. @Test
  139. public void testSerialization() {
  140. XYBoxAndWhiskerRenderer r1 = new XYBoxAndWhiskerRenderer();
  141. XYBoxAndWhiskerRenderer r2 = (XYBoxAndWhiskerRenderer)
  142. TestUtilities.serialised(r1);
  143. assertEquals(r1, r2);
  144. }
  145. /**
  146. * A test for bug report 2909215.
  147. */
  148. @Test
  149. public void test2909215() {
  150. DefaultBoxAndWhiskerXYDataset d1 = new DefaultBoxAndWhiskerXYDataset(
  151. "Series");
  152. d1.add(new Date(1L), new BoxAndWhiskerItem(new Double(1.0),
  153. new Double(2.0), new Double(3.0), new Double(4.0),
  154. new Double(5.0), new Double(6.0), null, null, null));
  155. JFreeChart chart = ChartFactory.createBoxAndWhiskerChart("Title", "X",
  156. "Y", d1, true);
  157. try {
  158. BufferedImage image = new BufferedImage(400, 200,
  159. BufferedImage.TYPE_INT_RGB);
  160. Graphics2D g2 = image.createGraphics();
  161. chart.draw(g2, new Rectangle2D.Double(0, 0, 400, 200), null, null);
  162. g2.dispose();
  163. }
  164. catch (Exception e) {
  165. fail("No exception should be thrown.");
  166. }
  167. }
  168. }