KickJava   Java API By Example, From Geeks To Geeks.

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


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  * SubCategoryAxisTests.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: SubCategoryAxisTests.java,v 1.1.2.1 2006/10/03 15:41:21 mungady Exp $
36  *
37  * Changes
38  * -------
39  * 12-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.awt.Color JavaDoc;
47 import java.awt.Font JavaDoc;
48 import java.io.ByteArrayInputStream JavaDoc;
49 import java.io.ByteArrayOutputStream JavaDoc;
50 import java.io.ObjectInput JavaDoc;
51 import java.io.ObjectInputStream JavaDoc;
52 import java.io.ObjectOutput JavaDoc;
53 import java.io.ObjectOutputStream JavaDoc;
54
55 import junit.framework.Test;
56 import junit.framework.TestCase;
57 import junit.framework.TestSuite;
58
59 import org.jfree.chart.axis.SubCategoryAxis;
60
61 /**
62  * Tests for the {@link SubCategoryAxis} class.
63  */

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

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

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

87     public void testEquals() {
88         
89         SubCategoryAxis a1 = new SubCategoryAxis("Test");
90         SubCategoryAxis a2 = new SubCategoryAxis("Test");
91         assertTrue(a1.equals(a2));
92         assertTrue(a2.equals(a1));
93         
94         // subcategories
95
a1.addSubCategory("Sub 1");
96         assertFalse(a1.equals(a2));
97         a2.addSubCategory("Sub 1");
98         assertTrue(a1.equals(a2));
99
100         // subLabelFont
101
a1.setSubLabelFont(new Font JavaDoc("Serif", Font.BOLD, 15));
102         assertFalse(a1.equals(a2));
103         a2.setSubLabelFont(new Font JavaDoc("Serif", Font.BOLD, 15));
104         assertTrue(a1.equals(a2));
105       
106         // subLabelPaint
107
a1.setSubLabelPaint(Color.red);
108         assertFalse(a1.equals(a2));
109         a2.setSubLabelPaint(Color.red);
110         assertTrue(a1.equals(a2));
111                 
112     }
113
114     /**
115      * Two objects that are equal are required to return the same hashCode.
116      */

117     public void testHashCode() {
118         SubCategoryAxis a1 = new SubCategoryAxis("Test");
119         SubCategoryAxis a2 = new SubCategoryAxis("Test");
120         assertTrue(a1.equals(a2));
121         int h1 = a1.hashCode();
122         int h2 = a2.hashCode();
123         assertEquals(h1, h2);
124     }
125     
126     /**
127      * Confirm that cloning works.
128      */

129     public void testCloning() {
130         SubCategoryAxis a1 = new SubCategoryAxis("Test");
131         SubCategoryAxis a2 = null;
132         try {
133             a2 = (SubCategoryAxis) a1.clone();
134         }
135         catch (CloneNotSupportedException JavaDoc e) {
136             System.err.println("Failed to clone.");
137         }
138         assertTrue(a1 != a2);
139         assertTrue(a1.getClass() == a2.getClass());
140         assertTrue(a1.equals(a2));
141     }
142
143     /**
144      * Serialize an instance, restore it, and check for equality.
145      */

146     public void testSerialization() {
147
148         SubCategoryAxis a1 = new SubCategoryAxis("Test Axis");
149         SubCategoryAxis a2 = null;
150
151         try {
152             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
153             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
154             out.writeObject(a1);
155             out.close();
156
157             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
158                 new ByteArrayInputStream JavaDoc(buffer.toByteArray())
159             );
160             a2 = (SubCategoryAxis) in.readObject();
161             in.close();
162         }
163         catch (Exception JavaDoc e) {
164             System.out.println(e.toString());
165         }
166         assertEquals(a1, a2);
167
168     }
169
170 }
171
Popular Tags