KickJava   Java API By Example, From Geeks To Geeks.

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


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  * HighLowRendererTests.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: HighLowRendererTests.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  * 01-Nov-2005 : Added tests for new fields (DG);
42  * 17-Aug-2006 : Added testFindRangeBounds() method (DG);
43  *
44  */

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

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

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

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

93     public void testEquals() {
94         HighLowRenderer r1 = new HighLowRenderer();
95         HighLowRenderer r2 = new HighLowRenderer();
96         assertEquals(r1, r2);
97         
98         // drawOpenTicks
99
r1.setDrawOpenTicks(false);
100         assertFalse(r1.equals(r2));
101         r2.setDrawOpenTicks(false);
102         assertTrue(r1.equals(r2));
103         
104         // drawCloseTicks
105
r1.setDrawCloseTicks(false);
106         assertFalse(r1.equals(r2));
107         r2.setDrawCloseTicks(false);
108         assertTrue(r1.equals(r2));
109         
110         // openTickPaint
111
r1.setOpenTickPaint(Color.red);
112         assertFalse(r1.equals(r2));
113         r2.setOpenTickPaint(Color.red);
114         assertTrue(r1.equals(r2));
115         
116         // closeTickPaint
117
r1.setCloseTickPaint(Color.blue);
118         assertFalse(r1.equals(r2));
119         r2.setCloseTickPaint(Color.blue);
120         assertTrue(r1.equals(r2));
121     }
122
123     /**
124      * Two objects that are equal are required to return the same hashCode.
125      */

126     public void testHashcode() {
127         HighLowRenderer r1 = new HighLowRenderer();
128         HighLowRenderer r2 = new HighLowRenderer();
129         assertTrue(r1.equals(r2));
130         int h1 = r1.hashCode();
131         int h2 = r2.hashCode();
132         assertEquals(h1, h2);
133     }
134     
135     /**
136      * Confirm that cloning works.
137      */

138     public void testCloning() {
139         HighLowRenderer r1 = new HighLowRenderer();
140         r1.setCloseTickPaint(Color.green);
141         HighLowRenderer r2 = null;
142         try {
143             r2 = (HighLowRenderer) r1.clone();
144         }
145         catch (CloneNotSupportedException JavaDoc e) {
146             e.printStackTrace();
147         }
148         assertTrue(r1 != r2);
149         assertTrue(r1.getClass() == r2.getClass());
150         assertTrue(r1.equals(r2));
151     }
152
153     /**
154      * Serialize an instance, restore it, and check for equality.
155      */

156     public void testSerialization() {
157
158         HighLowRenderer r1 = new HighLowRenderer();
159         r1.setCloseTickPaint(Color.green);
160         HighLowRenderer r2 = null;
161
162         try {
163             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
164             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
165             out.writeObject(r1);
166             out.close();
167
168             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
169                 new ByteArrayInputStream JavaDoc(buffer.toByteArray())
170             );
171             r2 = (HighLowRenderer) in.readObject();
172             in.close();
173         }
174         catch (Exception JavaDoc e) {
175             e.printStackTrace();
176         }
177         assertEquals(r1, r2);
178
179     }
180
181     /**
182      * Some checks for the findRangeBounds() method.
183      */

184     public void testFindRangeBounds() {
185         HighLowRenderer renderer = new HighLowRenderer();
186         
187         OHLCDataItem item1 = new OHLCDataItem(new Date JavaDoc(1L), 2.0, 4.0, 1.0, 3.0,
188                 100);
189         OHLCDataset dataset = new DefaultOHLCDataset("S1",
190                 new OHLCDataItem[] {item1});
191         Range range = renderer.findRangeBounds(dataset);
192         assertEquals(new Range(1.0, 4.0), range);
193
194         OHLCDataItem item2 = new OHLCDataItem(new Date JavaDoc(1L), -1.0, 3.0, -1.0,
195                 3.0, 100);
196         dataset = new DefaultOHLCDataset("S1", new OHLCDataItem[] {item1,
197                 item2});
198         range = renderer.findRangeBounds(dataset);
199         assertEquals(new Range(-1.0, 4.0), range);
200         
201         // try an empty dataset - should return a null range
202
dataset = new DefaultOHLCDataset("S1", new OHLCDataItem[] {});
203         range = renderer.findRangeBounds(dataset);
204         assertNull(range);
205         
206         // try a null dataset - should return a null range
207
range = renderer.findRangeBounds(null);
208         assertNull(range);
209     }
210
211     
212 }
213
Popular Tags