KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > data > time > junit > WeekTests


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
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 License
20  * along with this library; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22  *
23  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
24  * in the United States and other countries.]
25  *
26  * --------------
27  * WeekTests.java
28  * --------------
29  * (C) Copyright 2002-2005, by Object Refinery Limited.
30  *
31  * Original Author: David Gilbert (for Object Refinery Limited);
32  * Contributor(s): -;
33  *
34  * $Id: WeekTests.java,v 1.5 2005/03/11 14:37:23 mungady Exp $
35  *
36  * Changes
37  * -------
38  * 05-Apr-2002 : Version 1 (DG);
39  * 26-Jun-2002 : Removed unnecessary imports (DG);
40  * 17-Oct-2002 : Fixed errors reported by Checkstyle (DG);
41  * 13-Mar-2003 : Added serialization test (DG);
42  * 21-Oct-2003 : Added hashCode test (DG);
43  *
44  */

45
46 package org.jfree.data.time.junit;
47
48 import java.io.ByteArrayInputStream JavaDoc;
49 import java.io.ByteArrayOutputStream JavaDoc;
50 import java.io.ObjectInput JavaDoc;
51 import java.io.ObjectInputStream JavaDoc;
52 import java.io.ObjectOutput JavaDoc;
53 import java.io.ObjectOutputStream JavaDoc;
54 import java.util.Calendar JavaDoc;
55 import java.util.Locale JavaDoc;
56 import java.util.TimeZone JavaDoc;
57
58 import junit.framework.Test;
59 import junit.framework.TestCase;
60 import junit.framework.TestSuite;
61
62 import org.jfree.data.time.Week;
63
64 /**
65  * Tests for the {@link Week} class.
66  */

67 public class WeekTests extends TestCase {
68
69     /** A week. */
70     private Week w1Y1900;
71
72     /** A week. */
73     private Week w2Y1900;
74
75     /** A week. */
76     private Week w51Y9999;
77
78     /** A week. */
79     private Week w52Y9999;
80
81     /**
82      * Returns the tests as a test suite.
83      *
84      * @return The test suite.
85      */

86     public static Test suite() {
87         return new TestSuite(WeekTests.class);
88     }
89
90     /**
91      * Constructs a new set of tests.
92      *
93      * @param name the name of the tests.
94      */

95     public WeekTests(String JavaDoc name) {
96         super(name);
97     }
98
99     /**
100      * Common test setup.
101      */

102     protected void setUp() {
103         this.w1Y1900 = new Week(1, 1900);
104         this.w2Y1900 = new Week(2, 1900);
105         this.w51Y9999 = new Week(51, 9999);
106         this.w52Y9999 = new Week(52, 9999);
107     }
108
109     /**
110      * Tests the equals method.
111      */

112     public void testEquals() {
113         Week w1 = new Week(1, 2002);
114         Week w2 = new Week(1, 2002);
115         assertTrue(w1.equals(w2));
116         assertTrue(w2.equals(w1));
117         
118         w1 = new Week(2, 2002);
119         assertFalse(w1.equals(w2));
120         w2 = new Week(2, 2002);
121         assertTrue(w1.equals(w2));
122         
123         w1 = new Week(2, 2003);
124         assertFalse(w1.equals(w2));
125         w2 = new Week(2, 2003);
126         assertTrue(w1.equals(w2));
127     }
128
129     /**
130      * Request the week before week 1, 1900: it should be <code>null</code>.
131      */

132     public void testW1Y1900Previous() {
133         Week previous = (Week) this.w1Y1900.previous();
134         assertNull(previous);
135     }
136
137     /**
138      * Request the week after week 1, 1900: it should be week 2, 1900.
139      */

140     public void testW1Y1900Next() {
141         Week next = (Week) this.w1Y1900.next();
142         assertEquals(this.w2Y1900, next);
143     }
144
145     /**
146      * Request the week before w52, 9999: it should be week 51, 9999.
147      */

148     public void testW52Y9999Previous() {
149         Week previous = (Week) this.w52Y9999.previous();
150         assertEquals(this.w51Y9999, previous);
151     }
152
153     /**
154      * Request the week after w52, 9999: it should be <code>null</code>.
155      */

156     public void testW52Y9999Next() {
157         Week next = (Week) this.w52Y9999.next();
158         assertNull(next);
159     }
160
161     /**
162      * Serialize an instance, restore it, and check for equality.
163      */

164     public void testSerialization() {
165
166         Week w1 = new Week(24, 1999);
167         Week w2 = null;
168
169         try {
170             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
171             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
172             out.writeObject(w1);
173             out.close();
174
175             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
176                 new ByteArrayInputStream JavaDoc(buffer.toByteArray())
177             );
178             w2 = (Week) in.readObject();
179             in.close();
180         }
181         catch (Exception JavaDoc e) {
182             System.out.println(e.toString());
183         }
184         assertEquals(w1, w2);
185
186     }
187     
188     /**
189      * Two objects that are equal are required to return the same hashCode.
190      */

