KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > chart > labels > junit > StandardXYZToolTipGeneratorTests


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  * StandardXYZToolTipGeneratorTests.java
28  * -------------------------------------
29  * (C) Copyright 2003, 2004, by Object Refinery Limited and Contributors.
30  *
31  * Original Author: David Gilbert (for Object Refinery Limited);
32  * Contributor(s): -;
33  *
34  * $Id: StandardXYZToolTipGeneratorTests.java,v 1.2 2005/02/09 14:23:48 mungady Exp $
35  *
36  * Changes
37  * -------
38  * 23-Mar-2003 : Version 1 (DG);
39  *
40  */

41
42 package org.jfree.chart.labels.junit;
43
44 import java.io.ByteArrayInputStream JavaDoc;
45 import java.io.ByteArrayOutputStream JavaDoc;
46 import java.io.ObjectInput JavaDoc;
47 import java.io.ObjectInputStream JavaDoc;
48 import java.io.ObjectOutput JavaDoc;
49 import java.io.ObjectOutputStream JavaDoc;
50 import java.text.DateFormat JavaDoc;
51 import java.text.DecimalFormat JavaDoc;
52 import java.text.NumberFormat JavaDoc;
53 import java.text.SimpleDateFormat JavaDoc;
54
55 import junit.framework.Test;
56 import junit.framework.TestCase;
57 import junit.framework.TestSuite;
58
59 import org.jfree.chart.labels.StandardXYZToolTipGenerator;
60
61 /**
62  * Tests for the {@link StandardXYZToolTipGenerator} class.
63  */

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

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

87     public void testEquals() {
88
89         // some setup...
90
String JavaDoc f1 = "{1}";
91         String JavaDoc f2 = "{2}";
92         NumberFormat JavaDoc xnf1 = new DecimalFormat JavaDoc("0.00");
93         NumberFormat JavaDoc xnf2 = new DecimalFormat JavaDoc("0.000");
94         NumberFormat JavaDoc ynf1 = new DecimalFormat JavaDoc("0.00");
95         NumberFormat JavaDoc ynf2 = new DecimalFormat JavaDoc("0.000");
96         NumberFormat JavaDoc znf1 = new DecimalFormat JavaDoc("0.00");
97         NumberFormat JavaDoc znf2 = new DecimalFormat JavaDoc("0.000");
98         
99         DateFormat JavaDoc xdf1 = new SimpleDateFormat JavaDoc("d-MMM");
100         DateFormat JavaDoc xdf2 = new SimpleDateFormat JavaDoc("d-MMM-yyyy");
101         DateFormat JavaDoc ydf1 = new SimpleDateFormat JavaDoc("d-MMM");
102         DateFormat JavaDoc ydf2 = new SimpleDateFormat JavaDoc("d-MMM-yyyy");
103         DateFormat JavaDoc zdf1 = new SimpleDateFormat JavaDoc("d-MMM");
104         DateFormat JavaDoc zdf2 = new SimpleDateFormat JavaDoc("d-MMM-yyyy");
105         
106         StandardXYZToolTipGenerator g1 = null;
107         StandardXYZToolTipGenerator g2 = null;
108         
109         g1 = new StandardXYZToolTipGenerator(f1, xnf1, ynf1, znf1);
110         g2 = new StandardXYZToolTipGenerator(f1, xnf1, ynf1, znf1);
111         assertTrue(g1.equals(g2));
112
113         // format string...
114
g1 = new StandardXYZToolTipGenerator(f2, xnf1, ynf1, znf1);
115         assertFalse(g1.equals(g2));
116         g2 = new StandardXYZToolTipGenerator(f2, xnf1, ynf1, znf1);
117         assertTrue(g1.equals(g2));
118
119         // x number format
120
g1 = new StandardXYZToolTipGenerator(f2, xnf2, ynf1, znf1);
121         assertFalse(g1.equals(g2));
122         g2 = new StandardXYZToolTipGenerator(f2, xnf2, ynf1, znf1);
123         assertTrue(g1.equals(g2));
124
125         // y number format
126
g1 = new StandardXYZToolTipGenerator(f2, xnf2, ynf2, znf1);
127         assertFalse(g1.equals(g2));
128         g2 = new StandardXYZToolTipGenerator(f2, xnf2, ynf2, znf1);
129         assertTrue(g1.equals(g2));
130
131         // z number format
132
g1 = new StandardXYZToolTipGenerator(f2, xnf2, ynf2, znf2);
133         assertFalse(g1.equals(g2));
134         g2 = new StandardXYZToolTipGenerator(f2, xnf2, ynf2, znf2);
135         assertTrue(g1.equals(g2));
136
137         g1 = new StandardXYZToolTipGenerator(f2, xdf1, ydf1, zdf1);
138         g2 = new StandardXYZToolTipGenerator(f2, xdf1, ydf1, zdf1);
139         assertTrue(g1.equals(g2));
140         
141         // x date format
142
g1 = new StandardXYZToolTipGenerator(f2, xdf2, ydf1, zdf1);
143         assertFalse(g1.equals(g2));
144         g2 = new StandardXYZToolTipGenerator(f2, xdf2, ydf1, zdf1);
145         assertTrue(g1.equals(g2));
146
147         // y date format
148
g1 = new StandardXYZToolTipGenerator(f2, xdf2, ydf2, zdf1);
149         assertFalse(g1.equals(g2));
150         g2 = new StandardXYZToolTipGenerator(f2, xdf2, ydf2, zdf1);
151         assertTrue(g1.equals(g2));
152
153         // z date format
154
g1 = new StandardXYZToolTipGenerator(f2, xdf2, ydf2, zdf2);
155         assertFalse(g1.equals(g2));
156         g2 = new StandardXYZToolTipGenerator(f2, xdf2, ydf2, zdf2);
157         assertTrue(g1.equals(g2));
158
159     }
160     
161     /**
162      * Confirm that cloning works.
163      */

164     public void testCloning() {
165         StandardXYZToolTipGenerator g1 = new StandardXYZToolTipGenerator();
166         StandardXYZToolTipGenerator g2 = null;
167         try {
168             g2 = (StandardXYZToolTipGenerator) g1.clone();
169         }
170         catch (CloneNotSupportedException JavaDoc e) {
171             System.err.println("Failed to clone.");
172         }
173         assertTrue(g1 != g2);
174         assertTrue(g1.getClass() == g2.getClass());
175         assertTrue(g1.equals(g2));
176     }
177
178     /**
179      * Serialize an instance, restore it, and check for equality.
180      */

181     public void testSerialization() {
182
183         StandardXYZToolTipGenerator g1 = new StandardXYZToolTipGenerator();
184         StandardXYZToolTipGenerator g2 = null;
185
186         try {
187             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
188             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
189             out.writeObject(g1);
190             out.close();
191
192             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
193                 new ByteArrayInputStream JavaDoc(buffer.toByteArray())
194             );
195             g2 = (StandardXYZToolTipGenerator) in.readObject();
196             in.close();
197         }
198         catch (Exception JavaDoc e) {
199             System.out.println(e.toString());
200         }
201         assertEquals(g1, g2);
202
203     }
204
205 }
206
Popular Tags