KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > chart > title > junit > TextTitleTests


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
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22  * USA.
23  *
24  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
25  * in the United States and other countries.]
26  *
27  * -------------------
28  * TextTitleTests.java
29  * -------------------
30  * (C) Copyright 2004, 2005, by Object Refinery Limited and Contributors.
31  *
32  * Original Author: David Gilbert (for Object Refinery Limited);
33  * Contributor(s): -;
34  *
35  * $Id: TextTitleTests.java,v 1.1.2.1 2006/10/03 15:41:29 mungady Exp $
36  *
37  * Changes
38  * -------
39  * 17-Feb-2004 : Version 1 (DG);
40  * 06-Jun-2005 : Use GradientPaint in equals() test (DG);
41  * 07-Oct-2005 : Updated testEquals() (DG);
42  *
43  */

44
45 package org.jfree.chart.title.junit;
46
47 import java.awt.Color JavaDoc;
48 import java.awt.Font JavaDoc;
49 import java.awt.GradientPaint JavaDoc;
50 import java.io.ByteArrayInputStream JavaDoc;
51 import java.io.ByteArrayOutputStream JavaDoc;
52 import java.io.ObjectInput JavaDoc;
53 import java.io.ObjectInputStream JavaDoc;
54 import java.io.ObjectOutput JavaDoc;
55 import java.io.ObjectOutputStream JavaDoc;
56
57 import junit.framework.Test;
58 import junit.framework.TestCase;
59 import junit.framework.TestSuite;
60
61 import org.jfree.chart.title.TextTitle;
62 import org.jfree.ui.HorizontalAlignment;
63
64 /**
65  * Tests for the {@link TextTitle} class.
66  */

67 public class TextTitleTests extends TestCase {
68
69     /**
70      * Returns the tests as a test suite.
71      *
72      * @return The test suite.
73      */

74     public static Test suite() {
75         return new TestSuite(TextTitleTests.class);
76     }
77
78     /**
79      * Constructs a new set of tests.
80      *
81      * @param name the name of the tests.
82      */

83     public TextTitleTests(String JavaDoc name) {
84         super(name);
85     }
86
87     /**
88      * Check that the equals() method distinguishes all fields.
89      */

90     public void testEquals() {
91         TextTitle t1 = new TextTitle();
92         TextTitle t2 = new TextTitle();
93         assertEquals(t1, t2);
94         
95         t1.setText("Test 1");
96         assertFalse(t1.equals(t2));
97         t2.setText("Test 1");
98         assertTrue(t1.equals(t2));
99         
100         Font JavaDoc f = new Font JavaDoc("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 JavaDoc(1.0f, 2.0f, Color.red,
113                 3.0f, 4.0f, Color.blue));
114         assertFalse(t1.equals(t2));
115         t2.setPaint(new GradientPaint JavaDoc(1.0f, 2.0f, Color.red,
116                 3.0f, 4.0f, Color.blue));
117         assertTrue(t1.equals(t2));
118         
119         // backgroundPaint
120
t1.setBackgroundPaint(new GradientPaint JavaDoc(4.0f, 3.0f, Color.red,
121                 2.0f, 1.0f, Color.blue));
122         assertFalse(t1.equals(t2));
123         t2.setBackgroundPaint(new GradientPaint JavaDoc(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         }
150         catch (CloneNotSupportedException JavaDoc e) {
151             System.err.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 JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
168             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
169             out.writeObject(t1);
170             out.close();
171
172             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
173                 new ByteArrayInputStream JavaDoc(buffer.toByteArray())
174             );
175             t2 = (TextTitle) in.readObject();
176             in.close();
177         }
178         catch (Exception JavaDoc e) {
179             System.out.println(e.toString());
180         }
181         assertEquals(t1, t2);
182
183     }
184
185 }
186
Popular Tags