KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cruisecontrol > util > DateUtilTest


1 /********************************************************************************
2  * CruiseControl, a Continuous Integration Toolkit
3  * Copyright (c) 2001-2003, ThoughtWorks, Inc.
4  * 651 W Washington Ave. Suite 600
5  * Chicago, IL 60661 USA
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * + Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * + Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  *
20  * + Neither the name of ThoughtWorks, Inc., CruiseControl, nor the
21  * names of its contributors may be used to endorse or promote
22  * products derived from this software without specific prior
23  * written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
29  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  ********************************************************************************/

37 package net.sourceforge.cruisecontrol.util;
38
39 import junit.framework.TestCase;
40 import java.util.Calendar JavaDoc;
41
42 /**
43  * @author jfredrick
44  */

45 public class DateUtilTest extends TestCase {
46
47     private Calendar JavaDoc cal;
48
49     private Calendar JavaDoc cal2;
50
51     public void setUp() {
52         // create a couple calendars/dates
53
cal = Calendar.getInstance();
54         cal.set(2001, Calendar.NOVEMBER, 22, 10, 01, 01);
55         cal2 = Calendar.getInstance();
56         cal2.set(2001, Calendar.NOVEMBER, 22, 11, 01, 01);
57     }
58
59     public void testGetTimeFromDate() {
60         assertEquals(DateUtil.getTimeFromDate(cal.getTime()), 1001);
61         assertEquals(DateUtil.getTimeFromDate(cal2.getTime()), 1101);
62     }
63
64     public void testMilliTimeDifference() {
65         int noon = 1200;
66         int elevenThirty = 1130;
67         int oneFifteen = 1315;
68
69         long thirtyMinutes = 30 * 60 * 1000;
70         long hourFifteenMinutes = (60 + 15) * 60 * 1000;
71         long hourFortyFiveMinutes = (60 + 45) * 60 * 1000;
72
73         assertEquals(thirtyMinutes, DateUtil.milliTimeDifference(elevenThirty, noon));
74         assertEquals(hourFifteenMinutes, DateUtil.milliTimeDifference(noon, oneFifteen));
75         assertEquals(hourFortyFiveMinutes, DateUtil.milliTimeDifference(elevenThirty, oneFifteen));
76     }
77
78     public void testConvertToMillis() {
79         int noon = 1200;
80         int oneAM = 100;
81         int elevenFifteenPM = 2315;
82
83         long noonMillis = 12 * 60 * 60 * 1000;
84         long oneAMmillis = 1 * 60 * 60 * 1000;
85         long elevenFifteenPMmillis = (23 * 60 + 15) * 60 * 1000;
86
87         assertEquals(noonMillis, DateUtil.convertToMillis(noon));
88         assertEquals(oneAMmillis, DateUtil.convertToMillis(oneAM));
89         assertEquals(elevenFifteenPMmillis, DateUtil.convertToMillis(elevenFifteenPM));
90     }
91
92     public void testFormatTime() {
93         long fiveSeconds = 5 * 1000;
94         long oneHour = 60 * DateUtil.ONE_MINUTE;
95         long oneHourFiftyNineMinutes = 2 * oneHour - DateUtil.ONE_MINUTE;
96
97         String JavaDoc seconds = "5 seconds";
98         String JavaDoc hoursMinutesSeconds = "1 hours 59 minutes 5 seconds";
99         String JavaDoc negativeTime = "-1 hours -59 minutes -5 seconds";
100
101         assertEquals(seconds, DateUtil.formatTime(fiveSeconds));
102         assertEquals(hoursMinutesSeconds, DateUtil.formatTime(oneHourFiftyNineMinutes + fiveSeconds));
103         assertEquals(negativeTime, DateUtil.formatTime(-1 * (oneHourFiftyNineMinutes + fiveSeconds)));
104     }
105
106     public void testGetBuildTimeAsString() {
107         int minutes = 50;
108         int seconds = 30;
109         int millis = 123;
110         long timeInMillis = millis + seconds * 1000 + minutes * 60 * 1000;
111         assertEquals("50 minute(s) 30 second(s)",
112             DateUtil.getDurationAsString(timeInMillis));
113     }
114 }
115
Popular Tags