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: * XYSeriesCollectionTests.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: XYSeriesCollectionTests.java,v 1.1.2.4 2007/05/08 10:58:51 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 18-May-2003 : Version 1 (DG);
040: * 27-Nov-2006 : Updated testCloning() (DG);
041: * 08-Mar-2007 : Added testGetSeries() and testRemoveSeries() (DG);
042: * 08-May-2007 : Added testIndexOf() (DG);
043: *
044: */
045:
046: package org.jfree.data.xy.junit;
047:
048: import java.io.ByteArrayInputStream;
049: import java.io.ByteArrayOutputStream;
050: import java.io.ObjectInput;
051: import java.io.ObjectInputStream;
052: import java.io.ObjectOutput;
053: import java.io.ObjectOutputStream;
054:
055: import junit.framework.Test;
056: import junit.framework.TestCase;
057: import junit.framework.TestSuite;
058:
059: import org.jfree.data.xy.XYSeries;
060: import org.jfree.data.xy.XYSeriesCollection;
061:
062: /**
063: * Tests for the {@link XYSeriesCollection} class.
064: */
065: public class XYSeriesCollectionTests 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(XYSeriesCollectionTests.class);
074: }
075:
076: /**
077: * Constructs a new set of tests.
078: *
079: * @param name the name of the tests.
080: */
081: public XYSeriesCollectionTests(String name) {
082: super (name);
083: }
084:
085: /**
086: * Confirm that the equals method can distinguish all the required fields.
087: */
088: public void testEquals() {
089: XYSeries s1 = new XYSeries("Series");
090: s1.add(1.0, 1.1);
091: XYSeriesCollection c1 = new XYSeriesCollection();
092: c1.addSeries(s1);
093: XYSeries s2 = new XYSeries("Series");
094: s2.add(1.0, 1.1);
095: XYSeriesCollection c2 = new XYSeriesCollection();
096: c2.addSeries(s2);
097: assertTrue(c1.equals(c2));
098: assertTrue(c2.equals(c1));
099:
100: c1.addSeries(new XYSeries("Empty Series"));
101: assertFalse(c1.equals(c2));
102:
103: c2.addSeries(new XYSeries("Empty Series"));
104: assertTrue(c1.equals(c2));
105: }
106:
107: /**
108: * Confirm that cloning works.
109: */
110: public void testCloning() {
111: XYSeries s1 = new XYSeries("Series");
112: s1.add(1.0, 1.1);
113: XYSeriesCollection c1 = new XYSeriesCollection();
114: c1.addSeries(s1);
115: XYSeriesCollection c2 = null;
116: try {
117: c2 = (XYSeriesCollection) c1.clone();
118: } catch (CloneNotSupportedException e) {
119: e.printStackTrace();
120: }
121: assertTrue(c1 != c2);
122: assertTrue(c1.getClass() == c2.getClass());
123: assertTrue(c1.equals(c2));
124:
125: // check independence
126: s1.setDescription("XYZ");
127: assertFalse(c1.equals(c2));
128: }
129:
130: /**
131: * Serialize an instance, restore it, and check for equality.
132: */
133: public void testSerialization() {
134: XYSeries s1 = new XYSeries("Series");
135: s1.add(1.0, 1.1);
136: XYSeriesCollection c1 = new XYSeriesCollection();
137: c1.addSeries(s1);
138: XYSeriesCollection c2 = null;
139:
140: try {
141: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
142: ObjectOutput out = new ObjectOutputStream(buffer);
143: out.writeObject(c1);
144: out.close();
145:
146: ObjectInput in = new ObjectInputStream(
147: new ByteArrayInputStream(buffer.toByteArray()));
148: c2 = (XYSeriesCollection) in.readObject();
149: in.close();
150: } catch (Exception e) {
151: e.printStackTrace();
152: }
153: assertEquals(c1, c2);
154: }
155:
156: /**
157: * A test for bug report 1170825.
158: */
159: public void test1170825() {
160: XYSeries s1 = new XYSeries("Series1");
161: XYSeriesCollection dataset = new XYSeriesCollection();
162: dataset.addSeries(s1);
163: try {
164: /* XYSeries s = */dataset.getSeries(1);
165: } catch (IllegalArgumentException e) {
166: // correct outcome
167: } catch (IndexOutOfBoundsException e) {
168: assertTrue(false); // wrong outcome
169: }
170: }
171:
172: /**
173: * Some basic checks for the getSeries() method.
174: */
175: public void testGetSeries() {
176: XYSeriesCollection c = new XYSeriesCollection();
177: XYSeries s1 = new XYSeries("s1");
178: c.addSeries(s1);
179: assertEquals("s1", c.getSeries(0).getKey());
180:
181: boolean pass = false;
182: try {
183: c.getSeries(-1);
184: } catch (IllegalArgumentException e) {
185: pass = true;
186: }
187: assertTrue(pass);
188:
189: pass = false;
190: try {
191: c.getSeries(1);
192: } catch (IllegalArgumentException e) {
193: pass = true;
194: }
195: assertTrue(pass);
196: }
197:
198: /**
199: * Some basic checks for the removeSeries() method.
200: */
201: public void testRemoveSeries() {
202: XYSeriesCollection c = new XYSeriesCollection();
203: XYSeries s1 = new XYSeries("s1");
204: c.addSeries(s1);
205: c.removeSeries(0);
206: assertEquals(0, c.getSeriesCount());
207: c.addSeries(s1);
208:
209: boolean pass = false;
210: try {
211: c.removeSeries(-1);
212: } catch (IllegalArgumentException e) {
213: pass = true;
214: }
215: assertTrue(pass);
216:
217: pass = false;
218: try {
219: c.removeSeries(1);
220: } catch (IllegalArgumentException e) {
221: pass = true;
222: }
223: assertTrue(pass);
224: }
225:
226: /**
227: * Some tests for the indexOf() method.
228: */
229: public void testIndexOf() {
230: XYSeries s1 = new XYSeries("S1");
231: XYSeries s2 = new XYSeries("S2");
232: XYSeriesCollection dataset = new XYSeriesCollection();
233: assertEquals(-1, dataset.indexOf(s1));
234: assertEquals(-1, dataset.indexOf(s2));
235:
236: dataset.addSeries(s1);
237: assertEquals(0, dataset.indexOf(s1));
238: assertEquals(-1, dataset.indexOf(s2));
239:
240: dataset.addSeries(s2);
241: assertEquals(0, dataset.indexOf(s1));
242: assertEquals(1, dataset.indexOf(s2));
243:
244: dataset.removeSeries(s1);
245: assertEquals(-1, dataset.indexOf(s1));
246: assertEquals(0, dataset.indexOf(s2));
247:
248: XYSeries s2b = new XYSeries("S2");
249: assertEquals(0, dataset.indexOf(s2b));
250: }
251:
252: }
|