KickJava   Java API By Example, From Geeks To Geeks.

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


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  * CompositeTitleTests.java
29  * ------------------------
30  * (C) Copyright 2005, by Object Refinery Limited and Contributors.
31  *
32  * Original Author: David Gilbert (for Object Refinery Limited);
33  * Contributor(s): -;
34  *
35  * $Id: CompositeTitleTests.java,v 1.1.2.1 2006/10/03 15:41:29 mungady Exp $
36  *
37  * Changes
38  * -------
39  * 04-Feb-2005 : Version 1 (DG);
40  *
41  */

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

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

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

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

89     public void testEquals() {
90         CompositeTitle t1 = new CompositeTitle(new BlockContainer());
91         CompositeTitle t2 = new CompositeTitle(new BlockContainer());
92         assertEquals(t1, t2);
93         assertEquals(t2, t1);
94         
95         // margin
96
t1.setMargin(new RectangleInsets(1.0, 2.0, 3.0, 4.0));
97         assertFalse(t1.equals(t2));
98         t2.setMargin(new RectangleInsets(1.0, 2.0, 3.0, 4.0));
99         assertTrue(t1.equals(t2));
100         
101         // border
102
t1.setBorder(new BlockBorder(Color.red));
103         assertFalse(t1.equals(t2));
104         t2.setBorder(new BlockBorder(Color.red));
105         assertTrue(t1.equals(t2));
106        
107         // padding
108
t1.setPadding(new RectangleInsets(1.0, 2.0, 3.0, 4.0));
109         assertFalse(t1.equals(t2));
110         t2.setPadding(new RectangleInsets(1.0, 2.0, 3.0, 4.0));
111         assertTrue(t1.equals(t2));
112         
113         // contained titles
114
t1.getContainer().add(new TextTitle("T1"));
115         assertFalse(t1.equals(t2));
116         t2.getContainer().add(new TextTitle("T1"));
117         assertTrue(t1.equals(t2));
118         
119     }
120
121     /**
122      * Two objects that are equal are required to return the same hashCode.
123      */

124     public void testHashcode() {
125         CompositeTitle t1 = new CompositeTitle(new BlockContainer());
126         t1.getContainer().add(new TextTitle("T1"));
127         CompositeTitle t2 = new CompositeTitle(new BlockContainer());
128         t2.getContainer().add(new TextTitle("T1"));
129         assertTrue(t1.equals(t2));
130         int h1 = t1.hashCode();
131         int h2 = t2.hashCode();
132         assertEquals(h1, h2);
133     }
134     
135     /**
136      * Confirm that cloning works.
137      */

138     public void testCloning() {
139         CompositeTitle t1 = new CompositeTitle(new BlockContainer());
140         t1.getContainer().add(new TextTitle("T1"));
141         CompositeTitle t2 = null;
142         try {
143             t2 = (CompositeTitle) t1.clone();
144         }
145         catch (CloneNotSupportedException JavaDoc e) {
146             fail(e.toString());
147         }
148         assertTrue(t1 != t2);
149         assertTrue(t1.getClass() == t2.getClass());
150         assertTrue(t1.equals(t2));
151     }
152
153     /**
154      * Serialize an instance, restore it, and check for equality.
155      */

156     public void testSerialization() {
157         CompositeTitle t1 = new CompositeTitle(new BlockContainer());
158         t1.getContainer().add(new TextTitle("T1"));
159         CompositeTitle t2 = null;
160         try {
161             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
162             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
163             out.writeObject(t1);
164             out.close();
165
166             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
167                 new ByteArrayInputStream JavaDoc(buffer.toByteArray())
168             );
169             t2 = (CompositeTitle) in.readObject();
170             in.close();
171         }
172         catch (Exception JavaDoc e) {
173             System.out.println(e.toString());
174         }
175         assertEquals(t1, t2);
176     }
177
178 }
179
Popular Tags