001    /*
002     *  Copyright 2001-2005 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 ISOPeriodFormat.
032     *
033     * @author Stephen Colebourne
034     */
035    public class TestISOPeriodFormat extends TestCase {
036        
037        private static final Period PERIOD = new Period(1, 2, 3, 4, 5, 6, 7, 8);
038        private static final Period EMPTY_PERIOD = new Period(0, 0, 0, 0, 0, 0, 0, 0);
039        private static final Period YEAR_DAY_PERIOD = new Period(1, 0, 0, 4, 5, 6, 7, 8, PeriodType.yearDayTime());
040        private static final Period EMPTY_YEAR_DAY_PERIOD = new Period(0, 0, 0, 0, 0, 0, 0, 0, PeriodType.yearDayTime());
041        private static final Period TIME_PERIOD = new Period(0, 0, 0, 0, 5, 6, 7, 8);
042        private static final Period DATE_PERIOD = new Period(1, 2, 3, 4, 0, 0, 0, 0);
043    
044        private static final DateTimeZone PARIS = DateTimeZone.forID("Europe/Paris");
045        private static final DateTimeZone LONDON = DateTimeZone.forID("Europe/London");
046        private static final DateTimeZone TOKYO = DateTimeZone.forID("Asia/Tokyo");
047    
048        long y2002days = 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 
049                         366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 
050                         365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 +
051                         366 + 365;
052        // 2002-06-09
053        private long TEST_TIME_NOW =
054                (y2002days + 31L + 28L + 31L + 30L + 31L + 9L -1L) * DateTimeConstants.MILLIS_PER_DAY;
055    
056        private DateTimeZone originalDateTimeZone = null;
057        private TimeZone originalTimeZone = null;
058        private Locale originalLocale = null;
059    
060        public static void main(String[] args) {
061            junit.textui.TestRunner.run(suite());
062        }
063    
064        public static TestSuite suite() {
065            return new TestSuite(TestISOPeriodFormat.class);
066        }
067    
068        public TestISOPeriodFormat(String name) {
069            super(name);
070        }
071    
072        protected void setUp() throws Exception {
073            DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW);
074            originalDateTimeZone = DateTimeZone.getDefault();
075            originalTimeZone = TimeZone.getDefault();
076            originalLocale = Locale.getDefault();
077            DateTimeZone.setDefault(LONDON);
078            TimeZone.setDefault(TimeZone.getTimeZone("Europe/London"));
079            Locale.setDefault(Locale.UK);
080        }
081    
082        protected void tearDown() throws Exception {
083            DateTimeUtils.setCurrentMillisSystem();
084            DateTimeZone.setDefault(originalDateTimeZone);
085            TimeZone.setDefault(originalTimeZone);
086            Locale.setDefault(originalLocale);
087            originalDateTimeZone = null;
088            originalTimeZone = null;
089            originalLocale = null;
090        }
091    
092        //-----------------------------------------------------------------------
093        public void testSubclassableConstructor() {
094            ISOPeriodFormat f = new ISOPeriodFormat() {
095                // test constructor is protected
096            };
097            assertNotNull(f);
098        }
099    
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    }