KickJava   Java API By Example, From Geeks To Geeks.

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


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  * XYIntervalDataItemTests.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: XYIntervalDataItemTests.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.XYIntervalDataItem;
57
58 /**
59  * Tests for the {@link XYIntervalDataItem} class.
60  */

61 public class XYIntervalDataItemTests 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(XYIntervalDataItemTests.class);
70     }
71
72     /**
73      * Constructs a new set of tests.
74      *
75      * @param name the name of the tests.
76      */

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

85     public void testConstructor1() {
86         XYIntervalDataItem item1 = new XYIntervalDataItem(1.0, 0.5, 1.5, 2.0,
87                 1.9, 2.1);
88         assertEquals(new Double JavaDoc(1.0), item1.getX());
89         assertEquals(0.5, item1.getXLowValue(), EPSILON);
90         assertEquals(1.5, item1.getXHighValue(), EPSILON);
91         assertEquals(2.0, item1.getYValue(), EPSILON);
92         assertEquals(1.9, item1.getYLowValue(), EPSILON);
93         assertEquals(2.1, item1.getYHighValue(), EPSILON);
94     }
95     
96     /**
97      * Confirm that the equals method can distinguish all the required fields.
98      */

99     public void testEquals() {
100         XYIntervalDataItem item1 = new XYIntervalDataItem(1.0, 0.5, 1.5, 2.0,
101                 1.9, 2.1);
102         XYIntervalDataItem item2 = new XYIntervalDataItem(1.0, 0.5, 1.5, 2.0,
103                 1.9, 2.1);
104         assertTrue(item1.equals(item2));
105         assertTrue(item2.equals(item1));
106
107         // x
108
item1 = new XYIntervalDataItem(1.1, 0.5, 1.5, 2.0, 1.9, 2.1);
109         assertFalse(item1.equals(item2));
110         item2 = new XYIntervalDataItem(1.1, 0.5, 1.5, 2.0, 1.9, 2.1);
111         assertTrue(item1.equals(item2));
112         
113         // xLow
114
item1 = new XYIntervalDataItem(1.1, 0.55, 1.5, 2.0, 1.9, 2.1);
115         assertFalse(item1.equals(item2));
116         item2 = new XYIntervalDataItem(1.1, 0.55, 1.5, 2.0, 1.9, 2.1);
117         assertTrue(item1.equals(item2));
118
119         // xHigh
120
item1 = new XYIntervalDataItem(1.1, 0.55, 1.55, 2.0, 1.9, 2.1);
121         assertFalse(item1.equals(item2));
122         item2 = new XYIntervalDataItem(1.1, 0.55, 1.55, 2.0, 1.9, 2.1);
123         assertTrue(item1.equals(item2));
124
125         // y
126
item1 = new XYIntervalDataItem(1.1, 0.55, 1.55, 2.2, 1.9, 2.1);
127         assertFalse(item1.equals(item2));
128         item2 = new XYIntervalDataItem(1.1, 0.55, 1.55, 2.2, 1.9, 2.1);
129         assertTrue(item1.equals(item2));
130
131         // yLow
132
item1 = new XYIntervalDataItem(1.1, 0.55, 1.55, 2.2, 1.99, 2.1);
133         assertFalse(item1.equals(item2));
134         item2 = new XYIntervalDataItem(1.1, 0.55, 1.55, 2.2, 1.99, 2.1);
135         assertTrue(item1.equals(item2));
136
137         // yHigh
138
item1 = new XYIntervalDataItem(1.1, 0.55, 1.55, 2.2, 1.99, 2.11);
139         assertFalse(item1.equals(item2));
140         item2 = new XYIntervalDataItem(1.1, 0.55, 1.55, 2.2, 1.99, 2.11);
141         assertTrue(item1.equals(item2));
142     }
143
144     /**
145      * Some checks for the clone() method.
146      */

147     public void testCloning() {
148         XYIntervalDataItem item1 = new XYIntervalDataItem(1.0, 0.5, 1.5, 2.0,
149                 1.9, 2.1);
150         XYIntervalDataItem item2 = null;
151         try {
152             item2 = (XYIntervalDataItem) item1.clone();
153         }
154         catch (CloneNotSupportedException JavaDoc e) {
155             e.printStackTrace();
156         }
157         assertTrue(item1 != item2);
158         assertTrue(item1.getClass() == item2.getClass());
159         assertTrue(item1.equals(item2));
160     }
161
162     /**
163      * Serialize an instance, restore it, and check for equality.
164      */

165     public void testSerialization() {
166         XYIntervalDataItem item1 = new XYIntervalDataItem(1.0, 0.5, 1.5, 2.0,
167                 1.9, 2.1);
168         XYIntervalDataItem item2 = null;
169         try {
170             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
171             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
172             out.writeObject(item1);
173             out.close();
174
175             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
176                     new ByteArrayInputStream JavaDoc(buffer.toByteArray()));
177             item2 = (XYIntervalDataItem) in.readObject();
178             in.close();
179         }
180         catch (Exception JavaDoc e) {
181             e.printStackTrace();
182         }
183         assertEquals(item1, item2);
184     }
185
186 }
187
Popular Tags