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: * VectorSeriesTests.java
029: * ----------------------
030: * (C) Copyright 2007, by Object Refinery Limited and Contributors.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: VectorSeriesTests.java,v 1.1.2.1 2007/06/13 10:28:02 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 30-Jan-2007 : Version 1, based on XYSeriesTests (DG);
040: * 24-May-2007 : Updated for modified method names (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:
057: import org.jfree.data.xy.VectorSeries;
058: import org.jfree.data.xy.XYCoordinate;
059:
060: /**
061: * Tests for the {@link VectorSeries} class.
062: */
063: public class VectorSeriesTests extends TestCase {
064:
065: /**
066: * Returns the tests as a test suite.
067: *
068: * @return The test suite.
069: */
070: public static Test suite() {
071: return new TestSuite(VectorSeriesTests.class);
072: }
073:
074: /**
075: * Constructs a new set of tests.
076: *
077: * @param name the name of the tests.
078: */
079: public VectorSeriesTests(String name) {
080: super (name);
081: }
082:
083: /**
084: * Confirm that the equals method can distinguish all the required fields.
085: */
086: public void testEquals() {
087:
088: VectorSeries s1 = new VectorSeries("s1");
089: VectorSeries s2 = new VectorSeries("s1");
090: assertTrue(s1.equals(s2));
091:
092: // seriesKey
093: s1 = new VectorSeries("s2");
094: assertFalse(s1.equals(s2));
095: s2 = new VectorSeries("s2");
096: assertTrue(s1.equals(s2));
097:
098: // autoSort
099: s1 = new VectorSeries("s2", true, true);
100: assertFalse(s1.equals(s2));
101: s2 = new VectorSeries("s2", true, true);
102: assertTrue(s1.equals(s2));
103:
104: // allowDuplicateValues
105: s1 = new VectorSeries("s2", false, false);
106: assertFalse(s1.equals(s2));
107: s2 = new VectorSeries("s2", false, false);
108: assertTrue(s1.equals(s2));
109:
110: // add a value
111: s1.add(1.0, 0.5, 1.5, 2.0);
112: assertFalse(s1.equals(s2));
113: s2.add(1.0, 0.5, 1.5, 2.0);
114: assertTrue(s2.equals(s1));
115:
116: // add another value
117: s1.add(2.0, 0.5, 1.5, 2.0);
118: assertFalse(s1.equals(s2));
119: s2.add(2.0, 0.5, 1.5, 2.0);
120: assertTrue(s2.equals(s1));
121:
122: // remove a value
123: s1.remove(new XYCoordinate(1.0, 0.5));
124: assertFalse(s1.equals(s2));
125: s2.remove(new XYCoordinate(1.0, 0.5));
126: assertTrue(s2.equals(s1));
127:
128: }
129:
130: /**
131: * Confirm that cloning works.
132: */
133: public void testCloning() {
134: VectorSeries s1 = new VectorSeries("s1");
135: s1.add(1.0, 0.5, 1.5, 2.0);
136: VectorSeries s2 = null;
137: try {
138: s2 = (VectorSeries) s1.clone();
139: } catch (CloneNotSupportedException e) {
140: e.printStackTrace();
141: }
142: assertTrue(s1 != s2);
143: assertTrue(s1.getClass() == s2.getClass());
144: assertTrue(s1.equals(s2));
145: }
146:
147: /**
148: * Serialize an instance, restore it, and check for equality.
149: */
150: public void testSerialization() {
151: VectorSeries s1 = new VectorSeries("s1");
152: s1.add(1.0, 0.5, 1.5, 2.0);
153: VectorSeries s2 = null;
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 = (VectorSeries) 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: VectorSeries s1 = new VectorSeries("Series 1");
176: s1.add(1.0, 1.0, 1.0, 2.0);
177: s1.add(2.0, 2.0, 2.0, 3.0);
178: s1.add(3.0, 3.0, 3.0, 4.0);
179: assertEquals(0, s1.indexOf(new XYCoordinate(1.0, 1.0)));
180: }
181:
182: /**
183: * A check for the indexOf() method for an unsorted series.
184: */
185: public void testIndexOf2() {
186: VectorSeries s1 = new VectorSeries("Series 1");
187: s1.add(1.0, 1.0, 1.0, 2.0);
188: s1.add(3.0, 3.0, 3.0, 3.0);
189: s1.add(2.0, 2.0, 2.0, 2.0);
190: assertEquals(0, s1.indexOf(new XYCoordinate(1.0, 1.0)));
191: assertEquals(1, s1.indexOf(new XYCoordinate(3.0, 3.0)));
192: assertEquals(2, s1.indexOf(new XYCoordinate(2.0, 2.0)));
193: }
194:
195: /**
196: * Simple test for the remove() method.
197: */
198: public void testRemove() {
199: VectorSeries s1 = new VectorSeries("Series 1");
200: s1.add(1.0, 1.0, 1.0, 2.0);
201: s1.add(3.0, 3.0, 3.0, 3.0);
202: s1.add(2.0, 2.0, 2.0, 2.0);
203: assertEquals(3, s1.getItemCount());
204:
205: s1.remove(new XYCoordinate(2.0, 2.0));
206: assertEquals(3.0, s1.getXValue(1), EPSILON);
207:
208: s1.remove(new XYCoordinate(1.0, 1.0));
209: assertEquals(3.0, s1.getXValue(0), EPSILON);
210: }
211:
212: private static final double EPSILON = 0.0000000001;
213:
214: /**
215: * When items are added with duplicate x-values, we expect them to remain
216: * in the order they were added.
217: */
218: public void testAdditionOfDuplicateXValues() {
219: VectorSeries s1 = new VectorSeries("Series 1");
220: s1.add(1.0, 1.0, 1.0, 1.0);
221: s1.add(2.0, 2.0, 2.0, 2.0);
222: s1.add(2.0, 2.0, 3.0, 3.0);
223: s1.add(2.0, 3.0, 4.0, 4.0);
224: s1.add(3.0, 5.0, 5.0, 5.0);
225: assertEquals(1.0, s1.getVectorXValue(0), EPSILON);
226: assertEquals(2.0, s1.getVectorXValue(1), EPSILON);
227: assertEquals(3.0, s1.getVectorXValue(2), EPSILON);
228: assertEquals(4.0, s1.getVectorXValue(3), EPSILON);
229: assertEquals(5.0, s1.getVectorXValue(4), EPSILON);
230: }
231:
232: /**
233: * Some checks for the add() method for an UNSORTED series.
234: */
235: public void testAdd() {
236: VectorSeries series = new VectorSeries("Series", false, true);
237: series.add(5.0, 5.50, 5.50, 5.50);
238: series.add(5.1, 5.51, 5.51, 5.51);
239: series.add(6.0, 6.6, 6.6, 6.6);
240: series.add(3.0, 3.3, 3.3, 3.3);
241: series.add(4.0, 4.4, 4.4, 4.4);
242: series.add(2.0, 2.2, 2.2, 2.2);
243: series.add(1.0, 1.1, 1.1, 1.1);
244: assertEquals(5.5, series.getVectorXValue(0), EPSILON);
245: assertEquals(5.51, series.getVectorXValue(1), EPSILON);
246: assertEquals(6.6, series.getVectorXValue(2), EPSILON);
247: assertEquals(3.3, series.getVectorXValue(3), EPSILON);
248: assertEquals(4.4, series.getVectorXValue(4), EPSILON);
249: assertEquals(2.2, series.getVectorXValue(5), EPSILON);
250: assertEquals(1.1, series.getVectorXValue(6), EPSILON);
251: }
252:
253: /**
254: * A simple check that the maximumItemCount attribute is working.
255: */
256: public void testSetMaximumItemCount() {
257: VectorSeries s1 = new VectorSeries("S1");
258: assertEquals(Integer.MAX_VALUE, s1.getMaximumItemCount());
259: s1.setMaximumItemCount(2);
260: assertEquals(2, s1.getMaximumItemCount());
261: s1.add(1.0, 1.1, 1.1, 1.1);
262: s1.add(2.0, 2.2, 2.2, 2.2);
263: s1.add(3.0, 3.3, 3.3, 3.3);
264: assertEquals(2.0, s1.getXValue(0), EPSILON);
265: assertEquals(3.0, s1.getXValue(1), EPSILON);
266: }
267:
268: /**
269: * Check that the maximum item count can be applied retrospectively.
270: */
271: public void testSetMaximumItemCount2() {
272: VectorSeries s1 = new VectorSeries("S1");
273: s1.add(1.0, 1.1, 1.1, 1.1);
274: s1.add(2.0, 2.2, 2.2, 2.2);
275: s1.add(3.0, 3.3, 3.3, 3.3);
276: s1.setMaximumItemCount(2);
277: assertEquals(2.0, s1.getXValue(0), EPSILON);
278: assertEquals(3.0, s1.getXValue(1), EPSILON);
279: }
280:
281: }
|