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: * TextTitleTests.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: TextTitleTests.java,v 1.1.2.1 2006/10/03 15:41:29 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 17-Feb-2004 : Version 1 (DG);
040: * 06-Jun-2005 : Use GradientPaint in equals() test (DG);
041: * 07-Oct-2005 : Updated testEquals() (DG);
042: *
043: */
044:
045: package org.jfree.chart.title.junit;
046:
047: import java.awt.Color;
048: import java.awt.Font;
049: import java.awt.GradientPaint;
050: import java.io.ByteArrayInputStream;
051: import java.io.ByteArrayOutputStream;
052: import java.io.ObjectInput;
053: import java.io.ObjectInputStream;
054: import java.io.ObjectOutput;
055: import java.io.ObjectOutputStream;
056:
057: import junit.framework.Test;
058: import junit.framework.TestCase;
059: import junit.framework.TestSuite;
060:
061: import org.jfree.chart.title.TextTitle;
062: import org.jfree.ui.HorizontalAlignment;
063:
064: /**
065: * Tests for the {@link TextTitle} class.
066: */
067: public class TextTitleTests extends TestCase {
068:
069: /**
070: * Returns the tests as a test suite.
071: *
072: * @return The test suite.
073: */
074: public static Test suite() {
075: return new TestSuite(TextTitleTests.class);
076: }
077:
078: /**
079: * Constructs a new set of tests.
080: *
081: * @param name the name of the tests.
082: */
083: public TextTitleTests(String name) {
084: super (name);
085: }
086:
087: /**
088: * Check that the equals() method distinguishes all fields.
089: */
090: public void testEquals() {
091: TextTitle t1 = new TextTitle();
092: TextTitle t2 = new TextTitle();
093: assertEquals(t1, t2);
094:
095: t1.setText("Test 1");
096: assertFalse(t1.equals(t2));
097: t2.setText("Test 1");
098: assertTrue(t1.equals(t2));
099:
100: Font f = new Font("SansSerif", Font.PLAIN, 15);
101: t1.setFont(f);
102: assertFalse(t1.equals(t2));
103: t2.setFont(f);
104: assertTrue(t1.equals(t2));
105:
106: t1.setTextAlignment(HorizontalAlignment.RIGHT);
107: assertFalse(t1.equals(t2));
108: t2.setTextAlignment(HorizontalAlignment.RIGHT);
109: assertTrue(t1.equals(t2));
110:
111: // paint
112: t1.setPaint(new GradientPaint(1.0f, 2.0f, Color.red, 3.0f,
113: 4.0f, Color.blue));
114: assertFalse(t1.equals(t2));
115: t2.setPaint(new GradientPaint(1.0f, 2.0f, Color.red, 3.0f,
116: 4.0f, Color.blue));
117: assertTrue(t1.equals(t2));
118:
119: // backgroundPaint
120: t1.setBackgroundPaint(new GradientPaint(4.0f, 3.0f, Color.red,
121: 2.0f, 1.0f, Color.blue));
122: assertFalse(t1.equals(t2));
123: t2.setBackgroundPaint(new GradientPaint(4.0f, 3.0f, Color.red,
124: 2.0f, 1.0f, Color.blue));
125: assertTrue(t1.equals(t2));
126:
127: }
128:
129: /**
130: * Two objects that are equal are required to return the same hashCode.
131: */
132: public void testHashcode() {
133: TextTitle t1 = new TextTitle();
134: TextTitle t2 = new TextTitle();
135: assertTrue(t1.equals(t2));
136: int h1 = t1.hashCode();
137: int h2 = t2.hashCode();
138: assertEquals(h1, h2);
139: }
140:
141: /**
142: * Confirm that cloning works.
143: */
144: public void testCloning() {
145: TextTitle t1 = new TextTitle();
146: TextTitle t2 = null;
147: try {
148: t2 = (TextTitle) t1.clone();
149: } catch (CloneNotSupportedException e) {
150: System.err
151: .println("TextTitleTests.testCloning: failed to clone.");
152: }
153: assertTrue(t1 != t2);
154: assertTrue(t1.getClass() == t2.getClass());
155: assertTrue(t1.equals(t2));
156: }
157:
158: /**
159: * Serialize an instance, restore it, and check for equality.
160: */
161: public void testSerialization() {
162:
163: TextTitle t1 = new TextTitle("Test");
164: TextTitle t2 = null;
165:
166: try {
167: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
168: ObjectOutput out = new ObjectOutputStream(buffer);
169: out.writeObject(t1);
170: out.close();
171:
172: ObjectInput in = new ObjectInputStream(
173: new ByteArrayInputStream(buffer.toByteArray()));
174: t2 = (TextTitle) in.readObject();
175: in.close();
176: } catch (Exception e) {
177: System.out.println(e.toString());
178: }
179: assertEquals(t1, t2);
180:
181: }
182:
183: }
|