KickJava   Java API By Example, From Geeks To Geeks.

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


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  * DefaultCategoryDatasetTests.java
29  * --------------------------------
30  * (C) Copyright 2004, 2005, by Object Refinery Limited and Contributors.
31  *
32  * Original Author: David Gilbert (for Object Refinery Limited);
33  * Contributor(s): -;
34  *
35  * $Id: DefaultCategoryDatasetTests.java,v 1.1.2.1 2006/10/03 15:41:48 mungady Exp $
36  *
37  * Changes
38  * -------
39  * 23-Mar-2004 : Version 1 (DG);
40  *
41  */

42
43 package org.jfree.data.category.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
52 import junit.framework.Test;
53 import junit.framework.TestCase;
54 import junit.framework.TestSuite;
55
56 import org.jfree.data.UnknownKeyException;
57 import org.jfree.data.category.DefaultCategoryDataset;
58
59 /**
60  * Tests for the {@link DefaultCategoryDataset} class.
61  */

62 public class DefaultCategoryDatasetTests extends TestCase {
63     
64     /**
65      * Returns the tests as a test suite.
66      *
67      * @return The test suite.
68      */

69     public static Test suite() {
70         return new TestSuite(DefaultCategoryDatasetTests.class);
71     }
72
73     /**
74      * Constructs a new set of tests.
75      *
76      * @param name the name of the tests.
77      */

78     public DefaultCategoryDatasetTests(String JavaDoc name) {
79         super(name);
80     }
81     
82     /**
83      * Some checks for the getValue() method.
84      */

85     public void testGetValue() {
86         DefaultCategoryDataset d = new DefaultCategoryDataset();
87         d.addValue(1.0, "R1", "C1");
88         assertEquals(new Double JavaDoc(1.0), d.getValue("R1", "C1"));
89         boolean pass = false;
90         try {
91             d.getValue("XX", "C1");
92         }
93         catch (UnknownKeyException e) {
94             pass = true;
95         }
96         assertTrue(pass);
97         
98         pass = false;
99         try {
100             d.getValue("R1", "XX");
101         }
102         catch (UnknownKeyException e) {
103             pass = true;
104         }
105         assertTrue(pass);
106     }
107     
108     /**
109      * Some checks for the incrementValue() method.
110      */

111     public void testIncrementValue() {
112         DefaultCategoryDataset d = new DefaultCategoryDataset();
113         d.addValue(1.0, "R1", "C1");
114         d.incrementValue(2.0, "R1", "C1");
115         assertEquals(new Double JavaDoc(3.0), d.getValue("R1", "C1"));
116         
117         // increment a null value
118
d.addValue(null, "R2", "C1");
119         d.incrementValue(2.0, "R2", "C1");
120         assertEquals(new Double JavaDoc(2.0), d.getValue("R2", "C1"));
121         
122         // increment an unknown row
123
boolean pass = false;
124         try {
125             d.incrementValue(1.0, "XX", "C1");
126         }
127         catch (UnknownKeyException e) {
128             pass = true;
129         }
130         assertTrue(pass);
131         
132         // increment an unknown column
133
pass = false;
134         try {
135             d.incrementValue(1.0, "R1", "XX");
136         }
137         catch (UnknownKeyException e) {
138             pass = true;
139         }
140         assertTrue(pass);
141     }
142     
143     /**
144      * Some tests for the getRowCount() method.
145      */

146     public void testGetRowCount() {
147         DefaultCategoryDataset d = new DefaultCategoryDataset();
148         assertTrue(d.getRowCount() == 0);
149         
150         d.addValue(1.0, "R1", "C1");
151         assertTrue(d.getRowCount() == 1);
152         
153         d.addValue(1.0, "R2", "C1");
154         assertTrue(d.getRowCount() == 2);
155         
156         d.addValue(2.0, "R2", "C1");
157         assertTrue(d.getRowCount() == 2);
158         
159         // a row of all null values is still counted...
160
d.setValue(null, "R2", "C1");
161         assertTrue(d.getRowCount() == 2);
162     }
163
164     /**
165      * Some tests for the getColumnCount() method.
166      */

167     public void testGetColumnCount() {
168         DefaultCategoryDataset d = new DefaultCategoryDataset();
169         assertTrue(d.getColumnCount() == 0);
170         
171         d.addValue(1.0, "R1", "C1");
172         assertTrue(d.getColumnCount() == 1);
173         
174         d.addValue(1.0, "R1", "C2");
175         assertTrue(d.getColumnCount() == 2);
176         
177         d.addValue(2.0, "R1", "C2");
178         assertTrue(d.getColumnCount() == 2);
179         
180         // a column of all null values is still counted...
181
d.setValue(null, "R1", "C2");
182         assertTrue(d.getColumnCount() == 2);
183     }
184
185     /**
186      * Confirm that the equals method can distinguish all the required fields.
187      */

188     public void testEquals() {
189         DefaultCategoryDataset d1 = new DefaultCategoryDataset();
190         d1.setValue(23.4, "R1", "C1");
191         DefaultCategoryDataset d2 = new DefaultCategoryDataset();
192         d2.setValue(23.4, "R1", "C1");
193         assertTrue(d1.equals(d2));
194         assertTrue(d2.equals(d1));
195
196         d1.setValue(36.5, "R1", "C2");
197         assertFalse(d1.equals(d2));
198         d2.setValue(36.5, "R1", "C2");
199         assertTrue(d1.equals(d2));
200
201         d1.setValue(null, "R1", "C1");
202         assertFalse(d1.equals(d2));
203         d2.setValue(null, "R1", "C1");
204         assertTrue(d1.equals(d2));
205     }
206
207     /**
208      * Serialize an instance, restore it, and check for equality.
209      */

210     public void testSerialization() {
211
212         DefaultCategoryDataset d1 = new DefaultCategoryDataset();
213         d1.setValue(23.4, "R1", "C1");
214         DefaultCategoryDataset d2 = null;
215
216         try {
217             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
218             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
219             out.writeObject(d1);
220             out.close();
221
222             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
223                 new ByteArrayInputStream JavaDoc(buffer.toByteArray())
224             );
225             d2 = (DefaultCategoryDataset) in.readObject();
226             in.close();
227         }
228         catch (Exception JavaDoc e) {
229             System.out.println(e.toString());
230         }
231         assertEquals(d1, d2);
232
233     }
234     
235 }
236
Popular Tags