KickJava   Java API By Example, From Geeks To Geeks.

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


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  * StandardPieSectionLabelGeneratorTests.java
29  * ------------------------------------------
30  * (C) Copyright 2003, 2004, by Object Refinery Limited and Contributors.
31  *
32  * Original Author: David Gilbert (for Object Refinery Limited);
33  * Contributor(s): -;
34  *
35  * $Id: StandardPieSectionLabelGeneratorTests.java,v 1.1.2.1 2006/10/03 15:41:36 mungady Exp $
36  *
37  * Changes
38  * -------
39  * 18-Mar-2003 : Version 1 (DG);
40  * 13-Aug-2003 : Added clone tests (DG);
41  * 04-Mar-2004 : Added test for equals() method (DG);
42  *
43  */

44
45 package org.jfree.chart.labels.junit;
46
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 import java.text.DecimalFormat JavaDoc;
54 import java.text.NumberFormat JavaDoc;
55
56 import junit.framework.Test;
57 import junit.framework.TestCase;
58 import junit.framework.TestSuite;
59
60 import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
61
62 /**
63  * Tests for the {@link StandardPieSectionLabelGenerator} class.
64  */

65 public class StandardPieSectionLabelGeneratorTests extends TestCase {
66
67     /**
68      * Returns the tests as a test suite.
69      *
70      * @return The test suite.
71      */

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

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

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

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

160     public void testSerialization() {
161
162         StandardPieSectionLabelGenerator g1
163             = new StandardPieSectionLabelGenerator();
164         StandardPieSectionLabelGenerator g2 = null;
165
166         try {
167             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
168             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
169             out.writeObject(g1);
170             out.close();
171
172             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
173                 new ByteArrayInputStream JavaDoc(buffer.toByteArray())
174             );
175             g2 = (StandardPieSectionLabelGenerator) in.readObject();
176             in.close();
177         }
178         catch (Exception JavaDoc e) {
179             e.printStackTrace();
180         }
181         assertEquals(g1, g2);
182
183     }
184     
185     /**
186      * Runs the test suite using JUnit's text-based runner.
187      *
188      * @param args ignored.
189      */

190     public static void main(String JavaDoc[] args) {
191         junit.textui.TestRunner.run(suite());
192     }
193
194 }
195
Popular Tags