KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* ===========================================================
2  * JFreeChart : a free chart library for the Java(tm) platform
3  * ===========================================================
4  *
5  * (C) Copyright 2000-2006, 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  * BubbleXYItemLabelGeneratorTests.java
29  * ------------------------------------
30  * (C) Copyright 2006, by Object Refinery Limited and Contributors.
31  *
32  * Original Author: David Gilbert (for Object Refinery Limited);
33  * Contributor(s): -;
34  *
35  * $Id: BubbleXYItemLabelGeneratorTests.java,v 1.1.2.1 2006/10/03 15:41:36 mungady Exp $
36  *
37  * Changes
38  * -------
39  * 20-Jan-2006 : Version 1 (DG);
40  *
41  */

42
43 package org.jfree.chart.labels.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 import java.text.DateFormat JavaDoc;
52 import java.text.DecimalFormat JavaDoc;
53 import java.text.NumberFormat JavaDoc;
54 import java.text.SimpleDateFormat JavaDoc;
55
56 import junit.framework.Test;
57 import junit.framework.TestCase;
58 import junit.framework.TestSuite;
59
60 import org.jfree.chart.labels.BubbleXYItemLabelGenerator;
61 import org.jfree.data.xy.XYSeries;
62 import org.jfree.data.xy.XYSeriesCollection;
63
64 /**
65  * Tests for the {@link BubbleXYItemLabelGenerator} class.
66  */

67 public class BubbleXYItemLabelGeneratorTests extends TestCase {
68
69     /**
70      * Returns the tests as a test suite.
71      *
72      * @return The test suite.
73      */

74     public static Test suite() {
75         return new TestSuite(BubbleXYItemLabelGeneratorTests.class);
76     }
77
78     /**
79      * Constructs a new set of tests.
80      *
81      * @param name the name of the tests.
82      */

83     public BubbleXYItemLabelGeneratorTests(String JavaDoc name) {
84         super(name);
85     }
86
87     /**
88      * A series of tests for the equals() method.
89      */

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

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

178     public void testSerialization() {
179
180         BubbleXYItemLabelGenerator g1 = new BubbleXYItemLabelGenerator();
181         BubbleXYItemLabelGenerator g2 = null;
182
183         try {
184             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
185             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
186             out.writeObject(g1);
187             out.close();
188
189             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
190                     new ByteArrayInputStream JavaDoc(buffer.toByteArray()));
191             g2 = (BubbleXYItemLabelGenerator) in.readObject();
192             in.close();
193         }
194         catch (Exception JavaDoc e) {
195             System.out.println(e.toString());
196         }
197         assertEquals(g1, g2);
198
199     }
200     
201     /**
202      * Some checks for the testGenerateLabel() method.
203      */

204     public void testGenerateLabel() {
205         // check handling when the dataset is a regular XYDataset, not an
206
// XYZDataset...
207
XYSeries s1 = new XYSeries("S1");
208         s1.add(1.0, 2.0);
209         s1.add(2.2, 3.3);
210         XYSeriesCollection dataset = new XYSeriesCollection(s1);
211         BubbleXYItemLabelGenerator g = new BubbleXYItemLabelGenerator();
212         assertEquals("{3}", g.generateLabel(dataset, 0, 0));
213         assertEquals("{3}", g.generateLabel(dataset, 0, 1));
214     }
215
216 }
217
Popular Tags