KickJava   Java API By Example, From Geeks To Geeks.

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


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  * StandardPieItemLabelGeneratorTests.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: StandardPieItemLabelGeneratorTests.java,v 1.4 2005/02/09 14:23:48 mungady Exp $
35  *
36  * Changes
37  * -------
38  * 18-Mar-2003 : Version 1 (DG);
39  * 13-Aug-2003 : Added clone tests (DG);
40  * 04-Mar-2004 : Added test for equals() method (DG);
41  *
42  */

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

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

80     public StandardPieItemLabelGeneratorTests(String JavaDoc name) {
81         super(name);
82     }
83
84     /**
85      * Test that the equals() method distinguishes all fields.
86      */

87     public void testEquals() {
88         StandardPieItemLabelGenerator g1 = new StandardPieItemLabelGenerator();
89         StandardPieItemLabelGenerator g2 = new StandardPieItemLabelGenerator();
90         assertTrue(g1.equals(g2));
91         assertTrue(g2.equals(g1));
92         
93         g1 = new StandardPieItemLabelGenerator(
94             "{0}", new DecimalFormat JavaDoc("#,##0.00"),
95             NumberFormat.getPercentInstance()
96         );
97         assertFalse(g1.equals(g2));
98         g2 = new StandardPieItemLabelGenerator(
99             "{0}", new DecimalFormat JavaDoc("#,##0.00"),
100             NumberFormat.getPercentInstance()
101         );
102         assertTrue(g1.equals(g2));
103
104         g1 = new StandardPieItemLabelGenerator(
105             "{0} {1}", new DecimalFormat JavaDoc("#,##0.00"),
106             NumberFormat.getPercentInstance()
107         );
108         assertFalse(g1.equals(g2));
109         g2 = new StandardPieItemLabelGenerator(
110             "{0} {1}", new DecimalFormat JavaDoc("#,##0.00"),
111             NumberFormat.getPercentInstance()
112         );
113         assertTrue(g1.equals(g2));
114     
115         g1 = new StandardPieItemLabelGenerator(
116             "{0} {1}", new DecimalFormat JavaDoc("#,##0"),
117             NumberFormat.getPercentInstance()
118         );
119         assertFalse(g1.equals(g2));
120         g2 = new StandardPieItemLabelGenerator(
121             "{0} {1}", new DecimalFormat JavaDoc("#,##0"),
122             NumberFormat.getPercentInstance()
123         );
124         assertTrue(g1.equals(g2));
125
126         g1 = new StandardPieItemLabelGenerator(
127             "{0} {1}", new DecimalFormat JavaDoc("#,##0"), new DecimalFormat JavaDoc("0.000%")
128         );
129         assertFalse(g1.equals(g2));
130         g2 = new StandardPieItemLabelGenerator(
131             "{0} {1}", new DecimalFormat JavaDoc("#,##0"), new DecimalFormat JavaDoc("0.000%")
132         );
133         assertTrue(g1.equals(g2));
134     }
135     
136     /**
137      * Confirm that cloning works.
138      */

139     public void testCloning() {
140         StandardPieItemLabelGenerator g1 = new StandardPieItemLabelGenerator();
141         StandardPieItemLabelGenerator g2 = null;
142         try {
143             g2 = (StandardPieItemLabelGenerator) g1.clone();
144         }
145         catch (CloneNotSupportedException JavaDoc e) {
146             System.err.println("Failed to clone.");
147         }
148         assertTrue(g1 != g2);
149         assertTrue(g1.getClass() == g2.getClass());
150         assertTrue(g1.equals(g2));
151     }
152
153     /**
154      * Serialize an instance, restore it, and check for equality.
155      */

156     public void testSerialization() {
157
158         StandardPieItemLabelGenerator g1 = new StandardPieItemLabelGenerator();
159         StandardPieItemLabelGenerator g2 = null;
160
161         try {
162             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
163             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
164             out.writeObject(g1);
165             out.close();
166
167             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
168                 new ByteArrayInputStream JavaDoc(buffer.toByteArray())
169             );
170             g2 = (StandardPieItemLabelGenerator) in.readObject();
171             in.close();
172         }
173         catch (Exception JavaDoc e) {
174             e.printStackTrace();
175         }
176         assertEquals(g1, g2);
177
178     }
179
180 }
181
Popular Tags