001: /* ===========================================================
002: * JFreeChart : a free chart library for the Java(tm) platform
003: * ===========================================================
004: *
005: * (C) Copyright 2000-2005, 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: * CategoryTableXYDatasetTests.java
029: * --------------------------------
030: * (C) Copyright 2005, by Object Refinery Limited and Contributors.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: CategoryTableXYDatasetTests.java,v 1.1.2.1 2006/10/03 15:41:38 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 06-Oct-2005 : Version 1 (DG);
040: *
041: */
042:
043: package org.jfree.data.xy.junit;
044:
045: import java.io.ByteArrayInputStream;
046: import java.io.ByteArrayOutputStream;
047: import java.io.ObjectInput;
048: import java.io.ObjectInputStream;
049: import java.io.ObjectOutput;
050: import java.io.ObjectOutputStream;
051:
052: import junit.framework.Test;
053: import junit.framework.TestCase;
054: import junit.framework.TestSuite;
055:
056: import org.jfree.data.xy.CategoryTableXYDataset;
057:
058: /**
059: * Tests for the {@link CategoryTableXYDataset} class.
060: */
061: public class CategoryTableXYDatasetTests extends TestCase {
062:
063: /**
064: * Returns the tests as a test suite.
065: *
066: * @return The test suite.
067: */
068: public static Test suite() {
069: return new TestSuite(CategoryTableXYDatasetTests.class);
070: }
071:
072: /**
073: * Constructs a new set of tests.
074: *
075: * @param name the name of the tests.
076: */
077: public CategoryTableXYDatasetTests(String name) {
078: super (name);
079: }
080:
081: /**
082: * Confirm that the equals method can distinguish all the required fields.
083: */
084: public void testEquals() {
085:
086: CategoryTableXYDataset d1 = new CategoryTableXYDataset();
087: d1.add(1.0, 1.1, "Series 1");
088: d1.add(2.0, 2.2, "Series 1");
089:
090: CategoryTableXYDataset d2 = new CategoryTableXYDataset();
091: d2.add(1.0, 1.1, "Series 1");
092: d2.add(2.0, 2.2, "Series 1");
093:
094: assertTrue(d1.equals(d2));
095: assertTrue(d2.equals(d1));
096:
097: d1.add(3.0, 3.3, "Series 1");
098: assertFalse(d1.equals(d2));
099:
100: d2.add(3.0, 3.3, "Series 1");
101: assertTrue(d1.equals(d2));
102:
103: }
104:
105: /**
106: * Confirm that cloning works.
107: */
108: public void testCloning() {
109:
110: CategoryTableXYDataset d1 = new CategoryTableXYDataset();
111: d1.add(1.0, 1.1, "Series 1");
112: d1.add(2.0, 2.2, "Series 1");
113:
114: CategoryTableXYDataset d2 = null;
115: try {
116: d2 = (CategoryTableXYDataset) d1.clone();
117: } catch (CloneNotSupportedException e) {
118: e.printStackTrace();
119: }
120: assertTrue(d1 != d2);
121: assertTrue(d1.getClass() == d2.getClass());
122: assertTrue(d1.equals(d2));
123: }
124:
125: /**
126: * Serialize an instance, restore it, and check for equality.
127: */
128: public void testSerialization() {
129:
130: CategoryTableXYDataset d1 = new CategoryTableXYDataset();
131: d1.add(1.0, 1.1, "Series 1");
132: d1.add(2.0, 2.2, "Series 1");
133:
134: CategoryTableXYDataset d2 = null;
135:
136: try {
137: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
138: ObjectOutput out = new ObjectOutputStream(buffer);
139: out.writeObject(d1);
140: out.close();
141:
142: ObjectInput in = new ObjectInputStream(
143: new ByteArrayInputStream(buffer.toByteArray()));
144: d2 = (CategoryTableXYDataset) in.readObject();
145: in.close();
146: } catch (Exception e) {
147: e.printStackTrace();
148: }
149: assertEquals(d1, d2);
150:
151: }
152:
153: private static final double EPSILON = 0.0000000001;
154:
155: /**
156: * This is a test for bug 1312066 - adding a new series should trigger a
157: * recalculation of the interval width, if it is being automatically
158: * calculated.
159: */
160: public void testAddSeries() {
161: CategoryTableXYDataset d1 = new CategoryTableXYDataset();
162: d1.setAutoWidth(true);
163: d1.add(3.0, 1.1, "Series 1");
164: d1.add(7.0, 2.2, "Series 1");
165: assertEquals(3.0, d1.getXValue(0, 0), EPSILON);
166: assertEquals(7.0, d1.getXValue(0, 1), EPSILON);
167: assertEquals(1.0, d1.getStartXValue(0, 0), EPSILON);
168: assertEquals(5.0, d1.getStartXValue(0, 1), EPSILON);
169: assertEquals(5.0, d1.getEndXValue(0, 0), EPSILON);
170: assertEquals(9.0, d1.getEndXValue(0, 1), EPSILON);
171:
172: // now add some more data
173: d1.add(7.5, 1.1, "Series 2");
174: d1.add(9.0, 2.2, "Series 2");
175:
176: assertEquals(3.0, d1.getXValue(1, 0), EPSILON);
177: assertEquals(7.0, d1.getXValue(1, 1), EPSILON);
178: assertEquals(7.5, d1.getXValue(1, 2), EPSILON);
179: assertEquals(9.0, d1.getXValue(1, 3), EPSILON);
180:
181: assertEquals(7.25, d1.getStartXValue(1, 2), EPSILON);
182: assertEquals(8.75, d1.getStartXValue(1, 3), EPSILON);
183: assertEquals(7.75, d1.getEndXValue(1, 2), EPSILON);
184: assertEquals(9.25, d1.getEndXValue(1, 3), EPSILON);
185:
186: // and check the first series too...
187: assertEquals(2.75, d1.getStartXValue(0, 0), EPSILON);
188: assertEquals(6.75, d1.getStartXValue(0, 1), EPSILON);
189: assertEquals(3.25, d1.getEndXValue(0, 0), EPSILON);
190: assertEquals(7.25, d1.getEndXValue(0, 1), EPSILON);
191: }
192: }
|