KickJava   Java API By Example, From Geeks To Geeks.

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


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  * FixedMillisecondTests.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: FixedMillisecondTests.java,v 1.3 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  * 21-Oct-2003 : Added hashCode test (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
53 import junit.framework.Test;
54 import junit.framework.TestCase;
55 import junit.framework.TestSuite;
56
57 import org.jfree.data.time.FixedMillisecond;
58
59 /**
60  * Tests for the {@link FixedMillisecond} class.
61  */

62 public class FixedMillisecondTests 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(FixedMillisecondTests.class);
71     }
72
73     /**
74      * Constructs a new set of tests.
75      *
76      * @param name the name of the tests.
77      */

78     public FixedMillisecondTests(String JavaDoc name) {
79         super(name);
80     }
81
82     /**
83      * Serialize an instance, restore it, and check for equality.
84      */

85     public void testSerialization() {
86
87         FixedMillisecond m1 = new FixedMillisecond();
88         FixedMillisecond m2 = null;
89
90         try {
91             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
92             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
93             out.writeObject(m1);
94             out.close();
95
96             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
97                 new ByteArrayInputStream JavaDoc(buffer.toByteArray())
98             );
99             m2 = (FixedMillisecond) in.readObject();
100             in.close();
101         }
102         catch (Exception JavaDoc e) {
103             System.out.println(e.toString());
104         }
105         assertEquals(m1, m2);
106
107     }
108     
109     /**
110      * Two objects that are equal are required to return the same hashCode.
111      */

112     public void testHashcode() {
113         FixedMillisecond m1 = new FixedMillisecond(500000L);
114         FixedMillisecond m2 = new FixedMillisecond(500000L);
115         assertTrue(m1.equals(m2));
116         int h1 = m1.hashCode();
117         int h2 = m2.hashCode();
118         assertEquals(h1, h2);
119     }
120     
121     /**
122      * The {@link FixedMillisecond} class is immutable, so should not be
123      * {@link Cloneable}.
124      */

125     public void testNotCloneable() {
126         FixedMillisecond m = new FixedMillisecond(500000L);
127         assertFalse(m instanceof Cloneable JavaDoc);
128     }
129
130 }
131
Popular Tags