KickJava   Java API By Example, From Geeks To Geeks.

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


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  * CategoryLabelPositionTests.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: CategoryLabelPositionTests.java,v 1.1.2.1 2006/10/03 15:41:22 mungady Exp $
36  *
37  * Changes
38  * -------
39  * 17-Feb-2004 : Version 1 (DG);
40  * 07-Jan-2005 : Improved testEquals() code and added hashCode() test (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.CategoryLabelPosition;
58 import org.jfree.chart.axis.CategoryLabelWidthType;
59 import org.jfree.text.TextBlockAnchor;
60 import org.jfree.ui.RectangleAnchor;
61 import org.jfree.ui.TextAnchor;
62
63 /**
64  * Tests for the {@link CategoryLabelPosition} class.
65  */

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

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

89     public void testEquals() {
90         CategoryLabelPosition p1 = new CategoryLabelPosition(
91             RectangleAnchor.BOTTOM_LEFT, TextBlockAnchor.CENTER_RIGHT,
92             TextAnchor.BASELINE_LEFT, Math.PI / 4.0,
93             CategoryLabelWidthType.RANGE, 0.44f
94         );
95         CategoryLabelPosition p2 = new CategoryLabelPosition(
96             RectangleAnchor.BOTTOM_LEFT, TextBlockAnchor.CENTER_RIGHT,
97             TextAnchor.BASELINE_LEFT, Math.PI / 4.0,
98             CategoryLabelWidthType.RANGE, 0.44f
99         );
100         assertTrue(p1.equals(p2));
101         assertTrue(p2.equals(p1));
102         
103         p1 = new CategoryLabelPosition(
104             RectangleAnchor.TOP, TextBlockAnchor.CENTER_RIGHT,
105             TextAnchor.BASELINE_LEFT, Math.PI / 4.0,
106             CategoryLabelWidthType.RANGE, 0.44f
107         );
108         assertFalse(p1.equals(p2));
109         p2 = new CategoryLabelPosition(
110             RectangleAnchor.TOP, TextBlockAnchor.CENTER_RIGHT,
111             TextAnchor.BASELINE_LEFT, Math.PI / 4.0,
112             CategoryLabelWidthType.RANGE, 0.44f
113         );
114         assertTrue(p1.equals(p2));
115
116         p1 = new CategoryLabelPosition(RectangleAnchor.TOP,
117             TextBlockAnchor.CENTER, TextAnchor.BASELINE_LEFT, Math.PI / 4.0,
118             CategoryLabelWidthType.RANGE, 0.44f
119         );
120         assertFalse(p1.equals(p2));
121         p2 = new CategoryLabelPosition(RectangleAnchor.TOP,
122             TextBlockAnchor.CENTER, TextAnchor.BASELINE_LEFT, Math.PI / 4.0,
123             CategoryLabelWidthType.RANGE, 0.44f
124         );
125         assertTrue(p1.equals(p2));
126     
127         p1 = new CategoryLabelPosition(RectangleAnchor.TOP,
128             TextBlockAnchor.CENTER, TextAnchor.CENTER, Math.PI / 4.0,
129             CategoryLabelWidthType.RANGE, 0.44f
130         );
131         assertFalse(p1.equals(p2));
132         p2 = new CategoryLabelPosition(RectangleAnchor.TOP,
133             TextBlockAnchor.CENTER, TextAnchor.CENTER, Math.PI / 4.0,
134             CategoryLabelWidthType.RANGE, 0.44f
135         );
136         assertTrue(p1.equals(p2));
137     
138         p1 = new CategoryLabelPosition(
139             RectangleAnchor.TOP, TextBlockAnchor.CENTER, TextAnchor.CENTER,
140             Math.PI / 6.0, CategoryLabelWidthType.RANGE, 0.44f
141         );
142         assertFalse(p1.equals(p2));
143         p2 = new CategoryLabelPosition(
144             RectangleAnchor.TOP, TextBlockAnchor.CENTER, TextAnchor.CENTER,
145             Math.PI / 6.0, CategoryLabelWidthType.RANGE, 0.44f
146         );
147         assertTrue(p1.equals(p2));
148     
149         p1 = new CategoryLabelPosition(
150             RectangleAnchor.TOP, TextBlockAnchor.CENTER, TextAnchor.CENTER,
151             Math.PI / 6.0, CategoryLabelWidthType.CATEGORY, 0.44f
152         );
153         assertFalse(p1.equals(p2));
154         p2 = new CategoryLabelPosition(
155             RectangleAnchor.TOP, TextBlockAnchor.CENTER, TextAnchor.CENTER,
156             Math.PI / 6.0, CategoryLabelWidthType.CATEGORY, 0.44f
157         );
158         assertTrue(p1.equals(p2));
159     
160         p1 = new CategoryLabelPosition(
161             RectangleAnchor.TOP, TextBlockAnchor.CENTER, TextAnchor.CENTER,
162             Math.PI / 6.0, CategoryLabelWidthType.CATEGORY, 0.55f
163         );
164         assertFalse(p1.equals(p2));
165         p2 = new CategoryLabelPosition(
166             RectangleAnchor.TOP, TextBlockAnchor.CENTER, TextAnchor.CENTER,
167             Math.PI / 6.0, CategoryLabelWidthType.CATEGORY, 0.55f
168         );
169         assertTrue(p1.equals(p2));
170     }
171     
172     /**
173      * Two objects that are equal are required to return the same hashCode.
174      */

175     public void testHashCode() {
176         CategoryLabelPosition a1 = new CategoryLabelPosition();
177         CategoryLabelPosition a2 = new CategoryLabelPosition();
178         assertTrue(a1.equals(a2));
179         int h1 = a1.hashCode();
180         int h2 = a2.hashCode();
181         assertEquals(h1, h2);
182     }
183
184     /**
185      * Serialize an instance, restore it, and check for equality.
186      */

187     public void testSerialization() {
188
189         CategoryLabelPosition p1 = new CategoryLabelPosition();
190         CategoryLabelPosition p2 = null;
191
192         try {
193             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
194             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
195             out.writeObject(p1);
196             out.close();
197
198             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
199                 new ByteArrayInputStream JavaDoc(buffer.toByteArray())
200             );
201             p2 = (CategoryLabelPosition) in.readObject();
202             in.close();
203         }
204         catch (Exception JavaDoc e) {
205             System.out.println(e.toString());
206         }
207         assertEquals(p1, p2);
208     }
209
210 }
211
Popular Tags