KickJava   Java API By Example, From Geeks To Geeks.

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


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  * IntervalXYDelegateTests.java
29  * ----------------------------
30  * (C) Copyright 2005, by Object Refinery Limited and Contributors.
31  *
32  * Original Author: David Gilbert (for Object Refinery Limited);
33  * Contributor(s): -;
34  *
35  * $Id: IntervalXYDelegateTests.java,v 1.1.2.1 2006/10/03 15:41:38 mungady Exp $
36  *
37  * Changes
38  * -------
39  * 21-Feb-2005 : Version 1 (DG);
40  * 06-Oct-2005 : Updated for testEquals() for method name change (DG);
41  *
42  */

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

64 public class IntervalXYDelegateTests extends TestCase {
65
66     /**
67      * Returns the tests as a test suite.
68      *
69      * @return The test suite.
70      */

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

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

87     public void testEquals() {
88        XYSeries s1 = new XYSeries("Series");
89        s1.add(1.2, 3.4);
90        XYSeriesCollection c1 = new XYSeriesCollection();
91        c1.addSeries(s1);
92        IntervalXYDelegate d1 = new IntervalXYDelegate(c1);
93
94        XYSeries s2 = new XYSeries("Series");
95        XYSeriesCollection c2 = new XYSeriesCollection();
96        s2.add(1.2, 3.4);
97        c2.addSeries(s2);
98        IntervalXYDelegate d2 = new IntervalXYDelegate(c2);
99        
100        assertTrue(d1.equals(d2));
101        assertTrue(d2.equals(d1));
102        
103        d1.setAutoWidth(false);
104        assertFalse(d1.equals(d2));
105        d2.setAutoWidth(false);
106        assertTrue(d1.equals(d2));
107        
108        d1.setIntervalPositionFactor(0.123);
109        assertFalse(d1.equals(d2));
110        d2.setIntervalPositionFactor(0.123);
111        assertTrue(d1.equals(d2));
112       
113        d1.setFixedIntervalWidth(1.23);
114        assertFalse(d1.equals(d2));
115        d2.setFixedIntervalWidth(1.23);
116        assertTrue(d1.equals(d2));
117     }
118
119     /**
120      * Confirm that cloning works.
121      */

122     public void testCloning() {
123         XYSeries s1 = new XYSeries("Series");
124         s1.add(1.2, 3.4);
125         XYSeriesCollection c1 = new XYSeriesCollection();
126         c1.addSeries(s1);
127         IntervalXYDelegate d1 = new IntervalXYDelegate(c1);
128         
129         IntervalXYDelegate d2 = null;
130         try {
131             d2 = (IntervalXYDelegate) d1.clone();
132         }
133         catch (CloneNotSupportedException JavaDoc e) {
134             System.err.println("Failed to clone.");
135         }
136         assertTrue(d1 != d2);
137         assertTrue(d1.getClass() == d2.getClass());
138         assertTrue(d1.equals(d2));
139     }
140
141     /**
142      * Serialize an instance, restore it, and check for equality.
143      */

144     public void testSerialization() {
145         XYSeries s1 = new XYSeries("Series");
146         s1.add(1.2, 3.4);
147         XYSeriesCollection c1 = new XYSeriesCollection();
148         c1.addSeries(s1);
149         IntervalXYDelegate d1 = new IntervalXYDelegate(c1);
150         IntervalXYDelegate d2 = null;
151         try {
152             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
153             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
154             out.writeObject(d1);
155             out.close();
156
157             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
158                 new ByteArrayInputStream JavaDoc(buffer.toByteArray())
159             );
160             d2 = (IntervalXYDelegate) in.readObject();
161             in.close();
162         }
163         catch (Exception JavaDoc e) {
164             System.out.println(e.toString());
165         }
166         assertEquals(d1, d2);
167
168     }
169
170 }
171
Popular Tags