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: * XYIntervalSeriesTests.java
029: * --------------------------
030: * (C) Copyright 2006, 2007, by Object Refinery Limited and Contributors.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: XYIntervalSeriesTests.java,v 1.1.2.2 2007/02/13 16:12:46 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 20-Oct-2006 : Version 1, based on XYSeriesTests (DG);
040: * 13-Feb-2007 : Added testValues() (DG);
041: *
042: */
043:
044: package org.jfree.data.xy.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: import org.jfree.data.xy.XYIntervalSeries;
057:
058: /**
059: * Tests for the {@link XYIntervalSeries} class.
060: */
061: public class XYIntervalSeriesTests 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(XYIntervalSeriesTests.class);
070: }
071:
072: /**
073: * Constructs a new set of tests.
074: *
075: * @param name the name of the tests.
076: */
077: public XYIntervalSeriesTests(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: XYIntervalSeries s1 = new XYIntervalSeries("s1");
087: XYIntervalSeries s2 = new XYIntervalSeries("s1");
088: assertTrue(s1.equals(s2));
089:
090: // seriesKey
091: s1 = new XYIntervalSeries("s2");
092: assertFalse(s1.equals(s2));
093: s2 = new XYIntervalSeries("s2");
094: assertTrue(s1.equals(s2));
095:
096: // autoSort
097: s1 = new XYIntervalSeries("s2", false, true);
098: assertFalse(s1.equals(s2));
099: s2 = new XYIntervalSeries("s2", false, true);
100: assertTrue(s1.equals(s2));
101:
102: // allowDuplicateValues
103: s1 = new XYIntervalSeries("s2", false, false);
104: assertFalse(s1.equals(s2));
105: s2 = new XYIntervalSeries("s2", false, false);
106: assertTrue(s1.equals(s2));
107:
108: // add a value
109: s1.add(1.0, 0.5, 1.5, 2.0, 1.9, 2.1);
110: assertFalse(s1.equals(s2));
111: s2.add(1.0, 0.5, 1.5, 2.0, 1.9, 2.1);
112: assertTrue(s2.equals(s1));
113:
114: // add another value
115: s1.add(2.0, 0.5, 1.5, 2.0, 1.9, 2.1);
116: assertFalse(s1.equals(s2));
117: s2.add(2.0, 0.5, 1.5, 2.0, 1.9, 2.1);
118: assertTrue(s2.equals(s1));
119:
120: // remove a value
121: s1.remove(new Double(1.0));
122: assertFalse(s1.equals(s2));
123: s2.remove(new Double(1.0));
124: assertTrue(s2.equals(s1));
125:
126: }
127:
128: /**
129: * Confirm that cloning works.
130: */
131: public void testCloning() {
132: XYIntervalSeries s1 = new XYIntervalSeries("s1");
133: s1.add(1.0, 0.5, 1.5, 2.0, 1.9, 2.01);
134: XYIntervalSeries s2 = null;
135: try {
136: s2 = (XYIntervalSeries) s1.clone();
137: } catch (CloneNotSupportedException e) {
138: e.printStackTrace();
139: }
140: assertTrue(s1 != s2);
141: assertTrue(s1.getClass() == s2.getClass());
142: assertTrue(s1.equals(s2));
143: }
144:
145: /**
146: * Serialize an instance, restore it, and check for equality.
147: */
148: public void testSerialization() {
149:
150: XYIntervalSeries s1 = new XYIntervalSeries("s1");
151: s1.add(1.0, 0.5, 1.5, 2.0, 1.9, 2.1);
152: XYIntervalSeries s2 = null;
153:
154: try {
155: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
156: ObjectOutput out = new ObjectOutputStream(buffer);
157: out.writeObject(s1);
158: out.close();
159:
160: ObjectInput in = new ObjectInputStream(
161: new ByteArrayInputStream(buffer.toByteArray()));
162: s2 = (XYIntervalSeries) in.readObject();
163: in.close();
164: } catch (Exception e) {
165: e.printStackTrace();
166: }
167: assertEquals(s1, s2);
168:
169: }
170:
171: /**
172: * Simple test for the indexOf() method.
173: */
174: public void testIndexOf() {
175: XYIntervalSeries s1 = new XYIntervalSeries("Series 1");
176: s1.add(1.0, 1.0, 1.0, 2.0, 1.9, 2.1);
177: s1.add(2.0, 2.0, 2.0, 3.0, 2.9, 3.1);
178: s1.add(3.0, 3.0, 3.0, 4.0, 3.9, 4.1);
179: assertEquals(0, s1.indexOf(new Double(1.0)));
180: }
181:
182: /**
183: * A check for the indexOf() method for an unsorted series.
184: */
185: public void testIndexOf2() {
186: XYIntervalSeries s1 = new XYIntervalSeries("Series 1", false,
187: true);
188: s1.add(1.0, 1.0, 1.0, 2.0, 1.9, 2.1);
189: s1.add(3.0, 3.0, 3.0, 3.0, 2.9, 3.1);
190: s1.add(2.0, 2.0, 2.0, 2.0, 1.9, 2.1);
191: assertEquals(0, s1.indexOf(new Double(1.0)));
192: assertEquals(1, s1.indexOf(new Double(3.0)));
193: assertEquals(2, s1.indexOf(new Double(2.0)));
194: }
195:
196: /**
197: * Simple test for the remove() method.
198: */
199: public void testRemove() {
200: XYIntervalSeries s1 = new XYIntervalSeries("Series 1");
201: s1.add(1.0, 1.0, 1.0, 2.0, 1.9, 2.1);
202: s1.add(2.0, 2.0, 2.0, 2.0, 1.9, 2.1);
203: s1.add(3.0, 3.0, 3.0, 3.0, 2.9, 3.1);
204: assertEquals(3, s1.getItemCount());
205:
206: s1.remove(new Double(2.0));
207: assertEquals(new Double(3.0), s1.getX(1));
208:
209: s1.remove(new Double(1.0));
210: assertEquals(new Double(3.0), s1.getX(0));
211: }
212:
213: private static final double EPSILON = 0.0000000001;
214:
215: /**
216: * When items are added with duplicate x-values, we expect them to remain
217: * in the order they were added.
218: */
219: public void testAdditionOfDuplicateXValues() {
220: XYIntervalSeries s1 = new XYIntervalSeries("Series 1");
221: s1.add(1.0, 1.0, 1.0, 1.0, 1.0, 1.0);
222: s1.add(2.0, 2.0, 2.0, 2.0, 2.0, 2.0);
223: s1.add(2.0, 3.0, 3.0, 3.0, 3.0, 3.0);
224: s1.add(2.0, 4.0, 4.0, 4.0, 4.0, 4.0);
225: s1.add(3.0, 5.0, 5.0, 5.0, 5.0, 5.0);
226: assertEquals(1.0, s1.getYValue(0), EPSILON);
227: assertEquals(2.0, s1.getYValue(1), EPSILON);
228: assertEquals(3.0, s1.getYValue(2), EPSILON);
229: assertEquals(4.0, s1.getYValue(3), EPSILON);
230: assertEquals(5.0, s1.getYValue(4), EPSILON);
231: }
232:
233: /**
234: * Some checks for the add() method for an UNSORTED series.
235: */
236: public void testAdd() {
237: XYIntervalSeries series = new XYIntervalSeries("Series", false,
238: true);
239: series.add(5.0, 5.50, 5.50, 5.50, 5.50, 5.50);
240: series.add(5.1, 5.51, 5.51, 5.51, 5.51, 5.51);
241: series.add(6.0, 6.6, 6.6, 6.6, 6.6, 6.6);
242: series.add(3.0, 3.3, 3.3, 3.3, 3.3, 3.3);
243: series.add(4.0, 4.4, 4.4, 4.4, 4.4, 4.4);
244: series.add(2.0, 2.2, 2.2, 2.2, 2.2, 2.2);
245: series.add(1.0, 1.1, 1.1, 1.1, 1.1, 1.1);
246: assertEquals(5.5, series.getYValue(0), EPSILON);
247: assertEquals(5.51, series.getYValue(1), EPSILON);
248: assertEquals(6.6, series.getYValue(2), EPSILON);
249: assertEquals(3.3, series.getYValue(3), EPSILON);
250: assertEquals(4.4, series.getYValue(4), EPSILON);
251: assertEquals(2.2, series.getYValue(5), EPSILON);
252: assertEquals(1.1, series.getYValue(6), EPSILON);
253: }
254:
255: /**
256: * A simple check that the maximumItemCount attribute is working.
257: */
258: public void testSetMaximumItemCount() {
259: XYIntervalSeries s1 = new XYIntervalSeries("S1");
260: assertEquals(Integer.MAX_VALUE, s1.getMaximumItemCount());
261: s1.setMaximumItemCount(2);
262: assertEquals(2, s1.getMaximumItemCount());
263: s1.add(1.0, 1.1, 1.1, 1.1, 1.1, 1.1);
264: s1.add(2.0, 2.2, 2.2, 2.2, 2.2, 2.2);
265: s1.add(3.0, 3.3, 3.3, 3.3, 3.3, 3.3);
266: assertEquals(2.0, s1.getX(0).doubleValue(), EPSILON);
267: assertEquals(3.0, s1.getX(1).doubleValue(), EPSILON);
268: }
269:
270: /**
271: * Check that the maximum item count can be applied retrospectively.
272: */
273: public void testSetMaximumItemCount2() {
274: XYIntervalSeries s1 = new XYIntervalSeries("S1");
275: s1.add(1.0, 1.1, 1.1, 1.1, 1.1, 1.1);
276: s1.add(2.0, 2.2, 2.2, 2.2, 2.2, 2.2);
277: s1.add(3.0, 3.3, 3.3, 3.3, 2.2, 2.2);
278: s1.setMaximumItemCount(2);
279: assertEquals(2.0, s1.getX(0).doubleValue(), EPSILON);
280: assertEquals(3.0, s1.getX(1).doubleValue(), EPSILON);
281: }
282:
283: /**
284: * Some checks for the new accessor methods added in 1.0.5.
285: */
286: public void testValues() {
287: XYIntervalSeries s1 = new XYIntervalSeries("S1");
288: s1.add(2.0, 1.0, 3.0, 5.0, 4.0, 6.0);
289: assertEquals(2.0, s1.getX(0).doubleValue(), EPSILON);
290: assertEquals(1.0, s1.getXLowValue(0), EPSILON);
291: assertEquals(3.0, s1.getXHighValue(0), EPSILON);
292: assertEquals(5.0, s1.getYValue(0), EPSILON);
293: assertEquals(4.0, s1.getYLowValue(0), EPSILON);
294: assertEquals(6.0, s1.getYHighValue(0), EPSILON);
295: }
296:
297: }
|