KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > chart > axis > junit > SegmentedTimelineTests2


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  * SegmentedTimelineTests2.java
29  * ----------------------------
30  * (C) Copyright 2004, by Object Refinery Limited and Contributors.
31  *
32  * Original Author: David Gilbert (for Object Refinery Limited);
33  * Contributor(s): -;
34  *
35  * $Id: SegmentedTimelineTests2.java,v 1.1.2.1 2006/10/03 15:41:20 mungady Exp $
36  *
37  * Changes
38  * -------
39  * 02-Aug-2004 : Added standard header (DG);
40  *
41  */

42
43 package org.jfree.chart.axis.junit;
44
45 import java.util.Calendar JavaDoc;
46 import java.util.Date JavaDoc;
47 import java.util.Locale JavaDoc;
48
49 import junit.framework.TestCase;
50
51 import org.jfree.chart.axis.SegmentedTimeline;
52
53 /**
54  * @author fredatwork
55  */

56 public class SegmentedTimelineTests2 extends TestCase {
57
58     /**
59      * Constructor
60      */

61     public SegmentedTimelineTests2() {
62         super();
63     }
64
65     /**
66      * Test 1 checks 9am Friday 26 March 2004 converts to a timeline value and
67      * back again correctly. This is prior to Daylight Saving.
68      */

69     public void test1() {
70
71         Calendar JavaDoc cal = Calendar.getInstance(Locale.UK);
72         cal.set(Calendar.YEAR, 2004);
73         cal.set(Calendar.MONTH, Calendar.MARCH);
74         cal.set(Calendar.DAY_OF_MONTH, 26);
75         cal.set(Calendar.HOUR_OF_DAY, 9);
76         cal.set(Calendar.MINUTE, 0);
77         cal.set(Calendar.SECOND, 0);
78         cal.set(Calendar.MILLISECOND, 0);
79         Date JavaDoc date = cal.getTime();
80                   
81         SegmentedTimeline timeline = getTimeline();
82         long value = timeline.toTimelineValue(date);
83         long ms = timeline.toMillisecond(value);
84       
85         Calendar JavaDoc cal2 = Calendar.getInstance(Locale.UK);
86         cal2.setTime(new Date JavaDoc(ms));
87         Date JavaDoc reverted = cal2.getTime();
88       
89         assertTrue("test1", value == (900000 * 34)
90                 && date.getTime() == reverted.getTime());
91  
92     }
93
94     /**
95      * Test 2 checks 9.15am Friday 26 March 2004 converts to a timeline value
96      * and back again correctly. This is prior to Daylight Saving.
97      */

98     public void test2() {
99         Calendar JavaDoc cal = Calendar.getInstance(Locale.UK);
100         cal.set(Calendar.YEAR, 2004);
101         cal.set(Calendar.MONTH, Calendar.MARCH);
102         cal.set(Calendar.DAY_OF_MONTH, 26);
103         cal.set(Calendar.HOUR_OF_DAY, 9);
104         cal.set(Calendar.MINUTE, 15);
105         cal.set(Calendar.SECOND, 0);
106         cal.set(Calendar.MILLISECOND, 0);
107         Date JavaDoc date = cal.getTime();
108                   
109         SegmentedTimeline timeline = getTimeline();
110
111         long value = timeline.toTimelineValue(date);
112         long ms = timeline.toMillisecond(value);
113         Calendar JavaDoc cal2 = Calendar.getInstance(Locale.UK);
114         cal2.setTime(new Date JavaDoc(ms));
115         Date JavaDoc reverted = cal2.getTime();
116         assertTrue(
117             "test2", value == (900000 * 34 + 900000)
118             && date.getTime() == reverted.getTime()
119         );
120      }
121
122     /**
123      * Test 3 checks 9.30am Friday 26 March 2004 converts to a timeline value
124      * and back again correctly. This is prior to Daylight Saving.
125      */

126     public void test3() {
127         Calendar JavaDoc cal = Calendar.getInstance(Locale.UK);
128         cal.set(Calendar.YEAR, 2004);
129         cal.set(Calendar.MONTH, Calendar.MARCH);
130         cal.set(Calendar.DAY_OF_MONTH, 26);
131         cal.set(Calendar.HOUR_OF_DAY, 9);
132         cal.set(Calendar.MINUTE, 30);
133         cal.set(Calendar.SECOND, 0);
134         cal.set(Calendar.MILLISECOND, 0);
135         Date JavaDoc date = cal.getTime();
136         SegmentedTimeline timeline = getTimeline();
137
138         long value = timeline.toTimelineValue(date);
139         long ms = timeline.toMillisecond(value);
140       
141         Calendar JavaDoc cal2 = Calendar.getInstance(Locale.UK);
142         cal2.setTime(new Date JavaDoc(ms));
143         Date JavaDoc reverted = cal2.getTime();
144         assertTrue(
145             "test2", value == (900000 * 34 + 900000 * 2)
146             && date.getTime() == reverted.getTime()
147         );
148     }
149
150     /**
151      * Test 4 checks 9.30am Friday 26 March 2004 (+ 1 millisecond) converts to
152      * a timeline value and back again correctly. This is prior to Daylight
153      * Saving.
154      */

155     public void test4() {
156         Calendar JavaDoc cal = Calendar.getInstance(Locale.UK);
157         cal.set(Calendar.YEAR, 2004);
158         cal.set(Calendar.MONTH, Calendar.MARCH);
159         cal.set(Calendar.DAY_OF_MONTH, 26);
160         cal.set(Calendar.HOUR_OF_DAY, 9);
161         cal.set(Calendar.MINUTE, 30);
162         cal.set(Calendar.SECOND, 0);
163         cal.set(Calendar.MILLISECOND, 1);
164         Date JavaDoc date = cal.getTime();
165         SegmentedTimeline timeline = getTimeline();
166
167         long value = timeline.toTimelineValue(date);
168         long ms = timeline.toMillisecond(value);
169         Calendar JavaDoc cal2 = Calendar.getInstance(Locale.UK);
170         cal2.setTime(new Date JavaDoc(ms));
171         Date JavaDoc reverted = cal2.getTime();
172         assertTrue(
173             "test4", value == (900000 * 34 + 900000 * 2 + 1)
174             && date.getTime() == reverted.getTime()
175         );
176     }
177
178     /**
179      * Test 5 checks 5.30pm Thursday 25 March 2004 converts to a timeline
180      * value and back again correctly. As it is in the excluded segment, we
181      * expect it to map to 9am, Friday 26 March 2004. This is prior to
182      * Daylight Saving.
183      */

184     public void test5() {
185         Calendar JavaDoc cal = Calendar.getInstance(Locale.UK);
186         cal.set(Calendar.YEAR, 2004);
187         cal.set(Calendar.MONTH, Calendar.MARCH);
188         cal.set(Calendar.DAY_OF_MONTH, 25);
189         cal.set(Calendar.HOUR_OF_DAY, 17);
190         cal.set(Calendar.MINUTE, 30);
191         cal.set(Calendar.SECOND, 0);
192         cal.set(Calendar.MILLISECOND, 0);
193         Date JavaDoc date = cal.getTime();
194         SegmentedTimeline timeline = getTimeline();
195
196         long value = timeline.toTimelineValue(date);
197         long ms = timeline.toMillisecond(value);
198         Calendar JavaDoc cal2 = Calendar.getInstance(Locale.UK);
199         cal2.setTime(new Date JavaDoc(ms));
200         Date JavaDoc reverted = cal2.getTime();
201         Calendar JavaDoc expectedReverted = Calendar.getInstance(Locale.UK);
202         expectedReverted.set(Calendar.YEAR, 2004);
203         expectedReverted.set(Calendar.MONTH, Calendar.MARCH);
204         expectedReverted.set(Calendar.DAY_OF_MONTH, 26);
205         expectedReverted.set(Calendar.HOUR_OF_DAY, 9);
206         expectedReverted.set(Calendar.MINUTE, 0);
207         expectedReverted.set(Calendar.SECOND, 0);
208         expectedReverted.set(Calendar.MILLISECOND, 0);
209       
210         assertTrue(
211             "test5", value == (900000 * 34)
212             && expectedReverted.getTime().getTime() == reverted.getTime()
213         );
214     }
215
216     /**
217      * Test 6 checks that 9am on Sunday 28 March 2004 converts to the timeline
218      * value and back again correctly. Note that Saturday and Sunday are
219      * excluded from the timeline, so we expect the value to map to 9am on
220      * Monday 29 March 2004. This is during daylight saving.
221      */

222     public void test6() {
223         Calendar JavaDoc cal = Calendar.getInstance(Locale.UK);
224         cal.set(Calendar.YEAR, 2004);
225         cal.set(Calendar.MONTH, Calendar.MARCH);
226         cal.set(Calendar.DAY_OF_MONTH, 28);
227         cal.set(Calendar.HOUR_OF_DAY, 9);
228         cal.set(Calendar.MINUTE, 0);
229         cal.set(Calendar.SECOND, 0);
230         cal.set(Calendar.MILLISECOND, 0);
231         Date JavaDoc date = cal.getTime();
232         SegmentedTimeline timeline = getTimeline();
233
234         long value = timeline.toTimelineValue(date);
235         long ms = timeline.toMillisecond(value);
236         Calendar JavaDoc cal2 = Calendar.getInstance(Locale.UK);
237         cal2.setTime(new Date JavaDoc(ms));
238         Date JavaDoc reverted = cal2.getTime();
239       
240         Calendar JavaDoc expectedReverted = Calendar.getInstance(Locale.UK);
241         expectedReverted.set(Calendar.YEAR, 2004);
242         expectedReverted.set(Calendar.MONTH, Calendar.MARCH);
243         expectedReverted.set(Calendar.DAY_OF_MONTH, 29);
244         expectedReverted.set(Calendar.HOUR_OF_DAY, 9);
245         expectedReverted.set(Calendar.MINUTE, 0);
246         expectedReverted.set(Calendar.SECOND, 0);
247         expectedReverted.set(Calendar.MILLISECOND, 0);
248       
249         assertTrue(
250             "test6", value == (900000 * 34 * 2)
251             && expectedReverted.getTime().getTime() == reverted.getTime()
252         );
253     }
254              
255     /**
256      * Test 7 checks 9am Monday 29 March 2004 converts to a timeline value and
257      * back again correctly. This is during Daylight Saving.
258      */

259     public void test7() {
260         Calendar JavaDoc cal = Calendar.getInstance(Locale.UK);
261         cal.set(Calendar.YEAR, 2004);
262         cal.set(Calendar.MONTH, Calendar.MARCH);
263         cal.set(Calendar.DAY_OF_MONTH, 29);
264         cal.set(Calendar.HOUR_OF_DAY, 9);
265         cal.set(Calendar.MINUTE, 0);
266         cal.set(Calendar.SECOND, 0);
267         cal.set(Calendar.MILLISECOND, 0);
268         Date JavaDoc date = cal.getTime();
269                   
270         SegmentedTimeline timeline = getTimeline();
271         long value = timeline.toTimelineValue(date);
272         long ms = timeline.toMillisecond(value);
273       
274         Calendar JavaDoc cal2 = Calendar.getInstance(Locale.UK);
275         cal2.setTime(new Date JavaDoc(ms));
276         Date JavaDoc reverted = cal2.getTime();
277       
278         Calendar JavaDoc expectedReverted = Calendar.getInstance();
279         expectedReverted.set(Calendar.YEAR, 2004);
280         expectedReverted.set(Calendar.MONTH, Calendar.MARCH);
281         expectedReverted.set(Calendar.DAY_OF_MONTH, 29);
282         expectedReverted.set(Calendar.HOUR_OF_DAY, 9);
283         expectedReverted.set(Calendar.MINUTE, 0);
284         expectedReverted.set(Calendar.SECOND, 0);
285         expectedReverted.set(Calendar.MILLISECOND, 0);
286       
287         assertTrue(
288             "test7", value == (900000 * 34 * 2)
289             && expectedReverted.getTime().getTime() == reverted.getTime()
290         );
291     }
292
293     /**
294      * Test 8.
295      */

296     public void test8() {
297         Calendar JavaDoc cal = Calendar.getInstance(Locale.UK);
298         cal.set(Calendar.YEAR, 2004);
299         cal.set(Calendar.MONTH, Calendar.MARCH);
300         cal.set(Calendar.DAY_OF_MONTH, 29);
301         cal.set(Calendar.HOUR_OF_DAY, 10);
302         cal.set(Calendar.MINUTE, 0);
303         cal.set(Calendar.SECOND, 0);
304         cal.set(Calendar.MILLISECOND, 0);
305         Date JavaDoc date = cal.getTime();
306                   
307         SegmentedTimeline timeline = getTimeline();
308       
309         // Add exception in included segments
310
cal.set(Calendar.YEAR, 2004);
311         cal.set(Calendar.MONTH, Calendar.MARCH);
312         cal.set(Calendar.DAY_OF_MONTH, 29);
313         cal.set(Calendar.HOUR_OF_DAY, 9);
314         cal.set(Calendar.MINUTE, 15);
315         cal.set(Calendar.SECOND, 0);
316         cal.set(Calendar.MILLISECOND, 0);
317         timeline.addException(cal.getTime());
318             
319         long value = timeline.toTimelineValue(date);
320         long ms = timeline.toMillisecond(value);
321       
322         Calendar JavaDoc cal2 = Calendar.getInstance(Locale.UK);
323         cal2.setTime(new Date JavaDoc(ms));
324         Date JavaDoc reverted = cal2.getTime();
325       
326         Calendar JavaDoc expectedReverted = Calendar.getInstance();
327         expectedReverted.set(Calendar.YEAR, 2004);
328         expectedReverted.set(Calendar.MONTH, Calendar.MARCH);
329         expectedReverted.set(Calendar.DAY_OF_MONTH, 29);
330         expectedReverted.set(Calendar.HOUR_OF_DAY, 10);
331         expectedReverted.set(Calendar.MINUTE, 0);
332         expectedReverted.set(Calendar.SECOND, 0);
333         expectedReverted.set(Calendar.MILLISECOND, 0);
334       
335         assertTrue(
336             "test8", value == (900000 * 34 * 2 + 900000 * (4 - 1))
337             && expectedReverted.getTime().getTime() == reverted.getTime()
338         );
339     }
340    
341     /**
342      * Creates a timeline from 9am on Thursday 25 March 2004 to 5pm on
343      * Tuesday 30 March 2004, for the UK locale.
344      *
345      * @return A timeline.
346      */

347     private SegmentedTimeline getTimeline() {
348         Calendar JavaDoc cal = Calendar.getInstance(Locale.UK);
349         cal.set(Calendar.YEAR, 2004);
350         cal.set(Calendar.MONTH, Calendar.MARCH);
351         cal.set(Calendar.DAY_OF_MONTH, 25);
352         cal.set(Calendar.HOUR_OF_DAY, 9);
353         cal.set(Calendar.MINUTE, 0);
354         cal.set(Calendar.SECOND, 0);
355         cal.set(Calendar.MILLISECOND, 0);
356         Date JavaDoc from = cal.getTime();
357
358         cal = Calendar.getInstance(Locale.UK);
359         cal.set(Calendar.YEAR, 2004);
360         cal.set(Calendar.MONTH, Calendar.MARCH);
361         cal.set(Calendar.DAY_OF_MONTH, 30);
362         cal.set(Calendar.HOUR_OF_DAY, 17);
363         cal.set(Calendar.MINUTE, 30);
364         cal.set(Calendar.SECOND, 0);
365         cal.set(Calendar.MILLISECOND, 0);
366         Date JavaDoc to = cal.getTime();
367
368         return getTimeline(from, to);
369     }
370    
371     /**
372      * Returns a segmented timeline for 15 minute segments, Monday to Friday
373      * only, between 9am and 5pm. The UK locale is used.
374      *
375      * @param start the start date.
376      * @param end the end date.
377      *
378      * @return The timeline.
379      */

380     private SegmentedTimeline getTimeline(Date JavaDoc start, Date JavaDoc end) {
381       
382         Calendar JavaDoc cal = Calendar.getInstance(Locale.UK);
383         cal.set(Calendar.YEAR, 1970);
384         cal.set(Calendar.MONTH, Calendar.JANUARY);
385         cal.set(Calendar.DAY_OF_MONTH, 1);
386         cal.set(Calendar.HOUR_OF_DAY, 9);
387         cal.set(Calendar.MINUTE, 0);
388         cal.set(Calendar.SECOND, 0);
389         cal.set(Calendar.MILLISECOND, 0);
390         Date JavaDoc open = cal.getTime();
391
392         cal = Calendar.getInstance(Locale.UK);
393         cal.set(Calendar.YEAR, 1970);
394         cal.set(Calendar.MONTH, Calendar.JANUARY);
395         cal.set(Calendar.DAY_OF_MONTH, 1);
396         cal.set(Calendar.HOUR_OF_DAY, 17);
397         cal.set(Calendar.MINUTE, 30);
398         cal.set(Calendar.SECOND, 0);
399         cal.set(Calendar.MILLISECOND, 0);
400         Date JavaDoc close = cal.getTime();
401                         
402         SegmentedTimeline result = null;
403         // Create a segmented time line (segment size : 15 minutes)
404
long quarterHourCount = (close.getTime() - open.getTime())
405             / SegmentedTimeline.FIFTEEN_MINUTE_SEGMENT_SIZE;
406         long totalQuarterHourCount = SegmentedTimeline.DAY_SEGMENT_SIZE
407             / SegmentedTimeline.FIFTEEN_MINUTE_SEGMENT_SIZE;
408         result = new SegmentedTimeline(
409             SegmentedTimeline.FIFTEEN_MINUTE_SEGMENT_SIZE,
410             (int) quarterHourCount,
411             (int) (totalQuarterHourCount - quarterHourCount)
412         );
413         result.setAdjustForDaylightSaving(true);
414         // Set start time
415
result.setStartTime(start.getTime());
416         // Saturday and Sundays are non business hours
417
result.setBaseTimeline(
418             SegmentedTimeline.newMondayThroughFridayTimeline()
419         );
420         /* PUT exclusions in test */
421         if (start != null && end != null) {
422             result.addBaseTimelineExclusions(start.getTime(), end.getTime());
423         }
424           
425         return result;
426     }
427       
428     
429 }
430
Popular Tags