KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > data > xy > junit > XYSeriesCollectionTests


1 /* ===========================================================
2  * JFreeChart : a free chart library for the Java(tm) platform
3  * ===========================================================
4  *
5  * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
6  *
7  * Project Info: http://www.jfree.org/jfreechart/index.html
8  *
9  * This library is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17  * License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22  * USA.
23  *
24  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
25  * in the United States and other countries.]
26  *
27  * ----------------------------
28  * XYSeriesCollectionTests.java
29  * ----------------------------
30  * (C) Copyright 2003-2005, by Object Refinery Limited and Contributors.
31  *
32  * Original Author: David Gilbert (for Object Refinery Limited);
33  * Contributor(s): -;
34  *
35  * $Id: XYSeriesCollectionTests.java,v 1.1.2.1 2006/10/03 15:41:38 mungady Exp $
36  *
37  * Changes
38  * -------
39  * 18-May-2003 : Version 1 (DG);
40  *
41  */

42
43 package org.jfree.data.xy.junit;
44
45 import java.io.ByteArrayInputStream JavaDoc;
46 import java.io.ByteArrayOutputStream JavaDoc;
47 import java.io.ObjectInput JavaDoc;
48 import java.io.ObjectInputStream JavaDoc;
49 import java.io.ObjectOutput JavaDoc;
50 import java.io.ObjectOutputStream JavaDoc;
51
52 import junit.framework.Test;
53 import junit.framework.TestCase;
54 import junit.framework.TestSuite;
55
56 import org.jfree.data.xy.XYSeries;
57 import org.jfree.data.xy.XYSeriesCollection;
58
59 /**
60  * Tests for the {@link XYSeriesCollection} class.
61  */

62 public class XYSeriesCollectionTests extends TestCase {
63
64     /**
65      * Returns the tests as a test suite.
66      *
67      * @return The test suite.
68      */

69     public static Test suite() {
70         return new TestSuite(XYSeriesCollectionTests.class);
71     }
72
73     /**
74      * Constructs a new set of tests.
75      *
76      * @param name the name of the tests.
77      */

78     public XYSeriesCollectionTests(String JavaDoc name) {
79         super(name);
80     }
81
82     /**
83      * Confirm that the equals method can distinguish all the required fields.
84      */

85     public void testEquals() {
86         
87         XYSeries s1 = new XYSeries("Series");
88         s1.add(1.0, 1.1);
89         XYSeriesCollection c1 = new XYSeriesCollection();
90         c1.addSeries(s1);
91         XYSeries s2 = new XYSeries("Series");
92         s2.add(1.0, 1.1);
93         XYSeriesCollection c2 = new XYSeriesCollection();
94         c2.addSeries(s2);
95         assertTrue(c1.equals(c2));
96         assertTrue(c2.equals(c1));
97
98         c1.addSeries(new XYSeries("Empty Series"));
99         assertFalse(c1.equals(c2));
100
101         c2.addSeries(new XYSeries("Empty Series"));
102         assertTrue(c1.equals(c2));
103
104     }
105
106     /**
107      * Confirm that cloning works.
108      */

109     public void testCloning() {
110         XYSeries s1 = new XYSeries("Series");
111         s1.add(1.0, 1.1);
112         XYSeriesCollection c1 = new XYSeriesCollection();
113         c1.addSeries(s1);
114         XYSeriesCollection c2 = null;
115         try {
116             c2 = (XYSeriesCollection) c1.clone();
117         }
118         catch (CloneNotSupportedException JavaDoc e) {
119             System.err.println("Failed to clone.");
120         }
121         assertTrue(c1 != c2);
122         assertTrue(c1.getClass() == c2.getClass());
123         assertTrue(c1.equals(c2));
124     }
125
126     /**
127      * Serialize an instance, restore it, and check for equality.
128      */

129     public void testSerialization() {
130         XYSeries s1 = new XYSeries("Series");
131         s1.add(1.0, 1.1);
132         XYSeriesCollection c1 = new XYSeriesCollection();
133         c1.addSeries(s1);
134         XYSeriesCollection c2 = null;
135         
136         try {
137             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
138             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
139             out.writeObject(c1);
140             out.close();
141
142             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
143                 new ByteArrayInputStream JavaDoc(buffer.toByteArray())
144             );
145             c2 = (XYSeriesCollection) in.readObject();
146             in.close();
147         }
148         catch (Exception JavaDoc e) {
149             System.out.println(e.toString());
150         }
151         assertEquals(c1, c2);
152
153     }
154     
155     /**
156      * A test for bug report 1170825.
157      */

158     public void test1170825() {
159         XYSeries s1 = new XYSeries("Series1");
160         XYSeriesCollection dataset = new XYSeriesCollection();
161         dataset.addSeries(s1);
162         try {
163             /* XYSeries s = */ dataset.getSeries(1);
164         }
165         catch (IllegalArgumentException JavaDoc e) {
166             // correct outcome
167
}
168         catch (IndexOutOfBoundsException JavaDoc e) {
169             assertTrue(false); // wrong outcome
170
}
171     }
172     
173 }
174
Popular Tags