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: * DateTitleTests.java
029: * -------------------
030: * (C) Copyright 2004, 2005, by Object Refinery Limited and Contributors.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: DateTitleTests.java,v 1.1.2.1 2006/10/03 15:41:28 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 17-Feb-2004 : Version 1 (DG);
040: *
041: */
042:
043: package org.jfree.chart.title.junit;
044:
045: import java.awt.Color;
046: import java.awt.Font;
047: import java.io.ByteArrayInputStream;
048: import java.io.ByteArrayOutputStream;
049: import java.io.ObjectInput;
050: import java.io.ObjectInputStream;
051: import java.io.ObjectOutput;
052: import java.io.ObjectOutputStream;
053:
054: import junit.framework.Test;
055: import junit.framework.TestCase;
056: import junit.framework.TestSuite;
057:
058: import org.jfree.chart.title.DateTitle;
059:
060: /**
061: * Tests for the {@link DateTitle} class.
062: */
063: public class DateTitleTests 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(DateTitleTests.class);
072: }
073:
074: /**
075: * Constructs a new set of tests.
076: *
077: * @param name the name of the tests.
078: */
079: public DateTitleTests(String name) {
080: super (name);
081: }
082:
083: /**
084: * Check that the equals() method distinguishes all fields.
085: */
086: public void testEquals() {
087: DateTitle t1 = new DateTitle();
088: DateTitle t2 = new DateTitle();
089: assertEquals(t1, t2);
090:
091: t1.setText("Test 1");
092: assertFalse(t1.equals(t2));
093: t2.setText("Test 1");
094: assertTrue(t1.equals(t2));
095:
096: Font f = new Font("SansSerif", Font.PLAIN, 15);
097: t1.setFont(f);
098: assertFalse(t1.equals(t2));
099: t2.setFont(f);
100: assertTrue(t1.equals(t2));
101:
102: t1.setPaint(Color.blue);
103: assertFalse(t1.equals(t2));
104: t2.setPaint(Color.blue);
105: assertTrue(t1.equals(t2));
106:
107: t1.setBackgroundPaint(Color.blue);
108: assertFalse(t1.equals(t2));
109: t2.setBackgroundPaint(Color.blue);
110: assertTrue(t1.equals(t2));
111:
112: }
113:
114: /**
115: * Two objects that are equal are required to return the same hashCode.
116: */
117: public void testHashcode() {
118: DateTitle t1 = new DateTitle();
119: DateTitle t2 = new DateTitle();
120: assertTrue(t1.equals(t2));
121: int h1 = t1.hashCode();
122: int h2 = t2.hashCode();
123: assertEquals(h1, h2);
124: }
125:
126: /**
127: * Confirm that cloning works.
128: */
129: public void testCloning() {
130: DateTitle t1 = new DateTitle();
131: DateTitle t2 = null;
132: try {
133: t2 = (DateTitle) t1.clone();
134: } catch (CloneNotSupportedException e) {
135: System.err
136: .println("DateTitleTests.testCloning: failed to clone.");
137: }
138: assertTrue(t1 != t2);
139: assertTrue(t1.getClass() == t2.getClass());
140: assertTrue(t1.equals(t2));
141: }
142:
143: /**
144: * Serialize an instance, restore it, and check for equality.
145: */
146: public void testSerialization() {
147:
148: DateTitle t1 = new DateTitle();
149: DateTitle t2 = null;
150:
151: try {
152: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
153: ObjectOutput out = new ObjectOutputStream(buffer);
154: out.writeObject(t1);
155: out.close();
156:
157: ObjectInput in = new ObjectInputStream(
158: new ByteArrayInputStream(buffer.toByteArray()));
159: t2 = (DateTitle) in.readObject();
160: in.close();
161: } catch (Exception e) {
162: System.out.println(e.toString());
163: }
164: assertEquals(t1, t2);
165:
166: }
167:
168: }
|