KickJava   Java API By Example, From Geeks To Geeks.

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


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  * SimpleTimePeriodTests.java
28  * --------------------------
29  * (C) Copyright 2003-2005, by Object Refinery Limited.
30  *
31  * Original Author: David Gilbert (for Object Refinery Limited);
32  * Contributor(s): -;
33  *
34  * $Id: SimpleTimePeriodTests.java,v 1.5 2005/03/11 14:37:23 mungady Exp $
35  *
36  * Changes
37  * -------
38  * 13-Mar-2003 : Version 1 (DG);
39  * 21-Oct-2003 : Added hashCode() test (DG);
40  *
41  */

42
43 package org.jfree.data.time.junit;
44
45 import java.io.ByteArrayInputStream JavaDoc;
46 import java.io.ByteArrayOutputStream JavaDoc;
47 import java.io.ObjectInput JavaDoc;
48 import java.io.ObjectInputStream JavaDoc;
49 import java.io.ObjectOutput JavaDoc;
50 import java.io.ObjectOutputStream JavaDoc;
51 import java.util.Date JavaDoc;
52
53 import junit.framework.Test;
54 import junit.framework.TestCase;
55 import junit.framework.TestSuite;
56
57 import org.jfree.data.time.SimpleTimePeriod;
58
59 /**
60  * Tests for the {@link SimpleTimePeriod} class.
61  */

62 public class SimpleTimePeriodTests extends TestCase {
63
64     /**
65      * Returns the tests as a test suite.
66      *
67      * @return The test suite.
68      */

69     public static Test suite() {
70         return new TestSuite(SimpleTimePeriodTests.class);
71     }
72
73     /**
74      * Constructs a new set of tests.
75      *
76      * @param name the name of the tests.
77      */

78     public SimpleTimePeriodTests(String JavaDoc name) {
79         super(name);
80     }
81
82     /**
83      * Common test setup.
84      */

85     protected void setUp() {
86         // no setup
87
}
88
89     /**
90      * Check that an instance is equal to itself.
91      *
92      * SourceForge Bug ID: 558850.
93      */

94     public void testEqualsSelf() {
95         SimpleTimePeriod p = new SimpleTimePeriod(
96             new Date JavaDoc(1000L), new Date JavaDoc(1001L)
97         );
98         assertTrue(p.equals(p));
99     }
100
101     /**
102      * Test the equals() method.
103      */

104     public void testEquals() {
105         SimpleTimePeriod p1 = new SimpleTimePeriod(
106             new Date JavaDoc(1000L), new Date JavaDoc(1004L)
107         );
108         SimpleTimePeriod p2 = new SimpleTimePeriod(
109             new Date JavaDoc(1000L), new Date JavaDoc(1004L)
110         );
111         assertTrue(p1.equals(p2));
112         assertTrue(p2.equals(p1));
113         
114         p1 = new SimpleTimePeriod(new Date JavaDoc(1002L), new Date JavaDoc(1004L));
115         assertFalse(p1.equals(p2));
116         p2 = new SimpleTimePeriod(new Date JavaDoc(1002L), new Date JavaDoc(1004L));
117         assertTrue(p1.equals(p2));
118         
119         p1 = new SimpleTimePeriod(new Date JavaDoc(1002L), new Date JavaDoc(1003L));
120         assertFalse(p1.equals(p2));
121         p2 = new SimpleTimePeriod(new Date JavaDoc(1002L), new Date JavaDoc(1003L));
122         assertTrue(p1.equals(p2));
123     }
124
125     /**
126      * Serialize an instance, restore it, and check for equality.
127      */

128     public void testSerialization() {
129         SimpleTimePeriod p1 = new SimpleTimePeriod(
130             new Date JavaDoc(1000L), new Date JavaDoc(1001L)
131         );
132         SimpleTimePeriod p2 = null;
133         try {
134             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
135             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
136             out.writeObject(p1);
137             out.close();
138             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
139                 new ByteArrayInputStream JavaDoc(buffer.toByteArray())
140             );
141             p2 = (SimpleTimePeriod) in.readObject();
142             in.close();
143         }
144         catch (Exception JavaDoc e) {
145             System.out.println(e.toString());
146         }
147         assertEquals(p1, p2);
148     }
149     
150     /**
151      * Two objects that are equal are required to return the same hashCode.
152      */

153     public void testHashcode() {
154         SimpleTimePeriod s1 = new SimpleTimePeriod(
155             new Date JavaDoc(10L), new Date JavaDoc(20L)
156         );
157         SimpleTimePeriod s2 = new SimpleTimePeriod(
158             new Date JavaDoc(10L), new Date JavaDoc(20L)
159         );
160         assertTrue(s1.equals(s2));
161         int h1 = s1.hashCode();
162         int h2 = s2.hashCode();
163         assertEquals(h1, h2);
164     }
165
166     /**
167      * This class is immutable, so it should not implement Cloneable.
168      */

169     public void testClone() {
170         SimpleTimePeriod s1 = new SimpleTimePeriod(
171             new Date JavaDoc(10L), new Date JavaDoc(20L)
172         );
173         assertFalse(s1 instanceof Cloneable JavaDoc);
174     }
175     
176     /**
177      * Some checks for the compareTo() method.
178      */

179     public void testCompareTo() {
180         SimpleTimePeriod s1 = new SimpleTimePeriod(
181             new Date JavaDoc(10L), new Date JavaDoc(20L)
182         );
183         SimpleTimePeriod s2 = new SimpleTimePeriod(
184             new Date JavaDoc(10L), new Date JavaDoc(20L)
185         );
186         assertEquals(0, s1.compareTo(s2));
187         
188         s1 = new SimpleTimePeriod(new Date JavaDoc(9L), new Date JavaDoc(21L));
189         s2 = new SimpleTimePeriod(new Date JavaDoc(10L), new Date JavaDoc(20L));
190         assertEquals(-1, s1.compareTo(s2));
191         
192         s1 = new SimpleTimePeriod(new Date JavaDoc(11L), new Date JavaDoc(19L));
193         s2 = new SimpleTimePeriod(new Date JavaDoc(10L), new Date JavaDoc(20L));
194         assertEquals(1, s1.compareTo(s2));
195
196         s1 = new SimpleTimePeriod(new Date JavaDoc(9L), new Date JavaDoc(19L));
197         s2 = new SimpleTimePeriod(new Date JavaDoc(10L), new Date JavaDoc(20L));
198         assertEquals(-1, s1.compareTo(s2));
199     
200         s1 = new SimpleTimePeriod(new Date JavaDoc(11L), new Date JavaDoc(21));
201         s2 = new SimpleTimePeriod(new Date JavaDoc(10L), new Date JavaDoc(20L));
202         assertEquals(1, s1.compareTo(s2));
203
204         s1 = new SimpleTimePeriod(new Date JavaDoc(10L), new Date JavaDoc(18));
205         s2 = new SimpleTimePeriod(new Date JavaDoc(10L), new Date JavaDoc(20L));
206         assertEquals(-1, s1.compareTo(s2));
207
208         s1 = new SimpleTimePeriod(new Date JavaDoc(10L), new Date JavaDoc(22));
209         s2 = new SimpleTimePeriod(new Date JavaDoc(10L), new Date JavaDoc(20L));
210         assertEquals(1, s1.compareTo(s2));
211     }
212     
213 }
214
Popular Tags