001: /* ===========================================================
002: * JFreeChart : a free chart library for the Java(tm) platform
003: * ===========================================================
004: *
005: * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
006: *
007: * Project Info: http://www.jfree.org/jfreechart/index.html
008: *
009: * This library is free software; you can redistribute it and/or modify it
010: * under the terms of the GNU Lesser General Public License as published by
011: * the Free Software Foundation; either version 2.1 of the License, or
012: * (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful, but
015: * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
016: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
017: * License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library; if not, write to the Free Software
021: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
022: * USA.
023: *
024: * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
025: * in the United States and other countries.]
026: *
027: * --------------------------
028: * SimpleTimePeriodTests.java
029: * --------------------------
030: * (C) Copyright 2003-2005, by Object Refinery Limited.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: SimpleTimePeriodTests.java,v 1.1.2.1 2006/10/03 15:41:40 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 13-Mar-2003 : Version 1 (DG);
040: * 21-Oct-2003 : Added hashCode() test (DG);
041: *
042: */
043:
044: package org.jfree.data.time.junit;
045:
046: import java.io.ByteArrayInputStream;
047: import java.io.ByteArrayOutputStream;
048: import java.io.ObjectInput;
049: import java.io.ObjectInputStream;
050: import java.io.ObjectOutput;
051: import java.io.ObjectOutputStream;
052: import java.util.Date;
053:
054: import junit.framework.Test;
055: import junit.framework.TestCase;
056: import junit.framework.TestSuite;
057:
058: import org.jfree.data.time.SimpleTimePeriod;
059:
060: /**
061: * Tests for the {@link SimpleTimePeriod} class.
062: */
063: public class SimpleTimePeriodTests extends TestCase {
064:
065: /**
066: * Returns the tests as a test suite.
067: *
068: * @return The test suite.
069: */
070: public static Test suite() {
071: return new TestSuite(SimpleTimePeriodTests.class);
072: }
073:
074: /**
075: * Constructs a new set of tests.
076: *
077: * @param name the name of the tests.
078: */
079: public SimpleTimePeriodTests(String name) {
080: super (name);
081: }
082:
083: /**
084: * Common test setup.
085: */
086: protected void setUp() {
087: // no setup
088: }
089:
090: /**
091: * Check that an instance is equal to itself.
092: *
093: * SourceForge Bug ID: 558850.
094: */
095: public void testEqualsSelf() {
096: SimpleTimePeriod p = new SimpleTimePeriod(new Date(1000L),
097: new Date(1001L));
098: assertTrue(p.equals(p));
099: }
100:
101: /**
102: * Test the equals() method.
103: */
104: public void testEquals() {
105: SimpleTimePeriod p1 = new SimpleTimePeriod(new Date(1000L),
106: new Date(1004L));
107: SimpleTimePeriod p2 = new SimpleTimePeriod(new Date(1000L),
108: new Date(1004L));
109: assertTrue(p1.equals(p2));
110: assertTrue(p2.equals(p1));
111:
112: p1 = new SimpleTimePeriod(new Date(1002L), new Date(1004L));
113: assertFalse(p1.equals(p2));
114: p2 = new SimpleTimePeriod(new Date(1002L), new Date(1004L));
115: assertTrue(p1.equals(p2));
116:
117: p1 = new SimpleTimePeriod(new Date(1002L), new Date(1003L));
118: assertFalse(p1.equals(p2));
119: p2 = new SimpleTimePeriod(new Date(1002L), new Date(1003L));
120: assertTrue(p1.equals(p2));
121: }
122:
123: /**
124: * Serialize an instance, restore it, and check for equality.
125: */
126: public void testSerialization() {
127: SimpleTimePeriod p1 = new SimpleTimePeriod(new Date(1000L),
128: new Date(1001L));
129: SimpleTimePeriod p2 = null;
130: try {
131: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
132: ObjectOutput out = new ObjectOutputStream(buffer);
133: out.writeObject(p1);
134: out.close();
135: ObjectInput in = new ObjectInputStream(
136: new ByteArrayInputStream(buffer.toByteArray()));
137: p2 = (SimpleTimePeriod) in.readObject();
138: in.close();
139: } catch (Exception e) {
140: System.out.println(e.toString());
141: }
142: assertEquals(p1, p2);
143: }
144:
145: /**
146: * Two objects that are equal are required to return the same hashCode.
147: */
148: public void testHashcode() {
149: SimpleTimePeriod s1 = new SimpleTimePeriod(new Date(10L),
150: new Date(20L));
151: SimpleTimePeriod s2 = new SimpleTimePeriod(new Date(10L),
152: new Date(20L));
153: assertTrue(s1.equals(s2));
154: int h1 = s1.hashCode();
155: int h2 = s2.hashCode();
156: assertEquals(h1, h2);
157: }
158:
159: /**
160: * This class is immutable, so it should not implement Cloneable.
161: */
162: public void testClone() {
163: SimpleTimePeriod s1 = new SimpleTimePeriod(new Date(10L),
164: new Date(20L));
165: assertFalse(s1 instanceof Cloneable);
166: }
167:
168: /**
169: * Some checks for the compareTo() method.
170: */
171: public void testCompareTo() {
172: SimpleTimePeriod s1 = new SimpleTimePeriod(new Date(10L),
173: new Date(20L));
174: SimpleTimePeriod s2 = new SimpleTimePeriod(new Date(10L),
175: new Date(20L));
176: assertEquals(0, s1.compareTo(s2));
177:
178: s1 = new SimpleTimePeriod(new Date(9L), new Date(21L));
179: s2 = new SimpleTimePeriod(new Date(10L), new Date(20L));
180: assertEquals(-1, s1.compareTo(s2));
181:
182: s1 = new SimpleTimePeriod(new Date(11L), new Date(19L));
183: s2 = new SimpleTimePeriod(new Date(10L), new Date(20L));
184: assertEquals(1, s1.compareTo(s2));
185:
186: s1 = new SimpleTimePeriod(new Date(9L), new Date(19L));
187: s2 = new SimpleTimePeriod(new Date(10L), new Date(20L));
188: assertEquals(-1, s1.compareTo(s2));
189:
190: s1 = new SimpleTimePeriod(new Date(11L), new Date(21));
191: s2 = new SimpleTimePeriod(new Date(10L), new Date(20L));
192: assertEquals(1, s1.compareTo(s2));
193:
194: s1 = new SimpleTimePeriod(new Date(10L), new Date(18));
195: s2 = new SimpleTimePeriod(new Date(10L), new Date(20L));
196: assertEquals(-1, s1.compareTo(s2));
197:
198: s1 = new SimpleTimePeriod(new Date(10L), new Date(22));
199: s2 = new SimpleTimePeriod(new Date(10L), new Date(20L));
200: assertEquals(1, s1.compareTo(s2));
201: }
202:
203: }
|