191     public void testHashcode() {
192         Week w1 = new Week(2, 2003);
193         Week w2 = new Week(2, 2003);
194         assertTrue(w1.equals(w2));
195         int h1 = w1.hashCode();
196         int h2 = w2.hashCode();
197         assertEquals(h1, h2);
198     }
199     
200     /**
201      * The {@link Week} class is immutable, so should not be {@link Cloneable}.
202      */

203     public void testNotCloneable() {
204         Week w = new Week(1, 1999);
205         assertFalse(w instanceof Cloneable JavaDoc);
206     }
207     
208     /**
209      * The first week in 2005 should span the range:
210      *
211      * TimeZone | Start Millis | End Millis | Start Date | End Date
212      * -----------------+---------------+---------------+-------------+------------
213      * Europe/London | 1104710400000 | 1105315199999 | 3-Jan-2005 | 9-Jan-2005
214      * Europe/Paris | 1104706800000 | 1105311599999 | 3-Jan-2005 | 2-Jan-2005
215      * America/New_York | 1104037200000 | 1104641999999 | 26-Dec-2004 | 1-Jan-2005
216      *
217      * In London and Paris, Monday is the first day of the week, while in the US it is
218      * Sunday.
219      *
220      * Previously, we were using these values, but see Java Bug ID 4960215:
221      *
222      * TimeZone | Start Millis | End Millis | Start Date | End Date
223      * -----------------+---------------+---------------+-------------+------------
224      * Europe/London | 1104105600000 | 1104710399999 | 27-Dec-2004 | 2-Jan-2005
225      * Europe/Paris | 1104102000000 | 1104706799999 | 27-Dec-2004 | 2-Jan-2005
226      * America/New_York | 1104037200000 | 1104641999999 | 26-Dec-2004 | 1-Jan-2005
227      */

228     public void testWeek12005() {
229         Week w1 = new Week(1, 2005);
230         Calendar JavaDoc c1 = Calendar.getInstance(
231             TimeZone.getTimeZone("Europe/London"), Locale.UK
232         );
233         c1.setMinimalDaysInFirstWeek(4); // see Java Bug ID 4960215
234
assertEquals(1104710400000L, w1.getFirstMillisecond(c1));
235         assertEquals(1105315199999L, w1.getLastMillisecond(c1));
236         Calendar JavaDoc c2 = Calendar.getInstance(
237             TimeZone.getTimeZone("Europe/Paris"), Locale.FRANCE
238         );
239         c2.setMinimalDaysInFirstWeek(4); // see Java Bug ID 4960215
240
assertEquals(1104706800000L, w1.getFirstMillisecond(c2));
241         assertEquals(1105311599999L, w1.getLastMillisecond(c2));
242         Calendar JavaDoc c3 = Calendar.getInstance(
243             TimeZone.getTimeZone("America/New_York"), Locale.US
244         );
245         assertEquals(1104037200000L, w1.getFirstMillisecond(c3));
246         assertEquals(1104641999999L, w1.getLastMillisecond(c3));
247     }
248
249     /**
250      * The 53rd week in 2004 in London and Paris should span the range:
251      *
252      * TimeZone | Start Millis | End Millis | Start Date | End Date
253      * -----------------+---------------+---------------+-------------+------------
254      * Europe/London | 1104105600000 | 1104710399999 | 27-Dec-2004 | 02-Jan-2005
255      * Europe/Paris | 1104102000000 | 1104706799999 | 27-Dec-2004 | 02-Jan-2005
256      *
257      * The 53rd week in 2005 in New York should span the range:
258      *
259      * TimeZone | Start Millis | End Millis | Start Date | End Date
260      * -----------------+---------------+---------------+-------------+------------
261      * America/New_York | 1135486800000 | 1136091599999 | 25-Dec-2005 | 31-Dec-2005
262      *
263      * In London and Paris, Monday is the first day of the week, while in the US it is
264      * Sunday.
265      */

266     public void testWeek532005() {
267         Week w1 = new Week(53, 2004);
268         Calendar JavaDoc c1 = Calendar.getInstance(
269             TimeZone.getTimeZone("Europe/London"), Locale.UK
270         );
271         c1.setMinimalDaysInFirstWeek(4); // see Java Bug ID 4960215
272
assertEquals(1104105600000L, w1.getFirstMillisecond(c1));
273         assertEquals(1104710399999L, w1.getLastMillisecond(c1));
274         Calendar JavaDoc c2 = Calendar.getInstance(
275             TimeZone.getTimeZone("Europe/Paris"), Locale.FRANCE
276         );
277         c2.setMinimalDaysInFirstWeek(4); // see Java Bug ID 4960215
278
assertEquals(1104102000000L, w1.getFirstMillisecond(c2));
279         assertEquals(1104706799999L, w1.getLastMillisecond(c2));
280         w1 = new Week(53, 2005);
281         Calendar JavaDoc c3 = Calendar.getInstance(
282             TimeZone.getTimeZone("America/New_York"), Locale.US
283         );
284         assertEquals(1135486800000L, w1.getFirstMillisecond(c3));
285         assertEquals(1136091599999L, w1.getLastMillisecond(c3));
286     }
287
288 }
289
Popular Tags