PeriodAxisLabelInfoTest.java 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. * PeriodAxisLabelInfoTest.java
  29. * ----------------------------
  30. * (C) Copyright 2004-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. * 10-Jun-2003 : Version 1 (DG);
  38. * 07-Jan-2005 : Added test for hashCode() (DG);
  39. *
  40. */
  41. package org.jfree.chart.axis;
  42. import static org.junit.Assert.assertEquals;
  43. import static org.junit.Assert.assertFalse;
  44. import static org.junit.Assert.assertTrue;
  45. import java.awt.BasicStroke;
  46. import java.awt.Color;
  47. import java.awt.Font;
  48. import java.awt.Paint;
  49. import java.awt.Stroke;
  50. import java.text.DateFormat;
  51. import java.text.SimpleDateFormat;
  52. import java.util.Date;
  53. import java.util.Locale;
  54. import java.util.TimeZone;
  55. import org.jfree.chart.TestUtilities;
  56. import org.jfree.data.time.Day;
  57. import org.jfree.data.time.Month;
  58. import org.jfree.data.time.Year;
  59. import org.jfree.ui.RectangleInsets;
  60. import org.junit.Test;
  61. /**
  62. * Tests for the {@link PeriodAxisLabelInfo} class.
  63. */
  64. public class PeriodAxisLabelInfoTest {
  65. /**
  66. * Confirm that the equals method can distinguish all the required fields.
  67. */
  68. @Test
  69. public void testEquals() {
  70. PeriodAxisLabelInfo info1 = new PeriodAxisLabelInfo(Day.class,
  71. new SimpleDateFormat("d"));
  72. PeriodAxisLabelInfo info2 = new PeriodAxisLabelInfo(Day.class,
  73. new SimpleDateFormat("d"));
  74. assertTrue(info1.equals(info2));
  75. assertTrue(info2.equals(info1));
  76. Class c1 = Day.class;
  77. Class c2 = Month.class;
  78. DateFormat df1 = new SimpleDateFormat("d");
  79. DateFormat df2 = new SimpleDateFormat("MMM");
  80. RectangleInsets sp1 = new RectangleInsets(1, 1, 1, 1);
  81. RectangleInsets sp2 = new RectangleInsets(2, 2, 2, 2);
  82. Font lf1 = new Font("SansSerif", Font.PLAIN, 10);
  83. Font lf2 = new Font("SansSerif", Font.BOLD, 9);
  84. Paint lp1 = Color.black;
  85. Paint lp2 = Color.blue;
  86. boolean b1 = true;
  87. boolean b2 = false;
  88. Stroke s1 = new BasicStroke(0.5f);
  89. Stroke s2 = new BasicStroke(0.25f);
  90. Paint dp1 = Color.red;
  91. Paint dp2 = Color.green;
  92. info1 = new PeriodAxisLabelInfo(c2, df1, sp1, lf1, lp1, b1, s1, dp1);
  93. info2 = new PeriodAxisLabelInfo(c1, df1, sp1, lf1, lp1, b1, s1, dp1);
  94. assertFalse(info1.equals(info2));
  95. info2 = new PeriodAxisLabelInfo(c2, df1, sp1, lf1, lp1, b1, s1, dp1);
  96. assertTrue(info1.equals(info2));
  97. info1 = new PeriodAxisLabelInfo(c2, df2, sp1, lf1, lp1, b1, s1, dp1);
  98. assertFalse(info1.equals(info2));
  99. info2 = new PeriodAxisLabelInfo(c2, df2, sp1, lf1, lp1, b1, s1, dp1);
  100. assertTrue(info1.equals(info2));
  101. info1 = new PeriodAxisLabelInfo(c2, df2, sp2, lf1, lp1, b1, s1, dp1);
  102. assertFalse(info1.equals(info2));
  103. info2 = new PeriodAxisLabelInfo(c2, df2, sp2, lf1, lp1, b1, s1, dp1);
  104. assertTrue(info1.equals(info2));
  105. info1 = new PeriodAxisLabelInfo(c2, df2, sp2, lf2, lp1, b1, s1, dp1);
  106. assertFalse(info1.equals(info2));
  107. info2 = new PeriodAxisLabelInfo(c2, df2, sp2, lf2, lp1, b1, s1, dp1);
  108. assertTrue(info1.equals(info2));
  109. info1 = new PeriodAxisLabelInfo(c2, df2, sp2, lf2, lp2, b1, s1, dp1);
  110. assertFalse(info1.equals(info2));
  111. info2 = new PeriodAxisLabelInfo(c2, df2, sp2, lf2, lp2, b1, s1, dp1);
  112. assertTrue(info1.equals(info2));
  113. info1 = new PeriodAxisLabelInfo(c2, df2, sp2, lf2, lp2, b2, s1, dp1);
  114. assertFalse(info1.equals(info2));
  115. info2 = new PeriodAxisLabelInfo(c2, df2, sp2, lf2, lp2, b2, s1, dp1);
  116. assertTrue(info1.equals(info2));
  117. info1 = new PeriodAxisLabelInfo(c2, df2, sp2, lf2, lp2, b2, s2, dp1);
  118. assertFalse(info1.equals(info2));
  119. info2 = new PeriodAxisLabelInfo(c2, df2, sp2, lf2, lp2, b2, s2, dp1);
  120. assertTrue(info1.equals(info2));
  121. info1 = new PeriodAxisLabelInfo(c2, df2, sp2, lf2, lp2, b2, s2, dp2);
  122. assertFalse(info1.equals(info2));
  123. info2 = new PeriodAxisLabelInfo(c2, df2, sp2, lf2, lp2, b2, s2, dp2);
  124. assertTrue(info1.equals(info2));
  125. }
  126. /**
  127. * Two objects that are equal are required to return the same hashCode.
  128. */
  129. @Test
  130. public void testHashCode() {
  131. PeriodAxisLabelInfo info1 = new PeriodAxisLabelInfo(Day.class,
  132. new SimpleDateFormat("d"));
  133. PeriodAxisLabelInfo info2 = new PeriodAxisLabelInfo(Day.class,
  134. new SimpleDateFormat("d"));
  135. assertTrue(info1.equals(info2));
  136. int h1 = info1.hashCode();
  137. int h2 = info2.hashCode();
  138. assertEquals(h1, h2);
  139. }
  140. /**
  141. * Confirm that cloning works.
  142. */
  143. @Test
  144. public void testCloning() throws CloneNotSupportedException {
  145. PeriodAxisLabelInfo info1 = new PeriodAxisLabelInfo(Day.class,
  146. new SimpleDateFormat("d"));
  147. PeriodAxisLabelInfo info2 = (PeriodAxisLabelInfo) info1.clone();
  148. assertTrue(info1 != info2);
  149. assertTrue(info1.getClass() == info2.getClass());
  150. assertTrue(info1.equals(info2));
  151. }
  152. /**
  153. * Serialize an instance, restore it, and check for equality.
  154. */
  155. @Test
  156. public void testSerialization() {
  157. PeriodAxisLabelInfo info1 = new PeriodAxisLabelInfo(Day.class,
  158. new SimpleDateFormat("d"));
  159. PeriodAxisLabelInfo info2 = (PeriodAxisLabelInfo) TestUtilities.serialised(info1);
  160. assertEquals(info1, info2);
  161. }
  162. /**
  163. * A test for the createInstance() method.
  164. */
  165. @Test
  166. public void testCreateInstance() {
  167. TimeZone zone = TimeZone.getTimeZone("GMT");
  168. PeriodAxisLabelInfo info = new PeriodAxisLabelInfo(Day.class,
  169. new SimpleDateFormat("d"));
  170. Day d = (Day) info.createInstance(new Date(0L), zone, Locale.UK);
  171. assertEquals(new Day(1, 1, 1970), d);
  172. info = new PeriodAxisLabelInfo(Year.class, new SimpleDateFormat("YYYY"));
  173. Year y = (Year) info.createInstance(new Date(0L), zone, Locale.UK);
  174. assertEquals(new Year(1970), y);
  175. }
  176. }