KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > chart > annotations > junit > CategoryTextAnnotationTests


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  * CategoryTextAnnotationTests.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: CategoryTextAnnotationTests.java,v 1.3 2005/02/28 16:10:46 mungady Exp $
35  *
36  * Changes
37  * -------
38  * 19-Aug-2003 : Version 1 (DG);
39  * 07-Jan-2005 : Added hashCode() test (DG);
40  *
41  */

42
43 package org.jfree.chart.annotations.junit;
44
45 import java.io.ByteArrayInputStream JavaDoc;
46 import java.io.ByteArrayOutputStream JavaDoc;
47 import java.io.ObjectInput JavaDoc;
48 import java.io.ObjectInputStream JavaDoc;
49 import java.io.ObjectOutput JavaDoc;
50 import java.io.ObjectOutputStream JavaDoc;
51
52 import junit.framework.Test;
53 import junit.framework.TestCase;
54 import junit.framework.TestSuite;
55
56 import org.jfree.chart.annotations.CategoryTextAnnotation;
57 import org.jfree.chart.axis.CategoryAnchor;
58
59 /**
60  * Tests for the {@link CategoryTextAnnotation} class.
61  */

62 public class CategoryTextAnnotationTests extends TestCase {
63
64     /**
65      * Returns the tests as a test suite.
66      *
67      * @return The test suite.
68      */

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

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

85     public void testEquals() {
86         
87         CategoryTextAnnotation a1 = new CategoryTextAnnotation(
88             "Test", "Category", 1.0
89         );
90         CategoryTextAnnotation a2 = new CategoryTextAnnotation(
91             "Test", "Category", 1.0
92         );
93         assertTrue(a1.equals(a2));
94         
95         // category
96
a1.setCategory("Category 2");
97         assertFalse(a1.equals(a2));
98         a2.setCategory("Category 2");
99         assertTrue(a1.equals(a2));
100
101         // categoryAnchor
102
a1.setCategoryAnchor(CategoryAnchor.START);
103         assertFalse(a1.equals(a2));
104         a2.setCategoryAnchor(CategoryAnchor.START);
105         assertTrue(a1.equals(a2));
106
107         // value
108
a1.setValue(0.15);
109         assertFalse(a1.equals(a2));
110         a2.setValue(0.15);
111         assertTrue(a1.equals(a2));
112       
113     }
114
115     /**
116      * Two objects that are equal are required to return the same hashCode.
117      */

118     public void testHashcode() {
119         CategoryTextAnnotation a1 = new CategoryTextAnnotation(
120             "Test", "Category", 1.0
121         );
122         CategoryTextAnnotation a2 = new CategoryTextAnnotation(
123             "Test", "Category", 1.0
124         );
125         assertTrue(a1.equals(a2));
126         int h1 = a1.hashCode();
127         int h2 = a2.hashCode();
128         assertEquals(h1, h2);
129     }
130
131     /**
132      * Confirm that cloning works.
133      */

134     public void testCloning() {
135         CategoryTextAnnotation a1 = new CategoryTextAnnotation(
136             "Test", "Category", 1.0
137         );
138         CategoryTextAnnotation a2 = null;
139         try {
140             a2 = (CategoryTextAnnotation) a1.clone();
141         }
142         catch (CloneNotSupportedException JavaDoc e) {
143             System.err.println("Failed to clone.");
144         }
145         assertTrue(a1 != a2);
146         assertTrue(a1.getClass() == a2.getClass());
147         assertTrue(a1.equals(a2));
148     }
149
150     /**
151      * Serialize an instance, restore it, and check for equality.
152      */

153     public void testSerialization() {
154
155         CategoryTextAnnotation a1 = new CategoryTextAnnotation(
156             "Test", "Category", 1.0
157         );
158         CategoryTextAnnotation a2 = null;
159
160         try {
161             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
162             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
163             out.writeObject(a1);
164             out.close();
165
166             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
167                 new ByteArrayInputStream JavaDoc(buffer.toByteArray())
168             );
169             a2 = (CategoryTextAnnotation) in.readObject();
170             in.close();
171         }
172         catch (Exception JavaDoc e) {
173             System.out.println(e.toString());
174         }
175         assertEquals(a1, a2);
176
177     }
178
179 }
180
Popular Tags