MonthDateFormatTest.java 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. * MonthDateFormatTest.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. * 10-May-2005 : Version 1 (DG);
  38. *
  39. */
  40. package org.jfree.chart.axis;
  41. import static org.junit.Assert.assertEquals;
  42. import static org.junit.Assert.assertFalse;
  43. import static org.junit.Assert.assertTrue;
  44. import java.text.SimpleDateFormat;
  45. import java.util.Locale;
  46. import java.util.TimeZone;
  47. import org.jfree.chart.TestUtilities;
  48. import org.junit.Test;
  49. /**
  50. * Some tests for the {@link MonthDateFormat} class.
  51. */
  52. public class MonthDateFormatTest {
  53. /**
  54. * Confirm that the equals method can distinguish all the required fields.
  55. */
  56. @Test
  57. public void testEquals() {
  58. MonthDateFormat mf1 = new MonthDateFormat();
  59. MonthDateFormat mf2 = new MonthDateFormat();
  60. assertTrue(mf1.equals(mf2));
  61. assertTrue(mf2.equals(mf1));
  62. boolean[] showYear1 = new boolean [12];
  63. showYear1[0] = true;
  64. boolean[] showYear2 = new boolean [12];
  65. showYear1[1] = true;
  66. // time zone
  67. mf1 = new MonthDateFormat(TimeZone.getTimeZone("PST"), Locale.US, 1,
  68. showYear1, new SimpleDateFormat("yy"));
  69. assertFalse(mf1.equals(mf2));
  70. mf2 = new MonthDateFormat(TimeZone.getTimeZone("PST"), Locale.US, 1,
  71. showYear1, new SimpleDateFormat("yy"));
  72. assertTrue(mf1.equals(mf2));
  73. // locale
  74. mf1 = new MonthDateFormat(TimeZone.getTimeZone("PST"), Locale.FRANCE, 1,
  75. showYear1, new SimpleDateFormat("yy"));
  76. assertFalse(mf1.equals(mf2));
  77. mf2 = new MonthDateFormat(TimeZone.getTimeZone("PST"), Locale.FRANCE, 1,
  78. showYear1, new SimpleDateFormat("yy"));
  79. assertTrue(mf1.equals(mf2));
  80. // chars
  81. mf1 = new MonthDateFormat(TimeZone.getTimeZone("PST"), Locale.FRANCE, 2,
  82. showYear1, new SimpleDateFormat("yy"));
  83. assertFalse(mf1.equals(mf2));
  84. mf2 = new MonthDateFormat(TimeZone.getTimeZone("PST"), Locale.FRANCE, 2,
  85. showYear1, new SimpleDateFormat("yy"));
  86. assertTrue(mf1.equals(mf2));
  87. // showYear[]
  88. mf1 = new MonthDateFormat(TimeZone.getTimeZone("PST"), Locale.FRANCE, 2,
  89. showYear2, new SimpleDateFormat("yy"));
  90. assertFalse(mf1.equals(mf2));
  91. mf2 = new MonthDateFormat(TimeZone.getTimeZone("PST"), Locale.FRANCE, 2,
  92. showYear2, new SimpleDateFormat("yy"));
  93. assertTrue(mf1.equals(mf2));
  94. // yearFormatter
  95. mf1 = new MonthDateFormat(TimeZone.getTimeZone("PST"), Locale.FRANCE, 2,
  96. showYear2, new SimpleDateFormat("yyyy"));
  97. assertFalse(mf1.equals(mf2));
  98. mf2 = new MonthDateFormat(TimeZone.getTimeZone("PST"), Locale.FRANCE, 2,
  99. showYear2, new SimpleDateFormat("yyyy"));
  100. assertTrue(mf1.equals(mf2));
  101. }
  102. /**
  103. * Two objects that are equal are required to return the same hashCode.
  104. */
  105. @Test
  106. public void testHashCode() {
  107. MonthDateFormat mf1 = new MonthDateFormat();
  108. MonthDateFormat mf2 = new MonthDateFormat();
  109. assertTrue(mf1.equals(mf2));
  110. int h1 = mf1.hashCode();
  111. int h2 = mf2.hashCode();
  112. assertEquals(h1, h2);
  113. }
  114. /**
  115. * Confirm that cloning works.
  116. */
  117. @Test
  118. public void testCloning() {
  119. MonthDateFormat mf1 = new MonthDateFormat();
  120. MonthDateFormat mf2 = null;
  121. mf2 = (MonthDateFormat) mf1.clone();
  122. assertTrue(mf1 != mf2);
  123. assertTrue(mf1.getClass() == mf2.getClass());
  124. assertTrue(mf1.equals(mf2));
  125. }
  126. /**
  127. * Serialize an instance, restore it, and check for equality.
  128. */
  129. @Test
  130. public void testSerialization() {
  131. MonthDateFormat mf1 = new MonthDateFormat();
  132. MonthDateFormat mf2 = (MonthDateFormat) TestUtilities.serialised(mf1);
  133. assertTrue(mf1.equals(mf2));
  134. }
  135. }