KickJava   Java API By Example, From Geeks To Geeks.

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


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  * DateTitleTests.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: DateTitleTests.java,v 1.1.2.1 2006/10/03 15:41:28 mungady Exp $
36  *
37  * Changes
38  * -------
39  * 17-Feb-2004 : Version 1 (DG);
40  *
41  */

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

63 public class DateTitleTests extends TestCase {
64
65     /**
66      * Returns the tests as a test suite.
67      *
68      * @return The test suite.
69      */

70     public static Test suite() {
71         return new TestSuite(DateTitleTests.class);
72     }
73
74     /**
75      * Constructs a new set of tests.
76      *
77      * @param name the name of the tests.
78      */

79     public DateTitleTests(String JavaDoc name) {
80         super(name);
81     }
82
83     /**
84      * Check that the equals() method distinguishes all fields.
85      */

86     public void testEquals() {
87         DateTitle t1 = new DateTitle();
88         DateTitle t2 = new DateTitle();
89         assertEquals(t1, t2);
90         
91         t1.setText("Test 1");
92         assertFalse(t1.equals(t2));
93         t2.setText("Test 1");
94         assertTrue(t1.equals(t2));
95         
96         Font JavaDoc f = new Font JavaDoc("SansSerif", Font.PLAIN, 15);
97         t1.setFont(f);
98         assertFalse(t1.equals(t2));
99         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         }
135         catch (CloneNotSupportedException JavaDoc e) {
136             System.err.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 JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
153             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
154             out.writeObject(t1);
155             out.close();
156
157             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
158                 new ByteArrayInputStream JavaDoc(buffer.toByteArray())
159             );
160             t2 = (DateTitle) in.readObject();
161             in.close();
162         }
163         catch (Exception JavaDoc e) {
164             System.out.println(e.toString());
165         }
166         assertEquals(t1, t2);
167
168     }
169
170 }
171
Popular Tags