KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > chart > axis > junit > CategoryTickTests


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  * CategoryTickTests.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: CategoryTickTests.java,v 1.1.2.1 2006/10/03 15:41:23 mungady Exp $
36  *
37  * Changes
38  * -------
39  * 13-May-2004 : Version 1 (DG);
40  * 07-Jan-2005 : Added test for hashCode() (DG);
41  *
42  */

43
44 package org.jfree.chart.axis.junit;
45
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.axis.CategoryTick;
58 import org.jfree.text.TextBlock;
59 import org.jfree.text.TextBlockAnchor;
60 import org.jfree.text.TextLine;
61 import org.jfree.ui.TextAnchor;
62
63 /**
64  * Tests for the {@link CategoryTick} class.
65  */

66 public class CategoryTickTests 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(CategoryTickTests.class);
75     }
76
77     /**
78      * Constructs a new set of tests.
79      *
80      * @param name the name of the tests.
81      */

82     public CategoryTickTests(String JavaDoc name) {
83         super(name);
84     }
85
86     /**
87      * Confirm that the equals method can distinguish all the required fields.
88      */

89     public void testEquals() {
90         
91         Comparable JavaDoc c1 = "C1";
92         Comparable JavaDoc c2 = "C2";
93         TextBlock tb1 = new TextBlock();
94         tb1.addLine(new TextLine("Block 1"));
95         TextBlock tb2 = new TextBlock();
96         tb1.addLine(new TextLine("Block 2"));
97         TextBlockAnchor tba1 = TextBlockAnchor.CENTER;
98         TextBlockAnchor tba2 = TextBlockAnchor.BOTTOM_CENTER;
99         TextAnchor ta1 = TextAnchor.CENTER;
100         TextAnchor ta2 = TextAnchor.BASELINE_LEFT;
101         
102         CategoryTick t1 = new CategoryTick(c1, tb1, tba1, ta1, 1.0f);
103         CategoryTick t2 = new CategoryTick(c1, tb1, tba1, ta1, 1.0f);
104         assertTrue(t1.equals(t2));
105         
106         t1 = new CategoryTick(c2, tb1, tba1, ta1, 1.0f);
107         assertFalse(t1.equals(t2));
108         t2 = new CategoryTick(c2, tb1, tba1, ta1, 1.0f);
109         assertTrue(t1.equals(t2));
110
111         t1 = new CategoryTick(c2, tb2, tba1, ta1, 1.0f);
112         assertFalse(t1.equals(t2));
113         t2 = new CategoryTick(c2, tb2, tba1, ta1, 1.0f);
114         assertTrue(t1.equals(t2));
115
116         t1 = new CategoryTick(c2, tb2, tba2, ta1, 1.0f);
117         assertFalse(t1.equals(t2));
118         t2 = new CategoryTick(c2, tb2, tba2, ta1, 1.0f);
119         assertTrue(t1.equals(t2));
120
121         t1 = new CategoryTick(c2, tb2, tba2, ta2, 1.0f);
122         assertFalse(t1.equals(t2));
123         t2 = new CategoryTick(c2, tb2, tba2, ta2, 1.0f);
124         assertTrue(t1.equals(t2));
125
126         t1 = new CategoryTick(c2, tb2, tba2, ta2, 2.0f);
127         assertFalse(t1.equals(t2));
128         t2 = new CategoryTick(c2, tb2, tba2, ta2, 2.0f);
129         assertTrue(t1.equals(t2));
130         
131     }
132
133     /**
134      * Two objects that are equal are required to return the same hashCode.
135      */

136     public void testHashCode() {
137         Comparable JavaDoc c1 = "C1";
138         TextBlock tb1 = new TextBlock();
139         tb1.addLine(new TextLine("Block 1"));
140         tb1.addLine(new TextLine("Block 2"));
141         TextBlockAnchor tba1 = TextBlockAnchor.CENTER;
142         TextAnchor ta1 = TextAnchor.CENTER;
143         
144         CategoryTick t1 = new CategoryTick(c1, tb1, tba1, ta1, 1.0f);
145         CategoryTick t2 = new CategoryTick(c1, tb1, tba1, ta1, 1.0f);
146         assertTrue(t1.equals(t2));
147         int h1 = t1.hashCode();
148         int h2 = t2.hashCode();
149         assertEquals(h1, h2);
150     }
151
152     /**
153      * Confirm that cloning works.
154      */

155     public void testCloning() {
156         CategoryTick t1 = new CategoryTick(
157             "C1", new TextBlock(), TextBlockAnchor.CENTER,
158             TextAnchor.CENTER, 1.5f
159         );
160         CategoryTick t2 = null;
161         try {
162             t2 = (CategoryTick) t1.clone();
163         }
164         catch (CloneNotSupportedException JavaDoc e) {
165             System.err.println("Failed to clone.");
166         }
167         assertTrue(t1 != t2);
168         assertTrue(t1.getClass() == t2.getClass());
169         assertTrue(t1.equals(t2));
170     }
171
172     /**
173      * Serialize an instance, restore it, and check for equality.
174      */

175     public void testSerialization() {
176
177         CategoryTick t1 = new CategoryTick(
178             "C1", new TextBlock(), TextBlockAnchor.CENTER,
179             TextAnchor.CENTER, 1.5f
180         );
181         CategoryTick t2 = null;
182
183         try {
184             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
185             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
186             out.writeObject(t1);
187             out.close();
188
189             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
190                 new ByteArrayInputStream JavaDoc(buffer.toByteArray())
191             );
192             t2 = (CategoryTick) in.readObject();
193             in.close();
194         }
195         catch (Exception JavaDoc e) {
196             System.out.println(e.toString());
197         }
198         assertEquals(t1, t2);
199
200     }
201
202 }
203
Popular Tags