KickJava   Java API By Example, From Geeks To Geeks.

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


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  * CategoryLineAnnotationTests.java
29  * --------------------------------
30  * (C) Copyright 2005, by Object Refinery Limited and Contributors.
31  *
32  * Original Author: David Gilbert (for Object Refinery Limited);
33  * Contributor(s): -;
34  *
35  * $Id: CategoryLineAnnotationTests.java,v 1.1.2.1 2006/10/03 15:41:40 mungady Exp $
36  *
37  * Changes
38  * -------
39  * 29-Jul-2005 : Version 1 (DG);
40  *
41  */

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

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

79     public CategoryLineAnnotationTests(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         BasicStroke JavaDoc s1 = new BasicStroke JavaDoc(1.0f);
89         BasicStroke JavaDoc s2 = new BasicStroke JavaDoc(2.0f);
90         CategoryLineAnnotation a1 = new CategoryLineAnnotation(
91             "Category 1", 1.0, "Category 2", 2.0, Color.red, s1);
92         CategoryLineAnnotation a2 = new CategoryLineAnnotation(
93             "Category 1", 1.0, "Category 2", 2.0, Color.red, s1);
94         assertTrue(a1.equals(a2));
95         assertTrue(a2.equals(a1));
96         
97         // category 1
98
a1.setCategory1("Category A");
99         assertFalse(a1.equals(a2));
100         a2.setCategory1("Category A");
101         assertTrue(a1.equals(a2));
102
103         // value 1
104
a1.setValue1(0.15);
105         assertFalse(a1.equals(a2));
106         a2.setValue1(0.15);
107         assertTrue(a1.equals(a2));
108       
109         // category 2
110
a1.setCategory2("Category B");
111         assertFalse(a1.equals(a2));
112         a2.setCategory2("Category B");
113         assertTrue(a1.equals(a2));
114
115         // value 2
116
a1.setValue2(0.25);
117         assertFalse(a1.equals(a2));
118         a2.setValue2(0.25);
119         assertTrue(a1.equals(a2));
120         
121         // paint
122
a1.setPaint(Color.yellow);
123         assertFalse(a1.equals(a2));
124         a2.setPaint(Color.yellow);
125         assertTrue(a1.equals(a2));
126         
127         // stroke
128
a1.setStroke(s2);
129         assertFalse(a1.equals(a2));
130         a2.setStroke(s2);
131         assertTrue(a1.equals(a2));
132     }
133
134     /**
135      * Two objects that are equal are required to return the same hashCode.
136      */

137     public void testHashcode() {
138         CategoryLineAnnotation a1 = new CategoryLineAnnotation(
139             "Category 1", 1.0, "Category 2", 2.0, Color.red,
140             new BasicStroke JavaDoc(1.0f));
141         CategoryLineAnnotation a2 = new CategoryLineAnnotation(
142             "Category 1", 1.0, "Category 2", 2.0, Color.red,
143             new BasicStroke JavaDoc(1.0f));
144         assertTrue(a1.equals(a2));
145         int h1 = a1.hashCode();
146         int h2 = a2.hashCode();
147         assertEquals(h1, h2);
148     }
149
150     /**
151      * Confirm that cloning works.
152      */

153     public void testCloning() {
154         CategoryLineAnnotation a1 = new CategoryLineAnnotation(
155             "Category 1", 1.0, "Category 2", 2.0, Color.red,
156             new BasicStroke JavaDoc(1.0f));
157         CategoryLineAnnotation a2 = null;
158         try {
159             a2 = (CategoryLineAnnotation) a1.clone();
160         }
161         catch (CloneNotSupportedException JavaDoc e) {
162             e.printStackTrace();
163         }
164         assertTrue(a1 != a2);
165         assertTrue(a1.getClass() == a2.getClass());
166         assertTrue(a1.equals(a2));
167     }
168
169     /**
170      * Serialize an instance, restore it, and check for equality.
171      */

172     public void testSerialization() {
173
174         CategoryLineAnnotation a1 = new CategoryLineAnnotation(
175             "Category 1", 1.0, "Category 2", 2.0, Color.red,
176             new BasicStroke JavaDoc(1.0f));
177         CategoryLineAnnotation a2 = null;
178
179         try {
180             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
181             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
182             out.writeObject(a1);
183             out.close();
184
185             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
186                 new ByteArrayInputStream JavaDoc(buffer.toByteArray())
187             );
188             a2 = (CategoryLineAnnotation) in.readObject();
189             in.close();
190         }
191         catch (Exception JavaDoc e) {
192             e.printStackTrace();
193         }
194         assertEquals(a1, a2);
195
196     }
197
198 }
199
Popular Tags