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: * VectorSeriesCollectionTests.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: VectorSeriesCollectionTests.java,v 1.1.2.1 2007/06/13 10:28:02 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 30-Jan-2007 : Version 1 (DG);
040: * 24-May-2007 : Added testRemoveSeries() (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.VectorSeriesCollection;
059:
060: /**
061: * Tests for the {@link VectorSeriesCollection} class.
062: */
063: public class VectorSeriesCollectionTests 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(VectorSeriesCollectionTests.class);
072: }
073:
074: /**
075: * Constructs a new set of tests.
076: *
077: * @param name the name of the tests.
078: */
079: public VectorSeriesCollectionTests(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: VectorSeries s1 = new VectorSeries("Series");
088: s1.add(1.0, 1.1, 1.2, 1.3);
089: VectorSeriesCollection c1 = new VectorSeriesCollection();
090: c1.addSeries(s1);
091: VectorSeries s2 = new VectorSeries("Series");
092: s2.add(1.0, 1.1, 1.2, 1.3);
093: VectorSeriesCollection c2 = new VectorSeriesCollection();
094: c2.addSeries(s2);
095: assertTrue(c1.equals(c2));
096: assertTrue(c2.equals(c1));
097:
098: c1.addSeries(new VectorSeries("Empty Series"));
099: assertFalse(c1.equals(c2));
100:
101: c2.addSeries(new VectorSeries("Empty Series"));
102: assertTrue(c1.equals(c2));
103: }
104:
105: /**
106: * Confirm that cloning works.
107: */
108: public void testCloning() {
109: VectorSeries s1 = new VectorSeries("Series");
110: s1.add(1.0, 1.1, 1.2, 1.3);
111: VectorSeriesCollection c1 = new VectorSeriesCollection();
112: c1.addSeries(s1);
113: VectorSeriesCollection c2 = null;
114: try {
115: c2 = (VectorSeriesCollection) c1.clone();
116: } catch (CloneNotSupportedException e) {
117: e.printStackTrace();
118: }
119: assertTrue(c1 != c2);
120: assertTrue(c1.getClass() == c2.getClass());
121: assertTrue(c1.equals(c2));
122:
123: // check independence
124: s1.setDescription("XYZ");
125: assertFalse(c1.equals(c2));
126: }
127:
128: /**
129: * Serialize an instance, restore it, and check for equality.
130: */
131: public void testSerialization() {
132: VectorSeries s1 = new VectorSeries("Series");
133: s1.add(1.0, 1.1, 1.2, 1.3);
134: VectorSeriesCollection c1 = new VectorSeriesCollection();
135: c1.addSeries(s1);
136: VectorSeriesCollection c2 = null;
137:
138: try {
139: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
140: ObjectOutput out = new ObjectOutputStream(buffer);
141: out.writeObject(c1);
142: out.close();
143:
144: ObjectInput in = new ObjectInputStream(
145: new ByteArrayInputStream(buffer.toByteArray()));
146: c2 = (VectorSeriesCollection) in.readObject();
147: in.close();
148: } catch (Exception e) {
149: e.printStackTrace();
150: }
151: assertEquals(c1, c2);
152: }
153:
154: /**
155: * Some checks for the removeSeries() method.
156: */
157: public void testRemoveSeries() {
158: VectorSeries s1 = new VectorSeries("S1");
159: VectorSeries s2 = new VectorSeries("S2");
160: VectorSeriesCollection vsc = new VectorSeriesCollection();
161: vsc.addSeries(s1);
162: vsc.addSeries(s2);
163: assertEquals(2, vsc.getSeriesCount());
164: boolean b = vsc.removeSeries(s1);
165: assertTrue(b);
166: assertEquals(1, vsc.getSeriesCount());
167: assertEquals("S2", vsc.getSeriesKey(0));
168: b = vsc.removeSeries(new VectorSeries("NotInDataset"));
169: assertFalse(b);
170: assertEquals(1, vsc.getSeriesCount());
171: b = vsc.removeSeries(s2);
172: assertEquals(0, vsc.getSeriesCount());
173: }
174:
175: }
|