KickJava   Java API By Example, From Geeks To Geeks.

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


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  * SecondTests.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: SecondTests.java,v 1.4 2005/03/11 14:37:23 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-Oct-2003 : Added serialization test (DG);
41  * 11-Jan-2005 : Added test for non-clonability (DG);
42  *
43  */

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

69 public class SecondTests 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(SecondTests.class);
78     }
79
80     /**
81      * Constructs a new set of tests.
82      *
83      * @param name the name of the tests.
84      */

85     public SecondTests(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      * Test that a Second instance is equal to itself.
98      *
99      * SourceForge Bug ID: 558850.
100      */

101     public void testEqualsSelf() {
102         Second second = new Second();
103         assertTrue(second.equals(second));
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         Second second1 = new Second(34, minute1);
114         Day day2 = new Day(29, MonthConstants.MARCH, 2002);
115         Hour hour2 = new Hour(15, day2);
116         Minute minute2 = new Minute(15, hour2);
117         Second second2 = new Second(34, minute2);
118         assertTrue(second1.equals(second2));
119     }
120
121     /**
122      * In GMT, the 4.55:59pm on 21 Mar 2002 is java.util.Date(1016729759000L).
123      * Use this to check the Second constructor.
124      */

125     public void testDateConstructor1() {
126
127         TimeZone JavaDoc zone = TimeZone.getTimeZone("GMT");
128         Second s1 = new Second(new Date JavaDoc(1016729758999L), zone);
129         Second s2 = new Second(new Date JavaDoc(1016729759000L), zone);
130
131         assertEquals(58, s1.getSecond());
132         assertEquals(1016729758999L, s1.getLastMillisecond(zone));
133
134         assertEquals(59, s2.getSecond());
135         assertEquals(1016729759000L, s2.getFirstMillisecond(zone));
136
137     }
138
139     /**
140      * In Chicago, the 4.55:59pm on 21 Mar 2002 is
141      * java.util.Date(1016751359000L). Use this to check the Second constructor.
142      */

143     public void testDateConstructor2() {
144
145         TimeZone JavaDoc zone = TimeZone.getTimeZone("America/Chicago");
146         Second s1 = new Second(new Date JavaDoc(1016751358999L), zone);
147         Second s2 = new Second(new Date JavaDoc(1016751359000L), zone);
148
149         assertEquals(58, s1.getSecond());
150         assertEquals(1016751358999L, s1.getLastMillisecond(zone));
151
152         assertEquals(59, s2.getSecond());
153         assertEquals(1016751359000L, s2.getFirstMillisecond(zone));
154
155     }
156
157     /**
158      * Serialize an instance, restore it, and check for equality.
159      */

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

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

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