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: * CategoryToPieDatasetTests.java
029: * ------------------------------
030: * (C) Copyright 2006, by Object Refinery Limited and Contributors.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: CategoryToPieDatasetTests.java,v 1.1.2.1 2006/10/03 15:41:48 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 26-Jul-2006 : Version 1 (DG);
040: * 01-Aug-2006 : Added testGetIndex() method (DG);
041: *
042: */
043:
044: package org.jfree.data.category.junit;
045:
046: import java.io.ByteArrayInputStream;
047: import java.io.ByteArrayOutputStream;
048: import java.io.ObjectInput;
049: import java.io.ObjectInputStream;
050: import java.io.ObjectOutput;
051: import java.io.ObjectOutputStream;
052:
053: import junit.framework.Test;
054: import junit.framework.TestCase;
055: import junit.framework.TestSuite;
056:
057: import org.jfree.data.category.CategoryToPieDataset;
058: import org.jfree.data.category.DefaultCategoryDataset;
059: import org.jfree.data.general.DefaultPieDataset;
060: import org.jfree.util.TableOrder;
061:
062: /**
063: * Tests for the {@link CategoryToPieDataset} class.
064: */
065: public class CategoryToPieDatasetTests extends TestCase {
066:
067: /**
068: * Returns the tests as a test suite.
069: *
070: * @return The test suite.
071: */
072: public static Test suite() {
073: return new TestSuite(CategoryToPieDatasetTests.class);
074: }
075:
076: /**
077: * Constructs a new set of tests.
078: *
079: * @param name the name of the tests.
080: */
081: public CategoryToPieDatasetTests(String name) {
082: super (name);
083: }
084:
085: /**
086: * Some tests for the constructor.
087: */
088: public void testConstructor() {
089: // try a null source
090: CategoryToPieDataset p1 = new CategoryToPieDataset(null,
091: TableOrder.BY_COLUMN, 0);
092: assertNull(p1.getUnderlyingDataset());
093: assertEquals(p1.getItemCount(), 0);
094: assertTrue(p1.getKeys().isEmpty());
095: assertNull(p1.getValue("R1"));
096: }
097:
098: /**
099: * 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(1.1));
108: assertEquals(d1.getValue("C2"), new Double(2.2));
109:
110: // check negative index throws exception
111: try {
112: /* Number n = */d1.getValue(-1);
113: fail("Expected IndexOutOfBoundsException.");
114: } catch (IndexOutOfBoundsException e) {
115: // this is expected
116: }
117:
118: // check index == getItemCount() throws exception
119: try {
120: /* Number n = */d1.getValue(d1.getItemCount());
121: fail("Expected IndexOutOfBoundsException.");
122: } catch (IndexOutOfBoundsException e) {
123: // this is expected
124: }
125:
126: // test null source
127: CategoryToPieDataset p1 = new CategoryToPieDataset(null,
128: TableOrder.BY_COLUMN, 0);
129: try {
130: /* Number n = */p1.getValue(0);
131: fail("Expected IndexOutOfBoundsException.");
132: } catch (IndexOutOfBoundsException e) {
133: // this is expected
134: }
135: }
136:
137: /**
138: * Some checks for the getKey(int) method.
139: */
140: public void testGetKey() {
141: DefaultCategoryDataset underlying = new DefaultCategoryDataset();
142: underlying.addValue(1.1, "R1", "C1");
143: underlying.addValue(2.2, "R1", "C2");
144: CategoryToPieDataset d1 = new CategoryToPieDataset(underlying,
145: TableOrder.BY_ROW, 0);
146: assertEquals(d1.getKey(0), "C1");
147: assertEquals(d1.getKey(1), "C2");
148:
149: // check negative index throws exception
150: try {
151: /* Number n = */d1.getKey(-1);
152: fail("Expected IndexOutOfBoundsException.");
153: } catch (IndexOutOfBoundsException e) {
154: // this is expected
155: }
156:
157: // check index == getItemCount() throws exception
158: try {
159: /* Number n = */d1.getKey(d1.getItemCount());
160: fail("Expected IndexOutOfBoundsException.");
161: } catch (IndexOutOfBoundsException e) {
162: // this is expected
163: }
164:
165: // test null source
166: CategoryToPieDataset p1 = new CategoryToPieDataset(null,
167: TableOrder.BY_COLUMN, 0);
168: try {
169: /* Number n = */p1.getKey(0);
170: fail("Expected IndexOutOfBoundsException.");
171: } catch (IndexOutOfBoundsException e) {
172: // this is expected
173: }
174: }
175:
176: /**
177: * Some checks for the getIndex() method.
178: */
179: public void testGetIndex() {
180: DefaultCategoryDataset underlying = new DefaultCategoryDataset();
181: underlying.addValue(1.1, "R1", "C1");
182: underlying.addValue(2.2, "R1", "C2");
183: CategoryToPieDataset d1 = new CategoryToPieDataset(underlying,
184: TableOrder.BY_ROW, 0);
185: assertEquals(0, d1.getIndex("C1"));
186: assertEquals(1, d1.getIndex("C2"));
187: assertEquals(-1, d1.getIndex("XX"));
188:
189: // try null
190: boolean pass = false;
191: try {
192: d1.getIndex(null);
193: } catch (IllegalArgumentException e) {
194: pass = true;
195: }
196: assertTrue(pass);
197: }
198:
199: /**
200: * For datasets, the equals() method just checks keys and values.
201: */
202: public void testEquals() {
203: DefaultCategoryDataset underlying = new DefaultCategoryDataset();
204: underlying.addValue(1.1, "R1", "C1");
205: underlying.addValue(2.2, "R1", "C2");
206: CategoryToPieDataset d1 = new CategoryToPieDataset(underlying,
207: TableOrder.BY_COLUMN, 1);
208: DefaultPieDataset d2 = new DefaultPieDataset();
209: d2.setValue("R1", 2.2);
210: assertTrue(d1.equals(d2));
211: }
212:
213: /**
214: * Serialize an instance, restore it, and check for equality.
215: */
216: public void testSerialization() {
217: DefaultCategoryDataset underlying = new DefaultCategoryDataset();
218: underlying.addValue(1.1, "R1", "C1");
219: underlying.addValue(2.2, "R1", "C2");
220: CategoryToPieDataset d1 = new CategoryToPieDataset(underlying,
221: TableOrder.BY_COLUMN, 1);
222: CategoryToPieDataset d2 = null;
223:
224: try {
225: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
226: ObjectOutput out = new ObjectOutputStream(buffer);
227: out.writeObject(d1);
228: out.close();
229:
230: ObjectInput in = new ObjectInputStream(
231: new ByteArrayInputStream(buffer.toByteArray()));
232: d2 = (CategoryToPieDataset) in.readObject();
233: in.close();
234: } catch (Exception e) {
235: e.printStackTrace();
236: }
237: assertEquals(d1, d2);
238:
239: // regular equality for the datasets doesn't check the fields, just
240: // the data values...so let's check some more things...
241: assertEquals(d1.getUnderlyingDataset(), d2
242: .getUnderlyingDataset());
243: assertEquals(d1.getExtractType(), d2.getExtractType());
244: assertEquals(d1.getExtractIndex(), d2.getExtractIndex());
245: }
246:
247: }
|