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: * DefaultTableXYDatasetTests.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: DefaultTableXYDatasetTests.java,v 1.1.2.2 2007/03/08 13:57:09 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 23-Dec-2003 : Version 1 (DG);
040: * 06-Oct-2005 : Added test for new data updating interval width (DG);
041: * 08-Mar-2007 : Added testGetSeries() (DG);
042: *
043: */
044:
045: package org.jfree.data.xy.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.xy.DefaultTableXYDataset;
059: import org.jfree.data.xy.XYSeries;
060:
061: /**
062: * Tests for the {@link DefaultTableXYDataset} class.
063: */
064: public class DefaultTableXYDatasetTests extends TestCase {
065:
066: /**
067: * Returns the tests as a test suite.
068: *
069: * @return The test suite.
070: */
071: public static Test suite() {
072: return new TestSuite(DefaultTableXYDatasetTests.class);
073: }
074:
075: /**
076: * Constructs a new set of tests.
077: *
078: * @param name the name of the tests.
079: */
080: public DefaultTableXYDatasetTests(String name) {
081: super (name);
082: }
083:
084: /**
085: * Confirm that the equals method can distinguish all the required fields.
086: */
087: public void testEquals() {
088:
089: DefaultTableXYDataset d1 = new DefaultTableXYDataset();
090: XYSeries s1 = new XYSeries("Series 1", true, false);
091: s1.add(1.0, 1.1);
092: s1.add(2.0, 2.2);
093: d1.addSeries(s1);
094:
095: DefaultTableXYDataset d2 = new DefaultTableXYDataset();
096: XYSeries s2 = new XYSeries("Series 1", true, false);
097: s2.add(1.0, 1.1);
098: s2.add(2.0, 2.2);
099: d2.addSeries(s2);
100:
101: assertTrue(d1.equals(d2));
102: assertTrue(d2.equals(d1));
103:
104: s1.add(3.0, 3.3);
105: assertFalse(d1.equals(d2));
106:
107: s2.add(3.0, 3.3);
108: assertTrue(d1.equals(d2));
109:
110: }
111:
112: /**
113: * Confirm that cloning works.
114: */
115: public void testCloning() {
116: DefaultTableXYDataset d1 = new DefaultTableXYDataset();
117: XYSeries s1 = new XYSeries("Series 1", true, false);
118: s1.add(1.0, 1.1);
119: s1.add(2.0, 2.2);
120: d1.addSeries(s1);
121:
122: DefaultTableXYDataset d2 = null;
123: try {
124: d2 = (DefaultTableXYDataset) d1.clone();
125: } catch (CloneNotSupportedException e) {
126: e.printStackTrace();
127: }
128: assertTrue(d1 != d2);
129: assertTrue(d1.getClass() == d2.getClass());
130: assertTrue(d1.equals(d2));
131: }
132:
133: /**
134: * Serialize an instance, restore it, and check for equality.
135: */
136: public void testSerialization() {
137:
138: DefaultTableXYDataset d1 = new DefaultTableXYDataset();
139: XYSeries s1 = new XYSeries("Series 1", true, false);
140: s1.add(1.0, 1.1);
141: s1.add(2.0, 2.2);
142: d1.addSeries(s1);
143:
144: DefaultTableXYDataset d2 = null;
145:
146: try {
147: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
148: ObjectOutput out = new ObjectOutputStream(buffer);
149: out.writeObject(d1);
150: out.close();
151:
152: ObjectInput in = new ObjectInputStream(
153: new ByteArrayInputStream(buffer.toByteArray()));
154: d2 = (DefaultTableXYDataset) in.readObject();
155: in.close();
156: } catch (Exception e) {
157: e.printStackTrace();
158: }
159: assertEquals(d1, d2);
160:
161: }
162:
163: private static final double EPSILON = 0.0000000001;
164:
165: /**
166: * This is a test for bug 1312066 - adding a new series should trigger a
167: * recalculation of the interval width, if it is being automatically
168: * calculated.
169: */
170: public void testAddSeries() {
171: DefaultTableXYDataset d1 = new DefaultTableXYDataset();
172: d1.setAutoWidth(true);
173: XYSeries s1 = new XYSeries("Series 1", true, false);
174: s1.add(3.0, 1.1);
175: s1.add(7.0, 2.2);
176: d1.addSeries(s1);
177: assertEquals(3.0, d1.getXValue(0, 0), EPSILON);
178: assertEquals(7.0, d1.getXValue(0, 1), EPSILON);
179: assertEquals(1.0, d1.getStartXValue(0, 0), EPSILON);
180: assertEquals(5.0, d1.getStartXValue(0, 1), EPSILON);
181: assertEquals(5.0, d1.getEndXValue(0, 0), EPSILON);
182: assertEquals(9.0, d1.getEndXValue(0, 1), EPSILON);
183:
184: // now add another series
185: XYSeries s2 = new XYSeries("Series 2", true, false);
186: s2.add(7.5, 1.1);
187: s2.add(9.0, 2.2);
188: d1.addSeries(s2);
189:
190: assertEquals(3.0, d1.getXValue(1, 0), EPSILON);
191: assertEquals(7.0, d1.getXValue(1, 1), EPSILON);
192: assertEquals(7.5, d1.getXValue(1, 2), EPSILON);
193: assertEquals(9.0, d1.getXValue(1, 3), EPSILON);
194:
195: assertEquals(7.25, d1.getStartXValue(1, 2), EPSILON);
196: assertEquals(8.75, d1.getStartXValue(1, 3), EPSILON);
197: assertEquals(7.75, d1.getEndXValue(1, 2), EPSILON);
198: assertEquals(9.25, d1.getEndXValue(1, 3), EPSILON);
199:
200: // and check the first series too...
201: assertEquals(2.75, d1.getStartXValue(0, 0), EPSILON);
202: assertEquals(6.75, d1.getStartXValue(0, 1), EPSILON);
203: assertEquals(3.25, d1.getEndXValue(0, 0), EPSILON);
204: assertEquals(7.25, d1.getEndXValue(0, 1), EPSILON);
205: }
206:
207: /**
208: * Some basic checks for the getSeries() method.
209: */
210: public void testGetSeries() {
211: DefaultTableXYDataset d1 = new DefaultTableXYDataset();
212: XYSeries s1 = new XYSeries("Series 1", true, false);
213: d1.addSeries(s1);
214: assertEquals("Series 1", d1.getSeries(0).getKey());
215:
216: boolean pass = false;
217: try {
218: d1.getSeries(-1);
219: } catch (IllegalArgumentException e) {
220: pass = true;
221: }
222: assertTrue(pass);
223:
224: pass = false;
225: try {
226: d1.getSeries(1);
227: } catch (IllegalArgumentException e) {
228: pass = true;
229: }
230: assertTrue(pass);
231: }
232:
233: }
|