KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > data > category > junit > CategoryToPieDatasetTests


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  * CategoryToPieDatasetTests.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: CategoryToPieDatasetTests.java,v 1.1.2.1 2006/10/03 15:41:48 mungady Exp $
36  *
37  * Changes
38  * -------
39  * 26-Jul-2006 : Version 1 (DG);
40  * 01-Aug-2006 : Added testGetIndex() method (DG);
41  *
42  */

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

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

81     public CategoryToPieDatasetTests(String JavaDoc name) {
82         super(name);
83     }
84     
85     /**
86      * Some tests for the constructor.
87      */

88     public void testConstructor() {
89         // try a null source
90
CategoryToPieDataset p1 = new CategoryToPieDataset(null,
91                 TableOrder.BY_COLUMN, 0);
92         assertNull(p1.getUnderlyingDataset());
93         assertEquals(p1.getItemCount(), 0);
94         assertTrue(p1.getKeys().isEmpty());
95         assertNull(p1.getValue("R1"));
96     }
97     
98     /**
99      * Some checks for the getValue() method.
100      */

101     public void testGetValue() {
102         DefaultCategoryDataset underlying = new DefaultCategoryDataset();
103         underlying.addValue(1.1, "R1", "C1");
104         underlying.addValue(2.2, "R1", "C2");
105         CategoryToPieDataset d1 = new CategoryToPieDataset(underlying,
106                 TableOrder.BY_ROW, 0);
107         assertEquals(d1.getValue("C1"), new Double JavaDoc(1.1));
108         assertEquals(d1.getValue("C2"), new Double JavaDoc(2.2));
109
110         // check negative index throws exception
111
try {
112             /* Number n = */ d1.getValue(-1);
113             fail("Expected IndexOutOfBoundsException.");
114         }
115         catch (IndexOutOfBoundsException JavaDoc e) {
116             // this is expected
117
}
118         
119         // check index == getItemCount() throws exception
120
try {
121             /* Number n = */ d1.getValue(d1.getItemCount());
122             fail("Expected IndexOutOfBoundsException.");
123         }
124         catch (IndexOutOfBoundsException JavaDoc e) {
125             // this is expected
126
}
127
128         // test null source
129
CategoryToPieDataset p1 = new CategoryToPieDataset(null,
130                 TableOrder.BY_COLUMN, 0);
131         try {
132             /* Number n = */ p1.getValue(0);
133             fail("Expected IndexOutOfBoundsException.");
134         }
135         catch (IndexOutOfBoundsException JavaDoc e) {
136             // this is expected
137
}
138     }
139     
140     /**
141      * Some checks for the getKey(int) method.
142      */

143     public void testGetKey() {
144         DefaultCategoryDataset underlying = new DefaultCategoryDataset();
145         underlying.addValue(1.1, "R1", "C1");
146         underlying.addValue(2.2, "R1", "C2");
147         CategoryToPieDataset d1 = new CategoryToPieDataset(underlying,
148                 TableOrder.BY_ROW, 0);
149         assertEquals(d1.getKey(0), "C1");
150         assertEquals(d1.getKey(1), "C2");
151
152         // check negative index throws exception
153
try {
154             /* Number n = */ d1.getKey(-1);
155             fail("Expected IndexOutOfBoundsException.");
156         }
157         catch (IndexOutOfBoundsException JavaDoc e) {
158             // this is expected
159
}
160         
161         // check index == getItemCount() throws exception
162
try {
163             /* Number n = */ d1.getKey(d1.getItemCount());
164             fail("Expected IndexOutOfBoundsException.");
165         }
166         catch (IndexOutOfBoundsException JavaDoc e) {
167             // this is expected
168
}
169
170         // test null source
171
CategoryToPieDataset p1 = new CategoryToPieDataset(null,
172                 TableOrder.BY_COLUMN, 0);
173         try {
174             /* Number n = */ p1.getKey(0);
175             fail("Expected IndexOutOfBoundsException.");
176         }
177         catch (IndexOutOfBoundsException JavaDoc e) {
178             // this is expected
179
}
180     }
181
182     /**
183      * Some checks for the getIndex() method.
184      */

185     public void testGetIndex() {
186         DefaultCategoryDataset underlying = new DefaultCategoryDataset();
187         underlying.addValue(1.1, "R1", "C1");
188         underlying.addValue(2.2, "R1", "C2");
189         CategoryToPieDataset d1 = new CategoryToPieDataset(underlying,
190                 TableOrder.BY_ROW, 0);
191         assertEquals(0, d1.getIndex("C1"));
192         assertEquals(1, d1.getIndex("C2"));
193         assertEquals(-1, d1.getIndex("XX"));
194         
195         // try null
196
boolean pass = false;
197         try {
198             d1.getIndex(null);
199         }
200         catch (IllegalArgumentException JavaDoc e) {
201             pass = true;
202         }
203         assertTrue(pass);
204     }
205     
206     /**
207      * For datasets, the equals() method just checks keys and values.
208      */

209     public void testEquals() {
210         DefaultCategoryDataset underlying = new DefaultCategoryDataset();
211         underlying.addValue(1.1, "R1", "C1");
212         underlying.addValue(2.2, "R1", "C2");
213         CategoryToPieDataset d1 = new CategoryToPieDataset(underlying,
214                 TableOrder.BY_COLUMN, 1);
215         DefaultPieDataset d2 = new DefaultPieDataset();
216         d2.setValue("R1", 2.2);
217         assertTrue(d1.equals(d2));
218     }
219
220     /**
221      * Serialize an instance, restore it, and check for equality.
222      */

223     public void testSerialization() {
224         DefaultCategoryDataset underlying = new DefaultCategoryDataset();
225         underlying.addValue(1.1, "R1", "C1");
226         underlying.addValue(2.2, "R1", "C2");
227         CategoryToPieDataset d1 = new CategoryToPieDataset(underlying,
228                 TableOrder.BY_COLUMN, 1);
229         CategoryToPieDataset d2 = null;
230
231         try {
232             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
233             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
234             out.writeObject(d1);
235             out.close();
236
237             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
238                 new ByteArrayInputStream JavaDoc(buffer.toByteArray())
239             );
240             d2 = (CategoryToPieDataset) in.readObject();
241             in.close();
242         }
243         catch (Exception JavaDoc e) {
244             e.printStackTrace();
245         }
246         assertEquals(d1, d2);
247         
248         // regular equality for the datasets doesn't check the fields, just
249
// the data values...so let's check some more things...
250
assertEquals(d1.getUnderlyingDataset(), d2.getUnderlyingDataset());
251         assertEquals(d1.getExtractType(), d2.getExtractType());
252         assertEquals(d1.getExtractIndex(), d2.getExtractIndex());
253     }
254     
255 }
256
Popular Tags