View Javadoc

1   /*
2    *  Copyright 2001-2005 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 ISOPeriodFormat.
32   *
33   * @author Stephen Colebourne
34   */
35  public class TestISOPeriodFormat 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      public static void main(String[] args) {
61          junit.textui.TestRunner.run(suite());
62      }
63  
64      public static TestSuite suite() {
65          return new TestSuite(TestISOPeriodFormat.class);
66      }
67  
68      public TestISOPeriodFormat(String name) {
69          super(name);
70      }
71  
72      protected void setUp() throws Exception {
73          DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW);
74          originalDateTimeZone = DateTimeZone.getDefault();
75          originalTimeZone = TimeZone.getDefault();
76          originalLocale = Locale.getDefault();
77          DateTimeZone.setDefault(LONDON);
78          TimeZone.setDefault(TimeZone.getTimeZone("Europe/London"));
79          Locale.setDefault(Locale.UK);
80      }
81  
82      protected void tearDown() throws Exception {
83          DateTimeUtils.setCurrentMillisSystem();
84          DateTimeZone.setDefault(originalDateTimeZone);
85          TimeZone.setDefault(originalTimeZone);
86          Locale.setDefault(originalLocale);
87          originalDateTimeZone = null;
88          originalTimeZone = null;
89          originalLocale = null;
90      }
91  
92      //-----------------------------------------------------------------------
93      public void testSubclassableConstructor() {
94          ISOPeriodFormat f = new ISOPeriodFormat() {
95              // test constructor is protected
96          };
97          assertNotNull(f);
98      }
99  
100     //-----------------------------------------------------------------------
101     public void testFormatStandard() {
102         Period p = new Period(1, 2, 3, 4, 5, 6, 7, 8);
103         assertEquals("P1Y2M3W4DT5H6M7.008S", ISOPeriodFormat.standard().print(p));
104         p = new Period(1, 2, 3, 4, 5, 6 ,7, 0);
105         assertEquals("P1Y2M3W4DT5H6M7S", ISOPeriodFormat.standard().print(p));
106         
107         p = new Period(0);
108         assertEquals("PT0S", ISOPeriodFormat.standard().print(p));
109         p = new Period(0, PeriodType.standard().withMillisRemoved().withSecondsRemoved());
110         assertEquals("PT0M", ISOPeriodFormat.standard().print(p));
111         
112         assertEquals("P1Y4DT5H6M7.008S", ISOPeriodFormat.standard().print(YEAR_DAY_PERIOD));
113         assertEquals("PT0S", ISOPeriodFormat.standard().print(EMPTY_YEAR_DAY_PERIOD));
114         assertEquals("P1Y2M3W4D", ISOPeriodFormat.standard().print(DATE_PERIOD));
115         assertEquals("PT5H6M7.008S", ISOPeriodFormat.standard().print(TIME_PERIOD));
116     }
117 
118     public void testFormatStandard_negative() {
119         Period p = new Period(-1, -2, -3, -4, -5, -6, -7, -8);
120         assertEquals("P-1Y-2M-3W-4DT-5H-6M-7.008S", ISOPeriodFormat.standard().print(p));
121         
122         p = Period.years(-54);
123         assertEquals("P-54Y", ISOPeriodFormat.standard().print(p));
124         
125         p = Period.seconds(4).withMillis(-8);
126         assertEquals("PT3.992S", ISOPeriodFormat.standard().print(p));
127         
128         p = Period.seconds(-4).withMillis(8);
129         assertEquals("PT-3.992S", ISOPeriodFormat.standard().print(p));
130         
131         p = Period.seconds(-23);
132         assertEquals("PT-23S", ISOPeriodFormat.standard().print(p));
133         
134         p = Period.millis(-8);
135         assertEquals("PT-0.008S", ISOPeriodFormat.standard().print(p));
136     }
137 
138     //-----------------------------------------------------------------------
139     public void testFormatAlternate() {
140         Period p = new Period(1, 2, 3, 4, 5, 6 ,7, 8);
141         assertEquals("P00010204T050607.008", ISOPeriodFormat.alternate().print(p));
142         p = new Period(1, 2, 3, 4, 5, 6 ,7, 0);
143         assertEquals("P00010204T050607", ISOPeriodFormat.alternate().print(p));
144         
145         p = new Period(0);
146         assertEquals("P00000000T000000", ISOPeriodFormat.alternate().print(p));
147         p = new Period(0, PeriodType.standard().withMillisRemoved().withSecondsRemoved());
148         assertEquals("P00000000T000000", ISOPeriodFormat.alternate().print(p));
149         
150         assertEquals("P00010004T050607.008", ISOPeriodFormat.alternate().print(YEAR_DAY_PERIOD));
151         assertEquals("P00000000T000000", ISOPeriodFormat.alternate().print(EMPTY_YEAR_DAY_PERIOD));
152         assertEquals("P00010204T000000", ISOPeriodFormat.alternate().print(DATE_PERIOD));
153         assertEquals("P00000000T050607.008", ISOPeriodFormat.alternate().print(TIME_PERIOD));
154     }
155 
156     //-----------------------------------------------------------------------
157     public void testFormatAlternateExtended() {
158         Period p = new Period(1, 2, 3, 4, 5, 6 ,7, 8);
159         assertEquals("P0001-02-04T05:06:07.008", ISOPeriodFormat.alternateExtended().print(p));
160         p = new Period(1, 2, 3, 4, 5, 6 ,7, 0);
161         assertEquals("P0001-02-04T05:06:07", ISOPeriodFormat.alternateExtended().print(p));
162         
163         p = new Period(0);
164         assertEquals("P0000-00-00T00:00:00", ISOPeriodFormat.alternateExtended().print(p));
165         p = new Period(0, PeriodType.standard().withMillisRemoved().withSecondsRemoved());
166         assertEquals("P0000-00-00T00:00:00", ISOPeriodFormat.alternateExtended().print(p));
167         
168         assertEquals("P0001-00-04T05:06:07.008", ISOPeriodFormat.alternateExtended().print(YEAR_DAY_PERIOD));
169         assertEquals("P0000-00-00T00:00:00", ISOPeriodFormat.alternateExtended().print(EMPTY_YEAR_DAY_PERIOD));
170         assertEquals("P0001-02-04T00:00:00", ISOPeriodFormat.alternateExtended().print(DATE_PERIOD));
171         assertEquals("P0000-00-00T05:06:07.008", ISOPeriodFormat.alternateExtended().print(TIME_PERIOD));
172     }
173 
174     //-----------------------------------------------------------------------
175     public void testFormatAlternateWithWeeks() {
176         Period p = new Period(1, 2, 3, 4, 5, 6 ,7, 8);
177         assertEquals("P0001W0304T050607.008", ISOPeriodFormat.alternateWithWeeks().print(p));
178         p = new Period(1, 2, 3, 4, 5, 6 ,7, 0);
179         assertEquals("P0001W0304T050607", ISOPeriodFormat.alternateWithWeeks().print(p));
180         
181         p = new Period(0);
182         assertEquals("P0000W0000T000000", ISOPeriodFormat.alternateWithWeeks().print(p));
183         p = new Period(0, PeriodType.standard().withMillisRemoved().withSecondsRemoved());
184         assertEquals("P0000W0000T000000", ISOPeriodFormat.alternateWithWeeks().print(p));
185         
186         assertEquals("P0001W0004T050607.008", ISOPeriodFormat.alternateWithWeeks().print(YEAR_DAY_PERIOD));
187         assertEquals("P0000W0000T000000", ISOPeriodFormat.alternateWithWeeks().print(EMPTY_YEAR_DAY_PERIOD));
188         assertEquals("P0001W0304T000000", ISOPeriodFormat.alternateWithWeeks().print(DATE_PERIOD));
189         assertEquals("P0000W0000T050607.008", ISOPeriodFormat.alternateWithWeeks().print(TIME_PERIOD));
190     }
191 
192     //-----------------------------------------------------------------------
193     public void testFormatAlternateExtendedWithWeeks() {
194         Period p = new Period(1, 2, 3, 4, 5, 6 ,7, 8);
195         assertEquals("P0001-W03-04T05:06:07.008", ISOPeriodFormat.alternateExtendedWithWeeks().print(p));
196         p = new Period(1, 2, 3, 4, 5, 6 ,7, 0);
197         assertEquals("P0001-W03-04T05:06:07", ISOPeriodFormat.alternateExtendedWithWeeks().print(p));
198         
199         p = new Period(0);
200         assertEquals("P0000-W00-00T00:00:00", ISOPeriodFormat.alternateExtendedWithWeeks().print(p));
201         p = new Period(0, PeriodType.standard().withMillisRemoved().withSecondsRemoved());
202         assertEquals("P0000-W00-00T00:00:00", ISOPeriodFormat.alternateExtendedWithWeeks().print(p));
203         
204         assertEquals("P0001-W00-04T05:06:07.008", ISOPeriodFormat.alternateExtendedWithWeeks().print(YEAR_DAY_PERIOD));
205         assertEquals("P0000-W00-00T00:00:00", ISOPeriodFormat.alternateExtendedWithWeeks().print(EMPTY_YEAR_DAY_PERIOD));
206         assertEquals("P0001-W03-04T00:00:00", ISOPeriodFormat.alternateExtendedWithWeeks().print(DATE_PERIOD));
207         assertEquals("P0000-W00-00T05:06:07.008", ISOPeriodFormat.alternateExtendedWithWeeks().print(TIME_PERIOD));
208     }
209 
210 }