KickJava   Java API By Example, From Geeks To Geeks.

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


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  * DefaultWindDatasetTests.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: DefaultWindDatasetTests.java,v 1.1.2.1 2006/10/03 15:41:37 mungady Exp $
36  *
37  * Changes
38  * -------
39  * 12-Jul-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.time.Day;
57 import org.jfree.data.time.RegularTimePeriod;
58 import org.jfree.data.xy.DefaultWindDataset;
59
60 /**
61  * Tests for {@link DefaultWindDataset}.
62  */

63 public class DefaultWindDatasetTests extends TestCase {
64
65     /**
66      * Returns the tests as a test suite.
67      *
68      * @return The test suite.
69      */

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

79     public DefaultWindDatasetTests(String JavaDoc name) {
80         super(name);
81     }
82
83     /**
84      * Confirm that the equals method can distinguish all the required fields.
85      */

86     public void testEquals() {
87         DefaultWindDataset d1 = new DefaultWindDataset();
88         DefaultWindDataset d2 = new DefaultWindDataset();
89         assertTrue(d1.equals(d2));
90         assertTrue(d2.equals(d1));
91         
92         d1 = createSampleDataset1();
93         assertFalse(d1.equals(d2));
94         d2 = createSampleDataset1();
95         assertTrue(d1.equals(d2));
96     }
97
98     /**
99      * Confirm that cloning works.
100      */

101     public void testCloning() {
102         DefaultWindDataset d1 = new DefaultWindDataset();
103         DefaultWindDataset d2 = null;
104         try {
105             d2 = (DefaultWindDataset) d1.clone();
106         }
107         catch (CloneNotSupportedException JavaDoc e) {
108             e.printStackTrace();
109         }
110         assertTrue(d1 != d2);
111         assertTrue(d1.getClass() == d2.getClass());
112         assertTrue(d1.equals(d2));
113         
114         // try a dataset with some content...
115
d1 = createSampleDataset1();
116         d2 = null;
117         try {
118             d2 = (DefaultWindDataset) d1.clone();
119         }
120         catch (CloneNotSupportedException JavaDoc e) {
121             e.printStackTrace();
122         }
123         assertTrue(d1 != d2);
124         assertTrue(d1.getClass() == d2.getClass());
125         assertTrue(d1.equals(d2));
126     }
127
128     /**
129      * Serialize an instance, restore it, and check for equality.
130      */

131     public void testSerialization() {
132         DefaultWindDataset d1 = new DefaultWindDataset();
133         DefaultWindDataset d2 = null;
134         try {
135             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
136             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
137             out.writeObject(d1);
138             out.close();
139
140             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(new ByteArrayInputStream JavaDoc(
141                     buffer.toByteArray()));
142             d2 = (DefaultWindDataset) in.readObject();
143             in.close();
144         }
145         catch (Exception JavaDoc e) {
146             e.printStackTrace();
147         }
148         assertEquals(d1, d2);
149
150         // try a dataset with some content...
151
d1 = createSampleDataset1();
152         try {
153             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
154             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
155             out.writeObject(d1);
156             out.close();
157
158             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(new ByteArrayInputStream JavaDoc(
159                     buffer.toByteArray()));
160             d2 = (DefaultWindDataset) in.readObject();
161             in.close();
162         }
163         catch (Exception JavaDoc e) {
164             e.printStackTrace();
165         }
166         assertEquals(d1, d2);
167         
168     }
169     
170     /**
171      * Some checks for the getSeriesKey(int) method.
172      */

173     public void testGetSeriesKey() {
174         DefaultWindDataset d = createSampleDataset1();
175         assertEquals("Series 1", d.getSeriesKey(0));
176         assertEquals("Series 2", d.getSeriesKey(1));
177         
178         // check for series key out of bounds
179
boolean pass = false;
180         try {
181             /*Comparable k =*/ d.getSeriesKey(-1);
182         }
183         catch (IllegalArgumentException JavaDoc e) {
184             pass = true;
185         }
186         assertTrue(pass);
187         
188         pass = false;
189         try {
190             /*Comparable k =*/ d.getSeriesKey(2);
191         }
192         catch (IllegalArgumentException JavaDoc e) {
193             pass = true;
194         }
195         assertTrue(pass);
196     }
197     
198     /**
199      * Some checks for the indexOf(Comparable) method.
200      */

201     public void testIndexOf() {
202         DefaultWindDataset d = createSampleDataset1();
203         assertEquals(0, d.indexOf("Series 1"));
204         assertEquals(1, d.indexOf("Series 2"));
205         assertEquals(-1, d.indexOf("Green Eggs and Ham"));
206         assertEquals(-1, d.indexOf(null));
207     }
208     
209     /**
210      * Creates a sample dataset for testing.
211      *
212      * @return A sample dataset.
213      */

214     public DefaultWindDataset createSampleDataset1() {
215         Day t = new Day(1, 4, 2006);
216         Object JavaDoc[] item1 = createItem(t, 3, 7);
217         Object JavaDoc[] item2 = createItem(t.next(), 4, 8);
218         Object JavaDoc[] item3 = createItem(t.next(), 5, 9);
219         Object JavaDoc[][] series1 = new Object JavaDoc[][] {item1, item2, item3};
220         Object JavaDoc[] item1b = createItem(t, 6, 10);
221         Object JavaDoc[] item2b = createItem(t.next(), 7, 11);
222         Object JavaDoc[] item3b = createItem(t.next(), 8, 12);
223         Object JavaDoc[][] series2 = new Object JavaDoc[][] {item1b, item2b, item3b};
224         Object JavaDoc[][][] data = new Object JavaDoc[][][] {series1, series2};
225         return new DefaultWindDataset(data);
226     }
227     
228     /**
229      * Creates an array representing one item in a series.
230      *
231      * @param t the time period.
232      * @param dir the wind direction.
233      * @param force the wind force.
234      *
235      * @return An array containing the specified items.
236      */

237     private Object JavaDoc[] createItem(RegularTimePeriod t, int dir, int force) {
238         return new Object JavaDoc[] { new Long JavaDoc(t.getMiddleMillisecond()),
239                 new Integer JavaDoc(dir), new Integer JavaDoc(force) };
240     }
241 }
242
Popular Tags