KickJava   Java API By Example, From Geeks To Geeks.

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


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  * MinuteTests.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: MinuteTests.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.data.time.Minute;
64 import org.jfree.date.MonthConstants;
65
66 /**
67  * Tests for the {@link Minute} class.
68  */

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

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

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

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

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

109     public void testEquals() {
110         Day day1 = new Day(29, MonthConstants.MARCH, 2002);
111         Hour hour1 = new Hour(15, day1);
112         Minute minute1 = new Minute(15, hour1);
113         Day day2 = new Day(29, MonthConstants.MARCH, 2002);
114         Hour hour2 = new Hour(15, day2);
115         Minute minute2 = new Minute(15, hour2);
116         assertTrue(minute1.equals(minute2));
117     }
118
119     /**
120      * In GMT, the 4.55pm on 21 Mar 2002 is java.util.Date(1016729700000L).
121      * Use this to check the Minute constructor.
122      */

123     public void testDateConstructor1() {
124
125         TimeZone JavaDoc zone = TimeZone.getTimeZone("GMT");
126         Minute m1 = new Minute(new Date JavaDoc(1016729699999L), zone);
127         Minute m2 = new Minute(new Date JavaDoc(1016729700000L), zone);
128
129         assertEquals(54, m1.getMinute());
130         assertEquals(1016729699999L, m1.getLastMillisecond(zone));
131
132         assertEquals(55, m2.getMinute());
133         assertEquals(1016729700000L, m2.getFirstMillisecond(zone));
134
135     }
136
137     /**
138      * In Singapore, the 4.55pm on 21 Mar 2002 is
139      * java.util.Date(1,014,281,700,000L). Use this to check the Minute
140      * constructor.
141      */

142     public void testDateConstructor2() {
143
144         TimeZone JavaDoc zone = TimeZone.getTimeZone("Asia/Singapore");
145         Minute m1 = new Minute(new Date JavaDoc(1016700899999L), zone);
146         Minute m2 = new Minute(new Date JavaDoc(1016700900000L), zone);
147
148         assertEquals(54, m1.getMinute());
149         assertEquals(1016700899999L, m1.getLastMillisecond(zone));
150
151         assertEquals(55, m2.getMinute());
152         assertEquals(1016700900000L, m2.getFirstMillisecond(zone));
153
154     }
155
156     /**
157      * Serialize an instance, restore it, and check for equality.
158      */

159     public void testSerialization() {
160
161         Minute m1 = new Minute();
162         Minute m2 = null;
163
164         try {
165             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
166             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
167             out.writeObject(m1);
168             out.close();
169
170             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
171                 new ByteArrayInputStream JavaDoc(buffer.toByteArray())
172             );
173             m2 = (Minute) in.readObject();
174             in.close();
175         }
176         catch (Exception JavaDoc e) {
177             System.out.println(e.toString());
178         }
179         assertEquals(m1, m2);
180
181     }
182     
183     /**
184      * Two objects that are equal are required to return the same hashCode.
185      */

186     public void testHashcode() {
187         Minute m1 = new Minute(45, 5, 1, 2, 2003);
188         Minute m2 = new Minute(45, 5, 1, 2, 2003);
189         assertTrue(m1.equals(m2));
190         int h1 = m1.hashCode();
191         int h2 = m2.hashCode();
192         assertEquals(h1, h2);
193     }
194     
195     /**
196      * The {@link Minute} class is immutable, so should not be
197      * {@link Cloneable}.
198      */

199     public void testNotCloneable() {
200         Minute m = new Minute(45, 5, 1, 2, 2003);
201         assertFalse(m instanceof Cloneable JavaDoc);
202     }
203  
204 }
205
Popular Tags