001: /* ===========================================================
002: * JFreeChart : a free chart library for the Java(tm) platform
003: * ===========================================================
004: *
005: * (C) Copyright 2000-2007, 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: * QuarterDateFormatTests.java
029: * ---------------------------
030: * (C) Copyright 2005, 2007, by Object Refinery Limited and Contributors.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: QuarterDateFormatTests.java,v 1.1.2.2 2007/06/08 15:20:15 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 10-May-2005 : Version 1 (DG);
040: * 08-Jun-2007 : Added check for new field in testEquals() (DG);
041: *
042: */
043:
044: package org.jfree.chart.axis.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.TimeZone;
053:
054: import junit.framework.Test;
055: import junit.framework.TestCase;
056: import junit.framework.TestSuite;
057:
058: import org.jfree.chart.axis.QuarterDateFormat;
059:
060: /**
061: * Tests for the {@link QuarterDateFormat} class.
062: */
063: public class QuarterDateFormatTests 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(QuarterDateFormatTests.class);
072: }
073:
074: /**
075: * Constructs a new set of tests.
076: *
077: * @param name the name of the tests.
078: */
079: public QuarterDateFormatTests(String name) {
080: super (name);
081: }
082:
083: /**
084: * Confirm that the equals method can distinguish all the required fields.
085: */
086: public void testEquals() {
087: QuarterDateFormat qf1 = new QuarterDateFormat(TimeZone
088: .getTimeZone("GMT"),
089: new String[] { "1", "2", "3", "4" });
090: QuarterDateFormat qf2 = new QuarterDateFormat(TimeZone
091: .getTimeZone("GMT"),
092: new String[] { "1", "2", "3", "4" });
093: assertTrue(qf1.equals(qf2));
094: assertTrue(qf2.equals(qf1));
095:
096: qf1 = new QuarterDateFormat(TimeZone.getTimeZone("PST"),
097: new String[] { "1", "2", "3", "4" });
098: assertFalse(qf1.equals(qf2));
099: qf2 = new QuarterDateFormat(TimeZone.getTimeZone("PST"),
100: new String[] { "1", "2", "3", "4" });
101: assertTrue(qf1.equals(qf2));
102:
103: qf1 = new QuarterDateFormat(TimeZone.getTimeZone("PST"),
104: new String[] { "A", "2", "3", "4" });
105: assertFalse(qf1.equals(qf2));
106: qf2 = new QuarterDateFormat(TimeZone.getTimeZone("PST"),
107: new String[] { "A", "2", "3", "4" });
108: assertTrue(qf1.equals(qf2));
109:
110: qf1 = new QuarterDateFormat(TimeZone.getTimeZone("PST"),
111: new String[] { "A", "2", "3", "4" }, true);
112: assertFalse(qf1.equals(qf2));
113: qf2 = new QuarterDateFormat(TimeZone.getTimeZone("PST"),
114: new String[] { "A", "2", "3", "4" }, true);
115: assertTrue(qf1.equals(qf2));
116: }
117:
118: /**
119: * Two objects that are equal are required to return the same hashCode.
120: */
121: public void testHashCode() {
122: QuarterDateFormat qf1 = new QuarterDateFormat(TimeZone
123: .getTimeZone("GMT"),
124: new String[] { "1", "2", "3", "4" });
125: QuarterDateFormat qf2 = new QuarterDateFormat(TimeZone
126: .getTimeZone("GMT"),
127: new String[] { "1", "2", "3", "4" });
128: assertTrue(qf1.equals(qf2));
129: int h1 = qf1.hashCode();
130: int h2 = qf2.hashCode();
131: assertEquals(h1, h2);
132: }
133:
134: /**
135: * Confirm that cloning works.
136: */
137: public void testCloning() {
138: QuarterDateFormat qf1 = new QuarterDateFormat(TimeZone
139: .getTimeZone("GMT"),
140: new String[] { "1", "2", "3", "4" });
141: QuarterDateFormat qf2 = null;
142: qf2 = (QuarterDateFormat) qf1.clone();
143: assertTrue(qf1 != qf2);
144: assertTrue(qf1.getClass() == qf2.getClass());
145: assertTrue(qf1.equals(qf2));
146: }
147:
148: /**
149: * Serialize an instance, restore it, and check for equality.
150: */
151: public void testSerialization() {
152: QuarterDateFormat qf1 = new QuarterDateFormat(TimeZone
153: .getTimeZone("GMT"),
154: new String[] { "1", "2", "3", "4" });
155: QuarterDateFormat qf2 = null;
156: try {
157: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
158: ObjectOutput out = new ObjectOutputStream(buffer);
159: out.writeObject(qf1);
160: out.close();
161:
162: ObjectInput in = new ObjectInputStream(
163: new ByteArrayInputStream(buffer.toByteArray()));
164: qf2 = (QuarterDateFormat) in.readObject();
165: in.close();
166: } catch (Exception e) {
167: fail(e.toString());
168: }
169: assertTrue(qf1.equals(qf2));
170: }
171:
172: }
|