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 * DefaultCategoryDatasetTests.java
28 * --------------------------------
29 * (C) Copyright 2004 by Object Refinery Limited and Contributors.
30 *
31 * Original Author: David Gilbert (for Object Refinery Limited);
32 * Contributor(s): -;
33 *
34 * $Id: DefaultCategoryDatasetTests.java,v 1.3 2005/03/29 12:57:13 mungady Exp $
35 *
36 * Changes
37 * -------
38 * 23-Mar-2004 : Version 1 (DG);
39 *
40 */
41
42 package org.jfree.data.category.junit;
43
44 import java.io.ByteArrayInputStream;
45 import java.io.ByteArrayOutputStream;
46 import java.io.ObjectInput;
47 import java.io.ObjectInputStream;
48 import java.io.ObjectOutput;
49 import java.io.ObjectOutputStream;
50
51 import junit.framework.Test;
52 import junit.framework.TestCase;
53 import junit.framework.TestSuite;
54
55 import org.jfree.data.category.DefaultCategoryDataset;
56
57 /**
58 * Tests for the {@link DefaultCategoryDataset} class.
59 */
60 public class DefaultCategoryDatasetTests extends TestCase {
61
62 /**
63 * Returns the tests as a test suite.
64 *
65 * @return The test suite.
66 */
67 public static Test suite() {
68 return new TestSuite(DefaultCategoryDatasetTests.class);
69 }
70
71 /**
72 * Constructs a new set of tests.
73 *
74 * @param name the name of the tests.
75 */
76 public DefaultCategoryDatasetTests(String name) {
77 super(name);
78 }
79
80 /**
81 * Some tests for the getRowCount() method.
82 */
83 public void testGetRowCount() {
84 DefaultCategoryDataset d = new DefaultCategoryDataset();
85 assertTrue(d.getRowCount() == 0);
86
87 d.addValue(1.0, "R1", "C1");
88 assertTrue(d.getRowCount() == 1);
89
90 d.addValue(1.0, "R2", "C1");
91 assertTrue(d.getRowCount() == 2);
92
93 d.addValue(2.0, "R2", "C1");
94 assertTrue(d.getRowCount() == 2);
95
96 // a row of all null values is still counted...
97 d.setValue(null, "R2", "C1");
98 assertTrue(d.getRowCount() == 2);
99 }
100
101 /**
102 * Some tests for the getColumnCount() method.
103 */
104 public void testGetColumnCount() {
105 DefaultCategoryDataset d = new DefaultCategoryDataset();
106 assertTrue(d.getColumnCount() == 0);
107
108 d.addValue(1.0, "R1", "C1");
109 assertTrue(d.getColumnCount() == 1);
110
111 d.addValue(1.0, "R1", "C2");
112 assertTrue(d.getColumnCount() == 2);
113
114 d.addValue(2.0, "R1", "C2");
115 assertTrue(d.getColumnCount() == 2);
116
117 // a column of all null values is still counted...
118 d.setValue(null, "R1", "C2");
119 assertTrue(d.getColumnCount() == 2);
120 }
121
122 /**
123 * Confirm that the equals method can distinguish all the required fields.
124 */
125 public void testEquals() {
126 DefaultCategoryDataset d1 = new DefaultCategoryDataset();
127 d1.setValue(23.4, "R1", "C1");
128 DefaultCategoryDataset d2 = new DefaultCategoryDataset();
129 d2.setValue(23.4, "R1", "C1");
130 assertTrue(d1.equals(d2));
131 assertTrue(d2.equals(d1));
132
133 d1.setValue(36.5, "R1", "C2");
134 assertFalse(d1.equals(d2));
135 d2.setValue(36.5, "R1", "C2");
136 assertTrue(d1.equals(d2));
137
138 d1.setValue(null, "R1", "C1");
139 assertFalse(d1.equals(d2));
140 d2.setValue(null, "R1", "C1");
141 assertTrue(d1.equals(d2));
142 }
143
144 /**
145 * Serialize an instance, restore it, and check for equality.
146 */
147 public void testSerialization() {
148
149 DefaultCategoryDataset d1 = new DefaultCategoryDataset();
150 d1.setValue(23.4, "R1", "C1");
151 DefaultCategoryDataset d2 = null;
152
153 try {
154 ByteArrayOutputStream buffer = new ByteArrayOutputStream();
155 ObjectOutput out = new ObjectOutputStream(buffer);
156 out.writeObject(d1);
157 out.close();
158
159 ObjectInput in = new ObjectInputStream(
160 new ByteArrayInputStream(buffer.toByteArray())
161 );
162 d2 = (DefaultCategoryDataset) in.readObject();
163 in.close();
164 }
165 catch (Exception e) {
166 System.out.println(e.toString());
167 }
168 assertEquals(d1, d2);
169
170 }
171
172 }