001: /* ===========================================================
002: * JFreeChart : a free chart library for the Java(tm) platform
003: * ===========================================================
004: *
005: * (C) Copyright 2000-2007, by Object Refinery Limited and Contributors.
006: *
007: * Project Info: http://www.jfree.org/jfreechart/index.html
008: *
009: * This library is free software; you can redistribute it and/or modify it
010: * under the terms of the GNU Lesser General Public License as published by
011: * the Free Software Foundation; either version 2.1 of the License, or
012: * (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful, but
015: * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
016: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
017: * License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library; if not, write to the Free Software
021: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
022: * USA.
023: *
024: * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
025: * in the United States and other countries.]
026: *
027: * ------------------------------
028: * DefaultKeyedValues2DTests.java
029: * ------------------------------
030: * (C) Copyright 2003-2007 by Object Refinery Limited and Contributors.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: DefaultKeyedValues2DTests.java,v 1.1.2.4 2007/03/30 09:53:10 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 13-Mar-2003 : Version 1 (DG);
040: * 15-Sep-2004 : Updated cloning test (DG);
041: * 06-Oct-2005 : Added testEquals() (DG);
042: * 18-Jan-2007 : Added testSparsePopulation() (DG);
043: * 26-Feb-2007 : Added some basic tests (DG);
044: * 30-Mar-2007 : Added a test for bug 1690654 (DG);
045: *
046: */
047:
048: package org.jfree.data.junit;
049:
050: import java.io.ByteArrayInputStream;
051: import java.io.ByteArrayOutputStream;
052: import java.io.ObjectInput;
053: import java.io.ObjectInputStream;
054: import java.io.ObjectOutput;
055: import java.io.ObjectOutputStream;
056:
057: import junit.framework.Test;
058: import junit.framework.TestCase;
059: import junit.framework.TestSuite;
060:
061: import org.jfree.data.DefaultKeyedValues2D;
062: import org.jfree.data.UnknownKeyException;
063:
064: /**
065: * Tests for the {@link DefaultKeyedValues2D} class.
066: */
067: public class DefaultKeyedValues2DTests extends TestCase {
068:
069: /**
070: * Returns the tests as a test suite.
071: *
072: * @return The test suite.
073: */
074: public static Test suite() {
075: return new TestSuite(DefaultKeyedValues2DTests.class);
076: }
077:
078: /**
079: * Constructs a new set of tests.
080: *
081: * @param name the name of the tests.
082: */
083: public DefaultKeyedValues2DTests(String name) {
084: super (name);
085: }
086:
087: /**
088: * Some checks for the getValue() method.
089: */
090: public void testGetValue() {
091: DefaultKeyedValues2D d = new DefaultKeyedValues2D();
092: d.addValue(new Double(1.0), "R1", "C1");
093: assertEquals(new Double(1.0), d.getValue("R1", "C1"));
094: boolean pass = false;
095: try {
096: d.getValue("XX", "C1");
097: } catch (UnknownKeyException e) {
098: pass = true;
099: }
100: assertTrue(pass);
101:
102: pass = false;
103: try {
104: d.getValue("R1", "XX");
105: } catch (UnknownKeyException e) {
106: pass = true;
107: }
108: assertTrue(pass);
109: }
110:
111: /**
112: * Some checks for the clone() method.
113: */
114: public void testCloning() {
115: DefaultKeyedValues2D v1 = new DefaultKeyedValues2D();
116: v1.setValue(new Integer(1), "V1", "C1");
117: v1.setValue(null, "V2", "C1");
118: v1.setValue(new Integer(3), "V3", "C2");
119: DefaultKeyedValues2D v2 = null;
120: try {
121: v2 = (DefaultKeyedValues2D) v1.clone();
122: } catch (CloneNotSupportedException e) {
123: e.printStackTrace();
124: }
125: assertTrue(v1 != v2);
126: assertTrue(v1.getClass() == v2.getClass());
127: assertTrue(v1.equals(v2));
128:
129: // check that clone is independent of the original
130: v2.setValue(new Integer(2), "V2", "C1");
131: assertFalse(v1.equals(v2));
132: }
133:
134: /**
135: * Serialize an instance, restore it, and check for equality.
136: */
137: public void testSerialization() {
138:
139: DefaultKeyedValues2D kv2D1 = new DefaultKeyedValues2D();
140: kv2D1.addValue(new Double(234.2), "Row1", "Col1");
141: kv2D1.addValue(null, "Row1", "Col2");
142: kv2D1.addValue(new Double(345.9), "Row2", "Col1");
143: kv2D1.addValue(new Double(452.7), "Row2", "Col2");
144:
145: DefaultKeyedValues2D kv2D2 = null;
146:
147: try {
148: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
149: ObjectOutput out = new ObjectOutputStream(buffer);
150: out.writeObject(kv2D1);
151: out.close();
152:
153: ObjectInput in = new ObjectInputStream(
154: new ByteArrayInputStream(buffer.toByteArray()));
155: kv2D2 = (DefaultKeyedValues2D) in.readObject();
156: in.close();
157: } catch (Exception e) {
158: e.printStackTrace();
159: }
160: assertEquals(kv2D1, kv2D2);
161:
162: }
163:
164: /**
165: * Some checks for the equals() method.
166: */
167: public void testEquals() {
168: DefaultKeyedValues2D d1 = new DefaultKeyedValues2D();
169: DefaultKeyedValues2D d2 = new DefaultKeyedValues2D();
170: assertTrue(d1.equals(d2));
171: assertTrue(d2.equals(d1));
172:
173: d1.addValue(new Double(1.0), new Double(2.0), "S1");
174: assertFalse(d1.equals(d2));
175: d2.addValue(new Double(1.0), new Double(2.0), "S1");
176: assertTrue(d1.equals(d2));
177: }
178:
179: /**
180: * Populates a data structure with sparse entries, then checks that
181: * the unspecified entries return null.
182: */
183: public void testSparsePopulation() {
184: DefaultKeyedValues2D d = new DefaultKeyedValues2D();
185: d.addValue(new Integer(11), "R1", "C1");
186: d.addValue(new Integer(22), "R2", "C2");
187:
188: assertEquals(new Integer(11), d.getValue("R1", "C1"));
189: assertNull(d.getValue("R1", "C2"));
190: assertEquals(new Integer(22), d.getValue("R2", "C2"));
191: assertNull(d.getValue("R2", "C1"));
192: }
193:
194: /**
195: * Some basic checks for the getRowCount() method.
196: */
197: public void testRowCount() {
198: DefaultKeyedValues2D d = new DefaultKeyedValues2D();
199: assertEquals(0, d.getRowCount());
200: d.addValue(new Double(1.0), "R1", "C1");
201: assertEquals(1, d.getRowCount());
202: d.addValue(new Double(2.0), "R2", "C1");
203: assertEquals(2, d.getRowCount());
204: }
205:
206: /**
207: * Some basic checks for the getColumnCount() method.
208: */
209: public void testColumnCount() {
210: DefaultKeyedValues2D d = new DefaultKeyedValues2D();
211: assertEquals(0, d.getColumnCount());
212: d.addValue(new Double(1.0), "R1", "C1");
213: assertEquals(1, d.getColumnCount());
214: d.addValue(new Double(2.0), "R1", "C2");
215: assertEquals(2, d.getColumnCount());
216: }
217:
218: private static final double EPSILON = 0.0000000001;
219:
220: /**
221: * Some basic checks for the getValue(int, int) method.
222: */
223: public void testGetValue2() {
224: DefaultKeyedValues2D d = new DefaultKeyedValues2D();
225: boolean pass = false;
226: try {
227: d.getValue(0, 0);
228: } catch (IndexOutOfBoundsException e) {
229: pass = true;
230: }
231: assertTrue(pass);
232: d.addValue(new Double(1.0), "R1", "C1");
233: assertEquals(1.0, d.getValue(0, 0).doubleValue(), EPSILON);
234: d.addValue(new Double(2.0), "R2", "C2");
235: assertEquals(2.0, d.getValue(1, 1).doubleValue(), EPSILON);
236: assertNull(d.getValue(1, 0));
237: assertNull(d.getValue(0, 1));
238:
239: pass = false;
240: try {
241: d.getValue(2, 0);
242: } catch (IndexOutOfBoundsException e) {
243: pass = true;
244: }
245: assertTrue(pass);
246: }
247:
248: /**
249: * Some basic checks for the getRowKey() method.
250: */
251: public void testGetRowKey() {
252: DefaultKeyedValues2D d = new DefaultKeyedValues2D();
253: boolean pass = false;
254: try {
255: d.getRowKey(0);
256: } catch (IndexOutOfBoundsException e) {
257: pass = true;
258: }
259: assertTrue(pass);
260: d.addValue(new Double(1.0), "R1", "C1");
261: d.addValue(new Double(1.0), "R2", "C1");
262: assertEquals("R1", d.getRowKey(0));
263: assertEquals("R2", d.getRowKey(1));
264:
265: // check sorted rows
266: d = new DefaultKeyedValues2D(true);
267: d.addValue(new Double(1.0), "R1", "C1");
268: assertEquals("R1", d.getRowKey(0));
269: d.addValue(new Double(0.0), "R0", "C1");
270: assertEquals("R0", d.getRowKey(0));
271: assertEquals("R1", d.getRowKey(1));
272: }
273:
274: /**
275: * Some basic checks for the getColumnKey() method.
276: */
277: public void testGetColumnKey() {
278: DefaultKeyedValues2D d = new DefaultKeyedValues2D();
279: boolean pass = false;
280: try {
281: d.getColumnKey(0);
282: } catch (IndexOutOfBoundsException e) {
283: pass = true;
284: }
285: assertTrue(pass);
286: d.addValue(new Double(1.0), "R1", "C1");
287: d.addValue(new Double(1.0), "R1", "C2");
288: assertEquals("C1", d.getColumnKey(0));
289: assertEquals("C2", d.getColumnKey(1));
290: }
291:
292: /**
293: * Some basic checks for the removeValue() method.
294: */
295: public void testRemoveValue() {
296: DefaultKeyedValues2D d = new DefaultKeyedValues2D();
297: d.removeValue("R1", "C1");
298: d.addValue(new Double(1.0), "R1", "C1");
299: d.removeValue("R1", "C1");
300: assertEquals(0, d.getRowCount());
301: assertEquals(0, d.getColumnCount());
302:
303: d.addValue(new Double(1.0), "R1", "C1");
304: d.addValue(new Double(2.0), "R2", "C1");
305: d.removeValue("R1", "C1");
306: assertEquals(new Double(2.0), d.getValue(0, 0));
307: }
308:
309: /**
310: * A test for bug 1690654.
311: */
312: public void testRemoveValueBug1690654() {
313: DefaultKeyedValues2D d = new DefaultKeyedValues2D();
314: d.addValue(new Double(1.0), "R1", "C1");
315: d.addValue(new Double(2.0), "R2", "C2");
316: assertEquals(2, d.getColumnCount());
317: assertEquals(2, d.getRowCount());
318: d.removeValue("R2", "C2");
319: assertEquals(1, d.getColumnCount());
320: assertEquals(1, d.getRowCount());
321: assertEquals(new Double(1.0), d.getValue(0, 0));
322: }
323:
324: /**
325: * Some basic checks for the removeRow() method.
326: */
327: public void testRemoveRow() {
328: DefaultKeyedValues2D d = new DefaultKeyedValues2D();
329: boolean pass = false;
330: try {
331: d.removeRow(0);
332: } catch (IndexOutOfBoundsException e) {
333: pass = true;
334: }
335: assertTrue(pass);
336: }
337:
338: }
|