XYBoxAnnotationTest.java 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. * XYBoxAnnotationTest.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. * 19-Jan-2005 : Version 1 (DG);
  38. * 26-Feb-2008 : Added testDrawWithNullInfo() method (DG);
  39. * 23-Apr-2008 : Added testPublicCloneable() (DG);
  40. *
  41. */
  42. package org.jfree.chart.annotations;
  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 java.awt.BasicStroke;
  48. import java.awt.Color;
  49. import java.awt.GradientPaint;
  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.XYPlot;
  54. import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
  55. import org.jfree.data.xy.DefaultTableXYDataset;
  56. import org.jfree.data.xy.XYSeries;
  57. import org.jfree.util.PublicCloneable;
  58. import org.junit.Test;
  59. /**
  60. * Some tests for the {@link XYBoxAnnotation} class.
  61. */
  62. public class XYBoxAnnotationTest {
  63. /**
  64. * Confirm that the equals method can distinguish all the required fields.
  65. */
  66. @Test
  67. public void testEquals() {
  68. XYBoxAnnotation a1 = new XYBoxAnnotation(1.0, 2.0, 3.0, 4.0,
  69. new BasicStroke(1.2f), Color.red, Color.blue);
  70. XYBoxAnnotation a2 = new XYBoxAnnotation(1.0, 2.0, 3.0, 4.0,
  71. new BasicStroke(1.2f), Color.red, Color.blue);
  72. assertTrue(a1.equals(a2));
  73. assertTrue(a2.equals(a1));
  74. // x0
  75. a1 = new XYBoxAnnotation(2.0, 2.0, 3.0, 4.0, new BasicStroke(1.2f),
  76. Color.red, Color.blue);
  77. assertFalse(a1.equals(a2));
  78. a2 = new XYBoxAnnotation(2.0, 2.0, 3.0, 4.0, new BasicStroke(1.2f),
  79. Color.red, Color.blue);
  80. assertTrue(a1.equals(a2));
  81. // stroke
  82. a1 = new XYBoxAnnotation(1.0, 2.0, 3.0, 4.0, new BasicStroke(2.3f),
  83. Color.red, Color.blue);
  84. assertFalse(a1.equals(a2));
  85. a2 = new XYBoxAnnotation(1.0, 2.0, 3.0, 4.0, new BasicStroke(2.3f),
  86. Color.red, Color.blue);
  87. assertTrue(a1.equals(a2));
  88. GradientPaint gp1a = new GradientPaint(1.0f, 2.0f, Color.blue,
  89. 3.0f, 4.0f, Color.red);
  90. GradientPaint gp1b = new GradientPaint(1.0f, 2.0f, Color.blue,
  91. 3.0f, 4.0f, Color.red);
  92. GradientPaint gp2a = new GradientPaint(5.0f, 6.0f, Color.pink,
  93. 7.0f, 8.0f, Color.white);
  94. GradientPaint gp2b = new GradientPaint(5.0f, 6.0f, Color.pink,
  95. 7.0f, 8.0f, Color.white);
  96. // outlinePaint
  97. a1 = new XYBoxAnnotation(1.0, 2.0, 3.0, 4.0, new BasicStroke(2.3f),
  98. gp1a, Color.blue);
  99. assertFalse(a1.equals(a2));
  100. a2 = new XYBoxAnnotation(1.0, 2.0, 3.0, 4.0, new BasicStroke(2.3f),
  101. gp1b, Color.blue);
  102. assertTrue(a1.equals(a2));
  103. // fillPaint
  104. a1 = new XYBoxAnnotation(1.0, 2.0, 3.0, 4.0, new BasicStroke(2.3f),
  105. gp1a, gp2a);
  106. assertFalse(a1.equals(a2));
  107. a2 = new XYBoxAnnotation(1.0, 2.0, 3.0, 4.0, new BasicStroke(2.3f),
  108. gp1b, gp2b);
  109. assertTrue(a1.equals(a2));
  110. }
  111. /**
  112. * Two objects that are equal are required to return the same hashCode.
  113. */
  114. public void testHashCode() {
  115. XYBoxAnnotation a1 = new XYBoxAnnotation(1.0, 2.0, 3.0, 4.0,
  116. new BasicStroke(1.2f), Color.red, Color.blue);
  117. XYBoxAnnotation a2 = new XYBoxAnnotation(1.0, 2.0, 3.0, 4.0,
  118. new BasicStroke(1.2f), Color.red, Color.blue);
  119. assertTrue(a1.equals(a2));
  120. int h1 = a1.hashCode();
  121. int h2 = a2.hashCode();
  122. assertEquals(h1, h2);
  123. }
  124. /**
  125. * Confirm that cloning works.
  126. */
  127. public void testCloning() throws CloneNotSupportedException {
  128. XYBoxAnnotation a1 = new XYBoxAnnotation(1.0, 2.0, 3.0, 4.0,
  129. new BasicStroke(1.2f), Color.red, Color.blue);
  130. XYBoxAnnotation a2 = (XYBoxAnnotation) a1.clone();
  131. assertTrue(a1 != a2);
  132. assertTrue(a1.getClass() == a2.getClass());
  133. assertTrue(a1.equals(a2));
  134. }
  135. /**
  136. * Checks that this class implements PublicCloneable.
  137. */
  138. public void testPublicCloneable() {
  139. XYBoxAnnotation a1 = new XYBoxAnnotation(1.0, 2.0, 3.0, 4.0,
  140. new BasicStroke(1.2f), Color.red, Color.blue);
  141. assertTrue(a1 instanceof PublicCloneable);
  142. }
  143. /**
  144. * Serialize an instance, restore it, and check for equality.
  145. */
  146. public void testSerialization() {
  147. XYBoxAnnotation a1 = new XYBoxAnnotation(1.0, 2.0, 3.0, 4.0,
  148. new BasicStroke(1.2f), Color.red, Color.blue);
  149. XYBoxAnnotation a2 = (XYBoxAnnotation) TestUtilities.serialised(a1);
  150. assertEquals(a1, a2);
  151. }
  152. /**
  153. * Draws the chart with a <code>null</code> info object to make sure that
  154. * no exceptions are thrown.
  155. */
  156. public void testDrawWithNullInfo() {
  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, 15.5);
  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, 9.5);
  169. s2.add(20.0, 3.5);
  170. dataset.addSeries(s2);
  171. XYPlot plot = new XYPlot(dataset,
  172. new NumberAxis("X"), new NumberAxis("Y"),
  173. new XYLineAndShapeRenderer());
  174. plot.addAnnotation(new XYBoxAnnotation(10.0, 12.0, 3.0, 4.0,
  175. new BasicStroke(1.2f), Color.red, Color.blue));
  176. JFreeChart chart = new JFreeChart(plot);
  177. /* BufferedImage image = */ chart.createBufferedImage(300, 200,
  178. null);
  179. }
  180. catch (NullPointerException e) {
  181. fail("No exception should be triggered.");
  182. }
  183. }
  184. }