1. /* ===========================================================
  2. * JFreeChart : a free chart library for the Java(tm) platform
  3. * ===========================================================
  4. *
  5. * (C) Copyright 2000-2005, 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 under the terms
  10. * of the GNU Lesser General Public License as published by the Free Software Foundation;
  11. * either version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  14. * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. * See the GNU Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License along with this
  18. * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  19. * Boston, MA 02111-1307, USA.
  20. *
  21. * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
  22. * in the United States and other countries.]
  23. *
  24. * --------------
  25. * WeekTests.java
  26. * --------------
  27. * (C) Copyright 2002-2005, by Object Refinery Limited.
  28. *
  29. * Original Author: David Gilbert (for Object Refinery Limited);
  30. * Contributor(s): -;
  31. *
  32. * $Id: WeekTests.java,v 1.4 2005/01/11 16:00:35 mungady Exp $
  33. *
  34. * Changes
  35. * -------
  36. * 05-Apr-2002 : Version 1 (DG);
  37. * 26-Jun-2002 : Removed unnecessary imports (DG);
  38. * 17-Oct-2002 : Fixed errors reported by Checkstyle (DG);
  39. * 13-Mar-2003 : Added serialization test (DG);
  40. * 21-Oct-2003 : Added hashCode test (DG);
  41. *
  42. */
  43. package org.jfree.data.time.junit;
  44. import java.io.ByteArrayInputStream;
  45. import java.io.ByteArrayOutputStream;
  46. import java.io.ObjectInput;
  47. import java.io.ObjectInputStream;
  48. import java.io.ObjectOutput;
  49. import java.io.ObjectOutputStream;
  50. import java.util.Calendar;
  51. import java.util.Locale;
  52. import java.util.TimeZone;
  53. import junit.framework.Test;
  54. import junit.framework.TestCase;
  55. import junit.framework.TestSuite;
  56. import org.jfree.data.time.Week;
  57. /**
  58. * Tests for the {@link Week} class.
  59. */
  60. public class WeekTests extends TestCase {
  61. /** A week. */
  62. private Week w1Y1900;
  63. /** A week. */
  64. private Week w2Y1900;
  65. /** A week. */
  66. private Week w51Y9999;
  67. /** A week. */
  68. private Week w52Y9999;
  69. /**
  70. * Returns the tests as a test suite.
  71. *
  72. * @return The test suite.
  73. */
  74. public static Test suite() {
  75. return new TestSuite(WeekTests.class);
  76. }
  77. /**
  78. * Constructs a new set of tests.
  79. *
  80. * @param name the name of the tests.
  81. */
  82. public WeekTests(String name) {
  83. super(name);
  84. }
  85. /**
  86. * Common test setup.
  87. */
  88. protected void setUp() {
  89. this.w1Y1900 = new Week(1, 1900);
  90. this.w2Y1900 = new Week(2, 1900);
  91. this.w51Y9999 = new Week(51, 9999);
  92. this.w52Y9999 = new Week(52, 9999);
  93. }
  94. /**
  95. * Tests the equals method.
  96. */
  97. public void testEquals() {
  98. Week w1 = new Week(1, 2002);
  99. Week w2 = new Week(1, 2002);
  100. assertTrue(w1.equals(w2));
  101. assertTrue(w2.equals(w1));
  102. w1 = new Week(2, 2002);
  103. assertFalse(w1.equals(w2));
  104. w2 = new Week(2, 2002);
  105. assertTrue(w1.equals(w2));
  106. w1 = new Week(2, 2003);
  107. assertFalse(w1.equals(w2));
  108. w2 = new Week(2, 2003);
  109. assertTrue(w1.equals(w2));
  110. }
  111. /**
  112. * Request the week before week 1, 1900: it should be <code>null</code>.
  113. */
  114. public void testW1Y1900Previous() {
  115. Week previous = (Week) this.w1Y1900.previous();
  116. assertNull(previous);
  117. }
  118. /**
  119. * Request the week after week 1, 1900: it should be week 2, 1900.
  120. */
  121. public void testW1Y1900Next() {
  122. Week next = (Week) this.w1Y1900.next();
  123. assertEquals(this.w2Y1900, next);
  124. }
  125. /**
  126. * Request the week before w52, 9999: it should be week 51, 9999.
  127. */
  128. public void testW52Y9999Previous() {
  129. Week previous = (Week) this.w52Y9999.previous();
  130. assertEquals(this.w51Y9999, previous);
  131. }
  132. /**
  133. * Request the week after w52, 9999: it should be <code>null</code>.
  134. */
  135. public void testW52Y9999Next() {
  136. Week next = (Week) this.w52Y9999.next();
  137. assertNull(next);
  138. }
  139. /**
  140. * Serialize an instance, restore it, and check for equality.
  141. */
  142. public void testSerialization() {
  143. Week w1 = new Week(24, 1999);
  144. Week w2 = null;
  145. try {
  146. ByteArrayOutputStream buffer = new ByteArrayOutputStream();
  147. ObjectOutput out = new ObjectOutputStream(buffer);
  148. out.writeObject(w1);
  149. out.close();
  150. ObjectInput in = new ObjectInputStream(
  151. new ByteArrayInputStream(buffer.toByteArray())
  152. );
  153. w2 = (Week) in.readObject();
  154. in.close();
  155. }
  156. catch (Exception e) {
  157. System.out.println(e.toString());
  158. }
  159. assertEquals(w1, w2);
  160. }
  161. /**
  162. * Two objects that are equal are required to return the same hashCode.
  163. */
  164. public void testHashcode() {
  165. Week w1 = new Week(2, 2003);
  166. Week w2 = new Week(2, 2003);
  167. assertTrue(w1.equals(w2));
  168. int h1 = w1.hashCode();
  169. int h2 = w2.hashCode();
  170. assertEquals(h1, h2);
  171. }
  172. /**
  173. * The {@link Week} class is immutable, so should not be {@link Cloneable}.
  174. */
  175. public void testNotCloneable() {
  176. Week w = new Week(1, 1999);
  177. assertFalse(w instanceof Cloneable);
  178. }
  179. /**
  180. * The first week in 2005 should span the range:
  181. *
  182. * TimeZone | Start Millis | End Millis | Start Date | End Date
  183. * -----------------+---------------+---------------+-------------+------------
  184. * Europe/London | 1104710400000 | 1105315199999 | 3-Jan-2005 | 9-Jan-2005
  185. * Europe/Paris | 1104706800000 | 1105311599999 | 3-Jan-2005 | 2-Jan-2005
  186. * America/New_York | 1104037200000 | 1104641999999 | 26-Dec-2004 | 1-Jan-2005
  187. *
  188. * In London and Paris, Monday is the first day of the week, while in the US it is
  189. * Sunday.
  190. *
  191. * Previously, we were using these values, but see Java Bug ID 4960215:
  192. *
  193. * TimeZone | Start Millis | End Millis | Start Date | End Date
  194. * -----------------+---------------+---------------+-------------+------------
  195. * Europe/London | 1104105600000 | 1104710399999 | 27-Dec-2004 | 2-Jan-2005
  196. * Europe/Paris | 1104102000000 | 1104706799999 | 27-Dec-2004 | 2-Jan-2005
  197. * America/New_York | 1104037200000 | 1104641999999 | 26-Dec-2004 | 1-Jan-2005
  198. */
  199. public void testWeek12005() {
  200. Week w1 = new Week(1, 2005);
  201. Calendar c1 = Calendar.getInstance(TimeZone.getTimeZone("Europe/London"), Locale.UK);
  202. c1.setMinimalDaysInFirstWeek(4); // see Java Bug ID 4960215
  203. assertEquals(1104710400000L, w1.getFirstMillisecond(c1));
  204. assertEquals(1105315199999L, w1.getLastMillisecond(c1));
  205. Calendar c2 = Calendar.getInstance(TimeZone.getTimeZone("Europe/Paris"), Locale.FRANCE);
  206. c2.setMinimalDaysInFirstWeek(4); // see Java Bug ID 4960215
  207. assertEquals(1104706800000L, w1.getFirstMillisecond(c2));
  208. assertEquals(1105311599999L, w1.getLastMillisecond(c2));
  209. Calendar c3 = Calendar.getInstance(TimeZone.getTimeZone("America/New_York"), Locale.US);
  210. assertEquals(1104037200000L, w1.getFirstMillisecond(c3));
  211. assertEquals(1104641999999L, w1.getLastMillisecond(c3));
  212. }
  213. /**
  214. * The 53rd week in 2004 in London and Paris should span the range:
  215. *
  216. * TimeZone | Start Millis | End Millis | Start Date | End Date
  217. * -----------------+---------------+---------------+-------------+------------
  218. * Europe/London | 1104105600000 | 1104710399999 | 27-Dec-2004 | 02-Jan-2005
  219. * Europe/Paris | 1104102000000 | 1104706799999 | 27-Dec-2004 | 02-Jan-2005
  220. *
  221. * The 53rd week in 2005 in New York should span the range:
  222. *
  223. * TimeZone | Start Millis | End Millis | Start Date | End Date
  224. * -----------------+---------------+---------------+-------------+------------
  225. * America/New_York | 1135486800000 | 1136091599999 | 25-Dec-2005 | 31-Dec-2005
  226. *
  227. * In London and Paris, Monday is the first day of the week, while in the US it is
  228. * Sunday.
  229. */
  230. public void testWeek532005() {
  231. Week w1 = new Week(53, 2004);
  232. Calendar c1 = Calendar.getInstance(TimeZone.getTimeZone("Europe/London"), Locale.UK);
  233. c1.setMinimalDaysInFirstWeek(4); // see Java Bug ID 4960215
  234. assertEquals(1104105600000L, w1.getFirstMillisecond(c1));
  235. assertEquals(1104710399999L, w1.getLastMillisecond(c1));
  236. Calendar c2 = Calendar.getInstance(TimeZone.getTimeZone("Europe/Paris"), Locale.FRANCE);
  237. c2.setMinimalDaysInFirstWeek(4); // see Java Bug ID 4960215
  238. assertEquals(1104102000000L, w1.getFirstMillisecond(c2));
  239. assertEquals(1104706799999L, w1.getLastMillisecond(c2));
  240. w1 = new Week(53, 2005);
  241. Calendar c3 = Calendar.getInstance(TimeZone.getTimeZone("America/New_York"), Locale.US);
  242. assertEquals(1135486800000L, w1.getFirstMillisecond(c3));
  243. assertEquals(1136091599999L, w1.getLastMillisecond(c3));
  244. }
  245. }