KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > chart > renderer > xy > junit > CandlestickRendererTests


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  * CandlestickRendererTests.java
29  * -----------------------------
30  * (C) Copyright 2003-2006, by Object Refinery Limited and Contributors.
31  *
32  * Original Author: David Gilbert (for Object Refinery Limited);
33  * Contributor(s): -;
34  *
35  * $Id: CandlestickRendererTests.java,v 1.1.2.1 2006/10/03 15:41:45 mungady Exp $
36  *
37  * Changes
38  * -------
39  * 25-Mar-2003 : Version 1 (DG);
40  * 22-Oct-2003 : Added hashCode test (DG);
41  * 17-Aug-2006 : Strengthened testEquals() and added testFindRangeBounds()
42  * method (DG);
43  *
44  */

45
46 package org.jfree.chart.renderer.xy.junit;
47
48 import java.awt.Color JavaDoc;
49 import java.awt.GradientPaint JavaDoc;
50 import java.io.ByteArrayInputStream JavaDoc;
51 import java.io.ByteArrayOutputStream JavaDoc;
52 import java.io.ObjectInput JavaDoc;
53 import java.io.ObjectInputStream JavaDoc;
54 import java.io.ObjectOutput JavaDoc;
55 import java.io.ObjectOutputStream JavaDoc;
56 import java.util.Date JavaDoc;
57
58 import junit.framework.Test;
59 import junit.framework.TestCase;
60 import junit.framework.TestSuite;
61
62 import org.jfree.chart.renderer.xy.CandlestickRenderer;
63 import org.jfree.data.Range;
64 import org.jfree.data.xy.DefaultOHLCDataset;
65 import org.jfree.data.xy.OHLCDataItem;
66 import org.jfree.data.xy.OHLCDataset;
67
68 /**
69  * Tests for the {@link CandlestickRenderer} class.
70  */

