StandardXYItemLabelGeneratorTest.java 6.9 KB

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