KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* ===========================================================
2  * JFreeChart : a free chart library for the Java(tm) platform
3  * ===========================================================
4  *
5  * (C) Copyright 2000-2006, 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  * XIntervalDataItemTests.java
29  * ---------------------------
30  * (C) Copyright 2006, by Object Refinery Limited and Contributors.
31  *
32  * Original Author: David Gilbert (for Object Refinery Limited);
33  * Contributor(s): -;
34  *
35  * $Id: XIntervalDataItemTests.java,v 1.1.2.2 2006/10/21 05:11:04 mungady Exp $
36  *
37  * Changes
38  * -------
39  * 20-Oct-2006 : 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.XIntervalDataItem;
57
58 /**
59  * Tests for the {@link XIntervalDataItem} class.
60  */

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

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

77     public XIntervalDataItemTests(String JavaDoc name) {
78         super(name);
79     }
80
81     private static final double EPSILON = 0.00000000001;
82     
83     /**
84      * Some checks for the constructor.
85      */

86     public void testConstructor1() {
87         XIntervalDataItem item1 = new XIntervalDataItem(1.0, 2.0, 3.0, 4.0);
88         assertEquals(new Double JavaDoc(1.0), item1.getX());
89         assertEquals(2.0, item1.getXLowValue(), EPSILON);
90         assertEquals(3.0, item1.getXHighValue(), EPSILON);
91         assertEquals(4.0, item1.getYValue(), EPSILON);
92     }
93     
94     /**
95      * Confirm that the equals method can distinguish all the required fields.
96      */

97     public void testEquals() {
98         XIntervalDataItem item1 = new XIntervalDataItem(1.0, 2.0, 3.0, 4.0);
99         XIntervalDataItem item2 = new XIntervalDataItem(1.0, 2.0, 3.0, 4.0);
100         assertTrue(item1.equals(item2));
101         assertTrue(item2.equals(item1));
102
103         // x
104
item1 = new XIntervalDataItem(1.1, 2.0, 3.0, 4.0);
105         assertFalse(item1.equals(item2));
106         item2 = new XIntervalDataItem(1.1, 2.0, 3.0, 4.0);
107         assertTrue(item1.equals(item2));
108         
109         // xLow
110
item1 = new XIntervalDataItem(1.1, 2.2, 3.0, 4.0);
111         assertFalse(item1.equals(item2));
112         item2 = new XIntervalDataItem(1.1, 2.2, 3.0, 4.0);
113         assertTrue(item1.equals(item2));
114
115         // xHigh
116
item1 = new XIntervalDataItem(1.1, 2.2, 3.3, 4.0);
117         assertFalse(item1.equals(item2));
118         item2 = new XIntervalDataItem(1.1, 2.2, 3.3, 4.0);
119         assertTrue(item1.equals(item2));
120
121         // y
122
item1 = new XIntervalDataItem(1.1, 2.2, 3.3, 4.4);
123         assertFalse(item1.equals(item2));
124         item2 = new XIntervalDataItem(1.1, 2.2, 3.3, 4.4);
125         assertTrue(item1.equals(item2));
126
127     }
128
129     /**
130      * Some checks for the clone() method.
131      */

132     public void testCloning() {
133         XIntervalDataItem item1 = new XIntervalDataItem(1.0, 2.0, 3.0, 4.0);
134         XIntervalDataItem item2 = null;
135         try {
136             item2 = (XIntervalDataItem) item1.clone();
137         }
138         catch (CloneNotSupportedException JavaDoc e) {
139             e.printStackTrace();
140         }
141         assertTrue(item1 != item2);
142         assertTrue(item1.getClass() == item2.getClass());
143         assertTrue(item1.equals(item2));
144     }
145
146     /**
147      * Serialize an instance, restore it, and check for equality.
148      */

149     public void testSerialization() {
150         XIntervalDataItem item1 = new XIntervalDataItem(1.0, 2.0, 3.0, 4.0);
151         XIntervalDataItem item2 = null;
152         try {
153             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
154             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
155             out.writeObject(item1);
156             out.close();
157
158             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
159                     new ByteArrayInputStream JavaDoc(buffer.toByteArray()));
160             item2 = (XIntervalDataItem) in.readObject();
161             in.close();
162         }
163         catch (Exception JavaDoc e) {
164             e.printStackTrace();
165         }
166         assertEquals(item1, item2);
167     }
168
169 }
170
Popular Tags