KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > lang > time > DurationFormatUtilsTest


1 /*
2  * Copyright 2002-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.lang.time;
17
18 import java.lang.reflect.Constructor JavaDoc;
19 import java.lang.reflect.Modifier JavaDoc;
20 import java.util.Calendar JavaDoc;
21 import java.util.TimeZone JavaDoc;
22
23 import junit.framework.Test;
24 import junit.framework.TestCase;
25 import junit.framework.TestSuite;
26 import junit.textui.TestRunner;
27
28 /**
29  * TestCase for DurationFormatUtils.
30  *
31  * @author Apache Ant - DateUtilsTest
32  * @author <a HREF="mailto:sbailliez@apache.org">Stephane Bailliez</a>
33  * @author <a HREF="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a>
34  * @author Stephen Colebourne
35  * @author <a HREF="mailto:ggregory@seagullsw.com">Gary Gregory</a>
36  * @author Henri Yandell
37  */

38 public class DurationFormatUtilsTest extends TestCase {
39
40     public static void main(String JavaDoc[] args) {
41         TestRunner.run(suite());
42     }
43
44     public static Test suite() {
45         TestSuite suite = new TestSuite(DurationFormatUtilsTest.class);
46         suite.setName("DurationFormatUtils Tests");
47         return suite;
48     }
49
50     public DurationFormatUtilsTest(String JavaDoc s) {
51         super(s);
52     }
53
54     //-----------------------------------------------------------------------
55
public void testConstructor() {
56         assertNotNull(new DurationFormatUtils());
57         Constructor JavaDoc[] cons = DurationFormatUtils.class.getDeclaredConstructors();
58         assertEquals(1, cons.length);
59         assertEquals(true, Modifier.isPublic(cons[0].getModifiers()));
60         assertEquals(true, Modifier.isPublic(DurationFormatUtils.class.getModifiers()));
61         assertEquals(false, Modifier.isFinal(DurationFormatUtils.class.getModifiers()));
62     }
63     
64     //-----------------------------------------------------------------------
65
public void testFormatDurationWords(){
66         String JavaDoc text = null;
67         
68         text = DurationFormatUtils.formatDurationWords(50*1000, true, false);
69         assertEquals("50 seconds", text);
70         text = DurationFormatUtils.formatDurationWords(65*1000, true, false);
71         assertEquals("1 minute 5 seconds", text);
72         text = DurationFormatUtils.formatDurationWords(120*1000, true, false);
73         assertEquals("2 minutes 0 seconds", text);
74         text = DurationFormatUtils.formatDurationWords(121*1000, true, false);
75         assertEquals("2 minutes 1 second", text);
76         text = DurationFormatUtils.formatDurationWords(72*60*1000, true, false);
77         assertEquals("1 hour 12 minutes 0 seconds", text);
78         text = DurationFormatUtils.formatDurationWords(24*60*60*1000, true, false);
79         assertEquals("1 day 0 hours 0 minutes 0 seconds", text);
80         
81         text = DurationFormatUtils.formatDurationWords(50*1000, true, true);
82         assertEquals("50 seconds", text);
83         text = DurationFormatUtils.formatDurationWords(65*1000, true, true);
84         assertEquals("1 minute 5 seconds", text);
85         text = DurationFormatUtils.formatDurationWords(120*1000, true, true);
86         assertEquals("2 minutes", text);
87         text = DurationFormatUtils.formatDurationWords(121*1000, true, true);
88         assertEquals("2 minutes 1 second", text);
89         text = DurationFormatUtils.formatDurationWords(72*60*1000, true, true);
90         assertEquals("1 hour 12 minutes", text);
91         text = DurationFormatUtils.formatDurationWords(24*60*60*1000, true, true);
92         assertEquals("1 day", text);
93         
94         text = DurationFormatUtils.formatDurationWords(50*1000, false, true);
95         assertEquals("0 days 0 hours 0 minutes 50 seconds", text);
96         text = DurationFormatUtils.formatDurationWords(65*1000, false, true);
97         assertEquals("0 days 0 hours 1 minute 5 seconds", text);
98         text = DurationFormatUtils.formatDurationWords(120*1000, false, true);
99         assertEquals("0 days 0 hours 2 minutes", text);
100         text = DurationFormatUtils.formatDurationWords(121*1000, false, true);
101         assertEquals("0 days 0 hours 2 minutes 1 second", text);
102         text = DurationFormatUtils.formatDurationWords(72*60*1000, false, true);
103         assertEquals("0 days 1 hour 12 minutes", text);
104         text = DurationFormatUtils.formatDurationWords(24*60*60*1000, false, true);
105         assertEquals("1 day", text);
106         
107         text = DurationFormatUtils.formatDurationWords(50*1000, false, false);
108         assertEquals("0 days 0 hours 0 minutes 50 seconds", text);
109         text = DurationFormatUtils.formatDurationWords(65*1000, false, false);
110         assertEquals("0 days 0 hours 1 minute 5 seconds", text);
111         text = DurationFormatUtils.formatDurationWords(120*1000, false, false);
112         assertEquals("0 days 0 hours 2 minutes 0 seconds", text);
113         text = DurationFormatUtils.formatDurationWords(121*1000, false, false);
114         assertEquals("0 days 0 hours 2 minutes 1 second", text);
115         text = DurationFormatUtils.formatDurationWords(72*60*1000, false, false);
116         assertEquals("0 days 1 hour 12 minutes 0 seconds", text);
117         text = DurationFormatUtils.formatDurationWords(48*60*60*1000 + 72*60*1000 , false, false);
118         assertEquals("2 days 1 hour 12 minutes 0 seconds", text);
119     }
120
121     public void testFormatDurationHMS(){
122         long time = 0;
123         assertEquals("0:00:00.000", DurationFormatUtils.formatDurationHMS(time));
124         
125         time = 1;
126         assertEquals("0:00:00.001", DurationFormatUtils.formatDurationHMS(time));
127         
128         time = 15;
129         assertEquals("0:00:00.015", DurationFormatUtils.formatDurationHMS(time));
130         
131         time = 165;
132         assertEquals("0:00:00.165", DurationFormatUtils.formatDurationHMS(time));
133         
134         time = 1675;
135         assertEquals("0:00:01.675", DurationFormatUtils.formatDurationHMS(time));
136         
137         time = 13465;
138         assertEquals("0:00:13.465", DurationFormatUtils.formatDurationHMS(time));
139         
140         time = 72789;
141         assertEquals("0:01:12.789", DurationFormatUtils.formatDurationHMS(time));
142         
143         time = 12789 + 32 * 60000;
144         assertEquals("0:32:12.789", DurationFormatUtils.formatDurationHMS(time));
145         
146         time = 12789 + 62 * 60000;
147         assertEquals("1:02:12.789", DurationFormatUtils.formatDurationHMS(time));
148     }
149
150     public void testFormatDurationISO() {
151         assertEquals("P0Y0M0DT0H0M0.000S", DurationFormatUtils.formatDurationISO(0L));
152         assertEquals("P0Y0M0DT0H0M0.001S", DurationFormatUtils.formatDurationISO(1L));
153         assertEquals("P0Y0M0DT0H0M0.010S", DurationFormatUtils.formatDurationISO(10L));
154         assertEquals("P0Y0M0DT0H0M0.100S", DurationFormatUtils.formatDurationISO(100L));
155         assertEquals("P0Y0M0DT0H1M15.321S", DurationFormatUtils.formatDurationISO(75321L));
156     }
157
158     public void testFormatDuration() {
159         long duration = 0;
160         assertEquals( "0", DurationFormatUtils.formatDuration(duration, "y") );
161         assertEquals( "0", DurationFormatUtils.formatDuration(duration, "M") );
162         assertEquals( "0", DurationFormatUtils.formatDuration(duration, "d") );
163         assertEquals( "0", DurationFormatUtils.formatDuration(duration, "H") );
164         assertEquals( "0", DurationFormatUtils.formatDuration(duration, "m") );
165         assertEquals( "0", DurationFormatUtils.formatDuration(duration, "s") );
166         assertEquals( "0", DurationFormatUtils.formatDuration(duration, "S") );
167         assertEquals( "0000", DurationFormatUtils.formatDuration(duration, "SSSS") );
168         assertEquals( "0000", DurationFormatUtils.formatDuration(duration, "yyyy") );
169         assertEquals( "0000", DurationFormatUtils.formatDuration(duration, "yyMM") );
170
171         duration = 60 * 1000;
172         assertEquals( "0", DurationFormatUtils.formatDuration(duration, "y") );
173         assertEquals( "0", DurationFormatUtils.formatDuration(duration, "M") );
174         assertEquals( "0", DurationFormatUtils.formatDuration(duration, "d") );
175         assertEquals( "0", DurationFormatUtils.formatDuration(duration, "H") );
176         assertEquals( "1", DurationFormatUtils.formatDuration(duration, "m") );
177         assertEquals( "60", DurationFormatUtils.formatDuration(duration, "s") );
178         assertEquals( "60000", DurationFormatUtils.formatDuration(duration, "S") );
179         assertEquals( "01:00", DurationFormatUtils.formatDuration(duration, "mm:ss") );
180
181         Calendar JavaDoc base = Calendar.getInstance();
182         base.set(2000, 0, 1, 0, 0, 0);
183         base.set(Calendar.MILLISECOND, 0);
184         
185         Calendar JavaDoc cal = Calendar.getInstance();
186         cal.set(2003, 1, 1, 0, 0, 0);
187         cal.set(Calendar.MILLISECOND, 0);
188         duration = cal.getTime().getTime() - base.getTime().getTime(); // duration from 2000-01-01 to cal
189
// don't use 1970 in test as time zones were less reliable in 1970 than now
190
// remember that duration formatting ignores time zones, working on strict hour lengths
191
int days = 366 + 365 + 365 + 31;
192         assertEquals( "0 0 " + days, DurationFormatUtils.formatDuration(duration, "y M d") );
193     }
194
195     public void testFormatPeriodISO(){
196         TimeZone JavaDoc timeZone = TimeZone.getTimeZone("GMT-3");
197         Calendar JavaDoc base = Calendar.getInstance(timeZone);
198         base.set(1970, 0, 1, 0, 0, 0);
199         base.set(Calendar.MILLISECOND, 0);
200         
201         Calendar JavaDoc cal = Calendar.getInstance(timeZone);
202         cal.set(2002, 1, 23, 9, 11, 12);
203         cal.set(Calendar.MILLISECOND, 1);
204         String JavaDoc text;
205         // repeat a test from testDateTimeISO to compare extended and not extended.
206
text = DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.format(cal);
207         assertEquals("2002-02-23T09:11:12-03:00", text);
208         // test fixture is the same as above, but now with extended format.
209
text = DurationFormatUtils.formatPeriod(base.getTime().getTime(), cal.getTime().getTime(), DurationFormatUtils.ISO_EXTENDED_FORMAT_PATTERN, false, timeZone);
210         assertEquals("P32Y1M22DT9H11M12.001S", text);
211         // test fixture from example in http://www.w3.org/TR/xmlschema-2/#duration
212
cal.set(1971, 1, 3, 10, 30, 0);
213         cal.set(Calendar.MILLISECOND, 0);
214         text = DurationFormatUtils.formatPeriod(base.getTime().getTime(), cal.getTime().getTime(), DurationFormatUtils.ISO_EXTENDED_FORMAT_PATTERN, false, timeZone);
215         assertEquals("P1Y1M2DT10H30M0.000S", text);
216         // want a way to say 'don't print the seconds in format()' or other fields for that matter:
217
//assertEquals("P1Y2M3DT10H30M", text);
218
}
219
220     public void testFormatPeriod() {
221         Calendar JavaDoc cal1970 = Calendar.getInstance();
222         cal1970.set(1970, 0, 1, 0, 0, 0);
223         cal1970.set(Calendar.MILLISECOND, 0);
224         long time1970 = cal1970.getTime().getTime();
225         
226         assertEquals( "0", DurationFormatUtils.formatPeriod(time1970, time1970, "y") );
227         assertEquals( "0", DurationFormatUtils.formatPeriod(time1970, time1970, "M") );
228         assertEquals( "0", DurationFormatUtils.formatPeriod(time1970, time1970, "d") );
229         assertEquals( "0", DurationFormatUtils.formatPeriod(time1970, time1970, "H") );
230         assertEquals( "0", DurationFormatUtils.formatPeriod(time1970, time1970, "m") );
231         assertEquals( "0", DurationFormatUtils.formatPeriod(time1970, time1970, "s") );
232         assertEquals( "0", DurationFormatUtils.formatPeriod(time1970, time1970, "S") );
233         assertEquals( "0000", DurationFormatUtils.formatPeriod(time1970, time1970, "SSSS") );
234         assertEquals( "0000", DurationFormatUtils.formatPeriod(time1970, time1970, "yyyy") );
235         assertEquals( "0000", DurationFormatUtils.formatPeriod(time1970, time1970, "yyMM") );
236
237         long time = time1970 + 60 * 1000;
238         assertEquals( "0", DurationFormatUtils.formatPeriod(time1970, time, "y") );
239         assertEquals( "0", DurationFormatUtils.formatPeriod(time1970, time, "M") );
240         assertEquals( "0", DurationFormatUtils.formatPeriod(time1970, time, "d") );
241         assertEquals( "0", DurationFormatUtils.formatPeriod(time1970, time, "H") );
242         assertEquals( "1", DurationFormatUtils.formatPeriod(time1970, time, "m") );
243         assertEquals( "60", DurationFormatUtils.formatPeriod(time1970, time, "s") );
244         assertEquals( "60000", DurationFormatUtils.formatPeriod(time1970, time, "S") );
245         assertEquals( "01:00", DurationFormatUtils.formatPeriod(time1970, time, "mm:ss") );
246
247         Calendar JavaDoc cal = Calendar.getInstance();
248         cal.set(1973, 6, 1, 0, 0, 0);
249         cal.set(Calendar.MILLISECOND, 0);
250         time = cal.getTime().getTime();
251         assertEquals( "36", DurationFormatUtils.formatPeriod(time1970, time, "yM") );
252         assertEquals( "3 years 6 months", DurationFormatUtils.formatPeriod(time1970, time, "y' years 'M' months'") );
253         assertEquals( "03/06", DurationFormatUtils.formatPeriod(time1970, time, "yy/MM") );
254
255         cal.set(1973, 10, 1, 0, 0, 0);
256         cal.set(Calendar.MILLISECOND, 0);
257         time = cal.getTime().getTime();
258         assertEquals( "310", DurationFormatUtils.formatPeriod(time1970, time, "yM") );
259         assertEquals( "3 years 10 months", DurationFormatUtils.formatPeriod(time1970, time, "y' years 'M' months'") );
260         assertEquals( "03/10", DurationFormatUtils.formatPeriod(time1970, time, "yy/MM") );
261
262         cal.set(1974, 0, 1, 0, 0, 0);
263         cal.set(Calendar.MILLISECOND, 0);
264         time = cal.getTime().getTime();
265         assertEquals( "40", DurationFormatUtils.formatPeriod(time1970, time, "yM") );
266         assertEquals( "4 years 0 months", DurationFormatUtils.formatPeriod(time1970, time, "y' years 'M' months'") );
267         assertEquals( "04/00", DurationFormatUtils.formatPeriod(time1970, time, "yy/MM") );
268         assertEquals( "48", DurationFormatUtils.formatPeriod(time1970, time, "M") );
269         assertEquals( "48", DurationFormatUtils.formatPeriod(time1970, time, "MM") );
270         assertEquals( "048", DurationFormatUtils.formatPeriod(time1970, time, "MMM") );
271     }
272
273     public void testLexx() {
274         // tests each constant
275
assertArrayEquals(
276           new DurationFormatUtils.Token[] {
277             new DurationFormatUtils.Token( DurationFormatUtils.y, 1),
278             new DurationFormatUtils.Token( DurationFormatUtils.M, 1),
279             new DurationFormatUtils.Token( DurationFormatUtils.d, 1),
280             new DurationFormatUtils.Token( DurationFormatUtils.H, 1),
281             new DurationFormatUtils.Token( DurationFormatUtils.m, 1),
282             new DurationFormatUtils.Token( DurationFormatUtils.s, 1),
283             new DurationFormatUtils.Token( DurationFormatUtils.S, 1)
284           }, DurationFormatUtils.lexx("yMdHmsS")
285         );
286
287         // tests the ISO8601-like
288
assertArrayEquals(
289           new DurationFormatUtils.Token[] {
290             new DurationFormatUtils.Token( DurationFormatUtils.H, 1),
291             new DurationFormatUtils.Token( new StringBuffer JavaDoc(":"), 1),
292             new DurationFormatUtils.Token( DurationFormatUtils.m, 2),
293             new DurationFormatUtils.Token( new StringBuffer JavaDoc(":"), 1),
294             new DurationFormatUtils.Token( DurationFormatUtils.s, 2),
295             new DurationFormatUtils.Token( new StringBuffer JavaDoc("."), 1),
296             new DurationFormatUtils.Token( DurationFormatUtils.S, 3)
297           }, DurationFormatUtils.lexx("H:mm:ss.SSS")
298         );
299
300         // test the iso extended format
301
assertArrayEquals(
302           new DurationFormatUtils.Token[] {
303             new DurationFormatUtils.Token( new StringBuffer JavaDoc("P"), 1),
304             new DurationFormatUtils.Token( DurationFormatUtils.y, 4),
305             new DurationFormatUtils.Token( new StringBuffer JavaDoc("Y"), 1),
306             new DurationFormatUtils.Token( DurationFormatUtils.M, 1),
307             new DurationFormatUtils.Token( new StringBuffer JavaDoc("M"), 1),
308             new DurationFormatUtils.Token( DurationFormatUtils.d, 1),
309             new DurationFormatUtils.Token( new StringBuffer JavaDoc("DT"), 1),
310             new DurationFormatUtils.Token( DurationFormatUtils.H, 1),
311             new DurationFormatUtils.Token( new StringBuffer JavaDoc("H"), 1),
312             new DurationFormatUtils.Token( DurationFormatUtils.m, 1),
313             new DurationFormatUtils.Token( new StringBuffer JavaDoc("M"), 1),
314             new DurationFormatUtils.Token( DurationFormatUtils.s, 1),
315             new DurationFormatUtils.Token( new StringBuffer JavaDoc("."), 1),
316             new DurationFormatUtils.Token( DurationFormatUtils.S, 1),
317             new DurationFormatUtils.Token( new StringBuffer JavaDoc("S"), 1)
318           },
319           DurationFormatUtils.lexx(DurationFormatUtils.ISO_EXTENDED_FORMAT_PATTERN)
320         );
321
322         // test failures in equals
323
DurationFormatUtils.Token token = new DurationFormatUtils.Token( DurationFormatUtils.y, 4 );
324         assertFalse( "Token equal to non-Token class. ", token.equals(new Object JavaDoc()) );
325         assertFalse( "Token equal to Token with wrong value class. ", token.equals(
326             new DurationFormatUtils.Token( new Object JavaDoc() )
327         ) );
328         assertFalse( "Token equal to Token with different count. ", token.equals(
329             new DurationFormatUtils.Token( DurationFormatUtils.y, 1 )
330         ) );
331         DurationFormatUtils.Token numToken = new DurationFormatUtils.Token( new Integer JavaDoc(1), 4 );
332         assertTrue( "Token with Number value not equal to itself. ", numToken.equals( numToken ) );
333     }
334     private void assertArrayEquals(DurationFormatUtils.Token[] obj1, DurationFormatUtils.Token[] obj2) {
335         assertEquals( "Arrays are unequal length. ", obj1.length, obj2.length );
336         for(int i=0; i<obj1.length; i++) {
337             assertTrue( "Index " + i + " not equal, " + obj1[i] + " vs " + obj2, obj1[i].equals(obj2[i]));
338         }
339     }
340
341 }
342
Popular Tags