KickJava   Java API By Example, From Geeks To Geeks.

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


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  * MillisecondTests.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: MillisecondTests.java,v 1.6 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  * 21-Oct-2003 : Added hashCode tests (DG);
41  * 29-Apr-2004 : Added test for getMiddleMillisecond() method (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.Millisecond;
64 import org.jfree.data.time.Minute;
65 import org.jfree.data.time.Second;
66 import org.jfree.date.MonthConstants;
67
68 /**
69  * Tests for the {@link Millisecond} class.
70  */

71 public class MillisecondTests extends TestCase {
72
73     /**
74      * Returns the tests as a test suite.
75      *
76      * @return The test suite.
77      */

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

87     public MillisecondTests(String JavaDoc name) {
88         super(name);
89     }
90
91     /**
92      * Common test setup.
93      */

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

103     public void testEqualsSelf() {
104         Millisecond millisecond = new Millisecond();
105         assertTrue(millisecond.equals(millisecond));
106     }
107
108     /**
109      * Tests the equals method.
110      */

111     public void testEquals() {
112         Day day1 = new Day(29, MonthConstants.MARCH, 2002);
113         Hour hour1 = new Hour(15, day1);
114         Minute minute1 = new Minute(15, hour1);
115         Second second1 = new Second(34, minute1);
116         Millisecond milli1 = new Millisecond(999, second1);
117         Day day2 = new Day(29, MonthConstants.MARCH, 2002);
118         Hour hour2 = new Hour(15, day2);
119         Minute minute2 = new Minute(15, hour2);
120         Second second2 = new Second(34, minute2);
121         Millisecond milli2 = new Millisecond(999, second2);
122         assertTrue(milli1.equals(milli2));
123     }
124
125     /**
126      * In GMT, the 4.55:59.123pm on 21 Mar 2002 is
127      * java.util.Date(1016729759123L). Use this to check the Millisecond
128      * constructor.
129      */

130     public void testDateConstructor1() {
131
132         TimeZone JavaDoc zone = TimeZone.getTimeZone("GMT");
133         Millisecond m1 = new Millisecond(new Date JavaDoc(1016729759122L), zone);
134         Millisecond m2 = new Millisecond(new Date JavaDoc(1016729759123L), zone);
135
136         assertEquals(122, m1.getMillisecond());
137         assertEquals(1016729759122L, m1.getLastMillisecond(zone));
138
139         assertEquals(123, m2.getMillisecond());
140         assertEquals(1016729759123L, m2.getFirstMillisecond(zone));
141
142     }
143
144     /**
145      * In Tallinn, the 4.55:59.123pm on 21 Mar 2002 is
146      * java.util.Date(1016722559123L). Use this to check the Millisecond
147      * constructor.
148      */

149     public void testDateConstructor2() {
150
151         TimeZone JavaDoc zone = TimeZone.getTimeZone("Europe/Tallinn");
152         Millisecond m1 = new Millisecond(new Date JavaDoc(1016722559122L), zone);
153         Millisecond m2 = new Millisecond(new Date JavaDoc(1016722559123L), zone);
154
155         assertEquals(122, m1.getMillisecond());
156         assertEquals(1016722559122L, m1.getLastMillisecond(zone));
157
158         assertEquals(123, m2.getMillisecond());
159         assertEquals(1016722559123L, m2.getFirstMillisecond(zone));
160
161     }
162
163     /**
164      * Serialize an instance, restore it, and check for equality.
165      */

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

193     public void testHashcode() {
194         Millisecond m1 = new Millisecond(599, 23, 45, 7, 9, 10, 2007);
195         Millisecond m2 = new Millisecond(599, 23, 45, 7, 9, 10, 2007);
196         assertTrue(m1.equals(m2));
197         int hash1 = m1.hashCode();
198         int hash2 = m2.hashCode();
199         assertEquals(hash1, hash2);
200     }
201
202     /**
203      * A test for bug report 943985 - the calculation for the middle
204      * millisecond is incorrect for odd milliseconds.
205      */

206     public void test943985() {
207         Millisecond ms = new Millisecond(new java.util.Date JavaDoc(4));
208         assertEquals(ms.getFirstMillisecond(), ms.getMiddleMillisecond());
209         assertEquals(ms.getMiddleMillisecond(), ms.getLastMillisecond());
210         ms = new Millisecond(new java.util.Date JavaDoc(5));
211         assertEquals(ms.getFirstMillisecond(), ms.getMiddleMillisecond());
212         assertEquals(ms.getMiddleMillisecond(), ms.getLastMillisecond());
213     }
214     
215     /**
216      * The {@link Millisecond} class is immutable, so should not be
217      * {@link Cloneable}.
218      */

219     public void testNotCloneable() {
220         Millisecond m = new Millisecond(599, 23, 45, 7, 9, 10, 2007);
221         assertFalse(m instanceof Cloneable JavaDoc);
222     }
223
224 }
225
Popular Tags