71 public class CandlestickRendererTests extends TestCase {
72
73     /**
74      * Returns the tests as a test suite.
75      *
76      * @return The test suite.
77      */

78     public static Test suite() {
79         return new TestSuite(CandlestickRendererTests.class);
80     }
81
82     /**
83      * Constructs a new set of tests.
84      *
85      * @param name the name of the tests.
86      */

87     public CandlestickRendererTests(String JavaDoc name) {
88         super(name);
89     }
90
91     /**
92      * Check that the equals() method distinguishes all fields.
93      */

94     public void testEquals() {
95         CandlestickRenderer r1 = new CandlestickRenderer();
96         CandlestickRenderer r2 = new CandlestickRenderer();
97         assertEquals(r1, r2);
98         
99         // upPaint
100
r1.setUpPaint(new GradientPaint JavaDoc(1.0f, 2.0f, Color.red, 3.0f, 4.0f,
101                 Color.white));
102         assertFalse(r1.equals(r2));
103         r2.setUpPaint(new GradientPaint JavaDoc(1.0f, 2.0f, Color.red, 3.0f, 4.0f,
104                 Color.white));
105         assertTrue(r1.equals(r2));
106         
107         // downPaint
108
r1.setDownPaint(new GradientPaint JavaDoc(5.0f, 6.0f, Color.green, 7.0f, 8.0f,
109                 Color.yellow));
110         assertFalse(r1.equals(r2));
111         r2.setDownPaint(new GradientPaint JavaDoc(5.0f, 6.0f, Color.green, 7.0f, 8.0f,
112                 Color.yellow));
113         assertTrue(r1.equals(r2));
114         
115         // drawVolume
116
r1.setDrawVolume(false);
117         assertFalse(r1.equals(r2));
118         r2.setDrawVolume(false);
119         assertTrue(r1.equals(r2));
120         
121         // candleWidth
122
r1.setCandleWidth(3.3);
123         assertFalse(r1.equals(r2));
124         r2.setCandleWidth(3.3);
125         assertTrue(r1.equals(r2));
126         
127         // maxCandleWidthInMilliseconds
128
r1.setMaxCandleWidthInMilliseconds(123);
129         assertFalse(r1.equals(r2));
130         r2.setMaxCandleWidthInMilliseconds(123);
131         assertTrue(r1.equals(r2));
132         
133         // autoWidthMethod
134
r1.setAutoWidthMethod(CandlestickRenderer.WIDTHMETHOD_SMALLEST);
135         assertFalse(r1.equals(r2));
136         r2.setAutoWidthMethod(CandlestickRenderer.WIDTHMETHOD_SMALLEST);
137         assertTrue(r1.equals(r2));
138         
139         // autoWidthFactor
140
r1.setAutoWidthFactor(0.22);
141         assertFalse(r1.equals(r2));
142         r2.setAutoWidthFactor(0.22);
143         assertTrue(r1.equals(r2));
144         
145         // autoWidthGap
146
r1.setAutoWidthGap(1.1);
147         assertFalse(r1.equals(r2));
148         r2.setAutoWidthGap(1.1);
149         assertTrue(r1.equals(r2));
150     }
151
152     /**
153      * Two objects that are equal are required to return the same hashCode.
154      */

155     public void testHashcode() {
156         CandlestickRenderer r1 = new CandlestickRenderer();
157         CandlestickRenderer r2 = new CandlestickRenderer();
158         assertTrue(r1.equals(r2));
159         int h1 = r1.hashCode();
160         int h2 = r2.hashCode();
161         assertEquals(h1, h2);
162     }
163     
164     /**
165      * Confirm that cloning works.
166      */

167     public void testCloning() {
168         CandlestickRenderer r1 = new CandlestickRenderer();
169         CandlestickRenderer r2 = null;
170         try {
171             r2 = (CandlestickRenderer) r1.clone();
172         }
173         catch (CloneNotSupportedException JavaDoc e) {
174             e.printStackTrace();
175         }
176         assertTrue(r1 != r2);
177         assertTrue(r1.getClass() == r2.getClass());
178         assertTrue(r1.equals(r2));
179     }
180
181     /**
182      * Serialize an instance, restore it, and check for equality.
183      */

184     public void testSerialization() {
185
186         CandlestickRenderer r1 = new CandlestickRenderer();
187         CandlestickRenderer r2 = null;
188
189         try {
190             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
191             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
192             out.writeObject(r1);
193             out.close();
194
195             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
196                     new ByteArrayInputStream JavaDoc(buffer.toByteArray()));
197             r2 = (CandlestickRenderer) in.readObject();
198             in.close();
199         }
200         catch (Exception JavaDoc e) {
201             e.printStackTrace();
202         }
203         assertEquals(r1, r2);
204
205     }
206     
207     /**
208      * Some checks for the findRangeBounds() method.
209      */

210     public void testFindRangeBounds() {
211         CandlestickRenderer renderer = new CandlestickRenderer();
212         
213         OHLCDataItem item1 = new OHLCDataItem(new Date JavaDoc(1L), 2.0, 4.0, 1.0, 3.0,
214                 100);
215         OHLCDataset dataset = new DefaultOHLCDataset("S1",
216                 new OHLCDataItem[] {item1});
217         Range range = renderer.findRangeBounds(dataset);
218         assertEquals(new Range(1.0, 4.0), range);
219
220         OHLCDataItem item2 = new OHLCDataItem(new Date JavaDoc(1L), -1.0, 3.0, -1.0,
221                 3.0, 100);
222         dataset = new DefaultOHLCDataset("S1", new OHLCDataItem[] {item1,
223                 item2});
224         range = renderer.findRangeBounds(dataset);
225         assertEquals(new Range(-1.0, 4.0), range);
226         
227         // try an empty dataset - should return a null range
228
dataset = new DefaultOHLCDataset("S1", new OHLCDataItem[] {});
229         range = renderer.findRangeBounds(dataset);
230         assertNull(range);
231         
232         // try a null dataset - should return a null range
233
range = renderer.findRangeBounds(null);
234         assertNull(range);
235     }
236
237 }
238
Popular Tags