KickJava   Java API By Example, From Geeks To Geeks.

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


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 License
20  * along with this library; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22  *
23  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
24  * in the United States and other countries.]
25  *
26  * ----------------------
27  * CategoryAxisTests.java
28  * ----------------------
29  * (C) Copyright 2003-2005, by Object Refinery Limited and Contributors.
30  *
31  * Original Author: David Gilbert (for Object Refinery Limited);
32  * Contributor(s): -;
33  *
34  * $Id: CategoryAxisTests.java,v 1.4 2005/03/16 12:10:45 mungady Exp $
35  *
36  * Changes
37  * -------
38  * 18-Mar-2003 : Version 1 (DG);
39  * 13-Aug-2003 : Added clone() test (DG);
40  * 07-Jan-2005 : 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.CategoryAxis;
58 import org.jfree.chart.axis.CategoryLabelPositions;
59
60 /**
61  * Tests for the {@link CategoryAxis} class.
62  */

63 public class CategoryAxisTests 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(CategoryAxisTests.class);
72     }
73
74     /**
75      * Constructs a new set of tests.
76      *
77      * @param name the name of the tests.
78      */

79     public CategoryAxisTests(String JavaDoc name) {
80         super(name);
81     }
82
83     /**
84      * Confirm that the equals method can distinguish all the required fields.
85      */

86     public void testEquals() {
87         
88         CategoryAxis a1 = new CategoryAxis("Test");
89         CategoryAxis a2 = new CategoryAxis("Test");
90         assertTrue(a1.equals(a2));
91         
92         // lowerMargin
93
a1.setLowerMargin(0.15);
94         assertFalse(a1.equals(a2));
95         a2.setLowerMargin(0.15);
96         assertTrue(a1.equals(a2));
97
98         // upperMargin
99
a1.setUpperMargin(0.15);
100         assertFalse(a1.equals(a2));
101         a2.setUpperMargin(0.15);
102         assertTrue(a1.equals(a2));
103       
104         // categoryMargin
105
a1.setCategoryMargin(0.15);
106         assertFalse(a1.equals(a2));
107         a2.setCategoryMargin(0.15);
108         assertTrue(a1.equals(a2));
109         
110         // maxCategoryLabelWidthRatio
111
a1.setMaximumCategoryLabelWidthRatio(0.98f);
112         assertFalse(a1.equals(a2));
113         a2.setMaximumCategoryLabelWidthRatio(0.98f);
114         assertTrue(a1.equals(a2));
115         
116         // categoryLabelPositionOffset
117
a1.setCategoryLabelPositionOffset(11);
118         assertFalse(a1.equals(a2));
119         a2.setCategoryLabelPositionOffset(11);
120         assertTrue(a1.equals(a2));
121         
122         // categoryLabelPositions
123
a1.setCategoryLabelPositions(CategoryLabelPositions.DOWN_45);
124         assertFalse(a1.equals(a2));
125         a2.setCategoryLabelPositions(CategoryLabelPositions.DOWN_45);
126         assertTrue(a1.equals(a2));
127         
128         // categoryLabelToolTips
129
a1.addCategoryLabelToolTip("Test", "Check");
130         assertFalse(a1.equals(a2));
131         a2.addCategoryLabelToolTip("Test", "Check");
132         assertTrue(a1.equals(a2));
133         
134     }
135
136     /**
137      * Two objects that are equal are required to return the same hashCode.
138      */

139     public void testHashCode() {
140         CategoryAxis a1 = new CategoryAxis("Test");
141         CategoryAxis a2 = new CategoryAxis("Test");
142         assertTrue(a1.equals(a2));
143         int h1 = a1.hashCode();
144         int h2 = a2.hashCode();
145         assertEquals(h1, h2);
146     }
147
148     /**
149      * Confirm that cloning works.
150      */

151     public void testCloning() {
152         CategoryAxis a1 = new CategoryAxis("Test");
153         CategoryAxis a2 = null;
154         try {
155             a2 = (CategoryAxis) a1.clone();
156         }
157         catch (CloneNotSupportedException JavaDoc e) {
158             System.err.println("Failed to clone.");
159         }
160         assertTrue(a1 != a2);
161         assertTrue(a1.getClass() == a2.getClass());
162         assertTrue(a1.equals(a2));
163     }
164
165     /**
166      * Serialize an instance, restore it, and check for equality.
167      */

168     public void testSerialization() {
169
170         CategoryAxis a1 = new CategoryAxis("Test Axis");
171         CategoryAxis a2 = null;
172
173         try {
174             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
175             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
176             out.writeObject(a1);
177             out.close();
178
179             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
180                 new ByteArrayInputStream JavaDoc(buffer.toByteArray())
181             );
182             a2 = (CategoryAxis) in.readObject();
183             in.close();
184         }
185         catch (Exception JavaDoc e) {
186             System.out.println(e.toString());
187         }
188         assertEquals(a1, a2);
189
190     }
191
192 }
193
Popular Tags