KickJava   Java API By Example, From Geeks To Geeks.

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


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  * DefaultOHLCDatasetTests.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: DefaultOHLCDatasetTests.java,v 1.1.2.1 2006/10/03 15:41:38 mungady Exp $
36  *
37  * Changes
38  * -------
39  * 29-Apr-2005 : 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 import java.util.Date JavaDoc;
52
53 import junit.framework.Test;
54 import junit.framework.TestCase;
55 import junit.framework.TestSuite;
56
57 import org.jfree.data.Range;
58 import org.jfree.data.general.DatasetUtilities;
59 import org.jfree.data.xy.DefaultOHLCDataset;
60 import org.jfree.data.xy.OHLCDataItem;
61
62 /**
63  * Tests for the {@link DefaultOHLCDataset} class.
64  */

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

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

81     public DefaultOHLCDatasetTests(String JavaDoc name) {
82         super(name);
83     }
84
85     private static final double EPSILON = 0.0000000001;
86     
87     /**
88      * A small test for the data range calculated on this dataset.
89      */

90     public void testDataRange() {
91         OHLCDataItem[] data = new OHLCDataItem[3];
92         data[0] = new OHLCDataItem(new Date JavaDoc(11L), 2.0, 4.0, 1.0, 3.0, 100.0);
93         data[1] = new OHLCDataItem(new Date JavaDoc(22L), 4.0, 9.0, 2.0, 5.0, 120.0);
94         data[2] = new OHLCDataItem(new Date JavaDoc(33L), 3.0, 7.0, 3.0, 6.0, 140.0);
95         DefaultOHLCDataset d = new DefaultOHLCDataset("S1", data);
96         Range r = DatasetUtilities.findRangeBounds(d, false);
97         assertEquals(1.0, r.getLowerBound(), EPSILON);
98         assertEquals(9.0, r.getUpperBound(), EPSILON);
99     }
100     
101     /**
102      * Confirm that the equals method can distinguish all the required fields.
103      */

104     public void testEquals() {
105         DefaultOHLCDataset d1 = new DefaultOHLCDataset("Series 1",
106                 new OHLCDataItem[0]);
107         DefaultOHLCDataset d2 = new DefaultOHLCDataset("Series 1",
108                 new OHLCDataItem[0]);
109         assertTrue(d1.equals(d2));
110         assertTrue(d2.equals(d1));
111     }
112
113     /**
114      * Confirm that cloning works.
115      */

116     public void testCloning() {
117         DefaultOHLCDataset d1 = new DefaultOHLCDataset("Series 1",
118                 new OHLCDataItem[0]);
119         DefaultOHLCDataset d2 = null;
120         try {
121             d2 = (DefaultOHLCDataset) d1.clone();
122         }
123         catch (CloneNotSupportedException JavaDoc e) {
124             e.printStackTrace();
125         }
126         assertTrue(d1 != d2);
127         assertTrue(d1.getClass() == d2.getClass());
128         assertTrue(d1.equals(d2));
129     }
130
131     /**
132      * Serialize an instance, restore it, and check for equality.
133      */

134     public void testSerialization() {
135         DefaultOHLCDataset d1 = new DefaultOHLCDataset("Series 1",
136                 new OHLCDataItem[0]);
137         DefaultOHLCDataset d2 = null;
138
139         try {
140             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
141             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
142             out.writeObject(d1);
143             out.close();
144
145             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
146                 new ByteArrayInputStream JavaDoc(buffer.toByteArray())
147             );
148             d2 = (DefaultOHLCDataset) in.readObject();
149             in.close();
150         }
151         catch (Exception JavaDoc e) {
152             System.out.println(e.toString());
153         }
154         assertEquals(d1, d2);
155     }
156
157 }
158
Popular Tags