KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > data > general > junit > DefaultPieDatasetTests


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  * PieDatasetTests.java
29  * --------------------
30  * (C) Copyright 2003-2006, by Object Refinery Limited and Contributors.
31  *
32  * Original Author: David Gilbert (for Object Refinery Limited);
33  * Contributor(s): -;
34  *
35  * $Id: DefaultPieDatasetTests.java,v 1.1.2.1 2006/10/03 15:41:44 mungady Exp $
36  *
37  * Changes
38  * -------
39  * 18-Aug-2003 : Version 1 (DG);
40  * 31-Jul-2006 : Added test for new clear() method (DG);
41  * 01-Aug-2006 : Added testGetKey() and testGetIndex() methods (DG);
42  *
43  */

44
45 package org.jfree.data.general.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
54 import junit.framework.Test;
55 import junit.framework.TestCase;
56 import junit.framework.TestSuite;
57
58 import org.jfree.data.general.DatasetChangeEvent;
59 import org.jfree.data.general.DatasetChangeListener;
60 import org.jfree.data.general.DefaultPieDataset;
61
62 /**
63  * Tests for the {@link org.jfree.data.general.PieDataset} class.
64  */

65 public class DefaultPieDatasetTests extends TestCase
66     implements DatasetChangeListener {
67
68     private DatasetChangeEvent lastEvent;
69     
70     public void datasetChanged(DatasetChangeEvent event) {
71         this.lastEvent = event;
72     }
73
74     /**
75      * Returns the tests as a test suite.
76      *
77      * @return The test suite.
78      */

79     public static Test suite() {
80         return new TestSuite(DefaultPieDatasetTests.class);
81     }
82
83     /**
84      * Constructs a new set of tests.
85      *
86      * @param name the name of the tests.
87      */

88     public DefaultPieDatasetTests(String JavaDoc name) {
89         super(name);
90     }
91
92     /**
93      * Some tests for the clear() method.
94      */

95     public void testClear() {
96         DefaultPieDataset d = new DefaultPieDataset();
97         d.addChangeListener(this);
98         // no event is generated if the dataset is already empty
99
d.clear();
100         assertNull(this.lastEvent);
101         d.setValue("A", 1.0);
102         assertEquals(1, d.getItemCount());
103         this.lastEvent = null;
104         d.clear();
105         assertNotNull(this.lastEvent);
106         assertEquals(0, d.getItemCount());
107     }
108     
109     /**
110      * Some checks for the getKey(int) method.
111      */

112     public void testGetKey() {
113         DefaultPieDataset d = new DefaultPieDataset();
114         d.setValue("A", 1.0);
115         d.setValue("B", 2.0);
116         assertEquals("A", d.getKey(0));
117         assertEquals("B", d.getKey(1));
118         
119         boolean pass = false;
120         try {
121             d.getKey(-1);
122         }
123         catch (IndexOutOfBoundsException JavaDoc e) {
124             pass = true;
125         }
126         assertTrue(pass);
127         
128         pass = false;
129         try {
130             d.getKey(2);
131         }
132         catch (IndexOutOfBoundsException JavaDoc e) {
133             pass = true;
134         }
135         assertTrue(pass);
136     }
137     
138     /**
139      * Some checks for the getIndex() method.
140      */

141     public void testGetIndex() {
142         DefaultPieDataset d = new DefaultPieDataset();
143         d.setValue("A", 1.0);
144         d.setValue("B", 2.0);
145         assertEquals(0, d.getIndex("A"));
146         assertEquals(1, d.getIndex("B"));
147         assertEquals(-1, d.getIndex("XX"));
148         
149         boolean pass = false;
150         try {
151             d.getIndex(null);
152         }
153         catch (IllegalArgumentException JavaDoc e) {
154             pass = true;
155         }
156         assertTrue(pass);
157     }
158     
159     /**
160      * Confirm that cloning works.
161      */

162     public void testCloning() {
163         DefaultPieDataset d1 = new DefaultPieDataset();
164         d1.setValue("V1", new Integer JavaDoc(1));
165         d1.setValue("V2", null);
166         d1.setValue("V3", new Integer JavaDoc(3));
167         DefaultPieDataset d2 = null;
168         try {
169             d2 = (DefaultPieDataset) d1.clone();
170         }
171         catch (CloneNotSupportedException JavaDoc e) {
172             System.err.println("Failed to clone.");
173         }
174         assertTrue(d1 != d2);
175         assertTrue(d1.getClass() == d2.getClass());
176         assertTrue(d1.equals(d2));
177     }
178
179     /**
180      * Serialize an instance, restore it, and check for equality.
181      */

182     public void testSerialization() {
183
184         DefaultPieDataset d1 = new DefaultPieDataset();
185         d1.setValue("C1", new Double JavaDoc(234.2));
186         d1.setValue("C2", null);
187         d1.setValue("C3", new Double JavaDoc(345.9));
188         d1.setValue("C4", new Double JavaDoc(452.7));
189
190         DefaultPieDataset d2 = null;
191
192         try {
193             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
194             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
195             out.writeObject(d1);
196             out.close();
197
198             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
199                 new ByteArrayInputStream JavaDoc(buffer.toByteArray())
200             );
201             d2 = (DefaultPieDataset) in.readObject();
202             in.close();
203         }
204         catch (Exception JavaDoc e) {
205             System.out.println(e.toString());
206         }
207         assertEquals(d1, d2);
208
209     }
210
211 }
212
Popular Tags