KickJava   Java API By Example, From Geeks To Geeks.

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


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
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22  * USA.
23  *
24  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
25  * in the United States and other countries.]
26  *
27  * -------------------
28  * DateRangeTests.java
29  * -------------------
30  * (C) Copyright 2004, 2005, by Object Refinery Limited and Contributors.
31  *
32  * Original Author: David Gilbert (for Object Refinery Limited);
33  * Contributor(s): -;
34  *
35  * $Id: DateRangeTests.java,v 1.1.2.1 2006/10/03 15:41:39 mungady Exp $
36  *
37  * Changes
38  * -------
39  * 23-Mar-2004 : Version 1 (DG);
40  * 11-Jan-2005 : Added test to ensure Cloneable is not implemented (DG);
41  *
42  */

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

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

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

79     public DateRangeTests(String JavaDoc name) {
80         super(name);
81     }
82
83     /**
84      * Confirm that the equals method can distinguish all the required fields.
85      */

86     public void testEquals() {
87         DateRange r1 = new DateRange(new Date JavaDoc(1000L), new Date JavaDoc(2000L));
88         DateRange r2 = new DateRange(new Date JavaDoc(1000L), new Date JavaDoc(2000L));
89         assertTrue(r1.equals(r2));
90         assertTrue(r2.equals(r1));
91
92         r1 = new DateRange(new Date JavaDoc(1111L), new Date JavaDoc(2000L));
93         assertFalse(r1.equals(r2));
94         r2 = new DateRange(new Date JavaDoc(1111L), new Date JavaDoc(2000L));
95         assertTrue(r1.equals(r2));
96
97         r1 = new DateRange(new Date JavaDoc(1111L), new Date JavaDoc(2222L));
98         assertFalse(r1.equals(r2));
99         r2 = new DateRange(new Date JavaDoc(1111L), new Date JavaDoc(2222L));
100         assertTrue(r1.equals(r2));
101     }
102
103     /**
104      * Serialize an instance, restore it, and check for equality.
105      */

106     public void testSerialization() {
107         DateRange r1 = new DateRange(new Date JavaDoc(1000L), new Date JavaDoc(2000L));
108         DateRange r2 = null;
109
110         try {
111             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
112             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
113             out.writeObject(r1);
114             out.close();
115
116             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
117                 new ByteArrayInputStream JavaDoc(buffer.toByteArray())
118             );
119             r2 = (DateRange) in.readObject();
120             in.close();
121         }
122         catch (Exception JavaDoc e) {
123             System.out.println(e.toString());
124         }
125         assertEquals(r1, r2);
126     }
127     
128     /**
129      * The {@link DateRange} class is immutable, so it doesn't need to
130      * be cloneable.
131      */

132     public void testClone() {
133         DateRange r1 = new DateRange(new Date JavaDoc(1000L), new Date JavaDoc(2000L));
134         assertFalse(r1 instanceof Cloneable JavaDoc);
135     }
136     
137 }
138
Popular Tags