KickJava   Java API By Example, From Geeks To Geeks.

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


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  * HourTests.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: HourTests.java,v 1.5 2005/03/11 14:37:24 mungady Exp $
35  *
36  * Changes
37  * -------
38  * 29-Jan-2002 : Version 1 (DG);
39  * 17-Oct-2002 : Fixed errors reported by Checkstyle (DG);
40  * 13-Mar-2003 : Added serialization test (DG);
41  * 21-Oct-2003 : Added hashCode test (DG);
42  * 11-Jan-2005 : Added test for non-clonability (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.Date JavaDoc;
55 import java.util.TimeZone JavaDoc;
56
57 import junit.framework.Test;
58 import junit.framework.TestCase;
59 import junit.framework.TestSuite;
60
61 import org.jfree.data.time.Day;
62 import org.jfree.data.time.Hour;
63 import org.jfree.date.MonthConstants;
64
65 /**
66  * Tests for the {@link Hour} class.
67  */

68 public class HourTests extends TestCase {
69
70     /**
71      * Returns the tests as a test suite.
72      *
73      * @return The test suite.
74      */

75     public static Test suite() {
76         return new TestSuite(HourTests.class);
77     }
78
79     /**
80      * Constructs a new set of tests.
81      *
82      * @param name the name of the tests.
83      */

84     public HourTests(String JavaDoc name) {
85         super(name);
86     }
87
88     /**
89      * Common test setup.
90      */

91     protected void setUp() {
92         // no setup
93
}
94
95     /**
96      * Check that an Hour instance is equal to itself.
97      *
98      * SourceForge Bug ID: 558850.
99      */

100     public void testEqualsSelf() {
101         Hour hour = new Hour();
102         assertTrue(hour.equals(hour));
103     }
104
105     /**
106      * Tests the equals method.
107      */

108     public void testEquals() {
109         Hour hour1 = new Hour(15, new Day(29, MonthConstants.MARCH, 2002));
110         Hour hour2 = new Hour(15, new Day(29, MonthConstants.MARCH, 2002));
111         assertTrue(hour1.equals(hour2));
112     }
113
114     /**
115      * In GMT, the 4pm on 21 Mar 2002 is java.util.Date(1,014,307,200,000L).
116      * Use this to check the hour constructor.
117      */

118     public void testDateConstructor1() {
119
120         TimeZone JavaDoc zone = TimeZone.getTimeZone("GMT");
121         Hour h1 = new Hour(new Date JavaDoc(1014307199999L), zone);
122         Hour h2 = new Hour(new Date JavaDoc(1014307200000L), zone);
123
124         assertEquals(15, h1.getHour());
125         assertEquals(1014307199999L, h1.getLastMillisecond(zone));
126
127         assertEquals(16, h2.getHour());
128         assertEquals(1014307200000L, h2.getFirstMillisecond(zone));
129
130     }
131
132     /**
133      * In Sydney, the 4pm on 21 Mar 2002 is java.util.Date(1,014,267,600,000L).
134      * Use this to check the hour constructor.
135      */

136     public void testDateConstructor2() {
137
138         TimeZone JavaDoc zone = TimeZone.getTimeZone("Australia/Sydney");
139         Hour h1 = new Hour(new Date JavaDoc(1014267599999L), zone);
140         Hour h2 = new Hour (new Date JavaDoc(1014267600000L), zone);
141
142         assertEquals(15, h1.getHour());
143         assertEquals(1014267599999L, h1.getLastMillisecond(zone));
144
145         assertEquals(16, h2.getHour());
146         assertEquals(1014267600000L, h2.getFirstMillisecond(zone));
147
148     }
149
150     /**
151      * Set up an hour equal to hour zero, 1 January 1900. Request the
152      * previous hour, it should be null.
153      */

154     public void testFirstHourPrevious() {
155
156         Hour first = new Hour(0, new Day(1, MonthConstants.JANUARY, 1900));
157         Hour previous = (Hour) first.previous();
158         assertNull(previous);
159
160     }
161
162     /**
163      * Set up an hour equal to hour zero, 1 January 1900. Request the next
164      * hour, it should be null.
165      */

166     public void testFirstHourNext() {
167         Hour first = new Hour(0, new Day(1, MonthConstants.JANUARY, 1900));
168         Hour next = (Hour) first.next();
169         assertEquals(1, next.getHour());
170         assertEquals(1900, next.getYear());
171     }
172
173     /**
174      * Set up an hour equal to hour zero, 1 January 1900. Request the previous
175      * hour, it should be null.
176      */

177     public void testLastHourPrevious() {
178
179         Hour last = new Hour(23, new Day(31, MonthConstants.DECEMBER, 9999));
180         Hour previous = (Hour) last.previous();
181         assertEquals(22, previous.getHour());
182         assertEquals(9999, previous.getYear());
183
184     }
185
186     /**
187      * Set up an hour equal to hour zero, 1 January 1900. Request the next
188      * hour, it should be null.
189      */

190     public void testLastHourNext() {
191
192         Hour last = new Hour(23, new Day(31, MonthConstants.DECEMBER, 9999));
193         Hour next = (Hour) last.next();
194         assertNull(next);
195
196     }
197
198     /**
199      * Problem for date parsing.
200      */

201     public void testParseHour() {
202
203         // test 1...
204
Hour h = Hour.parseHour("2002-01-29 13");
205         assertEquals(13, h.getHour());
206
207     }
208
209     /**
210      * Serialize an instance, restore it, and check for equality.
211      */

212     public void testSerialization() {
213         Hour h1 = new Hour();
214         Hour h2 = null;
215
216         try {
217             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
218             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
219             out.writeObject(h1);
220             out.close();
221
222             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
223                 new ByteArrayInputStream JavaDoc(buffer.toByteArray())
224             );
225             h2 = (Hour) in.readObject();
226             in.close();
227         }
228         catch (Exception JavaDoc e) {
229             System.out.println(e.toString());
230         }
231         assertEquals(h1, h2);
232
233     }
234
235     /**
236      * Two objects that are equal are required to return the same hashCode.
237      */

238     public void testHashcode() {
239         Hour h1 = new Hour(7, 9, 10, 1999);
240         Hour h2 = new Hour(7, 9, 10, 1999);
241         assertTrue(h1.equals(h2));
242         int hash1 = h1.hashCode();
243         int hash2 = h2.hashCode();
244         assertEquals(hash1, hash2);
245     }
246
247     /**
248      * The {@link Hour} class is immutable, so should not be {@link Cloneable}.
249      */

250     public void testNotCloneable() {
251         Hour h = new Hour(7, 9, 10, 1999);
252         assertFalse(h instanceof Cloneable JavaDoc);
253     }
254
255 }
256
Popular Tags