001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.commons.lang.time;
019:
020: import java.lang.reflect.Constructor;
021: import java.lang.reflect.Modifier;
022: import java.util.Calendar;
023: import java.util.TimeZone;
024:
025: import junit.framework.Test;
026: import junit.framework.TestCase;
027: import junit.framework.TestSuite;
028: import junit.textui.TestRunner;
029:
030: /**
031: * TestCase for DurationFormatUtils.
032: *
033: * @author Apache Ant - DateUtilsTest
034: * @author <a href="mailto:sbailliez@apache.org">Stephane Bailliez</a>
035: * @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a>
036: * @author Stephen Colebourne
037: * @author <a href="mailto:ggregory@seagullsw.com">Gary Gregory</a>
038: */
039: public class DurationFormatUtilsTest extends TestCase {
040:
041: public static void main(String[] args) {
042: TestRunner.run(suite());
043: }
044:
045: public static Test suite() {
046: TestSuite suite = new TestSuite(DurationFormatUtilsTest.class);
047: suite.setName("DurationFormatUtils Tests");
048: return suite;
049: }
050:
051: public DurationFormatUtilsTest(String s) {
052: super (s);
053: }
054:
055: // -----------------------------------------------------------------------
056: public void testConstructor() {
057: assertNotNull(new DurationFormatUtils());
058: Constructor[] cons = DurationFormatUtils.class
059: .getDeclaredConstructors();
060: assertEquals(1, cons.length);
061: assertEquals(true, Modifier.isPublic(cons[0].getModifiers()));
062: assertEquals(true, Modifier.isPublic(DurationFormatUtils.class
063: .getModifiers()));
064: assertEquals(false, Modifier.isFinal(DurationFormatUtils.class
065: .getModifiers()));
066: }
067:
068: // -----------------------------------------------------------------------
069: public void testFormatDurationWords() {
070: String text = null;
071:
072: text = DurationFormatUtils.formatDurationWords(50 * 1000, true,
073: false);
074: assertEquals("50 seconds", text);
075: text = DurationFormatUtils.formatDurationWords(65 * 1000, true,
076: false);
077: assertEquals("1 minute 5 seconds", text);
078: text = DurationFormatUtils.formatDurationWords(120 * 1000,
079: true, false);
080: assertEquals("2 minutes 0 seconds", text);
081: text = DurationFormatUtils.formatDurationWords(121 * 1000,
082: true, false);
083: assertEquals("2 minutes 1 second", text);
084: text = DurationFormatUtils.formatDurationWords(72 * 60 * 1000,
085: true, false);
086: assertEquals("1 hour 12 minutes 0 seconds", text);
087: text = DurationFormatUtils.formatDurationWords(
088: 24 * 60 * 60 * 1000, true, false);
089: assertEquals("1 day 0 hours 0 minutes 0 seconds", text);
090:
091: text = DurationFormatUtils.formatDurationWords(50 * 1000, true,
092: true);
093: assertEquals("50 seconds", text);
094: text = DurationFormatUtils.formatDurationWords(65 * 1000, true,
095: true);
096: assertEquals("1 minute 5 seconds", text);
097: text = DurationFormatUtils.formatDurationWords(120 * 1000,
098: true, true);
099: assertEquals("2 minutes", text);
100: text = DurationFormatUtils.formatDurationWords(121 * 1000,
101: true, true);
102: assertEquals("2 minutes 1 second", text);
103: text = DurationFormatUtils.formatDurationWords(72 * 60 * 1000,
104: true, true);
105: assertEquals("1 hour 12 minutes", text);
106: text = DurationFormatUtils.formatDurationWords(
107: 24 * 60 * 60 * 1000, true, true);
108: assertEquals("1 day", text);
109:
110: text = DurationFormatUtils.formatDurationWords(50 * 1000,
111: false, true);
112: assertEquals("0 days 0 hours 0 minutes 50 seconds", text);
113: text = DurationFormatUtils.formatDurationWords(65 * 1000,
114: false, true);
115: assertEquals("0 days 0 hours 1 minute 5 seconds", text);
116: text = DurationFormatUtils.formatDurationWords(120 * 1000,
117: false, true);
118: assertEquals("0 days 0 hours 2 minutes", text);
119: text = DurationFormatUtils.formatDurationWords(121 * 1000,
120: false, true);
121: assertEquals("0 days 0 hours 2 minutes 1 second", text);
122: text = DurationFormatUtils.formatDurationWords(72 * 60 * 1000,
123: false, true);
124: assertEquals("0 days 1 hour 12 minutes", text);
125: text = DurationFormatUtils.formatDurationWords(
126: 24 * 60 * 60 * 1000, false, true);
127: assertEquals("1 day", text);
128:
129: text = DurationFormatUtils.formatDurationWords(50 * 1000,
130: false, false);
131: assertEquals("0 days 0 hours 0 minutes 50 seconds", text);
132: text = DurationFormatUtils.formatDurationWords(65 * 1000,
133: false, false);
134: assertEquals("0 days 0 hours 1 minute 5 seconds", text);
135: text = DurationFormatUtils.formatDurationWords(120 * 1000,
136: false, false);
137: assertEquals("0 days 0 hours 2 minutes 0 seconds", text);
138: text = DurationFormatUtils.formatDurationWords(121 * 1000,
139: false, false);
140: assertEquals("0 days 0 hours 2 minutes 1 second", text);
141: text = DurationFormatUtils.formatDurationWords(72 * 60 * 1000,
142: false, false);
143: assertEquals("0 days 1 hour 12 minutes 0 seconds", text);
144: text = DurationFormatUtils.formatDurationWords(24 * 60 * 60
145: * 1000 + 72 * 60 * 1000, false, false);
146: assertEquals("1 day 1 hour 12 minutes 0 seconds", text);
147: text = DurationFormatUtils.formatDurationWords(2 * 24 * 60 * 60
148: * 1000 + 72 * 60 * 1000, false, false);
149: assertEquals("2 days 1 hour 12 minutes 0 seconds", text);
150: for (int i = 2; i < 31; i++) {
151: text = DurationFormatUtils.formatDurationWords(i * 24 * 60
152: * 60 * 1000, false, false);
153: // assertEquals(i + " days 0 hours 0 minutes 0 seconds", text);
154: //
155: // junit.framework.ComparisonFailure: expected:<25 days 0 hours 0 minutes 0...> but was:<-24 days -17 hours
156: // -2 minutes -47...>
157: // at junit.framework.Assert.assertEquals(Assert.java:81)
158: // at junit.framework.Assert.assertEquals(Assert.java:87)
159: // at
160: // org.apache.commons.lang.time.DurationFormatUtilsTest.testFormatDurationWords(DurationFormatUtilsTest.java:124)
161: // at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
162: // at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
163: // at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
164: // at java.lang.reflect.Method.invoke(Method.java:324)
165: // at junit.framework.TestCase.runTest(TestCase.java:154)
166: // at junit.framework.TestCase.runBare(TestCase.java:127)
167: // at junit.framework.TestResult$1.protect(TestResult.java:106)
168: // at junit.framework.TestResult.runProtected(TestResult.java:124)
169: // at junit.framework.TestResult.run(TestResult.java:109)
170: // at junit.framework.TestCase.run(TestCase.java:118)
171: // at junit.framework.TestSuite.runTest(TestSuite.java:208)
172: // at junit.framework.TestSuite.run(TestSuite.java:203)
173: // at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:478)
174: // at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:344)
175: // at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
176: }
177: }
178:
179: /**
180: * Tests that "1 <unit>s" gets converted to "1 <unit>" but that "11 <unit>s" is left alone.
181: */
182: public void testFormatDurationPluralWords() {
183: long oneSecond = 1000;
184: long oneMinute = oneSecond * 60;
185: long oneHour = oneMinute * 60;
186: long oneDay = oneHour * 24;
187: String text = null;
188:
189: text = DurationFormatUtils.formatDurationWords(oneSecond,
190: false, false);
191: assertEquals("0 days 0 hours 0 minutes 1 second", text);
192: text = DurationFormatUtils.formatDurationWords(oneSecond * 2,
193: false, false);
194: assertEquals("0 days 0 hours 0 minutes 2 seconds", text);
195: text = DurationFormatUtils.formatDurationWords(oneSecond * 11,
196: false, false);
197: assertEquals("0 days 0 hours 0 minutes 11 seconds", text);
198:
199: text = DurationFormatUtils.formatDurationWords(oneMinute,
200: false, false);
201: assertEquals("0 days 0 hours 1 minute 0 seconds", text);
202: text = DurationFormatUtils.formatDurationWords(oneMinute * 2,
203: false, false);
204: assertEquals("0 days 0 hours 2 minutes 0 seconds", text);
205: text = DurationFormatUtils.formatDurationWords(oneMinute * 11,
206: false, false);
207: assertEquals("0 days 0 hours 11 minutes 0 seconds", text);
208: text = DurationFormatUtils.formatDurationWords(oneMinute
209: + oneSecond, false, false);
210: assertEquals("0 days 0 hours 1 minute 1 second", text);
211:
212: text = DurationFormatUtils.formatDurationWords(oneHour, false,
213: false);
214: assertEquals("0 days 1 hour 0 minutes 0 seconds", text);
215: text = DurationFormatUtils.formatDurationWords(oneHour * 2,
216: false, false);
217: assertEquals("0 days 2 hours 0 minutes 0 seconds", text);
218: text = DurationFormatUtils.formatDurationWords(oneHour * 11,
219: false, false);
220: assertEquals("0 days 11 hours 0 minutes 0 seconds", text);
221: text = DurationFormatUtils.formatDurationWords(oneHour
222: + oneMinute + oneSecond, false, false);
223: assertEquals("0 days 1 hour 1 minute 1 second", text);
224:
225: text = DurationFormatUtils.formatDurationWords(oneDay, false,
226: false);
227: assertEquals("1 day 0 hours 0 minutes 0 seconds", text);
228: text = DurationFormatUtils.formatDurationWords(oneDay * 2,
229: false, false);
230: assertEquals("2 days 0 hours 0 minutes 0 seconds", text);
231: text = DurationFormatUtils.formatDurationWords(oneDay * 11,
232: false, false);
233: assertEquals("11 days 0 hours 0 minutes 0 seconds", text);
234: text = DurationFormatUtils.formatDurationWords(oneDay + oneHour
235: + oneMinute + oneSecond, false, false);
236: assertEquals("1 day 1 hour 1 minute 1 second", text);
237: }
238:
239: public void testFormatDurationHMS() {
240: long time = 0;
241: assertEquals("0:00:00.000", DurationFormatUtils
242: .formatDurationHMS(time));
243:
244: time = 1;
245: assertEquals("0:00:00.001", DurationFormatUtils
246: .formatDurationHMS(time));
247:
248: time = 15;
249: assertEquals("0:00:00.015", DurationFormatUtils
250: .formatDurationHMS(time));
251:
252: time = 165;
253: assertEquals("0:00:00.165", DurationFormatUtils
254: .formatDurationHMS(time));
255:
256: time = 1675;
257: assertEquals("0:00:01.675", DurationFormatUtils
258: .formatDurationHMS(time));
259:
260: time = 13465;
261: assertEquals("0:00:13.465", DurationFormatUtils
262: .formatDurationHMS(time));
263:
264: time = 72789;
265: assertEquals("0:01:12.789", DurationFormatUtils
266: .formatDurationHMS(time));
267:
268: time = 12789 + 32 * 60000;
269: assertEquals("0:32:12.789", DurationFormatUtils
270: .formatDurationHMS(time));
271:
272: time = 12789 + 62 * 60000;
273: assertEquals("1:02:12.789", DurationFormatUtils
274: .formatDurationHMS(time));
275: }
276:
277: public void testFormatDurationISO() {
278: assertEquals("P0Y0M0DT0H0M0.000S", DurationFormatUtils
279: .formatDurationISO(0L));
280: assertEquals("P0Y0M0DT0H0M0.001S", DurationFormatUtils
281: .formatDurationISO(1L));
282: assertEquals("P0Y0M0DT0H0M0.010S", DurationFormatUtils
283: .formatDurationISO(10L));
284: assertEquals("P0Y0M0DT0H0M0.100S", DurationFormatUtils
285: .formatDurationISO(100L));
286: assertEquals("P0Y0M0DT0H1M15.321S", DurationFormatUtils
287: .formatDurationISO(75321L));
288: }
289:
290: public void testFormatDuration() {
291: long duration = 0;
292: assertEquals("0", DurationFormatUtils.formatDuration(duration,
293: "y"));
294: assertEquals("0", DurationFormatUtils.formatDuration(duration,
295: "M"));
296: assertEquals("0", DurationFormatUtils.formatDuration(duration,
297: "d"));
298: assertEquals("0", DurationFormatUtils.formatDuration(duration,
299: "H"));
300: assertEquals("0", DurationFormatUtils.formatDuration(duration,
301: "m"));
302: assertEquals("0", DurationFormatUtils.formatDuration(duration,
303: "s"));
304: assertEquals("0", DurationFormatUtils.formatDuration(duration,
305: "S"));
306: assertEquals("0000", DurationFormatUtils.formatDuration(
307: duration, "SSSS"));
308: assertEquals("0000", DurationFormatUtils.formatDuration(
309: duration, "yyyy"));
310: assertEquals("0000", DurationFormatUtils.formatDuration(
311: duration, "yyMM"));
312:
313: duration = 60 * 1000;
314: assertEquals("0", DurationFormatUtils.formatDuration(duration,
315: "y"));
316: assertEquals("0", DurationFormatUtils.formatDuration(duration,
317: "M"));
318: assertEquals("0", DurationFormatUtils.formatDuration(duration,
319: "d"));
320: assertEquals("0", DurationFormatUtils.formatDuration(duration,
321: "H"));
322: assertEquals("1", DurationFormatUtils.formatDuration(duration,
323: "m"));
324: assertEquals("60", DurationFormatUtils.formatDuration(duration,
325: "s"));
326: assertEquals("60000", DurationFormatUtils.formatDuration(
327: duration, "S"));
328: assertEquals("01:00", DurationFormatUtils.formatDuration(
329: duration, "mm:ss"));
330:
331: Calendar base = Calendar.getInstance();
332: base.set(2000, 0, 1, 0, 0, 0);
333: base.set(Calendar.MILLISECOND, 0);
334:
335: Calendar cal = Calendar.getInstance();
336: cal.set(2003, 1, 1, 0, 0, 0);
337: cal.set(Calendar.MILLISECOND, 0);
338: duration = cal.getTime().getTime() - base.getTime().getTime(); // duration from 2000-01-01 to cal
339: // don't use 1970 in test as time zones were less reliable in 1970 than now
340: // remember that duration formatting ignores time zones, working on strict hour lengths
341: int days = 366 + 365 + 365 + 31;
342: assertEquals("0 0 " + days, DurationFormatUtils.formatDuration(
343: duration, "y M d"));
344: }
345:
346: public void testFormatPeriodISO() {
347: TimeZone timeZone = TimeZone.getTimeZone("GMT-3");
348: Calendar base = Calendar.getInstance(timeZone);
349: base.set(1970, 0, 1, 0, 0, 0);
350: base.set(Calendar.MILLISECOND, 0);
351:
352: Calendar cal = Calendar.getInstance(timeZone);
353: cal.set(2002, 1, 23, 9, 11, 12);
354: cal.set(Calendar.MILLISECOND, 1);
355: String text;
356: // repeat a test from testDateTimeISO to compare extended and not extended.
357: text = DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT
358: .format(cal);
359: assertEquals("2002-02-23T09:11:12-03:00", text);
360: // test fixture is the same as above, but now with extended format.
361: text = DurationFormatUtils.formatPeriod(base.getTime()
362: .getTime(), cal.getTime().getTime(),
363: DurationFormatUtils.ISO_EXTENDED_FORMAT_PATTERN, false,
364: timeZone);
365: assertEquals("P32Y1M22DT9H11M12.001S", text);
366: // test fixture from example in http://www.w3.org/TR/xmlschema-2/#duration
367: cal.set(1971, 1, 3, 10, 30, 0);
368: cal.set(Calendar.MILLISECOND, 0);
369: text = DurationFormatUtils.formatPeriod(base.getTime()
370: .getTime(), cal.getTime().getTime(),
371: DurationFormatUtils.ISO_EXTENDED_FORMAT_PATTERN, false,
372: timeZone);
373: assertEquals("P1Y1M2DT10H30M0.000S", text);
374: // want a way to say 'don't print the seconds in format()' or other fields for that matter:
375: // assertEquals("P1Y2M3DT10H30M", text);
376: }
377:
378: public void testFormatPeriod() {
379: Calendar cal1970 = Calendar.getInstance();
380: cal1970.set(1970, 0, 1, 0, 0, 0);
381: cal1970.set(Calendar.MILLISECOND, 0);
382: long time1970 = cal1970.getTime().getTime();
383:
384: assertEquals("0", DurationFormatUtils.formatPeriod(time1970,
385: time1970, "y"));
386: assertEquals("0", DurationFormatUtils.formatPeriod(time1970,
387: time1970, "M"));
388: assertEquals("0", DurationFormatUtils.formatPeriod(time1970,
389: time1970, "d"));
390: assertEquals("0", DurationFormatUtils.formatPeriod(time1970,
391: time1970, "H"));
392: assertEquals("0", DurationFormatUtils.formatPeriod(time1970,
393: time1970, "m"));
394: assertEquals("0", DurationFormatUtils.formatPeriod(time1970,
395: time1970, "s"));
396: assertEquals("0", DurationFormatUtils.formatPeriod(time1970,
397: time1970, "S"));
398: assertEquals("0000", DurationFormatUtils.formatPeriod(time1970,
399: time1970, "SSSS"));
400: assertEquals("0000", DurationFormatUtils.formatPeriod(time1970,
401: time1970, "yyyy"));
402: assertEquals("0000", DurationFormatUtils.formatPeriod(time1970,
403: time1970, "yyMM"));
404:
405: long time = time1970 + 60 * 1000;
406: assertEquals("0", DurationFormatUtils.formatPeriod(time1970,
407: time, "y"));
408: assertEquals("0", DurationFormatUtils.formatPeriod(time1970,
409: time, "M"));
410: assertEquals("0", DurationFormatUtils.formatPeriod(time1970,
411: time, "d"));
412: assertEquals("0", DurationFormatUtils.formatPeriod(time1970,
413: time, "H"));
414: assertEquals("1", DurationFormatUtils.formatPeriod(time1970,
415: time, "m"));
416: assertEquals("60", DurationFormatUtils.formatPeriod(time1970,
417: time, "s"));
418: assertEquals("60000", DurationFormatUtils.formatPeriod(
419: time1970, time, "S"));
420: assertEquals("01:00", DurationFormatUtils.formatPeriod(
421: time1970, time, "mm:ss"));
422:
423: Calendar cal = Calendar.getInstance();
424: cal.set(1973, 6, 1, 0, 0, 0);
425: cal.set(Calendar.MILLISECOND, 0);
426: time = cal.getTime().getTime();
427: assertEquals("36", DurationFormatUtils.formatPeriod(time1970,
428: time, "yM"));
429: assertEquals("3 years 6 months", DurationFormatUtils
430: .formatPeriod(time1970, time, "y' years 'M' months'"));
431: assertEquals("03/06", DurationFormatUtils.formatPeriod(
432: time1970, time, "yy/MM"));
433:
434: cal.set(1973, 10, 1, 0, 0, 0);
435: cal.set(Calendar.MILLISECOND, 0);
436: time = cal.getTime().getTime();
437: assertEquals("310", DurationFormatUtils.formatPeriod(time1970,
438: time, "yM"));
439: assertEquals("3 years 10 months", DurationFormatUtils
440: .formatPeriod(time1970, time, "y' years 'M' months'"));
441: assertEquals("03/10", DurationFormatUtils.formatPeriod(
442: time1970, time, "yy/MM"));
443:
444: cal.set(1974, 0, 1, 0, 0, 0);
445: cal.set(Calendar.MILLISECOND, 0);
446: time = cal.getTime().getTime();
447: assertEquals("40", DurationFormatUtils.formatPeriod(time1970,
448: time, "yM"));
449: assertEquals("4 years 0 months", DurationFormatUtils
450: .formatPeriod(time1970, time, "y' years 'M' months'"));
451: assertEquals("04/00", DurationFormatUtils.formatPeriod(
452: time1970, time, "yy/MM"));
453: assertEquals("48", DurationFormatUtils.formatPeriod(time1970,
454: time, "M"));
455: assertEquals("48", DurationFormatUtils.formatPeriod(time1970,
456: time, "MM"));
457: assertEquals("048", DurationFormatUtils.formatPeriod(time1970,
458: time, "MMM"));
459: }
460:
461: public void testLexx() {
462: // tests each constant
463: assertArrayEquals(
464: new DurationFormatUtils.Token[] {
465: new DurationFormatUtils.Token(
466: DurationFormatUtils.y, 1),
467: new DurationFormatUtils.Token(
468: DurationFormatUtils.M, 1),
469: new DurationFormatUtils.Token(
470: DurationFormatUtils.d, 1),
471: new DurationFormatUtils.Token(
472: DurationFormatUtils.H, 1),
473: new DurationFormatUtils.Token(
474: DurationFormatUtils.m, 1),
475: new DurationFormatUtils.Token(
476: DurationFormatUtils.s, 1),
477: new DurationFormatUtils.Token(
478: DurationFormatUtils.S, 1) },
479: DurationFormatUtils.lexx("yMdHmsS"));
480:
481: // tests the ISO8601-like
482: assertArrayEquals(
483: new DurationFormatUtils.Token[] {
484: new DurationFormatUtils.Token(
485: DurationFormatUtils.H, 1),
486: new DurationFormatUtils.Token(new StringBuffer(
487: ":"), 1),
488: new DurationFormatUtils.Token(
489: DurationFormatUtils.m, 2),
490: new DurationFormatUtils.Token(new StringBuffer(
491: ":"), 1),
492: new DurationFormatUtils.Token(
493: DurationFormatUtils.s, 2),
494: new DurationFormatUtils.Token(new StringBuffer(
495: "."), 1),
496: new DurationFormatUtils.Token(
497: DurationFormatUtils.S, 3) },
498: DurationFormatUtils.lexx("H:mm:ss.SSS"));
499:
500: // test the iso extended format
501: assertArrayEquals(
502: new DurationFormatUtils.Token[] {
503: new DurationFormatUtils.Token(new StringBuffer(
504: "P"), 1),
505: new DurationFormatUtils.Token(
506: DurationFormatUtils.y, 4),
507: new DurationFormatUtils.Token(new StringBuffer(
508: "Y"), 1),
509: new DurationFormatUtils.Token(
510: DurationFormatUtils.M, 1),
511: new DurationFormatUtils.Token(new StringBuffer(
512: "M"), 1),
513: new DurationFormatUtils.Token(
514: DurationFormatUtils.d, 1),
515: new DurationFormatUtils.Token(new StringBuffer(
516: "DT"), 1),
517: new DurationFormatUtils.Token(
518: DurationFormatUtils.H, 1),
519: new DurationFormatUtils.Token(new StringBuffer(
520: "H"), 1),
521: new DurationFormatUtils.Token(
522: DurationFormatUtils.m, 1),
523: new DurationFormatUtils.Token(new StringBuffer(
524: "M"), 1),
525: new DurationFormatUtils.Token(
526: DurationFormatUtils.s, 1),
527: new DurationFormatUtils.Token(new StringBuffer(
528: "."), 1),
529: new DurationFormatUtils.Token(
530: DurationFormatUtils.S, 1),
531: new DurationFormatUtils.Token(new StringBuffer(
532: "S"), 1) },
533: DurationFormatUtils
534: .lexx(DurationFormatUtils.ISO_EXTENDED_FORMAT_PATTERN));
535:
536: // test failures in equals
537: DurationFormatUtils.Token token = new DurationFormatUtils.Token(
538: DurationFormatUtils.y, 4);
539: assertFalse("Token equal to non-Token class. ", token
540: .equals(new Object()));
541: assertFalse("Token equal to Token with wrong value class. ",
542: token
543: .equals(new DurationFormatUtils.Token(
544: new Object())));
545: assertFalse("Token equal to Token with different count. ",
546: token.equals(new DurationFormatUtils.Token(
547: DurationFormatUtils.y, 1)));
548: DurationFormatUtils.Token numToken = new DurationFormatUtils.Token(
549: new Integer(1), 4);
550: assertTrue("Token with Number value not equal to itself. ",
551: numToken.equals(numToken));
552: }
553:
554: // http://issues.apache.org/bugzilla/show_bug.cgi?id=38401
555: public void testBugzilla38401() {
556: assertEqualDuration("0000/00/30 16:00:00 000", new int[] {
557: 2006, 0, 26, 18, 47, 34 }, new int[] { 2006, 1, 26, 10,
558: 47, 34 }, "yyyy/MM/dd HH:mm:ss SSS");
559: }
560:
561: // https://issues.apache.org/jira/browse/LANG-281
562: public void testJiraLang281() {
563: assertEqualDuration("09", new int[] { 2005, 11, 31, 0, 0, 0 },
564: new int[] { 2006, 9, 6, 0, 0, 0 }, "MM");
565: }
566:
567: // Testing the under a day range in DurationFormatUtils.formatPeriod
568: public void testLowDurations() {
569: for (int hr = 0; hr < 24; hr++) {
570: for (int min = 0; min < 60; min++) {
571: for (int sec = 0; sec < 60; sec++) {
572: assertEqualDuration(hr + ":" + min + ":" + sec,
573: new int[] { 2000, 0, 1, 0, 0, 0, 0 },
574: new int[] { 2000, 0, 1, hr, min, sec },
575: "H:m:s");
576: }
577: }
578: }
579: }
580:
581: // Attempting to test edge cases in DurationFormatUtils.formatPeriod
582: public void testEdgeDurations() {
583: assertEqualDuration("01", new int[] { 2006, 0, 15, 0, 0, 0 },
584: new int[] { 2006, 2, 10, 0, 0, 0 }, "MM");
585: assertEqualDuration("12", new int[] { 2005, 0, 15, 0, 0, 0 },
586: new int[] { 2006, 0, 15, 0, 0, 0 }, "MM");
587: assertEqualDuration("12", new int[] { 2005, 0, 15, 0, 0, 0 },
588: new int[] { 2006, 0, 16, 0, 0, 0 }, "MM");
589: assertEqualDuration("11", new int[] { 2005, 0, 15, 0, 0, 0 },
590: new int[] { 2006, 0, 14, 0, 0, 0 }, "MM");
591:
592: assertEqualDuration("01 26",
593: new int[] { 2006, 0, 15, 0, 0, 0 }, new int[] { 2006,
594: 2, 10, 0, 0, 0 }, "MM dd");
595: assertEqualDuration("54", new int[] { 2006, 0, 15, 0, 0, 0 },
596: new int[] { 2006, 2, 10, 0, 0, 0 }, "dd");
597:
598: assertEqualDuration("09 12",
599: new int[] { 2006, 1, 20, 0, 0, 0 }, new int[] { 2006,
600: 11, 4, 0, 0, 0 }, "MM dd");
601: assertEqualDuration("287", new int[] { 2006, 1, 20, 0, 0, 0 },
602: new int[] { 2006, 11, 4, 0, 0, 0 }, "dd");
603:
604: assertEqualDuration("11 30", new int[] { 2006, 0, 2, 0, 0, 0 },
605: new int[] { 2007, 0, 1, 0, 0, 0 }, "MM dd");
606: assertEqualDuration("364", new int[] { 2006, 0, 2, 0, 0, 0 },
607: new int[] { 2007, 0, 1, 0, 0, 0 }, "dd");
608:
609: assertEqualDuration("12 00", new int[] { 2006, 0, 1, 0, 0, 0 },
610: new int[] { 2007, 0, 1, 0, 0, 0 }, "MM dd");
611: assertEqualDuration("365", new int[] { 2006, 0, 1, 0, 0, 0 },
612: new int[] { 2007, 0, 1, 0, 0, 0 }, "dd");
613:
614: assertEqualDuration("31", new int[] { 2006, 0, 1, 0, 0, 0 },
615: new int[] { 2006, 1, 1, 0, 0, 0 }, "dd");
616:
617: assertEqualDuration("92", new int[] { 2005, 9, 1, 0, 0, 0 },
618: new int[] { 2006, 0, 1, 0, 0, 0 }, "dd");
619: assertEqualDuration("77", new int[] { 2005, 9, 16, 0, 0, 0 },
620: new int[] { 2006, 0, 1, 0, 0, 0 }, "dd");
621:
622: // test month larger in start than end
623: assertEqualDuration("136", new int[] { 2005, 9, 16, 0, 0, 0 },
624: new int[] { 2006, 2, 1, 0, 0, 0 }, "dd");
625: // test when start in leap year
626: assertEqualDuration("136", new int[] { 2004, 9, 16, 0, 0, 0 },
627: new int[] { 2005, 2, 1, 0, 0, 0 }, "dd");
628: // test when end in leap year
629: assertEqualDuration("137", new int[] { 2003, 9, 16, 0, 0, 0 },
630: new int[] { 2004, 2, 1, 0, 0, 0 }, "dd");
631: // test when end in leap year but less than end of feb
632: assertEqualDuration("135", new int[] { 2003, 9, 16, 0, 0, 0 },
633: new int[] { 2004, 1, 28, 0, 0, 0 }, "dd");
634:
635: assertEqualDuration("364", new int[] { 2007, 0, 2, 0, 0, 0 },
636: new int[] { 2008, 0, 1, 0, 0, 0 }, "dd");
637: assertEqualDuration("729", new int[] { 2006, 0, 2, 0, 0, 0 },
638: new int[] { 2008, 0, 1, 0, 0, 0 }, "dd");
639:
640: assertEqualDuration("365", new int[] { 2007, 2, 2, 0, 0, 0 },
641: new int[] { 2008, 2, 1, 0, 0, 0 }, "dd");
642: assertEqualDuration("333", new int[] { 2007, 1, 2, 0, 0, 0 },
643: new int[] { 2008, 0, 1, 0, 0, 0 }, "dd");
644:
645: assertEqualDuration("28", new int[] { 2008, 1, 2, 0, 0, 0 },
646: new int[] { 2008, 2, 1, 0, 0, 0 }, "dd");
647: assertEqualDuration("393", new int[] { 2007, 1, 2, 0, 0, 0 },
648: new int[] { 2008, 2, 1, 0, 0, 0 }, "dd");
649:
650: assertEqualDuration("369", new int[] { 2004, 0, 29, 0, 0, 0 },
651: new int[] { 2005, 1, 1, 0, 0, 0 }, "dd");
652:
653: assertEqualDuration("338", new int[] { 2004, 1, 29, 0, 0, 0 },
654: new int[] { 2005, 1, 1, 0, 0, 0 }, "dd");
655:
656: assertEqualDuration("28", new int[] { 2004, 2, 8, 0, 0, 0 },
657: new int[] { 2004, 3, 5, 0, 0, 0 }, "dd");
658:
659: assertEqualDuration("48", new int[] { 1992, 1, 29, 0, 0, 0 },
660: new int[] { 1996, 1, 29, 0, 0, 0 }, "M");
661:
662: // this seems odd - and will fail if I throw it in as a brute force
663: // below as it expects the answer to be 12. It's a tricky edge case
664: assertEqualDuration("11", new int[] { 1996, 1, 29, 0, 0, 0 },
665: new int[] { 1997, 1, 28, 0, 0, 0 }, "M");
666: // again - this seems odd
667: assertEqualDuration("11 28",
668: new int[] { 1996, 1, 29, 0, 0, 0 }, new int[] { 1997,
669: 1, 28, 0, 0, 0 }, "M d");
670:
671: }
672:
673: public void testDurationsByBruteForce() {
674: bruteForce(2006, 0, 1, "d", Calendar.DAY_OF_MONTH);
675: bruteForce(2006, 0, 2, "d", Calendar.DAY_OF_MONTH);
676: bruteForce(2007, 1, 2, "d", Calendar.DAY_OF_MONTH);
677: bruteForce(2004, 1, 29, "d", Calendar.DAY_OF_MONTH);
678: bruteForce(1996, 1, 29, "d", Calendar.DAY_OF_MONTH);
679:
680: bruteForce(1969, 1, 28, "M", Calendar.MONTH); // tests for 48 years
681: //bruteForce(1996, 1, 29, "M", Calendar.MONTH); // this will fail
682: }
683:
684: private int FOUR_YEARS = 365 * 3 + 366;
685:
686: // Takes a minute to run, so generally turned off
687: // public void testBrutally() {
688: // Calendar c = Calendar.getInstance();
689: // c.set(2004, 0, 1, 0, 0, 0);
690: // for (int i=0; i < FOUR_YEARS; i++) {
691: // bruteForce(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH), "d", Calendar.DAY_OF_MONTH );
692: // c.add(Calendar.DAY_OF_MONTH, 1);
693: // }
694: // }
695:
696: private void bruteForce(int year, int month, int day,
697: String format, int calendarType) {
698: String msg = year + "-" + month + "-" + day + " to ";
699: Calendar c = Calendar.getInstance();
700: c.set(year, month, day, 0, 0, 0);
701: int[] array1 = new int[] { year, month, day, 0, 0, 0 };
702: int[] array2 = new int[] { year, month, day, 0, 0, 0 };
703: for (int i = 0; i < FOUR_YEARS; i++) {
704: array2[0] = c.get(Calendar.YEAR);
705: array2[1] = c.get(Calendar.MONTH);
706: array2[2] = c.get(Calendar.DAY_OF_MONTH);
707: String tmpMsg = msg + array2[0] + "-" + array2[1] + "-"
708: + array2[2] + " at ";
709: assertEqualDuration(tmpMsg + i, Integer.toString(i),
710: array1, array2, format);
711: c.add(calendarType, 1);
712: }
713: }
714:
715: private void assertEqualDuration(String expected, int[] start,
716: int[] end, String format) {
717: assertEqualDuration(null, expected, start, end, format);
718: }
719:
720: private void assertEqualDuration(String message, String expected,
721: int[] start, int[] end, String format) {
722: Calendar cal1 = Calendar.getInstance();
723: cal1.set(start[0], start[1], start[2], start[3], start[4],
724: start[5]);
725: cal1.set(Calendar.MILLISECOND, 0);
726: Calendar cal2 = Calendar.getInstance();
727: cal2.set(end[0], end[1], end[2], end[3], end[4], end[5]);
728: cal2.set(Calendar.MILLISECOND, 0);
729: long milli1 = cal1.getTime().getTime();
730: long milli2 = cal2.getTime().getTime();
731: String result = DurationFormatUtils.formatPeriod(milli1,
732: milli2, format);
733: if (message == null) {
734: assertEquals(expected, result);
735: } else {
736: assertEquals(message, expected, result);
737: }
738: }
739:
740: private void assertArrayEquals(DurationFormatUtils.Token[] obj1,
741: DurationFormatUtils.Token[] obj2) {
742: assertEquals("Arrays are unequal length. ", obj1.length,
743: obj2.length);
744: for (int i = 0; i < obj1.length; i++) {
745: assertTrue("Index " + i + " not equal, " + obj1[i] + " vs "
746: + obj2, obj1[i].equals(obj2[i]));
747: }
748: }
749:
750: }
|