KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

77     public YIntervalDataItemTests(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         YIntervalDataItem item1 = new YIntervalDataItem(1.0, 2.0, 3.0, 4.0);
88         assertEquals(new Double JavaDoc(1.0), item1.getX());
89         assertEquals(2.0, item1.getYValue(), EPSILON);
90         assertEquals(3.0, item1.getYLowValue(), EPSILON);
91         assertEquals(4.0, item1.getYHighValue(), EPSILON);
92     }
93     
94     /**
95      * Confirm that the equals method can distinguish all the required fields.
96      */

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

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

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