KickJava   Java API By Example, From Geeks To Geeks.

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


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  * CategoryLabelPositionsTests.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: CategoryLabelPositionsTests.java,v 1.1.2.1 2006/10/03 15:41:23 mungady Exp $
36  *
37  * Changes
38  * -------
39  * 17-Feb-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.CategoryLabelPosition;
58 import org.jfree.chart.axis.CategoryLabelPositions;
59 import org.jfree.text.TextBlockAnchor;
60 import org.jfree.ui.RectangleAnchor;
61
62 /**
63  * Tests for the {@link CategoryLabelPositions} class.
64  */

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

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

81     public CategoryLabelPositionsTests(String JavaDoc name) {
82         super(name);
83     }
84     
85     private static final RectangleAnchor RA_TOP = RectangleAnchor.TOP;
86     private static final RectangleAnchor RA_BOTTOM = RectangleAnchor.BOTTOM;
87     
88     /**
89      * Check that the equals method distinguishes all fields.
90      */

91     public void testEquals() {
92         CategoryLabelPositions p1 = new CategoryLabelPositions(
93             new CategoryLabelPosition(RA_TOP, TextBlockAnchor.CENTER),
94             new CategoryLabelPosition(RA_TOP, TextBlockAnchor.CENTER),
95             new CategoryLabelPosition(RA_TOP, TextBlockAnchor.CENTER),
96             new CategoryLabelPosition(RA_TOP, TextBlockAnchor.CENTER)
97         );
98         CategoryLabelPositions p2 = new CategoryLabelPositions(
99             new CategoryLabelPosition(RA_TOP, TextBlockAnchor.CENTER),
100             new CategoryLabelPosition(RA_TOP, TextBlockAnchor.CENTER),
101             new CategoryLabelPosition(RA_TOP, TextBlockAnchor.CENTER),
102             new CategoryLabelPosition(RA_TOP, TextBlockAnchor.CENTER)
103         );
104         assertEquals(p1, p2);
105
106         p1 = new CategoryLabelPositions(
107             new CategoryLabelPosition(RA_BOTTOM, TextBlockAnchor.TOP_CENTER),
108             new CategoryLabelPosition(RA_TOP, TextBlockAnchor.CENTER),
109             new CategoryLabelPosition(RA_TOP, TextBlockAnchor.CENTER),
110             new CategoryLabelPosition(RA_TOP, TextBlockAnchor.CENTER)
111         );
112         assertTrue(!p1.equals(p2));
113         p2 = new CategoryLabelPositions(
114             new CategoryLabelPosition(RA_BOTTOM, TextBlockAnchor.TOP_CENTER),
115             new CategoryLabelPosition(RA_TOP, TextBlockAnchor.CENTER),
116             new CategoryLabelPosition(RA_TOP, TextBlockAnchor.CENTER),
117             new CategoryLabelPosition(RA_TOP, TextBlockAnchor.CENTER)
118         );
119         assertTrue(p1.equals(p2));
120
121         p1 = new CategoryLabelPositions(
122             new CategoryLabelPosition(RA_BOTTOM, TextBlockAnchor.TOP_CENTER),
123             new CategoryLabelPosition(RA_BOTTOM, TextBlockAnchor.TOP_CENTER),
124             new CategoryLabelPosition(RA_TOP, TextBlockAnchor.CENTER),
125             new CategoryLabelPosition(RA_TOP, TextBlockAnchor.CENTER)
126         );
127         assertTrue(!p1.equals(p2));
128         p2 = new CategoryLabelPositions(
129             new CategoryLabelPosition(RA_BOTTOM, TextBlockAnchor.TOP_CENTER),
130             new CategoryLabelPosition(RA_BOTTOM, TextBlockAnchor.TOP_CENTER),
131             new CategoryLabelPosition(RA_TOP, TextBlockAnchor.CENTER),
132             new CategoryLabelPosition(RA_TOP, TextBlockAnchor.CENTER)
133         );
134         assertTrue(p1.equals(p2));
135
136         p1 = new CategoryLabelPositions(
137             new CategoryLabelPosition(RA_BOTTOM, TextBlockAnchor.TOP_CENTER),
138             new CategoryLabelPosition(RA_BOTTOM, TextBlockAnchor.TOP_CENTER),
139             new CategoryLabelPosition(RA_BOTTOM, TextBlockAnchor.TOP_CENTER),
140             new CategoryLabelPosition(RA_TOP, TextBlockAnchor.CENTER)
141         );
142         assertTrue(!p1.equals(p2));
143         p2 = new CategoryLabelPositions(
144             new CategoryLabelPosition(RA_BOTTOM, TextBlockAnchor.TOP_CENTER),
145             new CategoryLabelPosition(RA_BOTTOM, TextBlockAnchor.TOP_CENTER),
146             new CategoryLabelPosition(RA_BOTTOM, TextBlockAnchor.TOP_CENTER),
147             new CategoryLabelPosition(RA_TOP, TextBlockAnchor.CENTER)
148         );
149         assertTrue(p1.equals(p2));
150
151         p1 = new CategoryLabelPositions(
152             new CategoryLabelPosition(RA_BOTTOM, TextBlockAnchor.TOP_CENTER),
153             new CategoryLabelPosition(RA_BOTTOM, TextBlockAnchor.TOP_CENTER),
154             new CategoryLabelPosition(RA_BOTTOM, TextBlockAnchor.TOP_CENTER),
155             new CategoryLabelPosition(RA_BOTTOM, TextBlockAnchor.TOP_CENTER)
156         );
157         assertTrue(!p1.equals(p2));
158         p2 = new CategoryLabelPositions(
159             new CategoryLabelPosition(RA_BOTTOM, TextBlockAnchor.TOP_CENTER),
160             new CategoryLabelPosition(RA_BOTTOM, TextBlockAnchor.TOP_CENTER),
161             new CategoryLabelPosition(RA_BOTTOM, TextBlockAnchor.TOP_CENTER),
162             new CategoryLabelPosition(RA_BOTTOM, TextBlockAnchor.TOP_CENTER)
163         );
164         assertTrue(p1.equals(p2));
165     }
166     
167     /**
168      * Two objects that are equal are required to return the same hashCode.
169      */

170     public void testHashCode() {
171         CategoryLabelPositions p1 = new CategoryLabelPositions(
172             new CategoryLabelPosition(RA_TOP, TextBlockAnchor.CENTER),
173             new CategoryLabelPosition(RA_TOP, TextBlockAnchor.CENTER),
174             new CategoryLabelPosition(RA_TOP, TextBlockAnchor.CENTER),
175             new CategoryLabelPosition(RA_TOP, TextBlockAnchor.CENTER)
176         );
177         CategoryLabelPositions p2 = new CategoryLabelPositions(
178             new CategoryLabelPosition(RA_TOP, TextBlockAnchor.CENTER),
179             new CategoryLabelPosition(RA_TOP, TextBlockAnchor.CENTER),
180             new CategoryLabelPosition(RA_TOP, TextBlockAnchor.CENTER),
181             new CategoryLabelPosition(RA_TOP, TextBlockAnchor.CENTER)
182         );
183         assertTrue(p1.equals(p2));
184         int h1 = p1.hashCode();
185         int h2 = p2.hashCode();
186         assertEquals(h1, h2);
187     }
188
189     /**
190      * Serialize an instance, restore it, and check for equality.
191      */

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