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 PeriodFormat.
032     *
033     * @author Stephen Colebourne
034     */
035    public class TestPeriodFormatParsing 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 = (y2002days + 31L + 28L + 31L + 30L + 31L + 9L - 1L) * DateTimeConstants.MILLIS_PER_DAY;
054    
055        private DateTimeZone originalDateTimeZone = null;
056        private TimeZone originalTimeZone = null;
057        private Locale originalLocale = null;
058    
059        public static void main(String[] args) {
060            junit.textui.TestRunner.run(suite());
061        }
062    
063        public static TestSuite suite() {
064            return new TestSuite(TestPeriodFormatParsing.class);
065        }
066    
067        public TestPeriodFormatParsing(String name) {
068            super(name);
069        }
070    
071        protected void setUp() throws Exception {
072            DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW);
073            originalDateTimeZone = DateTimeZone.getDefault();
074            originalTimeZone = TimeZone.getDefault();
075            originalLocale = Locale.getDefault();
076            DateTimeZone.setDefault(LONDON);
077            TimeZone.setDefault(TimeZone.getTimeZone("Europe/London"));
078            Locale.setDefault(Locale.UK);
079        }
080    
081        protected void tearDown() throws Exception {
082            DateTimeUtils.setCurrentMillisSystem();
083            DateTimeZone.setDefault(originalDateTimeZone);
084            TimeZone.setDefault(originalTimeZone);
085            Locale.setDefault(originalLocale);
086            originalDateTimeZone = null;
087            originalTimeZone = null;
088            originalLocale = null;
089        }
090    
091        //-----------------------------------------------------------------------
092        public void testParseStandard1() {
093            PeriodFormatter parser = PeriodFormat.getDefault();
094            Period p = parser.parsePeriod("6 years, 3 months and 2 days");
095            assertEquals(new Period(6, 3, 0, 2, 0, 0, 0, 0), p);
096        }
097    
098        public void testParseCustom1() {
099            PeriodFormatter formatter = new PeriodFormatterBuilder()
100                .printZeroAlways()
101                .appendHours()
102                .appendSuffix(":")
103                .minimumPrintedDigits(2)
104                .appendMinutes()
105                .toFormatter();
106    
107            Period p;
108    
109            p = new Period(47, 55, 0, 0);
110            assertEquals("47:55", formatter.print(p));
111            assertEquals(p, formatter.parsePeriod("47:55"));
112            assertEquals(p, formatter.parsePeriod("047:055"));
113    
114            p = new Period(7, 5, 0, 0);
115            assertEquals("7:05", formatter.print(p));
116            assertEquals(p, formatter.parsePeriod("7:05"));
117            assertEquals(p, formatter.parsePeriod("7:5"));
118            assertEquals(p, formatter.parsePeriod("07:05"));
119    
120            p = new Period(0, 5, 0, 0);
121            assertEquals("0:05", formatter.print(p));
122            assertEquals(p, formatter.parsePeriod("0:05"));
123            assertEquals(p, formatter.parsePeriod("0:5"));
124            assertEquals(p, formatter.parsePeriod("00:005"));
125            assertEquals(p, formatter.parsePeriod("0:005"));
126    
127            p = new Period(0, 0, 0, 0);
128            assertEquals("0:00", formatter.print(p));
129            assertEquals(p, formatter.parsePeriod("0:00"));
130            assertEquals(p, formatter.parsePeriod("0:0"));
131            assertEquals(p, formatter.parsePeriod("00:00"));
132        }
133    
134    }