View Javadoc

1   /*
2    *  Copyright 2001-2006 Stephen Colebourne
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.joda.time.format;
17  
18  import java.util.Locale;
19  import java.util.TimeZone;
20  
21  import junit.framework.TestCase;
22  import junit.framework.TestSuite;
23  
24  import org.joda.time.DateTimeConstants;
25  import org.joda.time.DateTimeUtils;
26  import org.joda.time.DateTimeZone;
27  import org.joda.time.Period;
28  import org.joda.time.PeriodType;
29  
30  /**
31   * This class is a Junit unit test for PeriodFormatterBuilder.
32   *
33   * @author Stephen Colebourne
34   */
35  public class TestPeriodFormatterBuilder extends TestCase {
36      
37      private static final Period PERIOD = new Period(1, 2, 3, 4, 5, 6, 7, 8);
38      private static final Period EMPTY_PERIOD = new Period(0, 0, 0, 0, 0, 0, 0, 0);
39      private static final Period YEAR_DAY_PERIOD = new Period(1, 0, 0, 4, 5, 6, 7, 8, PeriodType.yearDayTime());
40      private static final Period EMPTY_YEAR_DAY_PERIOD = new Period(0, 0, 0, 0, 0, 0, 0, 0, PeriodType.yearDayTime());
41      private static final Period TIME_PERIOD = new Period(0, 0, 0, 0, 5, 6, 7, 8);
42      private static final Period DATE_PERIOD = new Period(1, 2, 3, 4, 0, 0, 0, 0);
43  
44      //private static final DateTimeZone PARIS = DateTimeZone.forID("Europe/Paris");
45      private static final DateTimeZone LONDON = DateTimeZone.forID("Europe/London");
46      //private static final DateTimeZone TOKYO = DateTimeZone.forID("Asia/Tokyo");
47  
48      long y2002days = 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 
49                       366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 
50                       365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 +
51                       366 + 365;
52      // 2002-06-09
53      private long TEST_TIME_NOW =
54              (y2002days + 31L + 28L + 31L + 30L + 31L + 9L -1L) * DateTimeConstants.MILLIS_PER_DAY;
55  
56      private DateTimeZone originalDateTimeZone = null;
57      private TimeZone originalTimeZone = null;
58      private Locale originalLocale = null;
59      
60      private PeriodFormatterBuilder builder;
61  
62      public static void main(String[] args) {
63          junit.textui.TestRunner.run(suite());
64      }
65  
66      public static TestSuite suite() {
67          return new TestSuite(TestPeriodFormatterBuilder.class);
68      }
69  
70      public TestPeriodFormatterBuilder(String name) {
71          super(name);
72      }
73  
74      protected void setUp() throws Exception {
75          DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW);
76          originalDateTimeZone = DateTimeZone.getDefault();
77          originalTimeZone = TimeZone.getDefault();
78          originalLocale = Locale.getDefault();
79          DateTimeZone.setDefault(LONDON);
80          TimeZone.setDefault(TimeZone.getTimeZone("Europe/London"));
81          Locale.setDefault(Locale.UK);
82          
83          builder = new PeriodFormatterBuilder();
84      }
85  
86      protected void tearDown() throws Exception {
87          DateTimeUtils.setCurrentMillisSystem();
88          DateTimeZone.setDefault(originalDateTimeZone);
89          TimeZone.setDefault(originalTimeZone);
90          Locale.setDefault(originalLocale);
91          originalDateTimeZone = null;
92          originalTimeZone = null;
93          originalLocale = null;
94      }
95  
96      //-----------------------------------------------------------------------
97      public void testToFormatterPrinterParser() {
98          builder.appendYears();
99          assertNotNull(builder.toFormatter());
100         assertNotNull(builder.toPrinter());
101         assertNotNull(builder.toParser());
102     }
103 
104     //-----------------------------------------------------------------------
105     public void testFormatYears() {
106         PeriodFormatter f = builder.appendYears().toFormatter();
107         assertEquals("1", f.print(PERIOD));
108         assertEquals(1, f.getPrinter().calculatePrintedLength(PERIOD, null));
109         assertEquals(1, f.getPrinter().countFieldsToPrint(PERIOD, Integer.MAX_VALUE, null));
110         
111         Period p = new Period(0, 0, 0, 0, 0, 0, 0, 0);
112         assertEquals("0", f.print(p));
113         assertEquals(1, f.getPrinter().calculatePrintedLength(p, null));
114         assertEquals(1, f.getPrinter().countFieldsToPrint(p, Integer.MAX_VALUE, null));
115     }
116 
117     public void testFormatMonths() {
118         PeriodFormatter f = builder.appendMonths().toFormatter();
119         assertEquals("2", f.print(PERIOD));
120         assertEquals(1, f.getPrinter().calculatePrintedLength(PERIOD, null));
121         assertEquals(1, f.getPrinter().countFieldsToPrint(PERIOD, Integer.MAX_VALUE, null));
122         
123         Period p = new Period(0, 0, 0, 0, 0, 0, 0, 0);
124         assertEquals("0", f.print(p));
125         assertEquals(1, f.getPrinter().calculatePrintedLength(p, null));
126         assertEquals(1, f.getPrinter().countFieldsToPrint(p, Integer.MAX_VALUE, null));
127     }
128 
129     public void testFormatWeeks() {
130         PeriodFormatter f = builder.appendWeeks().toFormatter();
131         assertEquals("3", f.print(PERIOD));
132         assertEquals(1, f.getPrinter().calculatePrintedLength(PERIOD, null));
133         assertEquals(1, f.getPrinter().countFieldsToPrint(PERIOD, Integer.MAX_VALUE, null));
134         
135         Period p = new Period(0, 0, 0, 0, 0, 0, 0, 0);
136         assertEquals("0", f.print(p));
137         assertEquals(1, f.getPrinter().calculatePrintedLength(p, null));
138         assertEquals(1, f.getPrinter().countFieldsToPrint(p, Integer.MAX_VALUE, null));
139     }
140 
141     public void testFormatDays() {
142         PeriodFormatter f = builder.appendDays().toFormatter();
143         assertEquals("4", f.print(PERIOD));
144         assertEquals(1, f.getPrinter().calculatePrintedLength(PERIOD, null));
145         assertEquals(1, f.getPrinter().countFieldsToPrint(PERIOD, Integer.MAX_VALUE, null));
146         
147         Period p = new Period(0, 0, 0, 0, 0, 0, 0, 0);
148         assertEquals("0", f.print(p));
149         assertEquals(1, f.getPrinter().calculatePrintedLength(p, null));
150         assertEquals(1, f.getPrinter().countFieldsToPrint(p, Integer.MAX_VALUE, null));
151     }
152 
153     public void testFormatHours() {
154         PeriodFormatter f = builder.appendHours().toFormatter();
155         assertEquals("5", f.print(PERIOD));
156         assertEquals(1, f.getPrinter().calculatePrintedLength(PERIOD, null));
157         assertEquals(1, f.getPrinter().countFieldsToPrint(PERIOD, Integer.MAX_VALUE, null));
158         
159         Period p = new Period(0, 0, 0, 0, 0, 0, 0, 0);
160         assertEquals("0", f.print(p));
161         assertEquals(1, f.getPrinter().calculatePrintedLength(p, null));
162         assertEquals(1, f.getPrinter().countFieldsToPrint(p, Integer.MAX_VALUE, null));
163     }
164 
165     public void testFormatMinutes() {
166         PeriodFormatter f = builder.appendMinutes().toFormatter();
167         assertEquals("6", f.print(PERIOD));
168         assertEquals(1, f.getPrinter().calculatePrintedLength(PERIOD, null));
169         assertEquals(1, f.getPrinter().countFieldsToPrint(PERIOD, Integer.MAX_VALUE, null));
170         
171         Period p = new Period(0, 0, 0, 0, 0, 0, 0, 0);
172         assertEquals("0", f.print(p));
173         assertEquals(1, f.getPrinter().calculatePrintedLength(p, null));
174         assertEquals(1, f.getPrinter().countFieldsToPrint(p, Integer.MAX_VALUE, null));
175     }
176 
177     public void testFormatSeconds() {
178         PeriodFormatter f = builder.appendSeconds().toFormatter();
179         assertEquals("7", f.print(PERIOD));
180         assertEquals(1, f.getPrinter().calculatePrintedLength(PERIOD, null));
181         assertEquals(1, f.getPrinter().countFieldsToPrint(PERIOD, Integer.MAX_VALUE, null));
182         
183         Period p = new Period(0, 0, 0, 0, 0, 0, 0, 0);
184         assertEquals("0", f.print(p));
185         assertEquals(1, f.getPrinter().calculatePrintedLength(p, null));
186         assertEquals(1, f.getPrinter().countFieldsToPrint(p, Integer.MAX_VALUE, null));
187     }
188 
189     public void testFormatSecondsWithMillis() {
190         PeriodFormatter f = builder.appendSecondsWithMillis().toFormatter();
191         Period p = new Period(0, 0, 0, 0, 0, 0, 7, 0);
192         assertEquals("7.000", f.print(p));
193         assertEquals(5, f.getPrinter().calculatePrintedLength(p, null));
194         assertEquals(1, f.getPrinter().countFieldsToPrint(p, Integer.MAX_VALUE, null));
195         
196         p = new Period(0, 0, 0, 0, 0, 0, 7, 1);
197         assertEquals("7.001", f.print(p));
198         assertEquals(5, f.getPrinter().calculatePrintedLength(p, null));
199         assertEquals(1, f.getPrinter().countFieldsToPrint(p, Integer.MAX_VALUE, null));
200         
201         p = new Period(0, 0, 0, 0, 0, 0, 7, 999);
202         assertEquals("7.999", f.print(p));
203         assertEquals(5, f.getPrinter().calculatePrintedLength(p, null));
204         assertEquals(1, f.getPrinter().countFieldsToPrint(p, Integer.MAX_VALUE, null));
205         
206         p = new Period(0, 0, 0, 0, 0, 0, 7, 1000);
207         assertEquals("8.000", f.print(p));
208         assertEquals(5, f.getPrinter().calculatePrintedLength(p, null));
209         assertEquals(1, f.getPrinter().countFieldsToPrint(p, Integer.MAX_VALUE, null));
210         
211         p = new Period(0, 0, 0, 0, 0, 0, 7, 1001);
212         assertEquals("8.001", f.print(p));
213         assertEquals(5, f.getPrinter().calculatePrintedLength(p, null));
214         assertEquals(1, f.getPrinter().countFieldsToPrint(p, Integer.MAX_VALUE, null));
215         
216         p = new Period(0, 0, 0, 0, 0, 0, 7, -1);
217         assertEquals("6.999", f.print(p));
218         assertEquals(5, f.getPrinter().calculatePrintedLength(p, null));
219         assertEquals(1, f.getPrinter().countFieldsToPrint(p, Integer.MAX_VALUE, null));
220         
221         p = new Period(0, 0, 0, 0, 0, 0, -7, 1);
222         assertEquals("-6.999", f.print(p));
223         assertEquals(6, f.getPrinter().calculatePrintedLength(p, null));
224         assertEquals(1, f.getPrinter().countFieldsToPrint(p, Integer.MAX_VALUE, null));
225         
226         p = new Period(0, 0, 0, 0, 0, 0, -7, -1);
227         assertEquals("-7.001", f.print(p));
228         assertEquals(6, f.getPrinter().calculatePrintedLength(p, null));
229         assertEquals(1, f.getPrinter().countFieldsToPrint(p, Integer.MAX_VALUE, null));
230         
231         p = new Period(0, 0, 0, 0, 0, 0, 0, 0);
232         assertEquals("0.000", f.print(p));
233         assertEquals(5, f.getPrinter().calculatePrintedLength(p, null));
234         assertEquals(1, f.getPrinter().countFieldsToPrint(p, Integer.MAX_VALUE, null));
235     }
236 
237     public void testFormatSecondsWithOptionalMillis() {
238         PeriodFormatter f = builder.appendSecondsWithOptionalMillis().toFormatter();
239         Period p = new Period(0, 0, 0, 0, 0, 0, 7, 0);
240         assertEquals("7", f.print(p));
241         assertEquals(1, f.getPrinter().calculatePrintedLength(p, null));
242         assertEquals(1, f.getPrinter().countFieldsToPrint(p, Integer.MAX_VALUE, null));
243         
244         p = new Period(0, 0, 0, 0, 0, 0, 7, 1);
245         assertEquals("7.001", f.print(p));
246         assertEquals(5, f.getPrinter().calculatePrintedLength(p, null));
247         assertEquals(1, f.getPrinter().countFieldsToPrint(p, Integer.MAX_VALUE, null));
248         
249         p = new Period(0, 0, 0, 0, 0, 0, 7, 999);
250         assertEquals("7.999", f.print(p));
251         assertEquals(5, f.getPrinter().calculatePrintedLength(p, null));
252         assertEquals(1, f.getPrinter().countFieldsToPrint(p, Integer.MAX_VALUE, null));
253         
254         p = new Period(0, 0, 0, 0, 0, 0, 7, 1000);
255         assertEquals("8", f.print(p));
256         assertEquals(1, f.getPrinter().calculatePrintedLength(p, null));
257         assertEquals(1, f.getPrinter().countFieldsToPrint(p, Integer.MAX_VALUE, null));
258         
259         p = new Period(0, 0, 0, 0, 0, 0, 7, 1001);
260         assertEquals("8.001", f.print(p));
261         assertEquals(5, f.getPrinter().calculatePrintedLength(p, null));
262         assertEquals(1, f.getPrinter().countFieldsToPrint(p, Integer.MAX_VALUE, null));
263         
264         p = new Period(0, 0, 0, 0, 0, 0, 7, -1);
265         assertEquals("6.999", f.print(p));
266         assertEquals(5, f.getPrinter().calculatePrintedLength(p, null));
267         assertEquals(1, f.getPrinter().countFieldsToPrint(p, Integer.MAX_VALUE, null));
268         
269         p = new Period(0, 0, 0, 0, 0, 0, -7, 1);
270         assertEquals("-6.999", f.print(p));
271         assertEquals(6, f.getPrinter().calculatePrintedLength(p, null));
272         assertEquals(1, f.getPrinter().countFieldsToPrint(p, Integer.MAX_VALUE, null));
273         
274         p = new Period(0, 0, 0, 0, 0, 0, -7, -1);
275         assertEquals("-7.001", f.print(p));
276         assertEquals(6, f.getPrinter().calculatePrintedLength(p, null));
277         assertEquals(1, f.getPrinter().countFieldsToPrint(p, Integer.MAX_VALUE, null));
278         
279         p = new Period(0, 0, 0, 0, 0, 0, 0, 0);
280         assertEquals("0", f.print(p));
281         assertEquals(1, f.getPrinter().calculatePrintedLength(p, null));
282         assertEquals(1, f.getPrinter().countFieldsToPrint(p, Integer.MAX_VALUE, null));
283     }
284 
285     public void testFormatMillis() {
286         PeriodFormatter f = builder.appendMillis().toFormatter();
287         assertEquals("8", f.print(PERIOD));
288         assertEquals(1, f.getPrinter().calculatePrintedLength(PERIOD, null));
289         assertEquals(1, f.getPrinter().countFieldsToPrint(PERIOD, Integer.MAX_VALUE, null));
290         
291         Period p = new Period(0, 0, 0, 0, 0, 0, 0, 0);
292         assertEquals("0", f.print(p));
293         assertEquals(1, f.getPrinter().calculatePrintedLength(p, null));
294         assertEquals(1, f.getPrinter().countFieldsToPrint(p, Integer.MAX_VALUE, null));
295     }
296 
297     public void testFormatMillis3Digit() {
298         PeriodFormatter f = builder.appendMillis3Digit().toFormatter();
299         assertEquals("008", f.print(PERIOD));
300         assertEquals(3, f.getPrinter().calculatePrintedLength(PERIOD, null));
301         assertEquals(1, f.getPrinter().countFieldsToPrint(PERIOD, Integer.MAX_VALUE, null));
302         
303         Period p = new Period(0, 0, 0, 0, 0, 0, 0, 0);
304         assertEquals("000", f.print(p));
305         assertEquals(3, f.getPrinter().calculatePrintedLength(p, null));
306         assertEquals(1, f.getPrinter().countFieldsToPrint(p, Integer.MAX_VALUE, null));
307     }
308 
309     //-----------------------------------------------------------------------
310     public void testFormatPrefixSimple1() {
311         PeriodFormatter f = builder.appendPrefix("Years:").appendYears().toFormatter();
312         assertEquals("Years:1", f.print(PERIOD));
313         assertEquals(7, f.getPrinter().calculatePrintedLength(PERIOD, null));
314         assertEquals(1, f.getPrinter().countFieldsToPrint(PERIOD, Integer.MAX_VALUE, null));
315         
316         Period p = new Period(0, 0, 0, 0, 0, 0, 0, 0);
317         assertEquals("Years:0", f.print(p));
318         assertEquals(7, f.getPrinter().calculatePrintedLength(p, null));
319         assertEquals(1, f.getPrinter().countFieldsToPrint(p, Integer.MAX_VALUE, null));
320     }
321 
322     public void testFormatPrefixSimple2() {
323         PeriodFormatter f = builder.appendPrefix("Hours:").appendHours().toFormatter();
324         assertEquals("Hours:5", f.print(PERIOD));
325         assertEquals(7, f.getPrinter().calculatePrintedLength(PERIOD, null));
326         assertEquals(1, f.getPrinter().countFieldsToPrint(PERIOD, Integer.MAX_VALUE, null));
327         
328         Period p = new Period(0, 0, 0, 0, 0, 0, 0, 0);
329         assertEquals("Hours:0", f.print(p));
330         assertEquals(7, f.getPrinter().calculatePrintedLength(p, null));
331         assertEquals(1, f.getPrinter().countFieldsToPrint(p, Integer.MAX_VALUE, null));
332     }
333 
334     public void testFormatPrefixSimple3() {
335         try {
336             builder.appendPrefix(null);
337             fail();
338         } catch (IllegalArgumentException ex) {}
339     }
340 
341     public void testFormatPrefixPlural1() {
342         PeriodFormatter f = builder.appendPrefix("Year:", "Years:").appendYears().toFormatter();
343         assertEquals("Year:1", f.print(PERIOD));
344         assertEquals(6, f.getPrinter().calculatePrintedLength(PERIOD, null));
345         assertEquals(1, f.getPrinter().countFieldsToPrint(PERIOD, Integer.MAX_VALUE, null));
346         
347         Period p = new Period(0, 0, 0, 0, 0, 0, 0, 0);
348         assertEquals("Years:0", f.print(p));
349         assertEquals(7, f.getPrinter().calculatePrintedLength(p, null));
350         assertEquals(1, f.getPrinter().countFieldsToPrint(p, Integer.MAX_VALUE, null));
351     }
352 
353     public void testFormatPrefixPlural2() {
354         PeriodFormatter f = builder.appendPrefix("Hour:", "Hours:").appendHours().toFormatter();
355         assertEquals("Hours:5", f.print(PERIOD));
356         assertEquals(7, f.getPrinter().calculatePrintedLength(PERIOD, null));
357         assertEquals(1, f.getPrinter().countFieldsToPrint(PERIOD, Integer.MAX_VALUE, null));
358         
359         Period p = new Period(0, 0, 0, 0, 0, 0, 0, 0);
360         assertEquals("Hours:0", f.print(p));
361         assertEquals(7, f.getPrinter().calculatePrintedLength(p, null));
362         assertEquals(1, f.getPrinter().countFieldsToPrint(p, Integer.MAX_VALUE, null));
363     }
364 
365     public void testFormatPrefixPlural3() {
366         try {
367             builder.appendPrefix(null, "");
368             fail();
369         } catch (IllegalArgumentException ex) {}
370         try {
371             builder.appendPrefix("", null);
372             fail();
373         } catch (IllegalArgumentException ex) {}
374         try {
375             builder.appendPrefix(null, null);
376             fail();
377         } catch (IllegalArgumentException ex) {}
378     }
379 
380     //-----------------------------------------------------------------------
381     public void testFormatSuffixSimple1() {
382         PeriodFormatter f = builder.appendYears().appendSuffix(" years").toFormatter();
383         assertEquals("1 years", f.print(PERIOD));
384         assertEquals(7, f.getPrinter().calculatePrintedLength(PERIOD, null));
385         assertEquals(1, f.getPrinter().countFieldsToPrint(PERIOD, Integer.MAX_VALUE, null));
386         
387         Period p = new Period(0, 0, 0, 0, 0, 0, 0, 0);
388         assertEquals("0 years", f.print(p));
389         assertEquals(7, f.getPrinter().calculatePrintedLength(p, null));
390         assertEquals(1, f.getPrinter().countFieldsToPrint(p, Integer.MAX_VALUE, null));
391     }
392 
393     public void testFormatSuffixSimple2() {
394         PeriodFormatter f = builder.appendHours().appendSuffix(" hours").toFormatter();
395         assertEquals("5 hours", f.print(PERIOD));
396         assertEquals(7, f.getPrinter().calculatePrintedLength(PERIOD, null));
397         assertEquals(1, f.getPrinter().countFieldsToPrint(PERIOD, Integer.MAX_VALUE, null));
398         
399         Period p = new Period(0, 0, 0, 0, 0, 0, 0, 0);
400         assertEquals("0 hours", f.print(p));
401         assertEquals(7, f.getPrinter().calculatePrintedLength(p, null));
402         assertEquals(1, f.getPrinter().countFieldsToPrint(p, Integer.MAX_VALUE, null));
403     }
404 
405     public void testFormatSuffixSimple3() {
406         try {
407             builder.appendSuffix(null);
408             fail();
409         } catch (IllegalArgumentException ex) {}
410     }
411 
412     public void testFormatSuffixSimple4() {
413         try {
414             builder.appendSuffix(" hours");
415             fail();
416         } catch (IllegalStateException ex) {}
417     }
418 
419     public void testFormatSuffixPlural1() {
420         PeriodFormatter f = builder.appendYears().appendSuffix(" year", " years").toFormatter();
421         assertEquals("1 year", f.print(PERIOD));
422         assertEquals(6, f.getPrinter().calculatePrintedLength(PERIOD, null));
423         assertEquals(1, f.getPrinter().countFieldsToPrint(PERIOD, Integer.MAX_VALUE, null));
424         
425         Period p = new Period(0, 0, 0, 0, 0, 0, 0, 0);
426         assertEquals("0 years", f.print(p));
427         assertEquals(7, f.getPrinter().calculatePrintedLength(p, null));
428         assertEquals(1, f.getPrinter().countFieldsToPrint(p, Integer.MAX_VALUE, null));
429     }
430 
431     public void testFormatSuffixPlural2() {
432         PeriodFormatter f = builder.appendHours().appendSuffix(" hour", " hours").toFormatter();
433         assertEquals("5 hours", f.print(PERIOD));
434         assertEquals(7, f.getPrinter().calculatePrintedLength(PERIOD, null));
435         assertEquals(1, f.getPrinter().countFieldsToPrint(PERIOD, Integer.MAX_VALUE, null));
436         
437         Period p = new Period(0, 0, 0, 0, 0, 0, 0, 0);
438         assertEquals("0 hours", f.print(p));
439         assertEquals(7, f.getPrinter().calculatePrintedLength(p, null));
440         assertEquals(1, f.getPrinter().countFieldsToPrint(p, Integer.MAX_VALUE, null));
441     }
442 
443     public void testFormatSuffixPlural3() {
444         try {
445             builder.appendSuffix(null, "");
446             fail();
447         } catch (IllegalArgumentException ex) {}
448         try {
449             builder.appendSuffix("", null);
450             fail();
451         } catch (IllegalArgumentException ex) {}
452         try {
453             builder.appendSuffix(null, null);
454             fail();
455         } catch (IllegalArgumentException ex) {}
456     }
457 
458     public void testFormatSuffixPlural4() {
459         try {
460             builder.appendSuffix(" hour", " hours");
461             fail();
462         } catch (IllegalStateException ex) {}
463     }
464 
465     //-----------------------------------------------------------------------
466     public void testFormatPrefixSuffix() {
467         PeriodFormatter f = builder.appendPrefix("P").appendYears().appendSuffix("Y").toFormatter();
468         assertEquals("P1Y", f.print(PERIOD));
469         assertEquals(3, f.getPrinter().calculatePrintedLength(PERIOD, null));
470         assertEquals(1, f.getPrinter().countFieldsToPrint(PERIOD, Integer.MAX_VALUE, null));
471         
472         Period p = new Period(0, 0, 0, 0, 0, 0, 0, 0);
473         assertEquals("P0Y", f.print(p));
474         assertEquals(3, f.getPrinter().calculatePrintedLength(p, null));
475         assertEquals(1, f.getPrinter().countFieldsToPrint(p, Integer.MAX_VALUE, null));
476     }
477 
478     //-----------------------------------------------------------------------
479     public void testFormatSeparatorSimple() {
480         PeriodFormatter f = builder.appendYears().appendSeparator("T").appendHours().toFormatter();
481         assertEquals("1T5", f.print(PERIOD));
482         assertEquals(3, f.getPrinter().calculatePrintedLength(PERIOD, null));
483         assertEquals(2, f.getPrinter().countFieldsToPrint(PERIOD, Integer.MAX_VALUE, null));
484         
485         assertEquals("5", f.print(TIME_PERIOD));
486         assertEquals(1, f.getPrinter().calculatePrintedLength(TIME_PERIOD, null));
487         assertEquals(1, f.getPrinter().countFieldsToPrint(TIME_PERIOD, Integer.MAX_VALUE, null));
488         
489         assertEquals("1", f.print(DATE_PERIOD));
490         assertEquals(1, f.getPrinter().calculatePrintedLength(DATE_PERIOD, null));
491         assertEquals(1, f.getPrinter().countFieldsToPrint(DATE_PERIOD, Integer.MAX_VALUE, null));
492     }
493 
494     public void testFormatSeparatorComplex() {
495         PeriodFormatter f = builder
496             .appendYears().appendSeparator(", ", " and ")
497             .appendHours().appendSeparator(", ", " and ")
498             .appendMinutes().appendSeparator(", ", " and ")
499             .toFormatter();
500         assertEquals("1, 5 and 6", f.print(PERIOD));
501         assertEquals(10, f.getPrinter().calculatePrintedLength(PERIOD, null));
502         assertEquals(3, f.getPrinter().countFieldsToPrint(PERIOD, Integer.MAX_VALUE, null));
503         
504         assertEquals("5 and 6", f.print(TIME_PERIOD));
505         assertEquals(7, f.getPrinter().calculatePrintedLength(TIME_PERIOD, null));
506         assertEquals(2, f.getPrinter().countFieldsToPrint(TIME_PERIOD, Integer.MAX_VALUE, null));
507         
508         assertEquals("1", f.print(DATE_PERIOD));
509         assertEquals(1, f.getPrinter().calculatePrintedLength(DATE_PERIOD, null));
510         assertEquals(1, f.getPrinter().countFieldsToPrint(DATE_PERIOD, Integer.MAX_VALUE, null));
511     }
512 
513     public void testFormatSeparatorIfFieldsAfter() {
514         PeriodFormatter f = builder.appendYears().appendSeparatorIfFieldsAfter("T").appendHours().toFormatter();
515         assertEquals("1T5", f.print(PERIOD));
516         assertEquals(3, f.getPrinter().calculatePrintedLength(PERIOD, null));
517         assertEquals(2, f.getPrinter().countFieldsToPrint(PERIOD, Integer.MAX_VALUE, null));
518         
519         assertEquals("T5", f.print(TIME_PERIOD));
520         assertEquals(2, f.getPrinter().calculatePrintedLength(TIME_PERIOD, null));
521         assertEquals(1, f.getPrinter().countFieldsToPrint(TIME_PERIOD, Integer.MAX_VALUE, null));
522         
523         assertEquals("1", f.print(DATE_PERIOD));
524         assertEquals(1, f.getPrinter().calculatePrintedLength(DATE_PERIOD, null));
525         assertEquals(1, f.getPrinter().countFieldsToPrint(DATE_PERIOD, Integer.MAX_VALUE, null));
526     }
527 
528     public void testFormatSeparatorIfFieldsBefore() {
529         PeriodFormatter f = builder.appendYears().appendSeparatorIfFieldsBefore("T").appendHours().toFormatter();
530         assertEquals("1T5", f.print(PERIOD));
531         assertEquals(3, f.getPrinter().calculatePrintedLength(PERIOD, null));
532         assertEquals(2, f.getPrinter().countFieldsToPrint(PERIOD, Integer.MAX_VALUE, null));
533         
534         assertEquals("5", f.print(TIME_PERIOD));
535         assertEquals(1, f.getPrinter().calculatePrintedLength(TIME_PERIOD, null));
536         assertEquals(1, f.getPrinter().countFieldsToPrint(TIME_PERIOD, Integer.MAX_VALUE, null));
537         
538         assertEquals("1T", f.print(DATE_PERIOD));
539         assertEquals(2, f.getPrinter().calculatePrintedLength(DATE_PERIOD, null));
540         assertEquals(1, f.getPrinter().countFieldsToPrint(DATE_PERIOD, Integer.MAX_VALUE, null));
541     }
542 
543     //-----------------------------------------------------------------------
544     public void testFormatLiteral() {
545         PeriodFormatter f = builder.appendLiteral("HELLO").toFormatter();
546         assertEquals("HELLO", f.print(PERIOD));
547         assertEquals(5, f.getPrinter().calculatePrintedLength(PERIOD, null));
548         assertEquals(0, f.getPrinter().countFieldsToPrint(PERIOD, Integer.MAX_VALUE, null));
549     }
550 
551     public void testFormatAppendFormatter() {
552         PeriodFormatter base = builder.appendYears().appendLiteral("-").toFormatter();
553         PeriodFormatter f = new PeriodFormatterBuilder().append(base).appendYears().toFormatter();
554         assertEquals("1-1", f.print(PERIOD));
555         assertEquals(3, f.getPrinter().calculatePrintedLength(PERIOD, null));
556         assertEquals(2, f.getPrinter().countFieldsToPrint(PERIOD, Integer.MAX_VALUE, null));
557     }
558 
559     public void testFormatMinDigits() {
560         PeriodFormatter f = new PeriodFormatterBuilder().minimumPrintedDigits(4).appendYears().toFormatter();
561         assertEquals("0001", f.print(PERIOD));
562         assertEquals(4, f.getPrinter().calculatePrintedLength(PERIOD, null));
563         assertEquals(1, f.getPrinter().countFieldsToPrint(PERIOD, Integer.MAX_VALUE, null));
564     }
565 
566     //-----------------------------------------------------------------------
567     public void testFormatPrintZeroDefault() {
568         PeriodFormatter f =
569             new PeriodFormatterBuilder()
570                 .appendYears().appendLiteral("-")
571                 .appendMonths().appendLiteral("-")
572                 .appendWeeks().appendLiteral("-")
573                 .appendDays().toFormatter();
574         assertEquals("1-2-3-4", f.print(PERIOD));
575         assertEquals(7, f.getPrinter().calculatePrintedLength(PERIOD, null));
576         assertEquals(4, f.getPrinter().countFieldsToPrint(PERIOD, Integer.MAX_VALUE, null));
577         
578         assertEquals("---0", f.print(EMPTY_YEAR_DAY_PERIOD));
579         assertEquals(4, f.getPrinter().calculatePrintedLength(EMPTY_YEAR_DAY_PERIOD, null));
580         assertEquals(1, f.getPrinter().countFieldsToPrint(EMPTY_YEAR_DAY_PERIOD, Integer.MAX_VALUE, null));
581         
582         assertEquals("1---4", f.print(YEAR_DAY_PERIOD));
583         assertEquals(5, f.getPrinter().calculatePrintedLength(YEAR_DAY_PERIOD, null));
584         assertEquals(2, f.getPrinter().countFieldsToPrint(YEAR_DAY_PERIOD, Integer.MAX_VALUE, null));
585         
586         assertEquals("---0", f.print(EMPTY_PERIOD));
587         assertEquals(4, f.getPrinter().calculatePrintedLength(EMPTY_PERIOD, null));
588         assertEquals(1, f.getPrinter().countFieldsToPrint(EMPTY_PERIOD, Integer.MAX_VALUE, null));
589         
590         // test only last instance of same field is output
591         f = new PeriodFormatterBuilder()
592                 .appendYears().appendLiteral("-")
593                 .appendYears().toFormatter();
594         assertEquals("-0", f.print(EMPTY_PERIOD));
595         assertEquals(2, f.getPrinter().calculatePrintedLength(EMPTY_PERIOD, null));
596         assertEquals(1, f.getPrinter().countFieldsToPrint(EMPTY_PERIOD, Integer.MAX_VALUE, null));
597     }
598 
599     public void testFormatPrintZeroRarelyLast() {
600         PeriodFormatter f =
601             new PeriodFormatterBuilder()
602                 .printZeroRarelyLast()
603                 .appendYears().appendLiteral("-")
604                 .appendMonths().appendLiteral("-")
605                 .appendWeeks().appendLiteral("-")
606                 .appendDays().toFormatter();
607         assertEquals("1-2-3-4", f.print(PERIOD));
608         assertEquals(7, f.getPrinter().calculatePrintedLength(PERIOD, null));
609         assertEquals(4, f.getPrinter().countFieldsToPrint(PERIOD, Integer.MAX_VALUE, null));
610         
611         assertEquals("---0", f.print(EMPTY_YEAR_DAY_PERIOD));
612         assertEquals(4, f.getPrinter().calculatePrintedLength(EMPTY_YEAR_DAY_PERIOD, null));
613         assertEquals(1, f.getPrinter().countFieldsToPrint(EMPTY_YEAR_DAY_PERIOD, Integer.MAX_VALUE, null));
614         
615         assertEquals("1---4", f.print(YEAR_DAY_PERIOD));
616         assertEquals(5, f.getPrinter().calculatePrintedLength(YEAR_DAY_PERIOD, null));
617         assertEquals(2, f.getPrinter().countFieldsToPrint(YEAR_DAY_PERIOD, Integer.MAX_VALUE, null));
618         
619         assertEquals("---0", f.print(EMPTY_PERIOD));
620         assertEquals(4, f.getPrinter().calculatePrintedLength(EMPTY_PERIOD, null));
621         assertEquals(1, f.getPrinter().countFieldsToPrint(EMPTY_PERIOD, Integer.MAX_VALUE, null));
622     }
623 
624     public void testFormatPrintZeroRarelyFirst() {
625         PeriodFormatter f =
626             new PeriodFormatterBuilder()
627                 .printZeroRarelyFirst()
628                 .appendYears().appendLiteral("-")
629                 .appendMonths().appendLiteral("-")
630                 .appendWeeks().appendLiteral("-")
631                 .appendDays().toFormatter();
632         assertEquals("1-2-3-4", f.print(PERIOD));
633         assertEquals(7, f.getPrinter().calculatePrintedLength(PERIOD, null));
634         assertEquals(4, f.getPrinter().countFieldsToPrint(PERIOD, Integer.MAX_VALUE, null));
635         
636         assertEquals("0---", f.print(EMPTY_YEAR_DAY_PERIOD));
637         assertEquals(4, f.getPrinter().calculatePrintedLength(EMPTY_YEAR_DAY_PERIOD, null));
638         assertEquals(1, f.getPrinter().countFieldsToPrint(EMPTY_YEAR_DAY_PERIOD, Integer.MAX_VALUE, null));
639         
640         assertEquals("1---4", f.print(YEAR_DAY_PERIOD));
641         assertEquals(5, f.getPrinter().calculatePrintedLength(YEAR_DAY_PERIOD, null));
642         assertEquals(2, f.getPrinter().countFieldsToPrint(YEAR_DAY_PERIOD, Integer.MAX_VALUE, null));
643         
644         assertEquals("0---", f.print(EMPTY_PERIOD));
645         assertEquals(4, f.getPrinter().calculatePrintedLength(EMPTY_PERIOD, null));
646         assertEquals(1, f.getPrinter().countFieldsToPrint(EMPTY_PERIOD, Integer.MAX_VALUE, null));
647     }
648 
649     public void testFormatPrintZeroRarelyFirstYears() {
650         PeriodFormatter f = new PeriodFormatterBuilder()
651             .printZeroRarelyFirst()
652             .appendYears().toFormatter();
653         assertEquals("0", f.print(EMPTY_PERIOD));
654     }
655 
656     public void testFormatPrintZeroRarelyFirstMonths() {
657         PeriodFormatter f = new PeriodFormatterBuilder()
658             .printZeroRarelyFirst()
659             .appendMonths().toFormatter();
660         assertEquals("0", f.print(EMPTY_PERIOD));
661     }
662 
663     public void testFormatPrintZeroRarelyFirstWeeks() {
664         PeriodFormatter f = new PeriodFormatterBuilder()
665             .printZeroRarelyFirst()
666             .appendWeeks().toFormatter();
667         assertEquals("0", f.print(EMPTY_PERIOD));
668     }
669 
670     public void testFormatPrintZeroRarelyFirstDays() {
671         PeriodFormatter f = new PeriodFormatterBuilder()
672             .printZeroRarelyFirst()
673             .appendDays().toFormatter();
674         assertEquals("0", f.print(EMPTY_PERIOD));
675     }
676 
677     public void testFormatPrintZeroRarelyFirstHours() {
678         PeriodFormatter f = new PeriodFormatterBuilder()
679             .printZeroRarelyFirst()
680             .appendHours().toFormatter();
681         assertEquals("0", f.print(EMPTY_PERIOD));
682     }
683 
684     public void testFormatPrintZeroRarelyFirstMinutes() {
685         PeriodFormatter f = new PeriodFormatterBuilder()
686             .printZeroRarelyFirst()
687             .appendMinutes().toFormatter();
688         assertEquals("0", f.print(EMPTY_PERIOD));
689     }
690 
691     public void testFormatPrintZeroRarelyFirstSeconds() {
692         PeriodFormatter f = new PeriodFormatterBuilder()
693             .printZeroRarelyFirst()
694             .appendSeconds().toFormatter();
695         assertEquals("0", f.print(EMPTY_PERIOD));
696     }
697 
698     public void testFormatPrintZeroIfSupported() {
699         PeriodFormatter f =
700             new PeriodFormatterBuilder()
701                 .printZeroIfSupported()
702                 .appendYears().appendLiteral("-")
703                 .appendMonths().appendLiteral("-")
704                 .appendWeeks().appendLiteral("-")
705                 .appendDays().toFormatter();
706         assertEquals("1-2-3-4", f.print(PERIOD));
707         assertEquals(7, f.getPrinter().calculatePrintedLength(PERIOD, null));
708         assertEquals(4, f.getPrinter().countFieldsToPrint(PERIOD, Integer.MAX_VALUE, null));
709         
710         assertEquals("0---0", f.print(EMPTY_YEAR_DAY_PERIOD));
711         assertEquals(5, f.getPrinter().calculatePrintedLength(EMPTY_YEAR_DAY_PERIOD, null));
712         assertEquals(2, f.getPrinter().countFieldsToPrint(EMPTY_YEAR_DAY_PERIOD, Integer.MAX_VALUE, null));
713         
714         assertEquals("1---4", f.print(YEAR_DAY_PERIOD));
715         assertEquals(5, f.getPrinter().calculatePrintedLength(YEAR_DAY_PERIOD, null));
716         assertEquals(2, f.getPrinter().countFieldsToPrint(YEAR_DAY_PERIOD, Integer.MAX_VALUE, null));
717         
718         assertEquals("0-0-0-0", f.print(EMPTY_PERIOD));
719         assertEquals(7, f.getPrinter().calculatePrintedLength(EMPTY_PERIOD, null));
720         assertEquals(4, f.getPrinter().countFieldsToPrint(EMPTY_PERIOD, Integer.MAX_VALUE, null));
721     }
722 
723     public void testFormatPrintZeroAlways() {
724         PeriodFormatter f =
725             new PeriodFormatterBuilder()
726                 .printZeroAlways()
727                 .appendYears().appendLiteral("-")
728                 .appendMonths().appendLiteral("-")
729                 .appendWeeks().appendLiteral("-")
730                 .appendDays().toFormatter();
731         assertEquals("1-2-3-4", f.print(PERIOD));
732         assertEquals(7, f.getPrinter().calculatePrintedLength(PERIOD, null));
733         assertEquals(4, f.getPrinter().countFieldsToPrint(PERIOD, Integer.MAX_VALUE, null));
734         
735         assertEquals("0-0-0-0", f.print(EMPTY_YEAR_DAY_PERIOD));
736         assertEquals(7, f.getPrinter().calculatePrintedLength(EMPTY_YEAR_DAY_PERIOD, null));
737         assertEquals(4, f.getPrinter().countFieldsToPrint(EMPTY_YEAR_DAY_PERIOD, Integer.MAX_VALUE, null));
738         
739         assertEquals("1-0-0-4", f.print(YEAR_DAY_PERIOD));
740         assertEquals(7, f.getPrinter().calculatePrintedLength(YEAR_DAY_PERIOD, null));
741         assertEquals(4, f.getPrinter().countFieldsToPrint(YEAR_DAY_PERIOD, Integer.MAX_VALUE, null));
742         
743         assertEquals("0-0-0-0", f.print(EMPTY_PERIOD));
744         assertEquals(7, f.getPrinter().calculatePrintedLength(EMPTY_PERIOD, null));
745         assertEquals(4, f.getPrinter().countFieldsToPrint(EMPTY_PERIOD, Integer.MAX_VALUE, null));
746     }
747 
748     public void testFormatPrintZeroNever() {
749         PeriodFormatter f =
750             new PeriodFormatterBuilder()
751                 .printZeroNever()
752                 .appendYears().appendLiteral("-")
753                 .appendMonths().appendLiteral("-")
754                 .appendWeeks().appendLiteral("-")
755                 .appendDays().toFormatter();
756         assertEquals("1-2-3-4", f.print(PERIOD));
757         assertEquals(7, f.getPrinter().calculatePrintedLength(PERIOD, null));
758         assertEquals(4, f.getPrinter().countFieldsToPrint(PERIOD, Integer.MAX_VALUE, null));
759         
760         assertEquals("---", f.print(EMPTY_YEAR_DAY_PERIOD));
761         assertEquals(3, f.getPrinter().calculatePrintedLength(EMPTY_YEAR_DAY_PERIOD, null));
762         assertEquals(0, f.getPrinter().countFieldsToPrint(EMPTY_YEAR_DAY_PERIOD, Integer.MAX_VALUE, null));
763         
764         assertEquals("1---4", f.print(YEAR_DAY_PERIOD));
765         assertEquals(5, f.getPrinter().calculatePrintedLength(YEAR_DAY_PERIOD, null));
766         assertEquals(2, f.getPrinter().countFieldsToPrint(YEAR_DAY_PERIOD, Integer.MAX_VALUE, null));
767         
768         assertEquals("---", f.print(EMPTY_PERIOD));
769         assertEquals(3, f.getPrinter().calculatePrintedLength(EMPTY_PERIOD, null));
770         assertEquals(0, f.getPrinter().countFieldsToPrint(EMPTY_PERIOD, Integer.MAX_VALUE, null));
771     }
772 
773     //-----------------------------------------------------------------------
774     public void testFormatAppend_PrinterParser_null_null() {
775         try {
776             new PeriodFormatterBuilder().append(null, null);
777             fail();
778         } catch (IllegalArgumentException ex) {}
779     }
780 
781     public void testFormatAppend_PrinterParser_Printer_null() {
782         PeriodPrinter printer = new PeriodFormatterBuilder().appendYears().appendLiteral("-").toPrinter();
783         PeriodFormatterBuilder bld = new PeriodFormatterBuilder().append(printer, null).appendMonths();
784         assertNotNull(bld.toPrinter());
785         assertNull(bld.toParser());
786         
787         PeriodFormatter f = bld.toFormatter();
788         assertEquals("1-2", f.print(PERIOD));
789         try {
790             f.parsePeriod("1-2");
791             fail();
792         } catch (UnsupportedOperationException ex) {}
793     }
794 
795     public void testFormatAppend_PrinterParser_null_Parser() {
796         PeriodParser parser = new PeriodFormatterBuilder().appendWeeks().appendLiteral("-").toParser();
797         PeriodFormatterBuilder bld = new PeriodFormatterBuilder().append(null, parser).appendMonths();
798         assertNull(bld.toPrinter());
799         assertNotNull(bld.toParser());
800         
801         PeriodFormatter f = bld.toFormatter();
802         try {
803             f.print(PERIOD);
804             fail();
805         } catch (UnsupportedOperationException ex) {}
806         assertEquals(new Period(0, 2, 1, 0, 0, 0, 0, 0), f.parsePeriod("1-2"));
807     }
808 
809     public void testFormatAppend_PrinterParser_PrinterParser() {
810         PeriodPrinter printer = new PeriodFormatterBuilder().appendYears().appendLiteral("-").toPrinter();
811         PeriodParser parser = new PeriodFormatterBuilder().appendWeeks().appendLiteral("-").toParser();
812         PeriodFormatterBuilder bld = new PeriodFormatterBuilder().append(printer, parser).appendMonths();
813         assertNotNull(bld.toPrinter());
814         assertNotNull(bld.toParser());
815         
816         PeriodFormatter f = bld.toFormatter();
817         assertEquals("1-2", f.print(PERIOD));
818         assertEquals(new Period(0, 2, 1, 0, 0, 0, 0, 0), f.parsePeriod("1-2"));
819     }
820 
821     public void testFormatAppend_PrinterParser_Printer_null_null_Parser() {
822         PeriodPrinter printer = new PeriodFormatterBuilder().appendYears().appendLiteral("-").toPrinter();
823         PeriodParser parser = new PeriodFormatterBuilder().appendWeeks().appendLiteral("-").toParser();
824         PeriodFormatterBuilder bld = new PeriodFormatterBuilder().append(printer, null).append(null, parser);
825         assertNull(bld.toPrinter());
826         assertNull(bld.toParser());
827         
828         try {
829             bld.toFormatter();
830             fail();
831         } catch (IllegalStateException ex) {}
832     }
833 
834     public void testFormatAppend_PrinterParserThenClear() {
835         PeriodPrinter printer = new PeriodFormatterBuilder().appendYears().appendLiteral("-").toPrinter();
836         PeriodParser parser = new PeriodFormatterBuilder().appendWeeks().appendLiteral("-").toParser();
837         PeriodFormatterBuilder bld = new PeriodFormatterBuilder().append(printer, null).append(null, parser);
838         assertNull(bld.toPrinter());
839         assertNull(bld.toParser());
840         bld.clear();
841         bld.appendMonths();
842         assertNotNull(bld.toPrinter());
843         assertNotNull(bld.toParser());
844     }
845 
846     public void testBug2495455() {
847         PeriodFormatter pfmt1 = new PeriodFormatterBuilder()
848             .appendLiteral("P")
849             .appendYears()
850             .appendSuffix("Y")
851             .appendMonths()
852             .appendSuffix("M")
853             .appendWeeks()
854             .appendSuffix("W")
855             .appendDays()
856             .appendSuffix("D")
857             .appendSeparatorIfFieldsAfter("T")
858             .appendHours()
859             .appendSuffix("H")
860             .appendMinutes()
861             .appendSuffix("M")
862             .appendSecondsWithOptionalMillis()
863             .appendSuffix("S")
864             .toFormatter();
865         PeriodFormatter pfmt2 = new PeriodFormatterBuilder()
866             .append(ISOPeriodFormat.standard())
867             .toFormatter();
868         pfmt1.parsePeriod("PT1003199059S");
869         pfmt2.parsePeriod("PT1003199059S");
870     }
871 
872 }