RelativeDateFormatTest.java 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. * RelativeDateFormatTest.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. * 23-Nov-2006 : Version 1 (DG);
  38. * 15-Feb-2008 : Added tests for negative dates (DG);
  39. * 01-Sep-2008 : Added a test for hours and minutes with leading zeroes (DG);
  40. * 06-Oct-2011 : Fixed bug 3418287 (DG);
  41. *
  42. */
  43. package org.jfree.chart.util;
  44. import java.text.DecimalFormat;
  45. import java.text.NumberFormat;
  46. import java.util.Date;
  47. import java.util.Locale;
  48. import org.junit.After;
  49. import org.junit.Before;
  50. import org.junit.Test;
  51. import static org.junit.Assert.assertTrue;
  52. import static org.junit.Assert.assertFalse;
  53. import static org.junit.Assert.assertEquals;
  54. /**
  55. * Tests for the {@link RelativeDateFormat} class.
  56. */
  57. public class RelativeDateFormatTest {
  58. private Locale savedLocale;
  59. /**
  60. * Set a known locale for the tests.
  61. */
  62. @Before
  63. public void setUp() throws Exception {
  64. this.savedLocale = Locale.getDefault();
  65. Locale.setDefault(Locale.UK);
  66. }
  67. /**
  68. * Restore the default locale after the tests complete.
  69. */
  70. @After
  71. public void tearDown() throws Exception {
  72. Locale.setDefault(this.savedLocale);
  73. }
  74. /**
  75. * Some checks for the formatting.
  76. */
  77. @Test
  78. public void testFormat() {
  79. RelativeDateFormat rdf = new RelativeDateFormat();
  80. String s = rdf.format(new Date(2 * 60L * 60L * 1000L + 122500L));
  81. assertEquals("2h2m2.500s", s);
  82. }
  83. /**
  84. * Test that we can configure the RelativeDateFormat to show
  85. * hh:mm:ss.
  86. */
  87. public void test2033092() {
  88. RelativeDateFormat rdf = new RelativeDateFormat();
  89. rdf.setShowZeroDays(false);
  90. rdf.setShowZeroHours(false);
  91. rdf.setMinuteSuffix(":");
  92. rdf.setHourSuffix(":");
  93. rdf.setSecondSuffix("");
  94. DecimalFormat hoursFormatter = new DecimalFormat();
  95. hoursFormatter.setMaximumFractionDigits(0);
  96. hoursFormatter.setMaximumIntegerDigits(2);
  97. hoursFormatter.setMinimumIntegerDigits(2);
  98. rdf.setHourFormatter(hoursFormatter);
  99. DecimalFormat minsFormatter = new DecimalFormat();
  100. minsFormatter.setMaximumFractionDigits(0);
  101. minsFormatter.setMaximumIntegerDigits(2);
  102. minsFormatter.setMinimumIntegerDigits(2);
  103. rdf.setMinuteFormatter(minsFormatter);
  104. DecimalFormat secondsFormatter = new DecimalFormat();
  105. secondsFormatter.setMaximumFractionDigits(0);
  106. secondsFormatter.setMaximumIntegerDigits(2);
  107. secondsFormatter.setMinimumIntegerDigits(2);
  108. rdf.setSecondFormatter(secondsFormatter);
  109. String s = rdf.format(new Date(2 * 60L * 60L * 1000L + 122500L));
  110. assertEquals("02:02:02", s);
  111. }
  112. /**
  113. * Check that the equals() method can distinguish all fields.
  114. */
  115. public void testEquals() {
  116. RelativeDateFormat df1 = new RelativeDateFormat();
  117. RelativeDateFormat df2 = new RelativeDateFormat();
  118. assertEquals(df1, df2);
  119. df1.setBaseMillis(123L);
  120. assertFalse(df1.equals(df2));
  121. df2.setBaseMillis(123L);
  122. assertTrue(df1.equals(df2));
  123. df1.setDayFormatter(new DecimalFormat("0%"));
  124. assertFalse(df1.equals(df2));
  125. df2.setDayFormatter(new DecimalFormat("0%"));
  126. assertTrue(df1.equals(df2));
  127. df1.setDaySuffix("D");
  128. assertFalse(df1.equals(df2));
  129. df2.setDaySuffix("D");
  130. assertTrue(df1.equals(df2));
  131. df1.setHourFormatter(new DecimalFormat("0%"));
  132. assertFalse(df1.equals(df2));
  133. df2.setHourFormatter(new DecimalFormat("0%"));
  134. assertTrue(df1.equals(df2));
  135. df1.setHourSuffix("H");
  136. assertFalse(df1.equals(df2));
  137. df2.setHourSuffix("H");
  138. assertTrue(df1.equals(df2));
  139. df1.setMinuteFormatter(new DecimalFormat("0%"));
  140. assertFalse(df1.equals(df2));
  141. df2.setMinuteFormatter(new DecimalFormat("0%"));
  142. assertTrue(df1.equals(df2));
  143. df1.setMinuteSuffix("M");
  144. assertFalse(df1.equals(df2));
  145. df2.setMinuteSuffix("M");
  146. assertTrue(df1.equals(df2));
  147. df1.setSecondSuffix("S");
  148. assertFalse(df1.equals(df2));
  149. df2.setSecondSuffix("S");
  150. assertTrue(df1.equals(df2));
  151. df1.setShowZeroDays(!df1.getShowZeroDays());
  152. assertFalse(df1.equals(df2));
  153. df2.setShowZeroDays(!df2.getShowZeroDays());
  154. assertTrue(df1.equals(df2));
  155. df1.setSecondFormatter(new DecimalFormat("0.0"));
  156. assertFalse(df1.equals(df2));
  157. df2.setSecondFormatter(new DecimalFormat("0.0"));
  158. assertTrue(df1.equals(df2));
  159. }
  160. /**
  161. * Two objects that are equal are required to return the same hashCode.
  162. */
  163. public void testHashCode() {
  164. RelativeDateFormat df1 = new RelativeDateFormat(123L);
  165. RelativeDateFormat df2 = new RelativeDateFormat(123L);
  166. assertTrue(df1.equals(df2));
  167. int h1 = df1.hashCode();
  168. int h2 = df2.hashCode();
  169. assertEquals(h1, h2);
  170. }
  171. /**
  172. * Confirm that cloning works.
  173. */
  174. public void testCloning() {
  175. NumberFormat nf = new DecimalFormat("0");
  176. RelativeDateFormat df1 = new RelativeDateFormat();
  177. df1.setSecondFormatter(nf);
  178. RelativeDateFormat df2 = null;
  179. df2 = (RelativeDateFormat) df1.clone();
  180. assertTrue(df1 != df2);
  181. assertTrue(df1.getClass() == df2.getClass());
  182. assertTrue(df1.equals(df2));
  183. // is the clone independent
  184. nf.setMinimumFractionDigits(2);
  185. assertFalse(df1.equals(df2));
  186. }
  187. /**
  188. * Some tests for negative dates.
  189. */
  190. public void testNegative() {
  191. NumberFormat nf = new DecimalFormat("0");
  192. RelativeDateFormat df1 = new RelativeDateFormat();
  193. df1.setSecondFormatter(nf);
  194. assertEquals("-0h0m1s", df1.format(new Date(-1000L)));
  195. }
  196. }