001: /* ===========================================================
002: * JFreeChart : a free chart library for the Java(tm) platform
003: * ===========================================================
004: *
005: * (C) Copyright 2000-2006, 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: * PieDatasetTests.java
029: * --------------------
030: * (C) Copyright 2003-2006, by Object Refinery Limited and Contributors.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: DefaultPieDatasetTests.java,v 1.1.2.1 2006/10/03 15:41:44 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 18-Aug-2003 : Version 1 (DG);
040: * 31-Jul-2006 : Added test for new clear() method (DG);
041: * 01-Aug-2006 : Added testGetKey() and testGetIndex() methods (DG);
042: *
043: */
044:
045: package org.jfree.data.general.junit;
046:
047: import java.io.ByteArrayInputStream;
048: import java.io.ByteArrayOutputStream;
049: import java.io.ObjectInput;
050: import java.io.ObjectInputStream;
051: import java.io.ObjectOutput;
052: import java.io.ObjectOutputStream;
053:
054: import junit.framework.Test;
055: import junit.framework.TestCase;
056: import junit.framework.TestSuite;
057:
058: import org.jfree.data.general.DatasetChangeEvent;
059: import org.jfree.data.general.DatasetChangeListener;
060: import org.jfree.data.general.DefaultPieDataset;
061:
062: /**
063: * Tests for the {@link org.jfree.data.general.PieDataset} class.
064: */
065: public class DefaultPieDatasetTests extends TestCase implements
066: DatasetChangeListener {
067:
068: private DatasetChangeEvent lastEvent;
069:
070: public void datasetChanged(DatasetChangeEvent event) {
071: this .lastEvent = event;
072: }
073:
074: /**
075: * Returns the tests as a test suite.
076: *
077: * @return The test suite.
078: */
079: public static Test suite() {
080: return new TestSuite(DefaultPieDatasetTests.class);
081: }
082:
083: /**
084: * Constructs a new set of tests.
085: *
086: * @param name the name of the tests.
087: */
088: public DefaultPieDatasetTests(String name) {
089: super (name);
090: }
091:
092: /**
093: * Some tests for the clear() method.
094: */
095: public void testClear() {
096: DefaultPieDataset d = new DefaultPieDataset();
097: d.addChangeListener(this );
098: // no event is generated if the dataset is already empty
099: 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: } catch (IndexOutOfBoundsException e) {
123: pass = true;
124: }
125: assertTrue(pass);
126:
127: pass = false;
128: try {
129: d.getKey(2);
130: } catch (IndexOutOfBoundsException e) {
131: pass = true;
132: }
133: assertTrue(pass);
134: }
135:
136: /**
137: * Some checks for the getIndex() method.
138: */
139: public void testGetIndex() {
140: DefaultPieDataset d = new DefaultPieDataset();
141: d.setValue("A", 1.0);
142: d.setValue("B", 2.0);
143: assertEquals(0, d.getIndex("A"));
144: assertEquals(1, d.getIndex("B"));
145: assertEquals(-1, d.getIndex("XX"));
146:
147: boolean pass = false;
148: try {
149: d.getIndex(null);
150: } catch (IllegalArgumentException e) {
151: pass = true;
152: }
153: assertTrue(pass);
154: }
155:
156: /**
157: * Confirm that cloning works.
158: */
159: public void testCloning() {
160: DefaultPieDataset d1 = new DefaultPieDataset();
161: d1.setValue("V1", new Integer(1));
162: d1.setValue("V2", null);
163: d1.setValue("V3", new Integer(3));
164: DefaultPieDataset d2 = null;
165: try {
166: d2 = (DefaultPieDataset) d1.clone();
167: } catch (CloneNotSupportedException e) {
168: System.err.println("Failed to clone.");
169: }
170: assertTrue(d1 != d2);
171: assertTrue(d1.getClass() == d2.getClass());
172: assertTrue(d1.equals(d2));
173: }
174:
175: /**
176: * Serialize an instance, restore it, and check for equality.
177: */
178: public void testSerialization() {
179:
180: DefaultPieDataset d1 = new DefaultPieDataset();
181: d1.setValue("C1", new Double(234.2));
182: d1.setValue("C2", null);
183: d1.setValue("C3", new Double(345.9));
184: d1.setValue("C4", new Double(452.7));
185:
186: DefaultPieDataset d2 = null;
187:
188: try {
189: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
190: ObjectOutput out = new ObjectOutputStream(buffer);
191: out.writeObject(d1);
192: out.close();
193:
194: ObjectInput in = new ObjectInputStream(
195: new ByteArrayInputStream(buffer.toByteArray()));
196: d2 = (DefaultPieDataset) in.readObject();
197: in.close();
198: } catch (Exception e) {
199: System.out.println(e.toString());
200: }
201: assertEquals(d1, d2);
202:
203: }
204:
205: }
|