BubbleXYItemLabelGeneratorTest.java 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. * BubbleXYItemLabelGeneratorTest.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. * 20-Jan-2006 : Version 1 (DG);
  38. * 23-Apr-2008 : Added testPublicCloneable() (DG);
  39. *
  40. */
  41. package org.jfree.chart.labels;
  42. import static org.junit.Assert.assertEquals;
  43. import static org.junit.Assert.assertFalse;
  44. import static org.junit.Assert.assertTrue;
  45. import java.text.DateFormat;
  46. import java.text.DecimalFormat;
  47. import java.text.NumberFormat;
  48. import java.text.SimpleDateFormat;
  49. import org.jfree.chart.TestUtilities;
  50. import org.jfree.data.xy.XYSeries;
  51. import org.jfree.data.xy.XYSeriesCollection;
  52. import org.jfree.util.PublicCloneable;
  53. import org.junit.Test;
  54. /**
  55. * Tests for the {@link BubbleXYItemLabelGenerator} class.
  56. */
  57. public class BubbleXYItemLabelGeneratorTest {
  58. /**
  59. * A series of tests for the equals() method.
  60. */
  61. @Test
  62. public void testEquals() {
  63. // some setup...
  64. String f1 = "{1}";
  65. String f2 = "{2}";
  66. NumberFormat xnf1 = new DecimalFormat("0.00");
  67. NumberFormat xnf2 = new DecimalFormat("0.000");
  68. NumberFormat ynf1 = new DecimalFormat("0.00");
  69. NumberFormat ynf2 = new DecimalFormat("0.000");
  70. NumberFormat znf1 = new DecimalFormat("0.00");
  71. NumberFormat znf2 = new DecimalFormat("0.000");
  72. BubbleXYItemLabelGenerator g1 = null;
  73. BubbleXYItemLabelGenerator g2 = null;
  74. g1 = new BubbleXYItemLabelGenerator(f1, xnf1, ynf1, znf1);
  75. g2 = new BubbleXYItemLabelGenerator(f1, xnf1, ynf1, znf1);
  76. assertTrue(g1.equals(g2));
  77. assertTrue(g2.equals(g1));
  78. g1 = new BubbleXYItemLabelGenerator(f2, xnf1, ynf1, znf1);
  79. assertFalse(g1.equals(g2));
  80. g2 = new BubbleXYItemLabelGenerator(f2, xnf1, ynf1, znf1);
  81. assertTrue(g1.equals(g2));
  82. g1 = new BubbleXYItemLabelGenerator(f2, xnf2, ynf1, znf1);
  83. assertFalse(g1.equals(g2));
  84. g2 = new BubbleXYItemLabelGenerator(f2, xnf2, ynf1, znf1);
  85. assertTrue(g1.equals(g2));
  86. g1 = new BubbleXYItemLabelGenerator(f2, xnf2, ynf2, znf1);
  87. assertFalse(g1.equals(g2));
  88. g2 = new BubbleXYItemLabelGenerator(f2, xnf2, ynf2, znf1);
  89. assertTrue(g1.equals(g2));
  90. g1 = new BubbleXYItemLabelGenerator(f2, xnf2, ynf2, znf2);
  91. assertFalse(g1.equals(g2));
  92. g2 = new BubbleXYItemLabelGenerator(f2, xnf2, ynf2, znf2);
  93. assertTrue(g1.equals(g2));
  94. DateFormat xdf1 = new SimpleDateFormat("d-MMM");
  95. DateFormat xdf2 = new SimpleDateFormat("d-MMM-yyyy");
  96. DateFormat ydf1 = new SimpleDateFormat("d-MMM");
  97. DateFormat ydf2 = new SimpleDateFormat("d-MMM-yyyy");
  98. DateFormat zdf1 = new SimpleDateFormat("d-MMM");
  99. DateFormat zdf2 = new SimpleDateFormat("d-MMM-yyyy");
  100. g1 = new BubbleXYItemLabelGenerator(f1, xdf1, ydf1, zdf1);
  101. g2 = new BubbleXYItemLabelGenerator(f1, xdf1, ydf1, zdf1);
  102. assertTrue(g1.equals(g2));
  103. assertTrue(g2.equals(g1));
  104. g1 = new BubbleXYItemLabelGenerator(f1, xdf2, ydf1, zdf1);
  105. assertFalse(g1.equals(g2));
  106. g2 = new BubbleXYItemLabelGenerator(f1, xdf2, ydf1, zdf1);
  107. assertTrue(g1.equals(g2));
  108. g1 = new BubbleXYItemLabelGenerator(f1, xdf2, ydf2, zdf1);
  109. assertFalse(g1.equals(g2));
  110. g2 = new BubbleXYItemLabelGenerator(f1, xdf2, ydf2, zdf1);
  111. assertTrue(g1.equals(g2));
  112. g1 = new BubbleXYItemLabelGenerator(f1, xdf2, ydf2, zdf2);
  113. assertFalse(g1.equals(g2));
  114. g2 = new BubbleXYItemLabelGenerator(f1, xdf2, ydf2, zdf2);
  115. assertTrue(g1.equals(g2));
  116. }
  117. /**
  118. * Simple check that hashCode is implemented.
  119. */
  120. @Test
  121. public void testHashCode() {
  122. BubbleXYItemLabelGenerator g1
  123. = new BubbleXYItemLabelGenerator();
  124. BubbleXYItemLabelGenerator g2
  125. = new BubbleXYItemLabelGenerator();
  126. assertTrue(g1.equals(g2));
  127. assertTrue(g1.hashCode() == g2.hashCode());
  128. }
  129. /**
  130. * Confirm that cloning works.
  131. */
  132. @Test
  133. public void testCloning() throws CloneNotSupportedException {
  134. BubbleXYItemLabelGenerator g1 = new BubbleXYItemLabelGenerator();
  135. BubbleXYItemLabelGenerator g2 = (BubbleXYItemLabelGenerator) g1.clone();
  136. assertTrue(g1 != g2);
  137. assertTrue(g1.getClass() == g2.getClass());
  138. assertTrue(g1.equals(g2));
  139. }
  140. /**
  141. * Check to ensure that this class implements PublicCloneable.
  142. */
  143. @Test
  144. public void testPublicCloneable() {
  145. BubbleXYItemLabelGenerator g1 = new BubbleXYItemLabelGenerator();
  146. assertTrue(g1 instanceof PublicCloneable);
  147. }
  148. /**
  149. * Serialize an instance, restore it, and check for equality.
  150. */
  151. @Test
  152. public void testSerialization() {
  153. BubbleXYItemLabelGenerator g1 = new BubbleXYItemLabelGenerator();
  154. BubbleXYItemLabelGenerator g2 = (BubbleXYItemLabelGenerator)
  155. TestUtilities.serialised(g1);
  156. assertEquals(g1, g2);
  157. }
  158. /**
  159. * Some checks for the testGenerateLabel() method.
  160. */
  161. @Test
  162. public void testGenerateLabel() {
  163. // check handling when the dataset is a regular XYDataset, not an
  164. // XYZDataset...
  165. XYSeries s1 = new XYSeries("S1");
  166. s1.add(1.0, 2.0);
  167. s1.add(2.2, 3.3);
  168. XYSeriesCollection dataset = new XYSeriesCollection(s1);
  169. BubbleXYItemLabelGenerator g = new BubbleXYItemLabelGenerator();
  170. assertEquals("{3}", g.generateLabel(dataset, 0, 0));
  171. assertEquals("{3}", g.generateLabel(dataset, 0, 1));
  172. }
  173. }