001: /*
002: * Copyright 2001-2006 Stephen Colebourne
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.joda.time.format;
017:
018: import java.util.Locale;
019: import java.util.TimeZone;
020:
021: import junit.framework.TestCase;
022: import junit.framework.TestSuite;
023:
024: import org.joda.time.DateTimeConstants;
025: import org.joda.time.DateTimeUtils;
026: import org.joda.time.DateTimeZone;
027: import org.joda.time.Period;
028: import org.joda.time.PeriodType;
029:
030: /**
031: * This class is a Junit unit test for PeriodFormatterBuilder.
032: *
033: * @author Stephen Colebourne
034: */
035: public class TestPeriodFormatterBuilder extends TestCase {
036:
037: private static final Period PERIOD = new Period(1, 2, 3, 4, 5, 6,
038: 7, 8);
039: private static final Period EMPTY_PERIOD = new Period(0, 0, 0, 0,
040: 0, 0, 0, 0);
041: private static final Period YEAR_DAY_PERIOD = new Period(1, 0, 0,
042: 4, 5, 6, 7, 8, PeriodType.yearDayTime());
043: private static final Period EMPTY_YEAR_DAY_PERIOD = new Period(0,
044: 0, 0, 0, 0, 0, 0, 0, PeriodType.yearDayTime());
045: private static final Period TIME_PERIOD = new Period(0, 0, 0, 0, 5,
046: 6, 7, 8);
047: private static final Period DATE_PERIOD = new Period(1, 2, 3, 4, 0,
048: 0, 0, 0);
049:
050: //private static final DateTimeZone PARIS = DateTimeZone.forID("Europe/Paris");
051: private static final DateTimeZone LONDON = DateTimeZone
052: .forID("Europe/London");
053: //private static final DateTimeZone TOKYO = DateTimeZone.forID("Asia/Tokyo");
054:
055: long y2002days = 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365
056: + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365
057: + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365
058: + 365 + 365 + 366 + 365;
059: // 2002-06-09
060: private long TEST_TIME_NOW = (y2002days + 31L + 28L + 31L + 30L
061: + 31L + 9L - 1L)
062: * DateTimeConstants.MILLIS_PER_DAY;
063:
064: private DateTimeZone originalDateTimeZone = null;
065: private TimeZone originalTimeZone = null;
066: private Locale originalLocale = null;
067:
068: private PeriodFormatterBuilder builder;
069:
070: public static void main(String[] args) {
071: junit.textui.TestRunner.run(suite());
072: }
073:
074: public static TestSuite suite() {
075: return new TestSuite(TestPeriodFormatterBuilder.class);
076: }
077:
078: public TestPeriodFormatterBuilder(String name) {
079: super (name);
080: }
081:
082: protected void setUp() throws Exception {
083: DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW);
084: originalDateTimeZone = DateTimeZone.getDefault();
085: originalTimeZone = TimeZone.getDefault();
086: originalLocale = Locale.getDefault();
087: DateTimeZone.setDefault(LONDON);
088: TimeZone.setDefault(TimeZone.getTimeZone("Europe/London"));
089: Locale.setDefault(Locale.UK);
090:
091: builder = new PeriodFormatterBuilder();
092: }
093:
094: protected void tearDown() throws Exception {
095: DateTimeUtils.setCurrentMillisSystem();
096: DateTimeZone.setDefault(originalDateTimeZone);
097: TimeZone.setDefault(originalTimeZone);
098: Locale.setDefault(originalLocale);
099: originalDateTimeZone = null;
100: originalTimeZone = null;
101: originalLocale = null;
102: }
103:
104: //-----------------------------------------------------------------------
105: public void testToFormatterPrinterParser() {
106: builder.appendYears();
107: assertNotNull(builder.toFormatter());
108: assertNotNull(builder.toPrinter());
109: assertNotNull(builder.toParser());
110: }
111:
112: //-----------------------------------------------------------------------
113: public void testFormatYears() {
114: PeriodFormatter f = builder.appendYears().toFormatter();
115: assertEquals("1", f.print(PERIOD));
116: assertEquals(1, f.getPrinter().calculatePrintedLength(PERIOD,
117: null));
118: assertEquals(1, f.getPrinter().countFieldsToPrint(PERIOD,
119: Integer.MAX_VALUE, null));
120:
121: Period p = new Period(0, 0, 0, 0, 0, 0, 0, 0);
122: assertEquals("0", f.print(p));
123: assertEquals(1, f.getPrinter().calculatePrintedLength(p, null));
124: assertEquals(1, f.getPrinter().countFieldsToPrint(p,
125: Integer.MAX_VALUE, null));
126: }
127:
128: public void testFormatMonths() {
129: PeriodFormatter f = builder.appendMonths().toFormatter();
130: assertEquals("2", f.print(PERIOD));
131: assertEquals(1, f.getPrinter().calculatePrintedLength(PERIOD,
132: null));
133: assertEquals(1, f.getPrinter().countFieldsToPrint(PERIOD,
134: Integer.MAX_VALUE, null));
135:
136: Period p = new Period(0, 0, 0, 0, 0, 0, 0, 0);
137: assertEquals("0", f.print(p));
138: assertEquals(1, f.getPrinter().calculatePrintedLength(p, null));
139: assertEquals(1, f.getPrinter().countFieldsToPrint(p,
140: Integer.MAX_VALUE, null));
141: }
142:
143: public void testFormatWeeks() {
144: PeriodFormatter f = builder.appendWeeks().toFormatter();
145: assertEquals("3", f.print(PERIOD));
146: assertEquals(1, f.getPrinter().calculatePrintedLength(PERIOD,
147: null));
148: assertEquals(1, f.getPrinter().countFieldsToPrint(PERIOD,
149: Integer.MAX_VALUE, null));
150:
151: Period p = new Period(0, 0, 0, 0, 0, 0, 0, 0);
152: assertEquals("0", f.print(p));
153: assertEquals(1, f.getPrinter().calculatePrintedLength(p, null));
154: assertEquals(1, f.getPrinter().countFieldsToPrint(p,
155: Integer.MAX_VALUE, null));
156: }
157:
158: public void testFormatDays() {
159: PeriodFormatter f = builder.appendDays().toFormatter();
160: assertEquals("4", f.print(PERIOD));
161: assertEquals(1, f.getPrinter().calculatePrintedLength(PERIOD,
162: null));
163: assertEquals(1, f.getPrinter().countFieldsToPrint(PERIOD,
164: Integer.MAX_VALUE, null));
165:
166: Period p = new Period(0, 0, 0, 0, 0, 0, 0, 0);
167: assertEquals("0", f.print(p));
168: assertEquals(1, f.getPrinter().calculatePrintedLength(p, null));
169: assertEquals(1, f.getPrinter().countFieldsToPrint(p,
170: Integer.MAX_VALUE, null));
171: }
172:
173: public void testFormatHours() {
174: PeriodFormatter f = builder.appendHours().toFormatter();
175: assertEquals("5", f.print(PERIOD));
176: assertEquals(1, f.getPrinter().calculatePrintedLength(PERIOD,
177: null));
178: assertEquals(1, f.getPrinter().countFieldsToPrint(PERIOD,
179: Integer.MAX_VALUE, null));
180:
181: Period p = new Period(0, 0, 0, 0, 0, 0, 0, 0);
182: assertEquals("0", f.print(p));
183: assertEquals(1, f.getPrinter().calculatePrintedLength(p, null));
184: assertEquals(1, f.getPrinter().countFieldsToPrint(p,
185: Integer.MAX_VALUE, null));
186: }
187:
188: public void testFormatMinutes() {
189: PeriodFormatter f = builder.appendMinutes().toFormatter();
190: assertEquals("6", f.print(PERIOD));
191: assertEquals(1, f.getPrinter().calculatePrintedLength(PERIOD,
192: null));
193: assertEquals(1, f.getPrinter().countFieldsToPrint(PERIOD,
194: Integer.MAX_VALUE, null));
195:
196: Period p = new Period(0, 0, 0, 0, 0, 0, 0, 0);
197: assertEquals("0", f.print(p));
198: assertEquals(1, f.getPrinter().calculatePrintedLength(p, null));
199: assertEquals(1, f.getPrinter().countFieldsToPrint(p,
200: Integer.MAX_VALUE, null));
201: }
202:
203: public void testFormatSeconds() {
204: PeriodFormatter f = builder.appendSeconds().toFormatter();
205: assertEquals("7", f.print(PERIOD));
206: assertEquals(1, f.getPrinter().calculatePrintedLength(PERIOD,
207: null));
208: assertEquals(1, f.getPrinter().countFieldsToPrint(PERIOD,
209: Integer.MAX_VALUE, null));
210:
211: Period p = new Period(0, 0, 0, 0, 0, 0, 0, 0);
212: assertEquals("0", f.print(p));
213: assertEquals(1, f.getPrinter().calculatePrintedLength(p, null));
214: assertEquals(1, f.getPrinter().countFieldsToPrint(p,
215: Integer.MAX_VALUE, null));
216: }
217:
218: public void testFormatSecondsWithMillis() {
219: PeriodFormatter f = builder.appendSecondsWithMillis()
220: .toFormatter();
221: Period p = new Period(0, 0, 0, 0, 0, 0, 7, 0);
222: assertEquals("7.000", f.print(p));
223: assertEquals(5, f.getPrinter().calculatePrintedLength(p, null));
224: assertEquals(1, f.getPrinter().countFieldsToPrint(p,
225: Integer.MAX_VALUE, null));
226:
227: p = new Period(0, 0, 0, 0, 0, 0, 7, 1);
228: assertEquals("7.001", f.print(p));
229: assertEquals(5, f.getPrinter().calculatePrintedLength(p, null));
230: assertEquals(1, f.getPrinter().countFieldsToPrint(p,
231: Integer.MAX_VALUE, null));
232:
233: p = new Period(0, 0, 0, 0, 0, 0, 7, 999);
234: assertEquals("7.999", f.print(p));
235: assertEquals(5, f.getPrinter().calculatePrintedLength(p, null));
236: assertEquals(1, f.getPrinter().countFieldsToPrint(p,
237: Integer.MAX_VALUE, null));
238:
239: p = new Period(0, 0, 0, 0, 0, 0, 7, 1000);
240: assertEquals("8.000", f.print(p));
241: assertEquals(5, f.getPrinter().calculatePrintedLength(p, null));
242: assertEquals(1, f.getPrinter().countFieldsToPrint(p,
243: Integer.MAX_VALUE, null));
244:
245: p = new Period(0, 0, 0, 0, 0, 0, 7, 1001);
246: assertEquals("8.001", f.print(p));
247: assertEquals(5, f.getPrinter().calculatePrintedLength(p, null));
248: assertEquals(1, f.getPrinter().countFieldsToPrint(p,
249: Integer.MAX_VALUE, null));
250:
251: p = new Period(0, 0, 0, 0, 0, 0, 7, -1);
252: assertEquals("6.999", f.print(p));
253: assertEquals(5, f.getPrinter().calculatePrintedLength(p, null));
254: assertEquals(1, f.getPrinter().countFieldsToPrint(p,
255: Integer.MAX_VALUE, null));
256:
257: p = new Period(0, 0, 0, 0, 0, 0, -7, 1);
258: assertEquals("-6.999", f.print(p));
259: assertEquals(6, f.getPrinter().calculatePrintedLength(p, null));
260: assertEquals(1, f.getPrinter().countFieldsToPrint(p,
261: Integer.MAX_VALUE, null));
262:
263: p = new Period(0, 0, 0, 0, 0, 0, -7, -1);
264: assertEquals("-7.001", f.print(p));
265: assertEquals(6, f.getPrinter().calculatePrintedLength(p, null));
266: assertEquals(1, f.getPrinter().countFieldsToPrint(p,
267: Integer.MAX_VALUE, null));
268:
269: p = new Period(0, 0, 0, 0, 0, 0, 0, 0);
270: assertEquals("0.000", f.print(p));
271: assertEquals(5, f.getPrinter().calculatePrintedLength(p, null));
272: assertEquals(1, f.getPrinter().countFieldsToPrint(p,
273: Integer.MAX_VALUE, null));
274: }
275:
276: public void testFormatSecondsWithOptionalMillis() {
277: PeriodFormatter f = builder.appendSecondsWithOptionalMillis()
278: .toFormatter();
279: Period p = new Period(0, 0, 0, 0, 0, 0, 7, 0);
280: assertEquals("7", f.print(p));
281: assertEquals(1, f.getPrinter().calculatePrintedLength(p, null));
282: assertEquals(1, f.getPrinter().countFieldsToPrint(p,
283: Integer.MAX_VALUE, null));
284:
285: p = new Period(0, 0, 0, 0, 0, 0, 7, 1);
286: assertEquals("7.001", f.print(p));
287: assertEquals(5, f.getPrinter().calculatePrintedLength(p, null));
288: assertEquals(1, f.getPrinter().countFieldsToPrint(p,
289: Integer.MAX_VALUE, null));
290:
291: p = new Period(0, 0, 0, 0, 0, 0, 7, 999);
292: assertEquals("7.999", f.print(p));
293: assertEquals(5, f.getPrinter().calculatePrintedLength(p, null));
294: assertEquals(1, f.getPrinter().countFieldsToPrint(p,
295: Integer.MAX_VALUE, null));
296:
297: p = new Period(0, 0, 0, 0, 0, 0, 7, 1000);
298: assertEquals("8", f.print(p));
299: assertEquals(1, f.getPrinter().calculatePrintedLength(p, null));
300: assertEquals(1, f.getPrinter().countFieldsToPrint(p,
301: Integer.MAX_VALUE, null));
302:
303: p = new Period(0, 0, 0, 0, 0, 0, 7, 1001);
304: assertEquals("8.001", f.print(p));
305: assertEquals(5, f.getPrinter().calculatePrintedLength(p, null));
306: assertEquals(1, f.getPrinter().countFieldsToPrint(p,
307: Integer.MAX_VALUE, null));
308:
309: p = new Period(0, 0, 0, 0, 0, 0, 7, -1);
310: assertEquals("6.999", f.print(p));
311: assertEquals(5, f.getPrinter().calculatePrintedLength(p, null));
312: assertEquals(1, f.getPrinter().countFieldsToPrint(p,
313: Integer.MAX_VALUE, null));
314:
315: p = new Period(0, 0, 0, 0, 0, 0, -7, 1);
316: assertEquals("-6.999", f.print(p));
317: assertEquals(6, f.getPrinter().calculatePrintedLength(p, null));
318: assertEquals(1, f.getPrinter().countFieldsToPrint(p,
319: Integer.MAX_VALUE, null));
320:
321: p = new Period(0, 0, 0, 0, 0, 0, -7, -1);
322: assertEquals("-7.001", f.print(p));
323: assertEquals(6, f.getPrinter().calculatePrintedLength(p, null));
324: assertEquals(1, f.getPrinter().countFieldsToPrint(p,
325: Integer.MAX_VALUE, null));
326:
327: p = new Period(0, 0, 0, 0, 0, 0, 0, 0);
328: assertEquals("0", f.print(p));
329: assertEquals(1, f.getPrinter().calculatePrintedLength(p, null));
330: assertEquals(1, f.getPrinter().countFieldsToPrint(p,
331: Integer.MAX_VALUE, null));
332: }
333:
334: public void testFormatMillis() {
335: PeriodFormatter f = builder.appendMillis().toFormatter();
336: assertEquals("8", f.print(PERIOD));
337: assertEquals(1, f.getPrinter().calculatePrintedLength(PERIOD,
338: null));
339: assertEquals(1, f.getPrinter().countFieldsToPrint(PERIOD,
340: Integer.MAX_VALUE, null));
341:
342: Period p = new Period(0, 0, 0, 0, 0, 0, 0, 0);
343: assertEquals("0", f.print(p));
344: assertEquals(1, f.getPrinter().calculatePrintedLength(p, null));
345: assertEquals(1, f.getPrinter().countFieldsToPrint(p,
346: Integer.MAX_VALUE, null));
347: }
348:
349: public void testFormatMillis3Digit() {
350: PeriodFormatter f = builder.appendMillis3Digit().toFormatter();
351: assertEquals("008", f.print(PERIOD));
352: assertEquals(3, f.getPrinter().calculatePrintedLength(PERIOD,
353: null));
354: assertEquals(1, f.getPrinter().countFieldsToPrint(PERIOD,
355: Integer.MAX_VALUE, null));
356:
357: Period p = new Period(0, 0, 0, 0, 0, 0, 0, 0);
358: assertEquals("000", f.print(p));
359: assertEquals(3, f.getPrinter().calculatePrintedLength(p, null));
360: assertEquals(1, f.getPrinter().countFieldsToPrint(p,
361: Integer.MAX_VALUE, null));
362: }
363:
364: //-----------------------------------------------------------------------
365: public void testFormatPrefixSimple1() {
366: PeriodFormatter f = builder.appendPrefix("Years:")
367: .appendYears().toFormatter();
368: assertEquals("Years:1", f.print(PERIOD));
369: assertEquals(7, f.getPrinter().calculatePrintedLength(PERIOD,
370: null));
371: assertEquals(1, f.getPrinter().countFieldsToPrint(PERIOD,
372: Integer.MAX_VALUE, null));
373:
374: Period p = new Period(0, 0, 0, 0, 0, 0, 0, 0);
375: assertEquals("Years:0", f.print(p));
376: assertEquals(7, f.getPrinter().calculatePrintedLength(p, null));
377: assertEquals(1, f.getPrinter().countFieldsToPrint(p,
378: Integer.MAX_VALUE, null));
379: }
380:
381: public void testFormatPrefixSimple2() {
382: PeriodFormatter f = builder.appendPrefix("Hours:")
383: .appendHours().toFormatter();
384: assertEquals("Hours:5", f.print(PERIOD));
385: assertEquals(7, f.getPrinter().calculatePrintedLength(PERIOD,
386: null));
387: assertEquals(1, f.getPrinter().countFieldsToPrint(PERIOD,
388: Integer.MAX_VALUE, null));
389:
390: Period p = new Period(0, 0, 0, 0, 0, 0, 0, 0);
391: assertEquals("Hours:0", f.print(p));
392: assertEquals(7, f.getPrinter().calculatePrintedLength(p, null));
393: assertEquals(1, f.getPrinter().countFieldsToPrint(p,
394: Integer.MAX_VALUE, null));
395: }
396:
397: public void testFormatPrefixSimple3() {
398: try {
399: builder.appendPrefix(null);
400: fail();
401: } catch (IllegalArgumentException ex) {
402: }
403: }
404:
405: public void testFormatPrefixPlural1() {
406: PeriodFormatter f = builder.appendPrefix("Year:", "Years:")
407: .appendYears().toFormatter();
408: assertEquals("Year:1", f.print(PERIOD));
409: assertEquals(6, f.getPrinter().calculatePrintedLength(PERIOD,
410: null));
411: assertEquals(1, f.getPrinter().countFieldsToPrint(PERIOD,
412: Integer.MAX_VALUE, null));
413:
414: Period p = new Period(0, 0, 0, 0, 0, 0, 0, 0);
415: assertEquals("Years:0", f.print(p));
416: assertEquals(7, f.getPrinter().calculatePrintedLength(p, null));
417: assertEquals(1, f.getPrinter().countFieldsToPrint(p,
418: Integer.MAX_VALUE, null));
419: }
420:
421: public void testFormatPrefixPlural2() {
422: PeriodFormatter f = builder.appendPrefix("Hour:", "Hours:")
423: .appendHours().toFormatter();
424: assertEquals("Hours:5", f.print(PERIOD));
425: assertEquals(7, f.getPrinter().calculatePrintedLength(PERIOD,
426: null));
427: assertEquals(1, f.getPrinter().countFieldsToPrint(PERIOD,
428: Integer.MAX_VALUE, null));
429:
430: Period p = new Period(0, 0, 0, 0, 0, 0, 0, 0);
431: assertEquals("Hours:0", f.print(p));
432: assertEquals(7, f.getPrinter().calculatePrintedLength(p, null));
433: assertEquals(1, f.getPrinter().countFieldsToPrint(p,
434: Integer.MAX_VALUE, null));
435: }
436:
437: public void testFormatPrefixPlural3() {
438: try {
439: builder.appendPrefix(null, "");
440: fail();
441: } catch (IllegalArgumentException ex) {
442: }
443: try {
444: builder.appendPrefix("", null);
445: fail();
446: } catch (IllegalArgumentException ex) {
447: }
448: try {
449: builder.appendPrefix(null, null);
450: fail();
451: } catch (IllegalArgumentException ex) {
452: }
453: }
454:
455: //-----------------------------------------------------------------------
456: public void testFormatSuffixSimple1() {
457: PeriodFormatter f = builder.appendYears()
458: .appendSuffix(" years").toFormatter();
459: assertEquals("1 years", f.print(PERIOD));
460: assertEquals(7, f.getPrinter().calculatePrintedLength(PERIOD,
461: null));
462: assertEquals(1, f.getPrinter().countFieldsToPrint(PERIOD,
463: Integer.MAX_VALUE, null));
464:
465: Period p = new Period(0, 0, 0, 0, 0, 0, 0, 0);
466: assertEquals("0 years", f.print(p));
467: assertEquals(7, f.getPrinter().calculatePrintedLength(p, null));
468: assertEquals(1, f.getPrinter().countFieldsToPrint(p,
469: Integer.MAX_VALUE, null));
470: }
471:
472: public void testFormatSuffixSimple2() {
473: PeriodFormatter f = builder.appendHours()
474: .appendSuffix(" hours").toFormatter();
475: assertEquals("5 hours", f.print(PERIOD));
476: assertEquals(7, f.getPrinter().calculatePrintedLength(PERIOD,
477: null));
478: assertEquals(1, f.getPrinter().countFieldsToPrint(PERIOD,
479: Integer.MAX_VALUE, null));
480:
481: Period p = new Period(0, 0, 0, 0, 0, 0, 0, 0);
482: assertEquals("0 hours", f.print(p));
483: assertEquals(7, f.getPrinter().calculatePrintedLength(p, null));
484: assertEquals(1, f.getPrinter().countFieldsToPrint(p,
485: Integer.MAX_VALUE, null));
486: }
487:
488: public void testFormatSuffixSimple3() {
489: try {
490: builder.appendSuffix(null);
491: fail();
492: } catch (IllegalArgumentException ex) {
493: }
494: }
495:
496: public void testFormatSuffixSimple4() {
497: try {
498: builder.appendSuffix(" hours");
499: fail();
500: } catch (IllegalStateException ex) {
501: }
502: }
503:
504: public void testFormatSuffixPlural1() {
505: PeriodFormatter f = builder.appendYears().appendSuffix(" year",
506: " years").toFormatter();
507: assertEquals("1 year", f.print(PERIOD));
508: assertEquals(6, f.getPrinter().calculatePrintedLength(PERIOD,
509: null));
510: assertEquals(1, f.getPrinter().countFieldsToPrint(PERIOD,
511: Integer.MAX_VALUE, null));
512:
513: Period p = new Period(0, 0, 0, 0, 0, 0, 0, 0);
514: assertEquals("0 years", f.print(p));
515: assertEquals(7, f.getPrinter().calculatePrintedLength(p, null));
516: assertEquals(1, f.getPrinter().countFieldsToPrint(p,
517: Integer.MAX_VALUE, null));
518: }
519:
520: public void testFormatSuffixPlural2() {
521: PeriodFormatter f = builder.appendHours().appendSuffix(" hour",
522: " hours").toFormatter();
523: assertEquals("5 hours", f.print(PERIOD));
524: assertEquals(7, f.getPrinter().calculatePrintedLength(PERIOD,
525: null));
526: assertEquals(1, f.getPrinter().countFieldsToPrint(PERIOD,
527: Integer.MAX_VALUE, null));
528:
529: Period p = new Period(0, 0, 0, 0, 0, 0, 0, 0);
530: assertEquals("0 hours", f.print(p));
531: assertEquals(7, f.getPrinter().calculatePrintedLength(p, null));
532: assertEquals(1, f.getPrinter().countFieldsToPrint(p,
533: Integer.MAX_VALUE, null));
534: }
535:
536: public void testFormatSuffixPlural3() {
537: try {
538: builder.appendSuffix(null, "");
539: fail();
540: } catch (IllegalArgumentException ex) {
541: }
542: try {
543: builder.appendSuffix("", null);
544: fail();
545: } catch (IllegalArgumentException ex) {
546: }
547: try {
548: builder.appendSuffix(null, null);
549: fail();
550: } catch (IllegalArgumentException ex) {
551: }
552: }
553:
554: public void testFormatSuffixPlural4() {
555: try {
556: builder.appendSuffix(" hour", " hours");
557: fail();
558: } catch (IllegalStateException ex) {
559: }
560: }
561:
562: //-----------------------------------------------------------------------
563: public void testFormatPrefixSuffix() {
564: PeriodFormatter f = builder.appendPrefix("P").appendYears()
565: .appendSuffix("Y").toFormatter();
566: assertEquals("P1Y", f.print(PERIOD));
567: assertEquals(3, f.getPrinter().calculatePrintedLength(PERIOD,
568: null));
569: assertEquals(1, f.getPrinter().countFieldsToPrint(PERIOD,
570: Integer.MAX_VALUE, null));
571:
572: Period p = new Period(0, 0, 0, 0, 0, 0, 0, 0);
573: assertEquals("P0Y", f.print(p));
574: assertEquals(3, f.getPrinter().calculatePrintedLength(p, null));
575: assertEquals(1, f.getPrinter().countFieldsToPrint(p,
576: Integer.MAX_VALUE, null));
577: }
578:
579: //-----------------------------------------------------------------------
580: public void testFormatSeparatorSimple() {
581: PeriodFormatter f = builder.appendYears().appendSeparator("T")
582: .appendHours().toFormatter();
583: assertEquals("1T5", f.print(PERIOD));
584: assertEquals(3, f.getPrinter().calculatePrintedLength(PERIOD,
585: null));
586: assertEquals(2, f.getPrinter().countFieldsToPrint(PERIOD,
587: Integer.MAX_VALUE, null));
588:
589: assertEquals("5", f.print(TIME_PERIOD));
590: assertEquals(1, f.getPrinter().calculatePrintedLength(
591: TIME_PERIOD, null));
592: assertEquals(1, f.getPrinter().countFieldsToPrint(TIME_PERIOD,
593: Integer.MAX_VALUE, null));
594:
595: assertEquals("1", f.print(DATE_PERIOD));
596: assertEquals(1, f.getPrinter().calculatePrintedLength(
597: DATE_PERIOD, null));
598: assertEquals(1, f.getPrinter().countFieldsToPrint(DATE_PERIOD,
599: Integer.MAX_VALUE, null));
600: }
601:
602: public void testFormatSeparatorComplex() {
603: PeriodFormatter f = builder.appendYears().appendSeparator(", ",
604: " and ").appendHours().appendSeparator(", ", " and ")
605: .appendMinutes().appendSeparator(", ", " and ")
606: .toFormatter();
607: assertEquals("1, 5 and 6", f.print(PERIOD));
608: assertEquals(10, f.getPrinter().calculatePrintedLength(PERIOD,
609: null));
610: assertEquals(3, f.getPrinter().countFieldsToPrint(PERIOD,
611: Integer.MAX_VALUE, null));
612:
613: assertEquals("5 and 6", f.print(TIME_PERIOD));
614: assertEquals(7, f.getPrinter().calculatePrintedLength(
615: TIME_PERIOD, null));
616: assertEquals(2, f.getPrinter().countFieldsToPrint(TIME_PERIOD,
617: Integer.MAX_VALUE, null));
618:
619: assertEquals("1", f.print(DATE_PERIOD));
620: assertEquals(1, f.getPrinter().calculatePrintedLength(
621: DATE_PERIOD, null));
622: assertEquals(1, f.getPrinter().countFieldsToPrint(DATE_PERIOD,
623: Integer.MAX_VALUE, null));
624: }
625:
626: public void testFormatSeparatorIfFieldsAfter() {
627: PeriodFormatter f = builder.appendYears()
628: .appendSeparatorIfFieldsAfter("T").appendHours()
629: .toFormatter();
630: assertEquals("1T5", f.print(PERIOD));
631: assertEquals(3, f.getPrinter().calculatePrintedLength(PERIOD,
632: null));
633: assertEquals(2, f.getPrinter().countFieldsToPrint(PERIOD,
634: Integer.MAX_VALUE, null));
635:
636: assertEquals("T5", f.print(TIME_PERIOD));
637: assertEquals(2, f.getPrinter().calculatePrintedLength(
638: TIME_PERIOD, null));
639: assertEquals(1, f.getPrinter().countFieldsToPrint(TIME_PERIOD,
640: Integer.MAX_VALUE, null));
641:
642: assertEquals("1", f.print(DATE_PERIOD));
643: assertEquals(1, f.getPrinter().calculatePrintedLength(
644: DATE_PERIOD, null));
645: assertEquals(1, f.getPrinter().countFieldsToPrint(DATE_PERIOD,
646: Integer.MAX_VALUE, null));
647: }
648:
649: public void testFormatSeparatorIfFieldsBefore() {
650: PeriodFormatter f = builder.appendYears()
651: .appendSeparatorIfFieldsBefore("T").appendHours()
652: .toFormatter();
653: assertEquals("1T5", f.print(PERIOD));
654: assertEquals(3, f.getPrinter().calculatePrintedLength(PERIOD,
655: null));
656: assertEquals(2, f.getPrinter().countFieldsToPrint(PERIOD,
657: Integer.MAX_VALUE, null));
658:
659: assertEquals("5", f.print(TIME_PERIOD));
660: assertEquals(1, f.getPrinter().calculatePrintedLength(
661: TIME_PERIOD, null));
662: assertEquals(1, f.getPrinter().countFieldsToPrint(TIME_PERIOD,
663: Integer.MAX_VALUE, null));
664:
665: assertEquals("1T", f.print(DATE_PERIOD));
666: assertEquals(2, f.getPrinter().calculatePrintedLength(
667: DATE_PERIOD, null));
668: assertEquals(1, f.getPrinter().countFieldsToPrint(DATE_PERIOD,
669: Integer.MAX_VALUE, null));
670: }
671:
672: //-----------------------------------------------------------------------
673: public void testFormatLiteral() {
674: PeriodFormatter f = builder.appendLiteral("HELLO")
675: .toFormatter();
676: assertEquals("HELLO", f.print(PERIOD));
677: assertEquals(5, f.getPrinter().calculatePrintedLength(PERIOD,
678: null));
679: assertEquals(0, f.getPrinter().countFieldsToPrint(PERIOD,
680: Integer.MAX_VALUE, null));
681: }
682:
683: public void testFormatAppendFormatter() {
684: PeriodFormatter base = builder.appendYears().appendLiteral("-")
685: .toFormatter();
686: PeriodFormatter f = new PeriodFormatterBuilder().append(base)
687: .appendYears().toFormatter();
688: assertEquals("1-1", f.print(PERIOD));
689: assertEquals(3, f.getPrinter().calculatePrintedLength(PERIOD,
690: null));
691: assertEquals(2, f.getPrinter().countFieldsToPrint(PERIOD,
692: Integer.MAX_VALUE, null));
693: }
694:
695: public void testFormatMinDigits() {
696: PeriodFormatter f = new PeriodFormatterBuilder()
697: .minimumPrintedDigits(4).appendYears().toFormatter();
698: assertEquals("0001", f.print(PERIOD));
699: assertEquals(4, f.getPrinter().calculatePrintedLength(PERIOD,
700: null));
701: assertEquals(1, f.getPrinter().countFieldsToPrint(PERIOD,
702: Integer.MAX_VALUE, null));
703: }
704:
705: //-----------------------------------------------------------------------
706: public void testFormatPrintZeroDefault() {
707: PeriodFormatter f = new PeriodFormatterBuilder().appendYears()
708: .appendLiteral("-").appendMonths().appendLiteral("-")
709: .appendWeeks().appendLiteral("-").appendDays()
710: .toFormatter();
711: assertEquals("1-2-3-4", f.print(PERIOD));
712: assertEquals(7, f.getPrinter().calculatePrintedLength(PERIOD,
713: null));
714: assertEquals(4, f.getPrinter().countFieldsToPrint(PERIOD,
715: Integer.MAX_VALUE, null));
716:
717: assertEquals("---0", f.print(EMPTY_YEAR_DAY_PERIOD));
718: assertEquals(4, f.getPrinter().calculatePrintedLength(
719: EMPTY_YEAR_DAY_PERIOD, null));
720: assertEquals(1, f.getPrinter().countFieldsToPrint(
721: EMPTY_YEAR_DAY_PERIOD, Integer.MAX_VALUE, null));
722:
723: assertEquals("1---4", f.print(YEAR_DAY_PERIOD));
724: assertEquals(5, f.getPrinter().calculatePrintedLength(
725: YEAR_DAY_PERIOD, null));
726: assertEquals(2, f.getPrinter().countFieldsToPrint(
727: YEAR_DAY_PERIOD, Integer.MAX_VALUE, null));
728:
729: assertEquals("---0", f.print(EMPTY_PERIOD));
730: assertEquals(4, f.getPrinter().calculatePrintedLength(
731: EMPTY_PERIOD, null));
732: assertEquals(1, f.getPrinter().countFieldsToPrint(EMPTY_PERIOD,
733: Integer.MAX_VALUE, null));
734:
735: // test only last instance of same field is output
736: f = new PeriodFormatterBuilder().appendYears().appendLiteral(
737: "-").appendYears().toFormatter();
738: assertEquals("-0", f.print(EMPTY_PERIOD));
739: assertEquals(2, f.getPrinter().calculatePrintedLength(
740: EMPTY_PERIOD, null));
741: assertEquals(1, f.getPrinter().countFieldsToPrint(EMPTY_PERIOD,
742: Integer.MAX_VALUE, null));
743: }
744:
745: public void testFormatPrintZeroRarelyLast() {
746: PeriodFormatter f = new PeriodFormatterBuilder()
747: .printZeroRarelyLast().appendYears().appendLiteral("-")
748: .appendMonths().appendLiteral("-").appendWeeks()
749: .appendLiteral("-").appendDays().toFormatter();
750: assertEquals("1-2-3-4", f.print(PERIOD));
751: assertEquals(7, f.getPrinter().calculatePrintedLength(PERIOD,
752: null));
753: assertEquals(4, f.getPrinter().countFieldsToPrint(PERIOD,
754: Integer.MAX_VALUE, null));
755:
756: assertEquals("---0", f.print(EMPTY_YEAR_DAY_PERIOD));
757: assertEquals(4, f.getPrinter().calculatePrintedLength(
758: EMPTY_YEAR_DAY_PERIOD, null));
759: assertEquals(1, f.getPrinter().countFieldsToPrint(
760: EMPTY_YEAR_DAY_PERIOD, Integer.MAX_VALUE, null));
761:
762: assertEquals("1---4", f.print(YEAR_DAY_PERIOD));
763: assertEquals(5, f.getPrinter().calculatePrintedLength(
764: YEAR_DAY_PERIOD, null));
765: assertEquals(2, f.getPrinter().countFieldsToPrint(
766: YEAR_DAY_PERIOD, Integer.MAX_VALUE, null));
767:
768: assertEquals("---0", f.print(EMPTY_PERIOD));
769: assertEquals(4, f.getPrinter().calculatePrintedLength(
770: EMPTY_PERIOD, null));
771: assertEquals(1, f.getPrinter().countFieldsToPrint(EMPTY_PERIOD,
772: Integer.MAX_VALUE, null));
773: }
774:
775: public void testFormatPrintZeroRarelyFirst() {
776: PeriodFormatter f = new PeriodFormatterBuilder()
777: .printZeroRarelyFirst().appendYears()
778: .appendLiteral("-").appendMonths().appendLiteral("-")
779: .appendWeeks().appendLiteral("-").appendDays()
780: .toFormatter();
781: assertEquals("1-2-3-4", f.print(PERIOD));
782: assertEquals(7, f.getPrinter().calculatePrintedLength(PERIOD,
783: null));
784: assertEquals(4, f.getPrinter().countFieldsToPrint(PERIOD,
785: Integer.MAX_VALUE, null));
786:
787: assertEquals("0---", f.print(EMPTY_YEAR_DAY_PERIOD));
788: assertEquals(4, f.getPrinter().calculatePrintedLength(
789: EMPTY_YEAR_DAY_PERIOD, null));
790: assertEquals(1, f.getPrinter().countFieldsToPrint(
791: EMPTY_YEAR_DAY_PERIOD, Integer.MAX_VALUE, null));
792:
793: assertEquals("1---4", f.print(YEAR_DAY_PERIOD));
794: assertEquals(5, f.getPrinter().calculatePrintedLength(
795: YEAR_DAY_PERIOD, null));
796: assertEquals(2, f.getPrinter().countFieldsToPrint(
797: YEAR_DAY_PERIOD, Integer.MAX_VALUE, null));
798:
799: assertEquals("0---", f.print(EMPTY_PERIOD));
800: assertEquals(4, f.getPrinter().calculatePrintedLength(
801: EMPTY_PERIOD, null));
802: assertEquals(1, f.getPrinter().countFieldsToPrint(EMPTY_PERIOD,
803: Integer.MAX_VALUE, null));
804: }
805:
806: public void testFormatPrintZeroIfSupported() {
807: PeriodFormatter f = new PeriodFormatterBuilder()
808: .printZeroIfSupported().appendYears()
809: .appendLiteral("-").appendMonths().appendLiteral("-")
810: .appendWeeks().appendLiteral("-").appendDays()
811: .toFormatter();
812: assertEquals("1-2-3-4", f.print(PERIOD));
813: assertEquals(7, f.getPrinter().calculatePrintedLength(PERIOD,
814: null));
815: assertEquals(4, f.getPrinter().countFieldsToPrint(PERIOD,
816: Integer.MAX_VALUE, null));
817:
818: assertEquals("0---0", f.print(EMPTY_YEAR_DAY_PERIOD));
819: assertEquals(5, f.getPrinter().calculatePrintedLength(
820: EMPTY_YEAR_DAY_PERIOD, null));
821: assertEquals(2, f.getPrinter().countFieldsToPrint(
822: EMPTY_YEAR_DAY_PERIOD, Integer.MAX_VALUE, null));
823:
824: assertEquals("1---4", f.print(YEAR_DAY_PERIOD));
825: assertEquals(5, f.getPrinter().calculatePrintedLength(
826: YEAR_DAY_PERIOD, null));
827: assertEquals(2, f.getPrinter().countFieldsToPrint(
828: YEAR_DAY_PERIOD, Integer.MAX_VALUE, null));
829:
830: assertEquals("0-0-0-0", f.print(EMPTY_PERIOD));
831: assertEquals(7, f.getPrinter().calculatePrintedLength(
832: EMPTY_PERIOD, null));
833: assertEquals(4, f.getPrinter().countFieldsToPrint(EMPTY_PERIOD,
834: Integer.MAX_VALUE, null));
835: }
836:
837: public void testFormatPrintZeroAlways() {
838: PeriodFormatter f = new PeriodFormatterBuilder()
839: .printZeroAlways().appendYears().appendLiteral("-")
840: .appendMonths().appendLiteral("-").appendWeeks()
841: .appendLiteral("-").appendDays().toFormatter();
842: assertEquals("1-2-3-4", f.print(PERIOD));
843: assertEquals(7, f.getPrinter().calculatePrintedLength(PERIOD,
844: null));
845: assertEquals(4, f.getPrinter().countFieldsToPrint(PERIOD,
846: Integer.MAX_VALUE, null));
847:
848: assertEquals("0-0-0-0", f.print(EMPTY_YEAR_DAY_PERIOD));
849: assertEquals(7, f.getPrinter().calculatePrintedLength(
850: EMPTY_YEAR_DAY_PERIOD, null));
851: assertEquals(4, f.getPrinter().countFieldsToPrint(
852: EMPTY_YEAR_DAY_PERIOD, Integer.MAX_VALUE, null));
853:
854: assertEquals("1-0-0-4", f.print(YEAR_DAY_PERIOD));
855: assertEquals(7, f.getPrinter().calculatePrintedLength(
856: YEAR_DAY_PERIOD, null));
857: assertEquals(4, f.getPrinter().countFieldsToPrint(
858: YEAR_DAY_PERIOD, Integer.MAX_VALUE, null));
859:
860: assertEquals("0-0-0-0", f.print(EMPTY_PERIOD));
861: assertEquals(7, f.getPrinter().calculatePrintedLength(
862: EMPTY_PERIOD, null));
863: assertEquals(4, f.getPrinter().countFieldsToPrint(EMPTY_PERIOD,
864: Integer.MAX_VALUE, null));
865: }
866:
867: public void testFormatPrintZeroNever() {
868: PeriodFormatter f = new PeriodFormatterBuilder()
869: .printZeroNever().appendYears().appendLiteral("-")
870: .appendMonths().appendLiteral("-").appendWeeks()
871: .appendLiteral("-").appendDays().toFormatter();
872: assertEquals("1-2-3-4", f.print(PERIOD));
873: assertEquals(7, f.getPrinter().calculatePrintedLength(PERIOD,
874: null));
875: assertEquals(4, f.getPrinter().countFieldsToPrint(PERIOD,
876: Integer.MAX_VALUE, null));
877:
878: assertEquals("---", f.print(EMPTY_YEAR_DAY_PERIOD));
879: assertEquals(3, f.getPrinter().calculatePrintedLength(
880: EMPTY_YEAR_DAY_PERIOD, null));
881: assertEquals(0, f.getPrinter().countFieldsToPrint(
882: EMPTY_YEAR_DAY_PERIOD, Integer.MAX_VALUE, null));
883:
884: assertEquals("1---4", f.print(YEAR_DAY_PERIOD));
885: assertEquals(5, f.getPrinter().calculatePrintedLength(
886: YEAR_DAY_PERIOD, null));
887: assertEquals(2, f.getPrinter().countFieldsToPrint(
888: YEAR_DAY_PERIOD, Integer.MAX_VALUE, null));
889:
890: assertEquals("---", f.print(EMPTY_PERIOD));
891: assertEquals(3, f.getPrinter().calculatePrintedLength(
892: EMPTY_PERIOD, null));
893: assertEquals(0, f.getPrinter().countFieldsToPrint(EMPTY_PERIOD,
894: Integer.MAX_VALUE, null));
895: }
896:
897: //-----------------------------------------------------------------------
898: public void testFormatAppend_PrinterParser_null_null() {
899: try {
900: new PeriodFormatterBuilder().append(null, null);
901: fail();
902: } catch (IllegalArgumentException ex) {
903: }
904: }
905:
906: public void testFormatAppend_PrinterParser_Printer_null() {
907: PeriodPrinter printer = new PeriodFormatterBuilder()
908: .appendYears().appendLiteral("-").toPrinter();
909: PeriodFormatterBuilder bld = new PeriodFormatterBuilder()
910: .append(printer, null).appendMonths();
911: assertNotNull(bld.toPrinter());
912: assertNull(bld.toParser());
913:
914: PeriodFormatter f = bld.toFormatter();
915: assertEquals("1-2", f.print(PERIOD));
916: try {
917: f.parsePeriod("1-2");
918: fail();
919: } catch (UnsupportedOperationException ex) {
920: }
921: }
922:
923: public void testFormatAppend_PrinterParser_null_Parser() {
924: PeriodParser parser = new PeriodFormatterBuilder()
925: .appendWeeks().appendLiteral("-").toParser();
926: PeriodFormatterBuilder bld = new PeriodFormatterBuilder()
927: .append(null, parser).appendMonths();
928: assertNull(bld.toPrinter());
929: assertNotNull(bld.toParser());
930:
931: PeriodFormatter f = bld.toFormatter();
932: try {
933: f.print(PERIOD);
934: fail();
935: } catch (UnsupportedOperationException ex) {
936: }
937: assertEquals(new Period(0, 2, 1, 0, 0, 0, 0, 0), f
938: .parsePeriod("1-2"));
939: }
940:
941: public void testFormatAppend_PrinterParser_PrinterParser() {
942: PeriodPrinter printer = new PeriodFormatterBuilder()
943: .appendYears().appendLiteral("-").toPrinter();
944: PeriodParser parser = new PeriodFormatterBuilder()
945: .appendWeeks().appendLiteral("-").toParser();
946: PeriodFormatterBuilder bld = new PeriodFormatterBuilder()
947: .append(printer, parser).appendMonths();
948: assertNotNull(bld.toPrinter());
949: assertNotNull(bld.toParser());
950:
951: PeriodFormatter f = bld.toFormatter();
952: assertEquals("1-2", f.print(PERIOD));
953: assertEquals(new Period(0, 2, 1, 0, 0, 0, 0, 0), f
954: .parsePeriod("1-2"));
955: }
956:
957: public void testFormatAppend_PrinterParser_Printer_null_null_Parser() {
958: PeriodPrinter printer = new PeriodFormatterBuilder()
959: .appendYears().appendLiteral("-").toPrinter();
960: PeriodParser parser = new PeriodFormatterBuilder()
961: .appendWeeks().appendLiteral("-").toParser();
962: PeriodFormatterBuilder bld = new PeriodFormatterBuilder()
963: .append(printer, null).append(null, parser);
964: assertNull(bld.toPrinter());
965: assertNull(bld.toParser());
966:
967: try {
968: bld.toFormatter();
969: fail();
970: } catch (IllegalStateException ex) {
971: }
972: }
973:
974: public void testFormatAppend_PrinterParserThenClear() {
975: PeriodPrinter printer = new PeriodFormatterBuilder()
976: .appendYears().appendLiteral("-").toPrinter();
977: PeriodParser parser = new PeriodFormatterBuilder()
978: .appendWeeks().appendLiteral("-").toParser();
979: PeriodFormatterBuilder bld = new PeriodFormatterBuilder()
980: .append(printer, null).append(null, parser);
981: assertNull(bld.toPrinter());
982: assertNull(bld.toParser());
983: bld.clear();
984: bld.appendMonths();
985: assertNotNull(bld.toPrinter());
986: assertNotNull(bld.toParser());
987: }
988:
989: }
|